2019-09-23 10:17:03 +02:00
|
|
|
|
|
|
|
import {mergeMap, distinctUntilChanged, debounceTime} from 'rxjs/operators';
|
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,
|
2019-08-01 09:54:40 +02:00
|
|
|
Grant = 2
|
2019-04-24 11:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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() {
|
2019-09-23 10:17:03 +02:00
|
|
|
this.filteredOptions = this.searchControl.valueChanges.pipe(debounceTime(500),distinctUntilChanged(),mergeMap(x => {
|
2019-04-24 11:26:53 +02:00
|
|
|
return this.searchBarService.search(x);
|
2019-09-23 10:17:03 +02:00
|
|
|
}),);
|
2019-04-24 11:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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]); }
|
2019-08-01 09:54:40 +02:00
|
|
|
if (selectedSearchBarItem.type === SearchBarType.Grant) { this.router.navigate(['grants/edit/' + selectedSearchBarItem.id]); }
|
2019-04-24 11:26:53 +02:00
|
|
|
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';
|
2019-08-01 09:54:40 +02:00
|
|
|
case SearchBarType.Grant: return 'Grant';
|
2019-04-24 11:26:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|