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

111 lines
3 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-05-18 17:38:46 +03:00
import {BaSidebarService} from './baSidebar.service';
2016-04-27 13:17:51 +03:00
@Component({
2016-05-18 17:38:46 +03:00
selector: 'ba-sidebar',
2016-04-29 17:27:19 +03:00
encapsulation: ViewEncapsulation.None,
styles: [require('./sidebar.scss')],
template: require('./sidebar.html'),
2016-05-18 17:38:46 +03:00
providers: [BaSidebarService]
2016-04-27 13:17:51 +03:00
})
2016-05-18 17:38:46 +03:00
export class BaSidebar {
2016-04-27 16:02:27 +03:00
2016-05-18 17:38:46 +03:00
public menuItems:Array<any>;
public menuHeight:number;
public isMenuCollapsed:boolean;
2016-05-18 17:38:46 +03:00
public showHoverElem:boolean;
public hoverElemHeight:number;
public hoverElemTop:number;
2016-04-28 16:44:32 +03:00
2016-05-18 17:38:46 +03:00
public outOfArea:number = -200;
2016-05-18 17:38:46 +03:00
public isMenuShouldCollapsed:boolean = false;
2016-05-02 12:45:56 +03:00
constructor(private _elementRef:ElementRef,
private _router:Router,
2016-05-18 17:38:46 +03:00
private _sidebarService:BaSidebarService,
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 17:38:46 +03:00
public ngOnInit():void {
2016-05-18 15:02:23 +03:00
if (this._shouldMenuCollapse()) {
this.menuCollapse();
}
}
2016-05-18 17:38:46 +03:00
public ngAfterViewInit():void {
this.updateSidebarHeight();
}
2016-05-18 15:02:23 +03:00
@HostListener('window:resize')
2016-05-18 17:38:46 +03:00
public onWindowResize():void {
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-05-18 17:38:46 +03:00
public menuExpand():void {
this.menuCollapseStateChange(false);
}
2016-05-18 17:38:46 +03:00
public menuCollapse():void {
this.menuCollapseStateChange(true);
}
2016-05-18 17:38:46 +03:00
public menuCollapseStateChange(isCollapsed):void {
this.isMenuCollapsed = isCollapsed;
2016-05-13 13:34:55 +03:00
this._state.notifyDataChanged('menu.isCollapsed', this.isMenuCollapsed);
}
2016-05-18 17:38:46 +03:00
public hoverItem($event):void {
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-05-18 17:38:46 +03:00
public updateSidebarHeight():void {
// 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-05-18 17:38:46 +03:00
public toggleSubMenu($event, item):boolean {
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 17:38:46 +03:00
private _shouldMenuCollapse():boolean {
2016-05-18 15:02:23 +03:00
return window.innerWidth <= layoutSizes.resWidthCollapseSidebar;
}
2016-05-18 17:38:46 +03:00
private _selectMenuItem(currentPath = null):void {
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
}
}