2019-09-23 10:17:03 +02:00
|
|
|
|
2020-01-03 15:52:27 +01: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";
|
2020-01-03 15:52:27 +01:00
|
|
|
import { TranslateService } from "@ngx-translate/core";
|
2019-04-24 11:26:53 +02:00
|
|
|
|
|
|
|
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[]>;
|
|
|
|
|
2020-01-03 15:52:27 +01:00
|
|
|
constructor(
|
|
|
|
private authentication: AuthService,
|
|
|
|
private router: Router,
|
|
|
|
private searchBarService: SearchBarService,
|
|
|
|
private language: TranslateService
|
|
|
|
) { }
|
2019-04-24 11:26:53 +02:00
|
|
|
|
|
|
|
ngOnInit() {
|
2020-01-03 15:52:27 +01: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);
|
2020-01-03 15:52:27 +01: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;
|
2020-01-03 15:52:27 +01:00
|
|
|
if (!selectedSearchBarItem.isPublished) {
|
|
|
|
if (selectedSearchBarItem.type === SearchBarType.Dataset) { this.router.navigate(['datasets/edit/' + selectedSearchBarItem.id]); }
|
|
|
|
if (selectedSearchBarItem.type === SearchBarType.Grant) { this.router.navigate(['plans/grant/' + selectedSearchBarItem.id]); }
|
|
|
|
if (selectedSearchBarItem.type === SearchBarType.Dmp) { this.router.navigate(['plans/overview/' + selectedSearchBarItem.id]); }
|
|
|
|
} else {
|
|
|
|
if (selectedSearchBarItem.type === SearchBarType.Dataset) { this.router.navigate(['datasets/publicEdit/' + selectedSearchBarItem.id]); }
|
|
|
|
if (selectedSearchBarItem.type === SearchBarType.Dmp) { this.router.navigate(['explore-plans/overview/' + selectedSearchBarItem.id]); }
|
|
|
|
}
|
2019-04-24 11:26:53 +02:00
|
|
|
}
|
|
|
|
|
2020-01-03 15:52:27 +01:00
|
|
|
transformType(type, isPublished) {
|
|
|
|
let subtitle: string;
|
2019-04-24 11:26:53 +02:00
|
|
|
switch (type) {
|
2020-01-03 15:52:27 +01:00
|
|
|
case SearchBarType.Dataset: {
|
|
|
|
subtitle = this.language.instant('NAV-BAR.SEARCH.DATASET');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SearchBarType.Dmp: {
|
|
|
|
subtitle = this.language.instant('NAV-BAR.SEARCH.DMP');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SearchBarType.Grant: {
|
|
|
|
subtitle = this.language.instant('NAV-BAR.SEARCH.GRANT');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isPublished) {
|
|
|
|
return subtitle + " - " + this.language.instant('NAV-BAR.SEARCH.PUBLISHED')
|
|
|
|
} else {
|
|
|
|
return subtitle;
|
2019-04-24 11:26:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|