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

129 lines
4.4 KiB
TypeScript
Raw Normal View History

2019-09-23 10:17:03 +02:00
import {mergeMap, distinctUntilChanged, debounceTime, takeUntil } from 'rxjs/operators';
2019-01-18 18:03:45 +01:00
import { Component, OnInit } from '@angular/core';
2018-08-24 17:21:02 +02:00
import { FormControl } from '@angular/forms';
2019-09-23 10:17:03 +02:00
import { MatDialog } from '@angular/material/dialog';
2018-08-24 17:21:02 +02:00
import { Router } from '@angular/router';
2019-01-18 18:03:45 +01:00
import { Observable } from 'rxjs';
import { BaseComponent } from '../../../core/common/base/base.component';
import { AppRole } from '../../../core/common/enum/app-role';
import { SearchBarItem } from '../../../core/model/dashboard/search-bar-item';
import { GrantCriteria } from '../../../core/query/grant/grant-criteria';
2019-01-18 18:03:45 +01:00
import { RequestItem } from '../../../core/query/request-item';
import { AuthService } from '../../../core/services/auth/auth.service';
import { ProgressIndicationService } from '../../../core/services/progress-indication/progress-indication-service';
import { GrantService } from '../../../core/services/grant/grant.service';
2019-01-18 18:03:45 +01:00
import { SearchBarService } from '../../../core/services/search-bar/search-bar.service';
import { SingleAutoCompleteConfiguration } from '../../../library/auto-complete/single/single-auto-complete-configuration';
import { UserDialogComponent } from './user-dialog/user-dialog.component';
export enum SearchBarType {
Dataset = 0,
Dmp = 1,
Grant = 2
2019-01-18 18:03:45 +01:00
}
2017-12-14 11:41:26 +01:00
@Component({
2018-10-05 17:00:54 +02:00
selector: 'app-navigation',
templateUrl: 'navigation.component.html',
2019-01-18 18:03:45 +01:00
styleUrls: ['./navigation.component.scss']
2017-12-14 11:41:26 +01:00
})
2019-01-18 18:03:45 +01:00
export class NavigationComponent extends BaseComponent implements OnInit {
progressIndication = false;
2018-10-05 17:00:54 +02:00
public search = false;
grantAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
2018-10-05 17:00:54 +02:00
public searchControl = new FormControl();
filteredOptions: Observable<SearchBarItem[]>;
events: string[] = [];
opened: boolean;
constructor(
private authentication: AuthService,
2019-01-18 18:03:45 +01:00
private dialog: MatDialog,
private grantService: GrantService,
2019-01-18 18:03:45 +01:00
private searchBarService: SearchBarService,
2018-10-05 17:00:54 +02:00
private router: Router,
private progressIndicationService: ProgressIndicationService
2018-10-05 17:00:54 +02:00
) {
2019-01-18 18:03:45 +01:00
super();
2018-10-05 17:00:54 +02:00
}
ngOnInit() {
2019-01-18 18:03:45 +01:00
this.progressIndicationService.getProgressIndicationObservable().pipe(takeUntil(this._destroyed)).subscribe(x => {
setTimeout(() => { this.progressIndication = x; });
});
this.grantAutoCompleteConfiguration = {
filterFn: this.searchGrant.bind(this),
initialItems: (extraData) => this.searchGrant(''),
2018-10-05 17:00:54 +02:00
displayFn: (item) => item['label'],
titleFn: (item) => item['label']
2018-10-05 17:00:54 +02:00
};
2019-09-23 10:17:03 +02:00
this.filteredOptions = this.searchControl.valueChanges.pipe(debounceTime(500),distinctUntilChanged(),mergeMap(x => {
2019-01-18 18:03:45 +01:00
return this.searchBarService.search(x);
2019-09-23 10:17:03 +02:00
}),);
2018-10-05 17:00:54 +02:00
}
searchGrant(query: string) {
const grantRequestItem: RequestItem<GrantCriteria> = new RequestItem();
grantRequestItem.criteria = new GrantCriteria();
grantRequestItem.criteria.like = query;
return this.grantService.getWithExternal(grantRequestItem);
2018-10-05 17:00:54 +02:00
}
public logout(): void {
this.authentication.logout();
}
public isAuthenticated(): boolean {
return !(!this.authentication.current());
}
public isAdmin(): boolean {
if (!this.authentication.current()) { return false; }
2019-01-18 18:03:45 +01:00
const principalRoles = this.authentication.current().authorities;
2018-10-05 17:00:54 +02:00
for (let i = 0; i < principalRoles.length; i++) {
2019-01-18 18:03:45 +01:00
if (principalRoles[i] === AppRole.Admin) {
2018-10-05 17:00:54 +02:00
return true;
}
}
return false;
}
public principalHasAvatar(): boolean {
return this.authentication.current().avatarUrl != null;
}
public getPrincipalAvatar(): string {
return this.authentication.current().avatarUrl;
}
openProfile() {
const dialogRef = this.dialog.open(UserDialogComponent, {
hasBackdrop: true,
autoFocus: false,
2018-10-05 17:00:54 +02:00
closeOnNavigation: true,
disableClose: false,
position: { top: '64px', right: '1em' }
2018-10-05 17:00:54 +02:00
});
}
onOptionSelected(event: any) {
this.search = false;
this.searchControl.patchValue(null);
const selectedSearchBarItem = event.option.value;
2019-01-18 18:03:45 +01:00
if (selectedSearchBarItem.type === SearchBarType.Dataset) { this.router.navigate(['datasets/edit/' + selectedSearchBarItem.id]); }
if (selectedSearchBarItem.type === SearchBarType.Grant) { this.router.navigate(['grants/edit/' + selectedSearchBarItem.id]); }
2019-01-18 18:03:45 +01:00
if (selectedSearchBarItem.type === SearchBarType.Dmp) { this.router.navigate(['plans/edit/' + selectedSearchBarItem.id]); }
2018-10-05 17:00:54 +02:00
}
transformType(type) {
switch (type) {
2019-01-18 18:03:45 +01:00
case SearchBarType.Dataset: return 'Dataset';
case SearchBarType.Dmp: return 'DMP';
case SearchBarType.Grant: return 'Grant';
2018-10-05 17:00:54 +02:00
}
}
2017-12-14 11:41:26 +01:00
}