openaire-library/sharedComponents/navigationBar.component.ts

140 lines
3.9 KiB
TypeScript

import {Component, Input} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Session, User} from '../login/utils/helper.class';
import {ConfigurationService} from '../utils/configuration/configuration.service';
import {MenuItem, RootMenuItem} from './menu';
import {EnvProperties} from '../utils/properties/env-properties';
@Component({
selector: 'navbar',
templateUrl: 'navigationBar.component.html'
})
export class NavigationBarComponent {
@Input() portal: string = "connect";
@Input() onlyTop: boolean;
@Input() logoPath: string = "assets/common-assets/";
@Input() userMenu: boolean = true;
@Input() showHomeMenuItem: boolean = false;
@Input() communityId;
@Input() showCommunityName: boolean = false;
@Input() userMenuItems: MenuItem[];
@Input() menuItems: RootMenuItem [];
@Input() community: { id: string, name: string, logoUrl: string };
@Input() showMenu: boolean = true;
@Input() homeurl: boolean = true;
@Input() properties: EnvProperties;
@Input() user: User;
@Input() enableSearch: boolean = false;
@Input() searchRoute: string = "/search/find";
@Input() searchPlaceHolder: string = "Search for research results";
keyword: string = "";
public isAuthorized: boolean = false;
sub: any;
showEntity = {};
showPage = {};
specialAnnouncementContent: string = null;
activeRouteEnabled = false;
constructor(private router: Router,
private route: ActivatedRoute,
private config: ConfigurationService) {
}
ngOnInit() {
this.activeRouteEnabled = false;
this.sub = this.route.queryParams.subscribe(params => {
this.initialize();
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
initialize() {
this.activeRouteEnabled = false;
this.isAuthorized = Session.isClaimsCurator(this.user) || Session.isPortalAdministrator(this.user);
if (this.properties.adminToolsAPIURL && this.communityId) {
this.config.getCommunityInformation(this.properties, this.communityId).subscribe(data => {
for (var i = 0; i < data['entities'].length; i++) {
this.showEntity["" + data['entities'][i]["pid"] + ""] = data['entities'][i]["isEnabled"];
}
for (var i = 0; i < data['pages'].length; i++) {
this.showPage[data['pages'][i]["route"]] = data['pages'][i]["isEnabled"];
}
},
error => {
this.handleError("Error getting community information (e.g. pages,entities) for community with id: " + this.communityId, error);
});
}
}
onClick(id: string) {
var el: HTMLElement = document.getElementById(id);
el.classList.remove('uk-open');
}
isEnabled(required, enabled) {
if (!required) {
return true;
}
for (let requiredEntity of required) {
if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
return false;
}
}
return true;
}
isAtleastOneEnabled(required, enabled) {
if (!required || required.length == 0) {
return true;
}
var count = required.length;
for (let requiredEntity of required) {
if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
count--;
}
}
return (count > 0) ? true : false;
}
private handleError(message: string, error) {
console.error("NavigationBar (component): " + message, error);
}
getCurrentRoute() {
return this.router.url.split('?')[0];
}
isTheActiveMenu(menu: RootMenuItem): boolean {
let currentRoute = this.getCurrentRoute();
if (!menu.rootItem.markAsActive) {
return false;
}
if (currentRoute == menu.rootItem.route) {
this.activeRouteEnabled = true;
return true;
} else if (menu.items.length > 0) {
for (let menuItem of menu.items) {
if (menuItem.route == currentRoute) {
this.activeRouteEnabled = true;
return true;
}
}
}
return false;
}
}