ngx-admin/src/app/theme/components/sidebar/sidebar.component.ts

113 lines
2.9 KiB
TypeScript
Raw Normal View History

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-13 13:34:55 +03:00
import {AppState} from '../../../app.state';
import {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-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 17:27:19 +03:00
isMenuShouldCollapsed:boolean = false;
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-13 13:34:55 +03:00
private _state:AppState) {
2016-05-02 12:45:56 +03:00
this.menuItems = this._sidebarService.getMenuItems();
2016-05-13 16:11:32 +03:00
this._router.root.subscribe((path) => this._selectMenuItem(path));
}
2016-05-18 15:02:23 +03:00
ngOnInit() {
if (this._shouldMenuCollapse()) {
this.menuCollapse();
}
}
ngAfterViewInit() {
this.updateSidebarHeight();
}
2016-05-18 15:02:23 +03:00
@HostListener('window:resize')
onWindowResize() {
2016-05-18 15:02:23 +03:00
var isMenuShouldCollapsed = this._shouldMenuCollapse();
if (this.isMenuShouldCollapsed !== isMenuShouldCollapsed) {
this.menuCollapseStateChange(isMenuShouldCollapsed);
}
this.isMenuShouldCollapsed = isMenuShouldCollapsed;
this.updateSidebarHeight();
}
2016-04-29 17:27:19 +03:00
menuExpand() {
this.menuCollapseStateChange(false);
}
2016-04-29 17:27:19 +03:00
menuCollapse() {
this.menuCollapseStateChange(true);
}
menuCollapseStateChange(isCollapsed) {
this.isMenuCollapsed = isCollapsed;
2016-05-13 13:34:55 +03:00
this._state.notifyDataChanged('menu.isCollapsed', this.isMenuCollapsed);
}
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;
}
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 17:27:19 +03:00
toggleSubMenu($event, item) {
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();
}
} else {
item.expanded = !item.expanded;
2016-04-28 16:37:07 +03:00
submenu.slideToggle();
}
return false;
}
2016-05-18 15:02:23 +03:00
private _shouldMenuCollapse() {
return window.innerWidth <= layoutSizes.resWidthCollapseSidebar;
}
2016-05-13 16:11:32 +03:00
private _selectMenuItem(currentPath = null) {
2016-05-03 11:06:39 +03:00
2016-05-13 18:40:34 +03:00
let currentMenu = this._sidebarService.setRouter(this._router).selectMenuItem(this.menuItems, currentPath);
2016-05-13 13:34:55 +03:00
this._state.notifyDataChanged('menu.activeLink', currentMenu);
2016-04-27 16:02:27 +03:00
}
}