]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { Component, OnInit } from '@angular/core' |
2 | import { Router } from '@angular/router' | |
471bc22f | 3 | |
df98563e C |
4 | import { Search } from './search.model' |
5 | import { SearchField } from './search-field.type' | |
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 | 18 | host: 'Pod Host', |
e822fdae | 19 | tags: 'Tags' |
df98563e | 20 | } |
fd45e8f4 | 21 | searchCriteria: Search = { |
4fd8aa32 C |
22 | field: 'name', |
23 | value: '' | |
df98563e | 24 | } |
471bc22f | 25 | |
df98563e | 26 | constructor (private searchService: SearchService, private router: Router) {} |
00a44645 | 27 | |
df98563e | 28 | ngOnInit () { |
c323efb9 | 29 | // Subscribe if the search changed |
bddab65a C |
30 | // Usually changed by videos list component |
31 | this.searchService.updateSearch.subscribe( | |
fd45e8f4 | 32 | newSearchCriteria => { |
00a44645 | 33 | // Put a field by default |
fd45e8f4 C |
34 | if (!newSearchCriteria.field) { |
35 | newSearchCriteria.field = 'name' | |
00a44645 C |
36 | } |
37 | ||
fd45e8f4 | 38 | this.searchCriteria = newSearchCriteria |
00a44645 | 39 | } |
df98563e | 40 | ) |
00a44645 C |
41 | } |
42 | ||
df98563e C |
43 | get choiceKeys () { |
44 | return Object.keys(this.fieldChoices) | |
471bc22f C |
45 | } |
46 | ||
df98563e C |
47 | choose ($event: MouseEvent, choice: SearchField) { |
48 | $event.preventDefault() | |
49 | $event.stopPropagation() | |
471bc22f | 50 | |
fd45e8f4 | 51 | this.searchCriteria.field = choice |
641f98b2 | 52 | |
fd45e8f4 | 53 | if (this.searchCriteria.value) { |
df98563e | 54 | this.doSearch() |
641f98b2 | 55 | } |
471bc22f C |
56 | } |
57 | ||
df98563e | 58 | doSearch () { |
9aa46b0c | 59 | if (this.router.url.indexOf('/videos/list') === -1) { |
df98563e | 60 | this.router.navigate([ '/videos/list' ]) |
9aa46b0c C |
61 | } |
62 | ||
fd45e8f4 | 63 | this.searchService.searchUpdated.next(this.searchCriteria) |
471bc22f C |
64 | } |
65 | ||
df98563e C |
66 | getStringChoice (choiceKey: SearchField) { |
67 | return this.fieldChoices[choiceKey] | |
4fd8aa32 | 68 | } |
471bc22f | 69 | } |