Fix router on /
[oweals/peertube.git] / client / src / app / shared / search / search.component.ts
1 import { Component, EventEmitter, Output, OnInit } from '@angular/core';
2
3 import { DROPDOWN_DIRECTIVES} from  'ng2-bootstrap/components/dropdown';
4
5 import { Search } from './search.model';
6 import { SearchField } from './search-field.type';
7 import { SearchService } from './search.service'; // Temporary
8
9 @Component({
10     selector: 'my-search',
11     template: require('./search.component.html'),
12     directives: [ DROPDOWN_DIRECTIVES ]
13 })
14
15 export class SearchComponent implements OnInit {
16   @Output() search = new EventEmitter<Search>();
17
18   fieldChoices = {
19     name: 'Name',
20     author: 'Author',
21     podUrl: 'Pod Url',
22     magnetUri: 'Magnet Uri',
23     tags: 'Tags'
24   };
25   searchCriterias: Search = {
26     field: 'name',
27     value: ''
28   };
29
30   constructor(private searchService: SearchService) {}
31
32   ngOnInit() {
33     this.searchService.searchChanged.subscribe(
34       newSearchCriterias => {
35         // Put a field by default
36         if (!newSearchCriterias.field) {
37           newSearchCriterias.field = 'name';
38         }
39
40         this.searchCriterias = newSearchCriterias;
41       }
42     );
43   }
44
45   get choiceKeys() {
46     return Object.keys(this.fieldChoices);
47   }
48
49   choose($event: MouseEvent, choice: SearchField) {
50     $event.preventDefault();
51     $event.stopPropagation();
52
53     this.searchCriterias.field = choice;
54
55     if (this.searchCriterias.value) {
56       this.doSearch();
57     }
58   }
59
60   doSearch() {
61     this.search.emit(this.searchCriterias);
62   }
63
64   getStringChoice(choiceKey: SearchField) {
65     return this.fieldChoices[choiceKey];
66   }
67 }