activate menu item

This commit is contained in:
nixa 2016-05-02 12:45:56 +03:00
parent 0a4cc28447
commit 0d8f5419ae
3 changed files with 28 additions and 67 deletions

View file

@ -10,7 +10,7 @@ import {PageTop, Sidebar} from '../theme';
encapsulation: ViewEncapsulation.None, encapsulation: ViewEncapsulation.None,
styles: [], styles: [],
directives: [PageTop, Sidebar], directives: [PageTop, Sidebar],
template: `<sidebar [routes]="getRoutes()"></sidebar><page-top></page-top><router-outlet></router-outlet>` template: `<sidebar></sidebar><page-top></page-top><router-outlet></router-outlet>`
}) })
@RouteConfig([ @RouteConfig([
{ {
@ -18,51 +18,18 @@ import {PageTop, Sidebar} from '../theme';
component: Dashboard, component: Dashboard,
path: '/dashboard', path: '/dashboard',
useAsDefault: true, useAsDefault: true,
data: {
title: 'Dashboard',
selected: false,
expanded: false,
sidebarMeta: {
icon: 'ion-android-home',
order: 0,
}
}
}, },
{ {
name: 'Ui', name: 'Ui',
component: Ui, component: Ui,
path: '/ui/...', path: '/ui/...',
data: {
title: 'UI Features',
selected: false,
expanded: false,
sidebarMeta: {
icon: 'ion-android-laptop',
order: 200,
}
}
}, },
]) ])
export class Pages { export class Pages {
private _routeConfig; constructor() {
constructor(private _router:Router) {
} }
ngOnInit() { ngOnInit() {
} }
getRoutes() {
if (!this._routeConfig) {
this._routeConfig = Reflect.getMetadata('annotations', this.constructor)
.filter(a => {
return a.constructor.name === 'RouteConfig';
})
.pop();
}
return this._routeConfig;
}
} }

View file

@ -16,8 +16,6 @@ import {SidebarStateService} from './sidebarState.service';
}) })
export class Sidebar { export class Sidebar {
@Input('routes') routes;
menuItems:Array<any>; menuItems:Array<any>;
menuHeight:number; menuHeight:number;
isMenuCollapsed:boolean; isMenuCollapsed:boolean;
@ -30,15 +28,17 @@ export class Sidebar {
isMenuShouldCollapsed:boolean = false; isMenuShouldCollapsed:boolean = false;
constructor(private elementRef:ElementRef, constructor(private _elementRef:ElementRef,
private router:Router, private _router:Router,
private _sidebarService:SidebarService, private _sidebarService:SidebarService,
private _sidebarStateService:SidebarStateService) { private _sidebarStateService:SidebarStateService) {
} }
ngOnInit() { ngOnInit() {
this.menuItems = this._sidebarService.getMenuItems(this.routes); this.menuItems = this._sidebarService.getMenuItems();
this.selectMenuItem(); this.selectMenuItem();
this._router.root.subscribe(() => this.selectMenuItem());
} }
// TODO: is it really the best event for this kind of things? // TODO: is it really the best event for this kind of things?
@ -80,7 +80,7 @@ export class Sidebar {
updateSidebarHeight() { updateSidebarHeight() {
// TODO: get rid of magic 84 constant // TODO: get rid of magic 84 constant
this.menuHeight = this.elementRef.nativeElement.childNodes[0].clientHeight - 84; this.menuHeight = this._elementRef.nativeElement.childNodes[0].clientHeight - 84;
} }
toggleSubMenu($event, item) { toggleSubMenu($event, item) {
@ -100,9 +100,9 @@ export class Sidebar {
} }
private selectMenuItem() { private selectMenuItem() {
let isCurrent = (instruction) => (instruction ? this.router.isRouteActive(this.router.generate([instruction])) : false); let isCurrent = (instruction) => (instruction ? this._router.isRouteActive(this._router.generate([instruction])) : false);
this.menuItems.forEach(function (menu) { this.menuItems.forEach(function (menu: any) {
menu.selected = isCurrent(menu.name); menu.selected = isCurrent(menu.name);
menu.expanded = menu.expanded || menu.selected; menu.expanded = menu.expanded || menu.selected;

View file

@ -4,6 +4,22 @@ import {Injectable} from 'angular2/core';
export class SidebarService { export class SidebarService {
staticMenuItems = [ staticMenuItems = [
{
title: 'Dashboard',
name: 'Dashboard',
icon: 'ion-android-home',
selected: false,
expanded: false,
order: 0
},
{
title: 'UI Features',
name: 'Ui',
icon: 'ion-android-laptop',
selected: false,
expanded: false,
order: 200
},
{ {
title: 'Pages', title: 'Pages',
icon: 'ion-document', icon: 'ion-document',
@ -58,29 +74,7 @@ export class SidebarService {
constructor() { constructor() {
} }
getMenuItems(routes) { getMenuItems() {
return this.staticMenuItems;
let menuItems = routes.configs
.filter(function (s) {
return s.data.sidebarMeta != null;
})
.map(function (s) {
var meta = s.data.sidebarMeta;
return {
title: s.data.title,
name: s.name,
level: 0,
order: meta.order,
icon: meta.icon
};
})
.sort(function (a, b) {
return (a.level - b.level) * 100 + a.order - b.order;
})
.filter(function (item) {
return item.level == 0;
});
return menuItems.concat(this.staticMenuItems);
} }
} }