/*-------------------------------------------------------------------------------------------------------------- * Copyright (c) insite-gmbh. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------------------------*/ import { Component, OnInit, OnDestroy, Input, Output, EventEmitter, Inject } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { Subscription } from 'rxjs/Rx'; import { IMENUPROVIDERSERVICE_TOKEN, IMenuProvider } from '../../../menu/src/interfaces/menuProvider'; import { IMenuItem } from '../../../menu/src/interfaces/menuItem'; import { DefaultMenuItem } from '../../../menu/src/default/defaultMenuItem'; import { EmptyMenuItem } from '../../../menu/src/default/emptyMenuItem'; @Component({ selector: 'button-menu', templateUrl: './@inax/buttonMenuUi/src/buttonMenu/buttonMenu.component.html', styleUrls: ['./@inax/buttonMenuUi/src/buttonMenu/buttonMenu.component.css'] }) /** * Base component for the ButtonMenu * @class ButtonMenuComponent * @classdesc Base component for the ButtonMenu * @author insite-gmbh */ export class ButtonMenuComponent implements OnInit, OnDestroy { private _subscription: Subscription; private _submenuSubscription: Subscription; public lines:Array> = []; public get numberOfLines():number{ return this.lines != null ? this.lines.length : 0; } /** * constructor. * @param {IMenuProvider} _menuProvider The menu provider is the service for the button menu, it is used to get the menu and * also to invoke the commands. */ constructor(@Inject(IMENUPROVIDERSERVICE_TOKEN) private _menuProvider: IMenuProvider) { } /** * This method will be called when the component is initializing * @method ngOnInit */ public ngOnInit() { this._subscription = this._menuProvider.subscribeToEntriesFor(null).subscribe(result => { this.lines = []; //Empty the array this.lines.push(this.createEmptyMenu(result)); this.lines.push(result); }); } /** * This method will be called when the component was destroyed * @method ngOnDestroy */ public ngOnDestroy() { if(this._subscription != null) this._subscription.unsubscribe(); if(this._submenuSubscription != null) this._submenuSubscription.unsubscribe(); } /** * This method will we called from the menu line to notify the menu when a selection was changed. * @method onSelected * @param {IMenuItem} parent Current selected parent menu item. */ public onSelected(parent:IMenuItem){ if(this._submenuSubscription != null) this._submenuSubscription.unsubscribe(); this._submenuSubscription = this._menuProvider.subscribeToEntriesFor(parent).subscribe(result => { this.lines = this.lines.reverse(); this.lines.pop(); this.lines.push(result); this.lines = this.lines.reverse(); }); } /** * This methods will get called if no main menu was selected, so it will generate a dummy submenu line. * @method registerEvents * @param {Array} mainLine The main menu */ private createEmptyMenu(mainLine:Array):Array{ let result = new Array(); if(mainLine != null && mainLine.length > 0){ let first = mainLine[0]; for(let i = 0; i < mainLine.length; i++){ result[i] = new EmptyMenuItem(i,first); } } return result; } }