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';
|
2016-07-08 18:03:48 +03:00
|
|
|
import {BaMenu} from '../baMenu';
|
|
|
|
|
import {routes} from '../../../../app/app.routes';
|
2016-07-17 19:41:00 +03:00
|
|
|
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'),
|
2016-07-08 18:03:48 +03:00
|
|
|
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
|
|
|
|
2016-07-08 18:03:48 +03:00
|
|
|
// here we declare which routes we want to use as a menu in our sidebar
|
2016-07-17 19:41:00 +03:00
|
|
|
public routes = _.cloneDeep(routes); // we're creating a deep copy since we are going to change that object
|
2016-07-08 18:03:48 +03:00
|
|
|
|
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-04-28 17:52:30 +03:00
|
|
|
|
2016-05-02 12:45:56 +03:00
|
|
|
|
2016-07-08 18:03:48 +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-04-28 13:08:33 +03:00
|
|
|
}
|
|
|
|
|
|
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 {
|
2016-07-08 18:03:48 +03:00
|
|
|
setTimeout(() => this.updateSidebarHeight());
|
2016-04-28 13:08:33 +03:00
|
|
|
}
|
|
|
|
|
|
2016-05-18 15:02:23 +03:00
|
|
|
@HostListener('window:resize')
|
2016-05-18 17:38:46 +03:00
|
|
|
public onWindowResize():void {
|
2016-04-28 17:52:30 +03:00
|
|
|
|
2016-05-18 15:02:23 +03:00
|
|
|
var isMenuShouldCollapsed = this._shouldMenuCollapse();
|
2016-04-28 17:52:30 +03:00
|
|
|
|
|
|
|
|
if (this.isMenuShouldCollapsed !== isMenuShouldCollapsed) {
|
2016-04-29 11:49:49 +03:00
|
|
|
this.menuCollapseStateChange(isMenuShouldCollapsed);
|
2016-04-28 17:52:30 +03:00
|
|
|
}
|
|
|
|
|
this.isMenuShouldCollapsed = isMenuShouldCollapsed;
|
2016-04-29 11:49:49 +03:00
|
|
|
this.updateSidebarHeight();
|
2016-04-28 17:52:30 +03:00
|
|
|
}
|
|
|
|
|
|
2016-05-18 17:38:46 +03:00
|
|
|
public menuExpand():void {
|
2016-04-29 11:49:49 +03:00
|
|
|
this.menuCollapseStateChange(false);
|
2016-04-28 13:08:33 +03:00
|
|
|
}
|
|
|
|
|
|
2016-05-18 17:38:46 +03:00
|
|
|
public menuCollapse():void {
|
2016-04-29 11:49:49 +03:00
|
|
|
this.menuCollapseStateChange(true);
|
2016-04-28 13:08:33 +03:00
|
|
|
}
|
|
|
|
|
|
2016-05-19 13:11:31 +03:00
|
|
|
public menuCollapseStateChange(isCollapsed:boolean):void {
|
2016-04-29 11:49:49 +03:00
|
|
|
this.isMenuCollapsed = isCollapsed;
|
2016-05-13 13:34:55 +03:00
|
|
|
this._state.notifyDataChanged('menu.isCollapsed', this.isMenuCollapsed);
|
2016-04-29 11:49:49 +03:00
|
|
|
}
|
|
|
|
|
|
2016-05-18 17:38:46 +03:00
|
|
|
public updateSidebarHeight():void {
|
2016-04-29 11:49:49 +03:00
|
|
|
// 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 11:49:49 +03:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|