2016-04-27 16:02:27 +03:00
|
|
|
import {Component, ElementRef} from 'angular2/core';
|
|
|
|
|
|
|
|
|
|
import {SidebarService} from './sidebar.service';
|
2016-04-28 15:08:48 +03:00
|
|
|
import {Router} from 'angular2/router';
|
2016-04-27 13:17:51 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'sidebar',
|
|
|
|
|
styles: [ require('./sidebar.scss') ],
|
|
|
|
|
template: require('./sidebar.html'),
|
2016-04-27 16:02:27 +03:00
|
|
|
providers: [SidebarService],
|
2016-04-27 13:17:51 +03:00
|
|
|
directives: [],
|
|
|
|
|
pipes: []
|
|
|
|
|
})
|
2016-04-27 16:02:27 +03:00
|
|
|
export class Sidebar {
|
2016-04-28 13:08:33 +03:00
|
|
|
elementRef: ElementRef;
|
2016-04-28 15:08:48 +03:00
|
|
|
router: Router;
|
2016-04-27 16:02:27 +03:00
|
|
|
|
2016-04-28 13:08:33 +03:00
|
|
|
menuItems: Array<any>;
|
|
|
|
|
menuHeight: number;
|
|
|
|
|
isMenuCollapsed: boolean;
|
|
|
|
|
|
2016-04-28 15:08:48 +03:00
|
|
|
constructor(el: ElementRef, router: Router, private _sidebarService: SidebarService) {
|
2016-04-28 13:08:33 +03:00
|
|
|
this.elementRef = el;
|
2016-04-28 15:08:48 +03:00
|
|
|
this.router = router;
|
2016-04-27 16:02:27 +03:00
|
|
|
this.menuItems = this._sidebarService.getMenuItems();
|
2016-04-28 15:08:48 +03:00
|
|
|
}
|
2016-04-28 13:08:33 +03:00
|
|
|
|
2016-04-28 15:08:48 +03:00
|
|
|
ngOnInit() {
|
2016-04-28 13:08:33 +03:00
|
|
|
this.selectMenuItem();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: is it really the best event for this kind of things?
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
|
// TODO: get rid of magic 84 constant
|
|
|
|
|
this.menuHeight = this.elementRef.nativeElement.childNodes[0].clientHeight - 84;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
menuExpand () {
|
|
|
|
|
this.isMenuCollapsed = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
menuCollapse () {
|
|
|
|
|
this.isMenuCollapsed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toggleSubMenu ($event, item) {
|
|
|
|
|
var submenu = $($event.currentTarget).next();
|
|
|
|
|
|
|
|
|
|
if (this.isMenuCollapsed) {
|
|
|
|
|
this.menuExpand();
|
|
|
|
|
if (!item.expanded) {
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
item.expanded = !item.expanded;
|
|
|
|
|
|
|
|
|
|
// TODO: incomplete
|
|
|
|
|
// submenu.slideToggle();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
item.expanded = !item.expanded;
|
|
|
|
|
|
|
|
|
|
// TODO: incomplete
|
|
|
|
|
// submenu.slideToggle();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-28 15:08:48 +03:00
|
|
|
private isActive(instruction: any[]): boolean {
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-28 13:08:33 +03:00
|
|
|
private selectMenuItem() {
|
2016-04-28 15:08:48 +03:00
|
|
|
let isCurrent = (instruction) => (this.router.isRouteActive(this.router.generate([instruction])));
|
2016-04-28 13:08:33 +03:00
|
|
|
|
|
|
|
|
this.menuItems.forEach(function (menu) {
|
2016-04-28 15:08:48 +03:00
|
|
|
|
|
|
|
|
menu.selected = isCurrent(menu.name);
|
2016-04-28 13:08:33 +03:00
|
|
|
menu.expanded = menu.expanded || menu.selected;
|
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-04-28 15:08:48 +03:00
|
|
|
subMenu.selected = isCurrent(subMenu.name) && !subMenu.disabled;
|
2016-04-28 13:08:33 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-04-27 16:02:27 +03:00
|
|
|
}
|
|
|
|
|
}
|