2016-05-11 17:38:01 +03:00
|
|
|
import {Injectable} from '@angular/core';
|
2016-05-13 16:11:32 +03:00
|
|
|
import {menuItems} from '../../../app.menu';
|
2016-07-06 21:26:34 +02:00
|
|
|
import {Router, UrlTree} from "@angular/router";
|
2016-04-27 16:02:27 +03:00
|
|
|
|
|
|
|
|
@Injectable()
|
2016-05-18 17:38:46 +03:00
|
|
|
export class BaSidebarService {
|
2016-04-27 16:02:27 +03:00
|
|
|
|
2016-07-06 21:26:34 +02:00
|
|
|
private _router:Router;
|
2016-05-13 18:40:34 +03:00
|
|
|
|
2016-05-18 17:38:46 +03:00
|
|
|
public getMenuItems():Array<Object> {
|
2016-05-13 16:11:32 +03:00
|
|
|
return menuItems;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 21:26:34 +02:00
|
|
|
public setRouter(router:Router):BaSidebarService {
|
2016-05-13 18:40:34 +03:00
|
|
|
this._router = router;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-29 12:01:38 +03:00
|
|
|
public selectMenuItem(items:Array<any>) {
|
2016-05-13 16:11:32 +03:00
|
|
|
let currentMenu;
|
|
|
|
|
|
|
|
|
|
let assignCurrent = (menu) => (menu.selected ? currentMenu = menu : null);
|
|
|
|
|
|
2016-07-06 21:26:34 +02:00
|
|
|
items.forEach((menu:any) => {
|
2016-05-13 16:11:32 +03:00
|
|
|
|
2016-07-06 21:26:34 +02:00
|
|
|
this._selectItem([menu.path], menu);
|
2016-05-13 16:11:32 +03:00
|
|
|
assignCurrent(menu);
|
2016-04-27 16:02:27 +03:00
|
|
|
|
2016-05-13 16:11:32 +03:00
|
|
|
if (menu.subMenu) {
|
|
|
|
|
menu.subMenu.forEach((subMenu) => {
|
2016-07-06 21:26:34 +02:00
|
|
|
this._selectItem([menu.path, subMenu.path], subMenu, menu);
|
2016-05-13 18:40:34 +03:00
|
|
|
assignCurrent(subMenu);
|
2016-05-13 16:11:32 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return currentMenu;
|
2016-04-29 17:27:19 +03:00
|
|
|
}
|
2016-04-28 13:08:33 +03:00
|
|
|
|
2016-06-29 12:01:38 +03:00
|
|
|
private _selectItem(instructions, item, parentMenu = null) {
|
2016-05-13 18:40:34 +03:00
|
|
|
let route = this._generateRoute(instructions);
|
2016-06-29 12:01:38 +03:00
|
|
|
item.selected = !item.disabled && this._isCurrent(route);
|
2016-05-13 18:40:34 +03:00
|
|
|
if (parentMenu) {
|
|
|
|
|
parentMenu.expanded = parentMenu.expanded || item.selected;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 21:26:34 +02:00
|
|
|
private _isCurrent(route:UrlTree):boolean {
|
|
|
|
|
if (!route)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return this._router.url === this._router.serializeUrl(route);
|
2016-05-13 16:11:32 +03:00
|
|
|
}
|
|
|
|
|
|
2016-07-06 21:26:34 +02:00
|
|
|
private _generateRoute(instructions:any[]):UrlTree {
|
|
|
|
|
if (!instructions)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
instructions = instructions.filter(item => !!item);
|
|
|
|
|
|
|
|
|
|
if (instructions.length === 0)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return this._router.createUrlTree(instructions);
|
2016-05-13 16:11:32 +03:00
|
|
|
}
|
2016-04-27 16:02:27 +03:00
|
|
|
}
|