/*-------------------------------------------------------------------------------------------------------------- * 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 } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { InaxTranslateService } from '../../../../@inax/translate'; import { Subscription } from 'rxjs/Rx'; //import { ButtonMenuUiResourceManager } from './../resources/resource.service'; import { IMenuItem } from '../../../menu/src/interfaces/menuItem'; @Component({ selector: 'menu-button', templateUrl: './@inax/buttonMenuUi/src/menuButton/menuButton.component.html', styleUrls: ['./@inax/buttonMenuUi/src/menuButton/menuButton.component.css'] }) export class MenuButtonComponent implements OnInit, OnDestroy{ private _isSelected: boolean = false; private _subscription: Subscription; @Input() public id:number; @Input() public menuItem:IMenuItem; @Output() public onSelected = new EventEmitter(); public get isSelected():boolean{ return this.menuItem.isSelected && !this.menuItem.isDisabled; } public get isDisabled():boolean{ return this.menuItem.isDisabled; } public get text():string{ return this.menuItem.text; } public get isRoot():boolean{ return this.menuItem.parent == null; } /** * constructor. * @param {Router} _router This is the router, it will be used to pass it to the MenuProvider, because only components get the * router injected. */ constructor(private _router:Router) { } /** * This method will be called when the component is initializing * @method ngOnInit */ public ngOnInit() { if(this._subscription == null && this.menuItem.provider != null){ this._subscription = this.menuItem.provider.selectionChangedNotifier.subscribe(entry =>{ if(entry == this.menuItem) this.onSelect(); }) } if(this.menuItem != null && this.menuItem.isSelected){ this.onSelect(); } } /** * This method will be called when the component was destroyed * @method ngOnDestroy */ public ngOnDestroy(){ if(this._subscription != null){ this._subscription.unsubscribe(); this._subscription = null; } } /** * This method will be called if anybody clicks on the button. * If the menuItem isn't diabled, the selectAction will be called, and if the result is true, the selction change will * be emitted to the line. */ public onSelect(){ if(!this.menuItem.isDisabled){ //Call the select action and then emit the item id to the parent if(this.menuItem.selectAction()){ this.onSelected.emit(this.menuItem); } } } }