From 8340acec7ab6b07731960c8b8bb7b3a2a399608a Mon Sep 17 00:00:00 2001 From: nixa <4dmitr@gmail.com> Date: Thu, 14 Jul 2016 12:05:05 +0300 Subject: [PATCH] fix(menu): fix #84 page title and breadcrumb not showing up --- .../components/baMenu/baMenu.component.ts | 21 +++++++++++++++---- .../theme/components/baMenu/baMenu.service.ts | 10 +++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/app/theme/components/baMenu/baMenu.component.ts b/src/app/theme/components/baMenu/baMenu.component.ts index c20f2756..471eca23 100644 --- a/src/app/theme/components/baMenu/baMenu.component.ts +++ b/src/app/theme/components/baMenu/baMenu.component.ts @@ -5,6 +5,7 @@ import {Subscription} from 'rxjs/Rx'; import {BaSlimScroll} from '../../../theme/directives'; import {BaMenuService} from './baMenu.service'; import {BaMenuItem} from './components/baMenuItem'; +import {AppState} from '../../../app.state'; @Component({ selector: 'ba-menu', @@ -29,15 +30,27 @@ export class BaMenu { protected _onRouteChange:Subscription; public outOfArea:number = -200; - constructor(private _router:Router, private _service:BaMenuService) { - + constructor(private _router:Router, private _service:BaMenuService, private _state:AppState) { this._onRouteChange = this._router.events.subscribe((event) => { - if (this.menuItems && event instanceof NavigationEnd) { - this.menuItems = this._service.selectMenuItem(this.menuItems); + + if (event instanceof NavigationEnd) { + if (this.menuItems) { + this.selectMenuAndNotify(); + } else { + // on page load we have to wait as event is fired before menu elements are prepared + setTimeout(() => this.selectMenuAndNotify()); + } } }); } + public selectMenuAndNotify():void { + if (this.menuItems) { + this.menuItems = this._service.selectMenuItem(this.menuItems); + this._state.notifyDataChanged('menu.activeLink', this._service.getCurrentItem()); + } + } + public ngOnInit():void { this.menuItems = this._service.convertRoutesToMenus(this.menuRoutes); } diff --git a/src/app/theme/components/baMenu/baMenu.service.ts b/src/app/theme/components/baMenu/baMenu.service.ts index 71ca44bb..c4084404 100644 --- a/src/app/theme/components/baMenu/baMenu.service.ts +++ b/src/app/theme/components/baMenu/baMenu.service.ts @@ -4,6 +4,8 @@ import {Router, UrlTree, RouterConfig} from '@angular/router'; @Injectable() export class BaMenuService { + protected _currentMenuItem = {}; + constructor(private _router:Router) { } @@ -12,11 +14,19 @@ export class BaMenuService { return this._skipEmpty(items); } + public getCurrentItem():any { + return this._currentMenuItem; + } + public selectMenuItem(menuItems:any[]):any[] { let items = []; menuItems.forEach((item) => { this._selectItem(item); + if (item.selected) { + this._currentMenuItem = item; + } + if (item.children && item.children.length > 0) { item.children = this.selectMenuItem(item.children); }