]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/search/search.component.ts
853f5dc7c2cab121aac6a51d678db5d8204b5ede
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / search / search.component.ts
1 import { Component, OnInit } from '@angular/core';
2 import { Router } from '@angular/router';
3
4 import { DROPDOWN_DIRECTIVES} from 'ng2-bootstrap/components/dropdown';
5
6 import { Search } from './search.model';
7 import { SearchField } from './search-field.type';
8 import { SearchService } from './search.service';
9
10 @Component({
11 selector: 'my-search',
12 template: require('./search.component.html'),
13 directives: [ DROPDOWN_DIRECTIVES ]
14 })
15
16 export class SearchComponent implements OnInit {
17 fieldChoices = {
18 name: 'Name',
19 author: 'Author',
20 podUrl: 'Pod Url',
21 magnetUri: 'Magnet Uri',
22 tags: 'Tags'
23 };
24 searchCriterias: Search = {
25 field: 'name',
26 value: ''
27 };
28
29 constructor(private searchService: SearchService, private router: Router) {}
30
31 ngOnInit() {
32 // Subscribe if the search changed
33 // Usually changed by videos list component
34 this.searchService.updateSearch.subscribe(
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
46 get choiceKeys() {
47 return Object.keys(this.fieldChoices);
48 }
49
50 choose($event: MouseEvent, choice: SearchField) {
51 $event.preventDefault();
52 $event.stopPropagation();
53
54 this.searchCriterias.field = choice;
55
56 if (this.searchCriterias.value) {
57 this.doSearch();
58 }
59 }
60
61 doSearch() {
62 if (this.router.url.indexOf('/videos/list') === -1) {
63 this.router.navigate([ '/videos/list' ]);
64 }
65
66 this.searchService.searchUpdated.next(this.searchCriterias);
67 }
68
69 getStringChoice(choiceKey: SearchField) {
70 return this.fieldChoices[choiceKey];
71 }
72 }