A simple menu module for Angular applications

How to install
npm i @nwx/menu |OR| yarn add @nwx/menu
How to use
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { MenuModule } from '@nwx/menu';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, MenuModule],
bootstrap: [AppComponent]
})
export class AppModule {}
import { Component } from '@angular/core';
import { MenuService, MenuItem } from '@nwx/menu';
export const AppMenuTree: MenuItem[] = [
{
name: 'Admin',
icon: 'wrench',
permissions: [
'admin_superuser',
'admin_staff',
'admin_finance',
'admin_hr'
],
children: [
{
name: 'Accounts',
icon: 'account',
link: '/admin/accounts/profile'
},
{
name: 'Settings',
icon: 'account-card-details',
link: '/admin/accounts/settings',
fullspan: true,
permissions: ['admin_root', 'admin_staff', 'admin_hr']
},
{
name: 'Subscriptions',
icon: ' account-multiple-check',
link: '/admin/accounts/Subscriptions',
fullspan: true,
permissions: ['admin_root', 'admin_staff', 'admin_finance']
}
]
},
{
name: 'Stocks',
icon: 'trending-up',
children: [
{
name: 'Sandbox Portfolio',
icon: 'account-check',
link: '/finance/stocks/own'
},
{
name: 'Wishlist',
icon: 'playlist-check',
link: '/finance/stocks/wishlist',
disabled: true
},
{
name: 'Portfolio',
icon: 'account-check',
link: '/finance/stocks/own',
permissions: ['subscriptions_level_1']
},
{
name: 'Trade',
icon: ' home-currency-usd',
link: '/finance/stocks/trade',
permissions: ['subscriptions_level_1']
}
]
},
{
name: 'Yahoo Finance',
icon: 'google-analytics',
link: 'https://yahoo.com',
external: true
},
{
name: 'Youtube',
icon: 'youtube',
link: 'https://youtube.com',
external: true,
target: '_blank'
}
];
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Neekware';
menuTree = null;
user: {
firstName: 'Val';
lastName: 'Neekman';
permissions: ['subscriptions_level_1'];
};
constructor(public menu: MenuService) {
this.log.info('AppComponent loaded ...');
this.buildMenu();
}
private hasPermission(node: MenuItem) {
const menuPerms = node.permissions || [];
if (menuPerms.length === 0) {
return true;
}
if (!this.user) {
return false;
}
const userPerms = this.user.permissions || [];
if (menuPerms.length === 0) {
return false;
}
const hasPerm = menuPerms.some(value => userPerms.indexOf(value) >= 0);
return hasPerm;
}
buildMenu() {
this.menu.PermissionVerificationFuncType(this.hasPermission.bind(this));
this.menuTree = this.menu.buildMenuTree(AppMenuTree);
}
}
Advanced usage:
Here are all the options for each menu items.
export class MenuItem {
name: string;
icon?: string;
link?: string;
fullspan?: boolean;
external?: boolean;
target?: string;
disabled?: boolean;
allowed?: boolean;
permissions?: string[];
children?: MenuItem[];
}
Here are the helper functions on each MenuNode.
for (const node in menuTree.children) {
}
Running the tests
To run the tests against the current environment:
npm run ci:all
License
Released under a (MIT) license.
Version
X.Y.Z Version
`MAJOR` version -- making incompatible API changes
`MINOR` version -- adding functionality in a backwards-compatible manner
`PATCH` version -- making backwards-compatible bug fixes