argos/dmp-frontend/src/app/ui/misc/search/search.component.ts

54 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-04-24 11:26:53 +02:00
import { Component, OnInit } from "@angular/core";
import { AuthService } from '../../../core/services/auth/auth.service';
import { FormControl } from "@angular/forms";
import { SearchBarService } from '../../../core/services/search-bar/search-bar.service';
import { Router } from '@angular/router';
import { Observable } from "rxjs";
import { SearchBarItem } from "../../../core/model/dashboard/search-bar-item";
export enum SearchBarType {
Dataset = 0,
Dmp = 1,
Project = 2
}
@Component({
selector: "app-search",
templateUrl: "./search.component.html",
styleUrls: ["./search.component.css"]
})
export class SearchComponent implements OnInit {
public search = false;
public searchControl = new FormControl();
filteredOptions: Observable<SearchBarItem[]>;
constructor(private authentication: AuthService, private router: Router, private searchBarService: SearchBarService) {}
ngOnInit() {
this.filteredOptions = this.searchControl.valueChanges.debounceTime(500).distinctUntilChanged().flatMap(x => {
return this.searchBarService.search(x);
});
}
public isAuthenticated(): boolean {
return !!this.authentication.current();
}
onOptionSelected(event: any) {
this.search = false;
this.searchControl.patchValue(null);
const selectedSearchBarItem = event.option.value;
if (selectedSearchBarItem.type === SearchBarType.Dataset) { this.router.navigate(['datasets/edit/' + selectedSearchBarItem.id]); }
if (selectedSearchBarItem.type === SearchBarType.Project) { this.router.navigate(['projects/edit/' + selectedSearchBarItem.id]); }
if (selectedSearchBarItem.type === SearchBarType.Dmp) { this.router.navigate(['plans/edit/' + selectedSearchBarItem.id]); }
}
transformType(type) {
switch (type) {
case SearchBarType.Dataset: return 'Dataset';
case SearchBarType.Dmp: return 'DMP';
case SearchBarType.Project: return 'Project';
}
}
}