import { Component } from "@angular/core" ; import { MenuItem } from "../menu-item" ; import { NavigationMenuConfig } from "../navigation-menu-config.interface" ; import { ConfigService } from "../../config/config.service" ; import { UserRoleGuard } from "../../permissions/user-role.guard" ; import { UntilDestroy , untilDestroyed } from "@ngneat/until-destroy" ; import { PREFIX_VIEW_CONFIG , ViewConfig , } from "../../view/dynamic-routing/view-config.interface" ; /**
 * Main app menu listing.
 */ @ UntilDestroy ( ) @ Component ( { selector : "app-navigation" , templateUrl : "./navigation.component.html" , styleUrls : [ "./navigation.component.scss" ] , } ) export class NavigationComponent { /** name of config array in the config json file */ private readonly CONFIG_ID = "navigationMenu" ; /** all menu items to be displayed */ public menuItems : MenuItem [ ] = [ ] ; constructor ( private userRoleGuard , private configService ) { this . configService . configUpdates . pipe ( untilDestroyed ( this ) ) . subscribe ( ( ) => this . initMenuItemsFromConfig ( ) ) ; } /**
     * Load menu items from config file
     */ private initMenuItemsFromConfig ( ) { this . menuItems = [ ] ; const config = this . configService . getConfig < NavigationMenuConfig > ( this . CONFIG_ID ) ; for ( const configItem of config . items ) { if ( this . checkMenuItemPermissions ( configItem . link ) ) { this . menuItems . push ( new MenuItem ( configItem . name , configItem . icon , configItem . link ) ) ; } } } /**
     * Check whether the user has the required rights
     */ private checkMenuItemPermissions ( link ) { const configPath = link . replace ( /^\// , "" ) ; const userRoles = this . configService . getConfig < ViewConfig > ( PREFIX_VIEW_CONFIG + configPath ) ?. permittedUserRoles ; return this . userRoleGuard . canActivate ( { routeConfig : { path : configPath } , data : { permittedUserRoles : userRoles } , } as any ) ; } }