2016-05-11 17:38:01 +03:00
|
|
|
import {Component, ElementRef, HostListener, ViewEncapsulation} from '@angular/core';
|
|
|
|
|
import {Router} from '@angular/router-deprecated';
|
2016-04-27 16:02:27 +03:00
|
|
|
|
2016-05-04 11:49:36 +03:00
|
|
|
import {ThemeGlobal, layoutSizes} from '../../../theme';
|
2016-04-27 16:02:27 +03:00
|
|
|
import {SidebarService} from './sidebar.service';
|
2016-04-27 13:17:51 +03:00
|
|
|
|
|
|
|
|
@Component({
|
2016-04-29 17:27:19 +03:00
|
|
|
selector: 'sidebar',
|
|
|
|
|
encapsulation: ViewEncapsulation.None,
|
|
|
|
|
styles: [require('./sidebar.scss')],
|
|
|
|
|
template: require('./sidebar.html'),
|
|
|
|
|
providers: [SidebarService],
|
|
|
|
|
directives: [],
|
|
|
|
|
pipes: []
|
2016-04-27 13:17:51 +03:00
|
|
|
})
|
2016-04-27 16:02:27 +03:00
|
|
|
export class Sidebar {
|
|
|
|
|
|
2016-04-29 17:27:19 +03:00
|
|
|
menuItems:Array<any>;
|
|
|
|
|
menuHeight:number;
|
|
|
|
|
isMenuCollapsed:boolean;
|
2016-04-28 13:08:33 +03:00
|
|
|
|
2016-04-29 17:27:19 +03:00
|
|
|
showHoverElem:boolean;
|
|
|
|
|
hoverElemHeight:number;
|
|
|
|
|
hoverElemTop:number;
|
2016-04-28 16:44:32 +03:00
|
|
|
|
2016-04-29 17:27:19 +03:00
|
|
|
outOfArea:number = -200;
|
2016-04-29 12:57:44 +03:00
|
|
|
|
2016-04-29 17:27:19 +03:00
|
|
|
isMenuShouldCollapsed:boolean = false;
|
2016-04-28 17:52:30 +03:00
|
|
|
|
2016-05-02 12:45:56 +03:00
|
|
|
constructor(private _elementRef:ElementRef,
|
|
|
|
|
private _router:Router,
|
2016-04-29 17:27:19 +03:00
|
|
|
private _sidebarService:SidebarService,
|
2016-05-02 14:40:12 +03:00
|
|
|
private _themeGlobal:ThemeGlobal) {
|
2016-05-02 12:45:56 +03:00
|
|
|
|
2016-04-29 17:27:19 +03:00
|
|
|
}
|
2016-04-28 13:08:33 +03:00
|
|
|
|
2016-04-28 15:08:48 +03:00
|
|
|
ngOnInit() {
|
2016-05-02 12:45:56 +03:00
|
|
|
this.menuItems = this._sidebarService.getMenuItems();
|
2016-04-28 13:08:33 +03:00
|
|
|
this.selectMenuItem();
|
2016-05-02 12:45:56 +03:00
|
|
|
this._router.root.subscribe(() => this.selectMenuItem());
|
2016-04-28 13:08:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: is it really the best event for this kind of things?
|
|
|
|
|
ngAfterViewInit() {
|
2016-04-29 11:49:49 +03:00
|
|
|
this.updateSidebarHeight();
|
2016-04-28 13:08:33 +03:00
|
|
|
}
|
|
|
|
|
|
2016-04-28 17:52:30 +03:00
|
|
|
@HostListener('window:resize', ['$event'])
|
|
|
|
|
onWindowResize($event) {
|
|
|
|
|
|
|
|
|
|
var isMenuShouldCollapsed = $event.target.innerWidth <= layoutSizes.resWidthCollapseSidebar;
|
|
|
|
|
|
|
|
|
|
if (this.isMenuShouldCollapsed !== isMenuShouldCollapsed) {
|
2016-04-29 11:49:49 +03:00
|
|
|
this.menuCollapseStateChange(isMenuShouldCollapsed);
|
2016-04-28 17:52:30 +03:00
|
|
|
}
|
|
|
|
|
this.isMenuShouldCollapsed = isMenuShouldCollapsed;
|
2016-04-29 11:49:49 +03:00
|
|
|
this.updateSidebarHeight();
|
2016-04-28 17:52:30 +03:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 17:27:19 +03:00
|
|
|
menuExpand() {
|
2016-04-29 11:49:49 +03:00
|
|
|
this.menuCollapseStateChange(false);
|
2016-04-28 13:08:33 +03:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 17:27:19 +03:00
|
|
|
menuCollapse() {
|
2016-04-29 11:49:49 +03:00
|
|
|
this.menuCollapseStateChange(true);
|
2016-04-28 13:08:33 +03:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 11:49:49 +03:00
|
|
|
menuCollapseStateChange(isCollapsed) {
|
|
|
|
|
this.isMenuCollapsed = isCollapsed;
|
2016-05-02 14:40:12 +03:00
|
|
|
this._themeGlobal.setData('menu.isCollapsed', this.isMenuCollapsed);
|
2016-04-29 11:49:49 +03:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 17:27:19 +03:00
|
|
|
hoverItem($event) {
|
2016-04-28 16:44:32 +03:00
|
|
|
this.showHoverElem = true;
|
2016-04-29 17:27:19 +03:00
|
|
|
this.hoverElemHeight = $event.currentTarget.clientHeight;
|
2016-04-28 16:44:32 +03:00
|
|
|
// TODO: get rid of magic 66 constant
|
|
|
|
|
this.hoverElemTop = $event.currentTarget.getBoundingClientRect().top - 66;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 11:49:49 +03:00
|
|
|
updateSidebarHeight() {
|
|
|
|
|
// TODO: get rid of magic 84 constant
|
2016-05-02 12:45:56 +03:00
|
|
|
this.menuHeight = this._elementRef.nativeElement.childNodes[0].clientHeight - 84;
|
2016-04-29 11:49:49 +03:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 17:27:19 +03:00
|
|
|
toggleSubMenu($event, item) {
|
2016-04-28 13:08:33 +03:00
|
|
|
var submenu = $($event.currentTarget).next();
|
|
|
|
|
|
|
|
|
|
if (this.isMenuCollapsed) {
|
|
|
|
|
this.menuExpand();
|
|
|
|
|
if (!item.expanded) {
|
2016-04-28 16:37:07 +03:00
|
|
|
item.expanded = !item.expanded;
|
|
|
|
|
submenu.slideToggle();
|
2016-04-28 13:08:33 +03:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
item.expanded = !item.expanded;
|
2016-04-28 16:37:07 +03:00
|
|
|
submenu.slideToggle();
|
2016-04-28 13:08:33 +03:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 11:06:39 +03:00
|
|
|
// TODO: there is a bug in the router thus all child routers are considered as active
|
2016-04-28 13:08:33 +03:00
|
|
|
private selectMenuItem() {
|
2016-05-02 14:40:12 +03:00
|
|
|
let currentMenu;
|
|
|
|
|
|
2016-05-02 16:55:58 +03:00
|
|
|
let isCurrent = (instructions) => (instructions.filter(i => typeof i !== 'undefined').length > 0 ? this._router.isRouteActive(this._router.generate(instructions)) : false);
|
2016-05-02 14:40:12 +03:00
|
|
|
let assignCurrent = (menu) => (menu.selected ? currentMenu = menu : null);
|
2016-04-28 13:08:33 +03:00
|
|
|
|
2016-05-02 12:45:56 +03:00
|
|
|
this.menuItems.forEach(function (menu: any) {
|
2016-04-28 15:08:48 +03:00
|
|
|
|
2016-05-02 16:55:58 +03:00
|
|
|
menu.selected = isCurrent([menu.name]);
|
2016-04-28 13:08:33 +03:00
|
|
|
menu.expanded = menu.expanded || menu.selected;
|
2016-05-02 14:40:12 +03:00
|
|
|
assignCurrent(menu);
|
2016-04-28 15:08:48 +03:00
|
|
|
|
2016-04-28 13:08:33 +03:00
|
|
|
if (menu.subMenu) {
|
|
|
|
|
menu.subMenu.forEach(function (subMenu) {
|
2016-05-02 16:55:58 +03:00
|
|
|
subMenu.selected = isCurrent([menu.name, subMenu.name]) && !subMenu.disabled;
|
2016-05-02 14:40:12 +03:00
|
|
|
assignCurrent(menu);
|
2016-04-28 13:08:33 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-05-03 11:06:39 +03:00
|
|
|
|
2016-05-02 14:40:12 +03:00
|
|
|
// notifies all subscribers
|
|
|
|
|
this._themeGlobal.setData('menu.activeLink', currentMenu);
|
2016-04-27 16:02:27 +03:00
|
|
|
}
|
|
|
|
|
}
|