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