]>
Commit | Line | Data |
---|---|---|
bddab65a | 1 | import { Component, OnInit } from '@angular/core'; |
471bc22f C |
2 | |
3 | import { DROPDOWN_DIRECTIVES} from 'ng2-bootstrap/components/dropdown'; | |
4 | ||
41a2aee3 C |
5 | import { Search } from './search.model'; |
6 | import { SearchField } from './search-field.type'; | |
0629423c | 7 | import { SearchService } from './search.service'; |
471bc22f C |
8 | |
9 | @Component({ | |
10 | selector: 'my-search', | |
4a6995be | 11 | template: require('./search.component.html'), |
471bc22f C |
12 | directives: [ DROPDOWN_DIRECTIVES ] |
13 | }) | |
14 | ||
00a44645 | 15 | export class SearchComponent implements OnInit { |
471bc22f | 16 | fieldChoices = { |
aff038cd C |
17 | name: 'Name', |
18 | author: 'Author', | |
19 | podUrl: 'Pod Url', | |
e822fdae C |
20 | magnetUri: 'Magnet Uri', |
21 | tags: 'Tags' | |
aff038cd | 22 | }; |
4fd8aa32 C |
23 | searchCriterias: Search = { |
24 | field: 'name', | |
25 | value: '' | |
26 | }; | |
471bc22f | 27 | |
00a44645 C |
28 | constructor(private searchService: SearchService) {} |
29 | ||
30 | ngOnInit() { | |
bddab65a C |
31 | // Subscribe is the search changed |
32 | // Usually changed by videos list component | |
33 | this.searchService.updateSearch.subscribe( | |
00a44645 C |
34 | newSearchCriterias => { |
35 | // Put a field by default | |
36 | if (!newSearchCriterias.field) { | |
37 | newSearchCriterias.field = 'name'; | |
38 | } | |
39 | ||
40 | this.searchCriterias = newSearchCriterias; | |
41 | } | |
42 | ); | |
43 | } | |
44 | ||
471bc22f C |
45 | get choiceKeys() { |
46 | return Object.keys(this.fieldChoices); | |
47 | } | |
48 | ||
ccf6ed16 | 49 | choose($event: MouseEvent, choice: SearchField) { |
471bc22f C |
50 | $event.preventDefault(); |
51 | $event.stopPropagation(); | |
52 | ||
53 | this.searchCriterias.field = choice; | |
641f98b2 | 54 | |
70af9a0d | 55 | if (this.searchCriterias.value) { |
641f98b2 C |
56 | this.doSearch(); |
57 | } | |
471bc22f C |
58 | } |
59 | ||
ccf6ed16 | 60 | doSearch() { |
bddab65a | 61 | this.searchService.searchUpdated.next(this.searchCriterias); |
471bc22f C |
62 | } |
63 | ||
4fd8aa32 C |
64 | getStringChoice(choiceKey: SearchField) { |
65 | return this.fieldChoices[choiceKey]; | |
66 | } | |
471bc22f | 67 | } |