1 import { Component, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
4 import { Search } from './search.model'
5 import { SearchField } from './search-field.type'
6 import { SearchService } from './search.service'
10 templateUrl: './search.component.html',
11 styleUrls: [ './search.component.scss' ]
14 export class SearchComponent implements OnInit {
19 magnetUri: 'Magnet URI',
22 searchCriterias: Search = {
27 constructor (private searchService: SearchService, private router: Router) {}
30 // Subscribe if the search changed
31 // Usually changed by videos list component
32 this.searchService.updateSearch.subscribe(
33 newSearchCriterias => {
34 // Put a field by default
35 if (!newSearchCriterias.field) {
36 newSearchCriterias.field = 'name'
39 this.searchCriterias = newSearchCriterias
45 return Object.keys(this.fieldChoices)
48 choose ($event: MouseEvent, choice: SearchField) {
49 $event.preventDefault()
50 $event.stopPropagation()
52 this.searchCriterias.field = choice
54 if (this.searchCriterias.value) {
60 if (this.router.url.indexOf('/videos/list') === -1) {
61 this.router.navigate([ '/videos/list' ])
64 this.searchService.searchUpdated.next(this.searchCriterias)
67 getStringChoice (choiceKey: SearchField) {
68 return this.fieldChoices[choiceKey]