2019-09-23 10:17:03 +02:00
|
|
|
|
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-12-11 15:51:03 +01:00
|
|
|
import { AppRole } from '@app/core/common/enum/app-role';
|
|
|
|
import { SearchBarItem } from '@app/core/model/dashboard/search-bar-item';
|
|
|
|
import { GrantCriteria } from '@app/core/query/grant/grant-criteria';
|
|
|
|
import { RequestItem } from '@app/core/query/request-item';
|
|
|
|
import { AuthService } from '@app/core/services/auth/auth.service';
|
|
|
|
import { GrantService } from '@app/core/services/grant/grant.service';
|
|
|
|
import { ProgressIndicationService } from '@app/core/services/progress-indication/progress-indication-service';
|
|
|
|
import { SearchBarService } from '@app/core/services/search-bar/search-bar.service';
|
|
|
|
import { SingleAutoCompleteConfiguration } from '@app/library/auto-complete/single/single-auto-complete-configuration';
|
|
|
|
import { UserDialogComponent } from '@app/ui/misc/navigation/user-dialog/user-dialog.component';
|
|
|
|
import { BaseComponent } from '@common/base/base.component';
|
2019-01-18 18:03:45 +01:00
|
|
|
import { Observable } from 'rxjs';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { debounceTime, distinctUntilChanged, mergeMap, takeUntil } from 'rxjs/operators';
|
2019-01-18 18:03:45 +01:00
|
|
|
|
|
|
|
export enum SearchBarType {
|
|
|
|
Dataset = 0,
|
|
|
|
Dmp = 1,
|
2019-08-01 09:54:40 +02:00
|
|
|
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;
|
2019-08-01 09:54:40 +02:00
|
|
|
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,
|
2019-08-01 09:54:40 +02:00
|
|
|
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,
|
2019-09-06 12:00:01 +02:00
|
|
|
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; });
|
|
|
|
});
|
|
|
|
|
2019-08-01 09:54:40 +02:00
|
|
|
this.grantAutoCompleteConfiguration = {
|
|
|
|
filterFn: this.searchGrant.bind(this),
|
|
|
|
initialItems: (extraData) => this.searchGrant(''),
|
2018-10-05 17:00:54 +02:00
|
|
|
displayFn: (item) => item['label'],
|
2019-01-21 12:14:20 +01:00
|
|
|
titleFn: (item) => item['label']
|
2018-10-05 17:00:54 +02:00
|
|
|
};
|
|
|
|
|
2019-12-11 15:51:03 +01: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-12-11 15:51:03 +01:00
|
|
|
}));
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 09:54:40 +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 {
|
2019-11-22 17:28:20 +01:00
|
|
|
return this.authentication.current().avatarUrl != null && this.authentication.current().avatarUrl.length > 0;
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getPrincipalAvatar(): string {
|
|
|
|
return this.authentication.current().avatarUrl;
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:28:20 +01:00
|
|
|
public getDefaultAvatar(): string {
|
|
|
|
return 'assets/images/profile-placeholder.png';
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
openProfile() {
|
|
|
|
const dialogRef = this.dialog.open(UserDialogComponent, {
|
|
|
|
hasBackdrop: true,
|
2019-01-21 12:14:20 +01:00
|
|
|
autoFocus: false,
|
2018-10-05 17:00:54 +02:00
|
|
|
closeOnNavigation: true,
|
|
|
|
disableClose: false,
|
2020-08-26 15:15:57 +02:00
|
|
|
position: { top: '64px', right: '1em' },
|
|
|
|
panelClass: 'custom-userbox'
|
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]); }
|
2019-08-01 09:54:40 +02:00
|
|
|
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';
|
2019-08-01 09:54:40 +02:00
|
|
|
case SearchBarType.Grant: return 'Grant';
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-14 11:41:26 +01:00
|
|
|
}
|