monitor-dashboard/src/app/app.component.ts

123 lines
4.8 KiB
TypeScript
Raw Normal View History

import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
import {ActivatedRoute, NavigationEnd, Params, Router} from '@angular/router';
import {EnvProperties} from './openaireLibrary/utils/properties/env-properties';
import {EnvironmentSpecificService} from './openaireLibrary/utils/properties/environment-specific.service';
import {Session, User} from './openaireLibrary/login/utils/helper.class';
import {UserManagementService} from "./openaireLibrary/services/user-management.service";
import {StakeholderService} from "./services/stakeholder.service";
import {BehaviorSubject, Subscriber} from "rxjs";
import {LayoutService} from "./library/sharedComponents/sidebar/layout.service";
import {MenuItem} from "./openaireLibrary/sharedComponents/menu";
import {Item} from "./utils/entities/sidebar";
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit, OnDestroy {
properties: EnvProperties;
user: User;
params: BehaviorSubject<Params> = new BehaviorSubject<Params>(null);
hasSidebar: boolean = false;
hasHeader: boolean = false;
userMenuItems: MenuItem[] = [];
adminMenuItems: Item[] = [];
private subscriptions: any[] = [];
constructor(private route: ActivatedRoute,
private propertiesService: EnvironmentSpecificService,
private router: Router,
private userManagementService: UserManagementService,
private layoutService: LayoutService,
private stakeholderService: StakeholderService,
private cdr: ChangeDetectorRef) {
this.subscriptions.push(this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
let r = this.route;
while (r.firstChild) {
r = r.firstChild;
}
let params = r.snapshot.params;
this.params.next(params);
}
}));
}
ngOnInit() {
this.subscriptions.push(this.layoutService.hasSidebar.subscribe(hasSidebar => {
this.hasSidebar = hasSidebar;
this.cdr.detectChanges();
}));
this.subscriptions.push(this.layoutService.hasHeader.subscribe(hasHeader => {
this.hasHeader = hasHeader;
this.cdr.detectChanges();
}));
this.propertiesService.loadEnvironment()
.then(properties => {
this.properties = properties;
this.subscriptions.push(this.params.subscribe( params => {
if (params && params['stakeholder']) {
if (!this.stakeholderService.stakeholder ||
this.stakeholderService.stakeholder.alias !== params['stakeholder']) {
this.stakeholderService.getStakeholder(this.properties.monitorServiceAPIURL, params['stakeholder']).subscribe(stakeholder => {
this.stakeholderService.setStakeholder(stakeholder);
this.layoutService.setOpen(true);
});
}
} else {
this.stakeholderService.setStakeholder(null);
}
}));
this.subscriptions.push(this.userManagementService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
this.user = user;
this.buildMenu();
}, error => {
console.log("App couldn't fetch properties");
console.log(error);
}));
});
}
public ngOnDestroy() {
this.subscriptions.forEach(value => {
if (value instanceof Subscriber) {
value.unsubscribe();
}
});
}
public get open() {
return this.layoutService.open;
}
public toggleOpen(event = null) {
if (!event) {
this.layoutService.setOpen(!this.open);
} else if (event && event['value'] === true) {
this.layoutService.setOpen(false);
}
}
buildMenu() {
this.userMenuItems = [];
if (Session.isPortalAdministrator(this.user)) {
this.userMenuItems.push(new MenuItem("", "Manage helptexts",
((this.properties.environment == "beta") ? "https://beta.admin.connect.openaire.eu" : "https://admin.explore.openaire.eu") + "/dashboard?communityId=openaire", "", true, [], [], {}))
}
if (this.user) {
this.userMenuItems.push(new MenuItem("", "User information", "", "/user-info", false, [], [], {}));
}
if (this.adminMenuItems.length == 0) {
//nstructor(id: string, name: string, route: string, items: Item[], icon, open: boolean) {
this.adminMenuItems.push(new Item("stakeholders", "Manage Stakeholders", "/admin", [], null, false));
this.adminMenuItems.push(new Item("pages", "Pages", "/pages", [], null, false));
this.adminMenuItems.push(new Item("monitor", "Monitor", "/portals", [], null, false));
this.adminMenuItems.push(new Item("entities", "Entities", "/entities", [], null, false));
this.adminMenuItems.push(new Item("helptexts", "Help texts", "/helptexts?communityId=openaire", [], null, false));
}
}
}