a bit more of a refactoring

This commit is contained in:
nixa 2016-05-13 18:40:34 +03:00
parent 1d6c0554a3
commit b50775a96a
3 changed files with 27 additions and 12 deletions

View file

@ -12,7 +12,9 @@ export class ContentTop {
constructor(private _state:AppState) {
this._state.subscribe('menu.activeLink', (activeLink) => {
this.activePageTitle = activeLink.title;
if (activeLink) {
this.activePageTitle = activeLink.title;
}
});
}
}

View file

@ -96,7 +96,7 @@ export class Sidebar {
private _selectMenuItem(currentPath = null) {
let currentMenu = this._sidebarService.selectMenuItem(this._router, this.menuItems, currentPath);
let currentMenu = this._sidebarService.setRouter(this._router).selectMenuItem(this.menuItems, currentPath);
this._state.notifyDataChanged('menu.activeLink', currentMenu);
}
}

View file

@ -4,38 +4,51 @@ import {menuItems} from '../../../app.menu';
@Injectable()
export class SidebarService {
private _router;
getMenuItems() {
return menuItems;
}
selectMenuItem(router, items:Array<any>, currentPath:string) {
setRouter(router) {
this._router = router;
return this;
}
selectMenuItem(items:Array<any>, currentPath:string) {
let currentMenu;
let assignCurrent = (menu) => (menu.selected ? currentMenu = menu : null);
items.forEach((menu: any) => {
menu.selected = this._isCurrent(router, this._generateRoute(router, [menu.component]));
menu.expanded = menu.expanded || menu.selected;
this._selectItem(currentPath, [menu.component], menu);
assignCurrent(menu);
if (menu.subMenu) {
menu.subMenu.forEach((subMenu) => {
let route = this._generateRoute(router, [menu.component, subMenu.component]);
subMenu.selected = !subMenu.disabled && this._isCurrent(router, route) && this._resolvePath(route, '') == currentPath;
assignCurrent(menu);
this._selectItem(currentPath, [menu.component, subMenu.component], subMenu, menu);
assignCurrent(subMenu);
});
}
});
return currentMenu;
}
private _isCurrent(router, route) {
return route ? router.isRouteActive(route) : false;
private _selectItem(currentPath, instructions, item, parentMenu = null) {
let route = this._generateRoute(instructions);
item.selected = !item.disabled && this._isCurrent(route) && this._resolvePath(route, '') == currentPath;
if (parentMenu) {
parentMenu.expanded = parentMenu.expanded || item.selected;
}
}
private _generateRoute(router, instructions) {
return instructions.filter(i => typeof i !== 'undefined').length > 0 ? router.generate(instructions) : null;
private _isCurrent(route) {
return route ? this._router.isRouteActive(route) : false;
}
private _generateRoute(instructions) {
return instructions.filter(i => typeof i !== 'undefined').length > 0 ? this._router.generate(instructions) : null;
}
private _resolvePath(instruction, collected) {