-import { Component, EventEmitter, Output } from '@angular/core';
-
-import { DROPDOWN_DIRECTIVES} from 'ng2-bootstrap/components/dropdown';
+import { Component, OnInit } from '@angular/core';
+import { Router } from '@angular/router';
import { Search } from './search.model';
import { SearchField } from './search-field.type';
+import { SearchService } from './search.service';
@Component({
- selector: 'my-search',
- template: require('./search.component.html'),
- directives: [ DROPDOWN_DIRECTIVES ]
+ selector: 'my-search',
+ templateUrl: './search.component.html'
})
-export class SearchComponent {
- @Output() search = new EventEmitter<Search>();
-
+export class SearchComponent implements OnInit {
fieldChoices = {
name: 'Name',
author: 'Author',
- podUrl: 'Pod Url',
- magnetUri: 'Magnet Uri'
+ host: 'Pod Host',
+ magnetUri: 'Magnet URI',
+ tags: 'Tags'
};
searchCriterias: Search = {
field: 'name',
value: ''
};
+ constructor(private searchService: SearchService, private router: Router) {}
+
+ ngOnInit() {
+ // Subscribe if the search changed
+ // Usually changed by videos list component
+ this.searchService.updateSearch.subscribe(
+ newSearchCriterias => {
+ // Put a field by default
+ if (!newSearchCriterias.field) {
+ newSearchCriterias.field = 'name';
+ }
+
+ this.searchCriterias = newSearchCriterias;
+ }
+ );
+ }
+
get choiceKeys() {
return Object.keys(this.fieldChoices);
}
$event.stopPropagation();
this.searchCriterias.field = choice;
+
+ if (this.searchCriterias.value) {
+ this.doSearch();
+ }
}
doSearch() {
- this.search.emit(this.searchCriterias);
+ if (this.router.url.indexOf('/videos/list') === -1) {
+ this.router.navigate([ '/videos/list' ]);
+ }
+
+ this.searchService.searchUpdated.next(this.searchCriterias);
}
getStringChoice(choiceKey: SearchField) {