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