argos/dmp-frontend/src/app/shared/components/navigation/navigation.component.ts

133 lines
4.5 KiB
TypeScript
Raw Normal View History

2018-06-29 10:29:43 +02:00
import { sample } from 'rxjs/operators';
2018-07-11 15:47:36 +02:00
import { Component, EventEmitter, Input, Output, ElementRef } from '@angular/core';
2017-12-14 11:41:26 +01:00
import { Principal } from '../../../models/login/Principal';
import { AuthService } from '../../../services/auth/auth.service';
2018-03-28 15:24:47 +02:00
import { LanguageResolverService } from '../../../services/language-resolver/language-resolver.service';
2018-08-24 17:21:02 +02:00
import { MatSidenav, MatDialog } from '@angular/material';
import { UserDialogComponent } from '../user-dialog/user-dialog.component';
import { SingleAutoCompleteConfiguration } from '../autocompletes/single/single-auto-complete-configuration';
import { ProjectCriteria } from '../../../models/criteria/project/ProjectCriteria';
import { RequestItem } from '../../../models/criteria/RequestItem';
import { ProjectService } from '../../../services/project/project.service';
import { FormControl } from '@angular/forms';
import { DashboardService } from '../../../services/dashboard/dashboard.service';
import { Observable } from 'rxjs/Observable';
2018-09-18 14:41:24 +02:00
import { SearchBarItem, SearchBarItemType } from '../../../models/dashboard/SearchBarItem';
2018-08-24 17:21:02 +02:00
import { SearchBarType } from '../search-bar/types/search-bar-type';
import { Router } from '@angular/router';
2017-12-14 11:41:26 +01:00
@Component({
2018-05-28 11:50:42 +02:00
selector: 'app-navigation',
templateUrl: 'navigation.component.html',
2018-08-24 17:21:02 +02:00
styleUrls: ['./navigation.component.scss'],
providers: [ProjectService]
2017-12-14 11:41:26 +01:00
})
export class NavigationComponent {
2018-05-28 11:50:42 +02:00
invert = false;
2018-08-24 17:21:02 +02:00
public search = false;
projectAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
public searchControl = new FormControl();
filteredOptions: Observable<SearchBarItem[]>;
2018-05-28 11:50:42 +02:00
2018-08-24 17:21:02 +02:00
constructor(
private authentication: AuthService,
private languageResolver: LanguageResolverService,
public dialog: MatDialog,
private projectService: ProjectService,
private dashBoardService: DashboardService,
private router: Router,
2018-05-28 11:50:42 +02:00
2018-08-24 17:21:02 +02:00
) {
}
ngOnInit() {
this.projectAutoCompleteConfiguration = {
filterFn: this.searchProject.bind(this),
items: this.searchProject(''),
displayFn: (item) => item["label"],
titleFn: (item) => item["label"],
//mapFn: (item) => new JsonSerializer<ProjectReference>().fromJSONArray(item, ProjectReference).map(item => item.toDropdownList()),
loadDataOnStart: true
};
2018-09-18 14:41:24 +02:00
this.filteredOptions = this.searchControl.valueChanges.debounceTime(500).distinctUntilChanged().flatMap(x => {
2018-08-24 17:21:02 +02:00
return this.dashBoardService.searchUserItems(x);
})
}
searchProject(query: string) {
let projectRequestItem: RequestItem<ProjectCriteria> = new RequestItem();
projectRequestItem.criteria = new ProjectCriteria();
projectRequestItem.criteria.like = query;
return this.projectService.getWithExternal(projectRequestItem);
2018-05-28 11:50:42 +02:00
}
public logout(): void {
this.authentication.logout();
}
public isAuthenticated(): boolean {
return !(!this.authentication.current())
}
2018-06-29 10:29:43 +02:00
@Input()
2018-07-11 15:47:36 +02:00
sideNav: MatSidenav;
2018-06-29 10:29:43 +02:00
@Output()
sidenavOpenChanges = new EventEmitter();
events: string[] = [];
opened: boolean;
2018-05-28 11:50:42 +02:00
public isAdmin(): boolean {
if (!this.authentication.current()) { return false }
const principalRoles = this.authentication.current().appRoles;
for (let i = 0; i < principalRoles.length; i++) {
if (principalRoles[i] === Principal.AppRole.Admin) {
return true;
}
}
return false;
}
public principalHasAvatar(): boolean {
return this.authentication.current().avatarUrl != null;
}
public getPrincipalAvatar(): string {
return this.authentication.current().avatarUrl;
}
2018-06-29 10:29:43 +02:00
onSideNavClick() {
2018-07-11 15:47:36 +02:00
this.sideNav.toggle();
2018-06-29 10:29:43 +02:00
}
2018-08-24 17:21:02 +02:00
openProfile() {
let dialogRef = this.dialog.open(UserDialogComponent, {
hasBackdrop: true,
closeOnNavigation: true,
disableClose: false,
position: { top: "64px", left: "85%" }
});
}
onOptionSelected(event: any) {
this.search = false;
this.searchControl.patchValue(null);
let 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.DATAMANAGEMENTPLAN) this.router.navigate(["dmps/edit/" + selectedSearchBarItem.id])
}
2018-09-18 14:41:24 +02:00
transformType(type) {
switch(type){
case SearchBarItemType.DATASET: return "Dataset"
case SearchBarItemType.DMP: return "DMP"
case SearchBarItemType.PROJECT: return "Project"
}
}
2017-12-14 11:41:26 +01:00
}