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