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

77 lines
2.1 KiB
TypeScript
Raw Normal View History

2016-07-07 16:30:00 +03:00
import {Component, ElementRef, HostListener, ViewEncapsulation} from '@angular/core';
import {AppState} from '../../../app.state';
import {layoutSizes} from '../../../theme';
import {BaMenu} from '../baMenu';
import {routes} from '../../../../app/app.routes';
import * as _ from 'lodash';
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,
2016-05-18 17:48:15 +03:00
styles: [require('./baSidebar.scss')],
template: require('./baSidebar.html'),
providers: [],
directives: [BaMenu]
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
// here we declare which routes we want to use as a menu in our sidebar
public routes = _.cloneDeep(routes); // we're creating a deep copy since we are going to change that object
2016-05-18 17:38:46 +03:00
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;
2016-05-02 12:45:56 +03:00
constructor(private _elementRef:ElementRef, private _state:AppState) {
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
}