openaire-library/sharedComponents/navigationBar.component.ts

180 lines
5.5 KiB
TypeScript

import {Component, Input, OnDestroy, OnInit} 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';
import {Subscription} from 'rxjs';
import {HelpContentService} from '../services/help-content.service';
export interface Header {
route?: string,
url?: string,
title: string,
logoUrl: string,
logoSmallUrl: string,
position: 'left' | 'center' | 'right',
badge: boolean
stickyAnimation?: boolean
}
@Component({
selector: 'navbar',
templateUrl: 'navigationBar.component.html'
})
export class NavigationBarComponent implements OnInit, OnDestroy {
@Input() portal: string = 'connect';
@Input() dark: boolean = false;
@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() header: Header;
@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';
@Input() showLogo: boolean = true;
@Input() offCanvasFlip: boolean = false;
keyword: string = '';
public isAuthorized: boolean = false;
subs: Subscription[] = [];
showEntity = {};
showPage = {};
specialAnnouncementContent: string = null;
public customMenuItems: MenuItem[] = [];
constructor(private router: Router,
private route: ActivatedRoute,
private config: ConfigurationService,
private _helpContentService: HelpContentService) {
}
ngOnInit() {
this.initialize();
}
ngOnDestroy() {
for (let sub of this.subs) {
sub.unsubscribe();
}
}
initialize() {
if ((['provide', 'develop']).indexOf(this.portal) != -1) {
this.header = {
route: '/',
url: null,
title: this.portal,
logoUrl: this.logoPath + 'logo-large-' + this.portal + '.png',
logoSmallUrl: this.logoPath + 'logo-small-' + this.portal + '.png',
position: 'left',
badge: true
};
}
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 => {
this.subs.push(this.config.communityInformationState.subscribe(data => {
if (data) {
if (data['entities']) {
for (var i = 0; i < data['entities'].length; i++) {
this.showEntity['' + data['entities'][i]['pid'] + ''] = data['entities'][i]['isEnabled'];
}
}
if (data['pages']) {
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);
}));
}
if(this.properties.environment == "development" && this.portal != 'connect' && this.portal != 'connect-admin' && this.properties.adminToolsPortalType == 'community') {
this.subs.push(
this._helpContentService.getMenuItems(this.portal).subscribe(
data => {
this.customMenuItems = data;
},
error => this.handleError("Server error fetching custom menu items", error)
)
);
}
}
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);
}
get currentRoute() {
return {
route: this.router.url.split('?')[0].split('#')[0],
fragment: this.route.snapshot.fragment
}
}
isTheActiveMenu(menu: RootMenuItem): boolean {
if (!menu.rootItem.markAsActive) {
return false;
}
if (this.isTheActiveMenuItem(menu.rootItem)) {
return true;
} else if (menu.items.length > 0) {
for (let menuItem of menu.items) {
if (this.isTheActiveMenuItem(menuItem)) {
return true;
}
}
}
return false;
}
isTheActiveMenuItem(menuItem: MenuItem): boolean {
let currentRoute = this.currentRoute;
return (menuItem.route == currentRoute.route || menuItem.route == (currentRoute.route + "/")) &&
currentRoute.fragment == menuItem.fragment;
}
}