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

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2016-07-07 16:30:00 +03:00
import {Component, ElementRef, HostListener, ViewEncapsulation} from '@angular/core';
import {GlobalState} from '../../../global.state';
2016-07-07 16:30:00 +03:00
import {layoutSizes} from '../../../theme';
2016-04-27 13:17:51 +03:00
import 'style-loader!./baSidebar.scss';
2016-04-27 13:17:51 +03:00
@Component({
2016-05-18 17:38:46 +03:00
selector: 'ba-sidebar',
templateUrl: './baSidebar.html'
2016-04-27 13:17:51 +03:00
})
2016-05-18 17:38:46 +03:00
export class BaSidebar {
public menuHeight:number;
2016-05-19 13:11:31 +03:00
public isMenuCollapsed:boolean = false;
2016-05-18 17:38:46 +03:00
public isMenuShouldCollapsed:boolean = false;
constructor(private _elementRef:ElementRef, private _state:GlobalState) {
2016-06-16 16:15:19 +03:00
this._state.subscribe('menu.isCollapsed', (isCollapsed) => {
this.isMenuCollapsed = isCollapsed;
});
}
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 {
setTimeout(() => 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-19 13:11:31 +03:00
public menuCollapseStateChange(isCollapsed:boolean):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 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
private _shouldMenuCollapse():boolean {
2016-05-18 15:02:23 +03:00
return window.innerWidth <= layoutSizes.resWidthCollapseSidebar;
}
2016-04-27 16:02:27 +03:00
}