]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/search/search.component.ts
Better typescript typing for a better world
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / search / search.component.ts
CommitLineData
df98563e
C
1import { Component, OnInit } from '@angular/core'
2import { Router } from '@angular/router'
471bc22f 3
df98563e
C
4import { Search } from './search.model'
5import { SearchField } from './search-field.type'
6import { 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 14export 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'
df98563e 21 }
4fd8aa32
C
22 searchCriterias: Search = {
23 field: 'name',
24 value: ''
df98563e 25 }
471bc22f 26
df98563e 27 constructor (private searchService: SearchService, private router: Router) {}
00a44645 28
df98563e 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) {
df98563e 36 newSearchCriterias.field = 'name'
00a44645
C
37 }
38
df98563e 39 this.searchCriterias = newSearchCriterias
00a44645 40 }
df98563e 41 )
00a44645
C
42 }
43
df98563e
C
44 get choiceKeys () {
45 return Object.keys(this.fieldChoices)
471bc22f
C
46 }
47
df98563e
C
48 choose ($event: MouseEvent, choice: SearchField) {
49 $event.preventDefault()
50 $event.stopPropagation()
471bc22f 51
df98563e 52 this.searchCriterias.field = choice
641f98b2 53
70af9a0d 54 if (this.searchCriterias.value) {
df98563e 55 this.doSearch()
641f98b2 56 }
471bc22f
C
57 }
58
df98563e 59 doSearch () {
9aa46b0c 60 if (this.router.url.indexOf('/videos/list') === -1) {
df98563e 61 this.router.navigate([ '/videos/list' ])
9aa46b0c
C
62 }
63
df98563e 64 this.searchService.searchUpdated.next(this.searchCriterias)
471bc22f
C
65 }
66
df98563e
C
67 getStringChoice (choiceKey: SearchField) {
68 return this.fieldChoices[choiceKey]
4fd8aa32 69 }
471bc22f 70}