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