openaire-library/sharedComponents/menu.ts

87 lines
2.7 KiB
TypeScript

export class MenuItem {
_id: string = ""; // for root menu in order to close the dropdown when clicked
title: string = "";
type: string = "internal";
url: string = ""; // external url
route: string = ""; // internal url - using angular routing and components
routeActive: string = ""; // route to check if it is active
needsAuthorization: boolean = false; // needs admin rights - mainly for user menu at this point
entitiesRequired: string[] = []; // openaire entities used in page "publication, dataset, organization, software, project, datasource"
routeRequired: string[] = []; // the routes that if aren't enable the menu item doesn't make sense
params: any = {};
fragment: string;
markAsActive: boolean;
items: MenuItem[] = [];
icon: string;
open: boolean;
customClass: string = null;
isFeatured: boolean;
constructor(id: string, title: string, url: string, route: string, needsAuthorization: boolean, entitiesRequired: string[], routeRequired: string[], params, items=null, icon=null, fragment = null, customClass = null, routeActive = null) {
this._id = id;
this.title = title;
this.url = url;
this.route = route;
this.routeActive = routeActive;
this.needsAuthorization = needsAuthorization;
this.entitiesRequired = entitiesRequired;
this.routeRequired = routeRequired;
this.params = params;
this.markAsActive = true;
this.items = items;
this.icon = icon;
this.fragment = fragment;
this.customClass = customClass;
}
public setMarkAsActive(showActive: boolean) {
this.markAsActive = showActive;
}
public static isTheActiveMenu(menu: MenuItem, currentRoute: string): boolean {
if (!menu.markAsActive) {
return false;
}
if (menu.route.length > 0 && ((menu.routeActive && currentRoute.includes(menu.routeActive)) || currentRoute == menu.route)) {
return true;
} else if (menu.items.length > 0) {
for (let menuItem of menu.items) {
if (menuItem.route == currentRoute || currentRoute.indexOf(menuItem.route) != -1) {
return true;
}
}
}
return false;
}
}
export class RootMenuItem {
rootItem: MenuItem;
items: MenuItem[] = [];
public static isTheActiveRootMenu(menu: RootMenuItem, currentRoute: string): boolean {
if (!menu.rootItem.markAsActive) {
return false;
}
if (currentRoute == menu.rootItem.route) {
return true;
} else if (menu.items.length > 0) {
for (let menuItem of menu.items) {
let isActive = MenuItem.isTheActiveMenu(menuItem, currentRoute);
if (isActive) {
return true;
}
}
return false;
}
}
}
export class SideMenuItem {
rootItem: MenuItem;
items: RootMenuItem[] = [];
ukIcon: string = '';
}