1 import { Component, OnInit } from '@angular/core';
2 import { Router } from '@angular/router';
4 import { DROPDOWN_DIRECTIVES} from 'ng2-bootstrap/components/dropdown';
6 import { Search } from './search.model';
7 import { SearchField } from './search-field.type';
8 import { SearchService } from './search.service';
11 selector: 'my-search',
12 template: require('./search.component.html'),
13 directives: [ DROPDOWN_DIRECTIVES ]
16 export class SearchComponent implements OnInit {
21 magnetUri: 'Magnet Uri',
24 searchCriterias: Search = {
29 constructor(private searchService: SearchService, private router: Router) {}
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';
41 this.searchCriterias = newSearchCriterias;
47 return Object.keys(this.fieldChoices);
50 choose($event: MouseEvent, choice: SearchField) {
51 $event.preventDefault();
52 $event.stopPropagation();
54 this.searchCriterias.field = choice;
56 if (this.searchCriterias.value) {
62 if (this.router.url.indexOf('/videos/list') === -1) {
63 this.router.navigate([ '/videos/list' ]);
66 this.searchService.searchUpdated.next(this.searchCriterias);
69 getStringChoice(choiceKey: SearchField) {
70 return this.fieldChoices[choiceKey];