import {Component, OnInit, Output, EventEmitter, Input} from '@angular/core'; import {NavigationStart, Router} from '@angular/router'; import { Utils } from '../../../utils/utils' import { SideNavConfig } from '../../../utils/interface'; import { SidePanelsService } from '../../../utils/side-panels.service'; import { KeycloakService } from '../../../utils/keycloak.service'; import { UserConfigService } from '../../../utils/user-config.service'; import { includes } from 'lodash'; import {CommonUIAppConfig, MockUserConfig} from '../../../mockup'; declare var CONFIG: any; @Component({ selector: 'esp-header', templateUrl: './header.component.html', styleUrls: ['./header.component.scss'], }) export class HeaderComponent implements OnInit { @Output() headerEvent: EventEmitter = new EventEmitter(); i18n: any; userDetails: any; sideBarMenuStatus = false; sideBarMenuConfig: SideNavConfig; contentOptions = []; shortUserName = 'US'; disableAlerts = true; showRefresh = true; systemDown = false; currentApp: string; urlPath: string; currentAppName: string; assetUrl: string = CONFIG.BASE_URL; newHeader = false; newStyledApps = ['cp', 'dp', 'fars', 'lcp', 'repl', 'cu', 'iop']; // Todo: Add new applications that have new HEADER UI menuOptions = []; @Input() menuOptionsInput: []; @Input() noConfig: boolean; // Only used for Common UI constructor( private sidePanel: SidePanelsService, private router: Router, private userConfig: UserConfigService ) {} ngOnInit(): void { if (!this.noConfig) { this.userConfig.headercast.subscribe(response => { this.userDetails = response ?? null; this.showRefresh = response && response['configJson'] ? response['configJson']['pageRefresh'] : false; this.i18n = response && response['i18n'] && ['translations'] ? response['i18n']['translations'] : null; if(this.i18n){ this.menuOptions = [{label:this.i18n['log-out'] || "Log Out",value:"logOut"}]; } this.shortUserName = response['shortUserName'] ?? 'US'; this.currentAppName = this.getCurrentAppName(response); this.checkAppAccess(); this.hasNewHeader(response['applicationName']); }); } else { this.userDetails = CommonUIAppConfig ?? null; this.showRefresh = CommonUIAppConfig && CommonUIAppConfig['configJson'] ? CommonUIAppConfig['configJson']['pageRefresh'] : false; this.i18n = CommonUIAppConfig && CommonUIAppConfig['i18n'] && ['translations'] ? CommonUIAppConfig['i18n']['translations'] : null; if(this.i18n){ this.menuOptions = [{label:this.i18n['log-out'] || "Log Out",value:"logOut"}]; } this.shortUserName = CommonUIAppConfig['shortUserName'] ?? 'US'; this.currentAppName = this.getCurrentAppName(CommonUIAppConfig); // this.checkAppAccess(); this.hasNewHeader(CommonUIAppConfig['applicationName']); //set MockuserConfig for Common UI this.userConfig.setMockUserConfig(MockUserConfig); } this.sidePanel.cast.subscribe((sidePanelConfig: SideNavConfig) => { this.sideBarMenuStatus = sidePanelConfig.status; }); this.router.events.subscribe(val => { this.disableAlerts = this.router.url === '/demand-forecasting/sense-check' || includes(this.router.url, '/demand-planning'); }); this.sidePanel.activeTab$.subscribe(isDisabled => { this.disableAlerts = isDisabled; }); this.userConfig.systemDownCast.subscribe(isSystemDown => { this.systemDown = isSystemDown; }) this.currentApp = this.userConfig.currentApp; this.router.events.subscribe(event => { if (event instanceof NavigationStart) { if (event.url !== undefined) { this.urlPath = this.convertUrl(event.url); this.userConfig.iopUrl.next(this.urlPath); } } }); if (this.menuOptionsInput && this.menuOptionsInput.length > 0) { this.menuOptionsInput.forEach(option => { this.menuOptions.push(option); }) } } hasNewHeader(appName): void { let returnBoolean = false; for (let i=0; i< this.newStyledApps.length; i++) { if (appName === this.newStyledApps[i]) { returnBoolean = true; break; } } this.newHeader = returnBoolean; } getCurrentAppName(response): string { if (response) { if (response['applicationName'] === 'vm') { return 'aivm'; } else { return response['applicationName']; } } else { return null; } } convertUrl(url): string { const result = url.includes('#') ? url.split('#')[0] : url; const resultArr = result.split('/'); if (resultArr[resultArr.length - 1] === 'rule-sets') { return 'order-promising' } else { return resultArr[resultArr.length - 1]; } } checkAppAccess() { const applications = this.userConfig.getApplicationsForUser(); const userconfig = this.userConfig.getUserConfig(); const { defaultApp } = userconfig; let defAppName = ''; const appName = Utils.getHeaderTitle(); let isAppAccess = false; if (appName === 'Login') { isAppAccess = true; } else if (appName !== 'Login' && applications.length > 0) { applications.forEach(app => { if (app.i18n['translations'][defaultApp]) { defAppName = app.i18n['translations'][defaultApp]; } if (app.i18n['translations'][app['applicationName']] === appName) { isAppAccess = true; } }); } } refreshPage(): void { this.userConfig.refreshPage({ refresh: true }); } toggleMenuSideBar(position: string, type: string): void { this.sideBarMenuStatus = !this.sideBarMenuStatus; // Check the interface file for more options to pass to the sideNavigation this.sideBarMenuConfig = { status: this.sideBarMenuStatus, position, showBackdrop: true, type, closeOnClickOutside: true, }; this.sidePanel.editSidePanelConfig(this.sideBarMenuConfig); } logout() { this.userConfig.addAuditLogs(this.currentAppName, 'logout'); setTimeout(()=> { this.headerEvent.emit({source: "logout", data: {}}); }, 200); } performRightMenuFunctions(options:any) { switch (options?.value || options) { case 'logOut': this.logout(); break; case 'Settings': this.sidePanel.headerSettingsSubs.next(true); break; } } }