aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/search/search.component.ts
blob: 9218850ed7e5668eee0de1750971380a924fdbc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Component, EventEmitter, Output, OnInit } from '@angular/core';

import { DROPDOWN_DIRECTIVES} from  'ng2-bootstrap/components/dropdown';

import { Search } from './search.model';
import { SearchField } from './search-field.type';
import { SearchService } from './search.service'; // Temporary

@Component({
    selector: 'my-search',
    template: require('./search.component.html'),
    directives: [ DROPDOWN_DIRECTIVES ]
})

export class SearchComponent implements OnInit {
  @Output() search = new EventEmitter<Search>();

  fieldChoices = {
    name: 'Name',
    author: 'Author',
    podUrl: 'Pod Url',
    magnetUri: 'Magnet Uri',
    tags: 'Tags'
  };
  searchCriterias: Search = {
    field: 'name',
    value: ''
  };

  constructor(private searchService: SearchService) {}

  ngOnInit() {
    this.searchService.searchChanged.subscribe(
      newSearchCriterias => {
        // Put a field by default
        if (!newSearchCriterias.field) {
          newSearchCriterias.field = 'name';
        }

        this.searchCriterias = newSearchCriterias;
      }
    );
  }

  get choiceKeys() {
    return Object.keys(this.fieldChoices);
  }

  choose($event: MouseEvent, choice: SearchField) {
    $event.preventDefault();
    $event.stopPropagation();

    this.searchCriterias.field = choice;

    if (this.searchCriterias.value) {
      this.doSearch();
    }
  }

  doSearch() {
    this.search.emit(this.searchCriterias);
  }

  getStringChoice(choiceKey: SearchField) {
    return this.fieldChoices[choiceKey];
  }
}