ng-ytl-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
144 lines (125 loc) • 3.85 kB
text/typescript
import {
Component,
EventEmitter,
Host,
HostBinding,
HostListener,
Input,
Optional,
Output,
ViewEncapsulation,
} from '@angular/core';
import { toBoolean } from '../util/convert';
import { NzLayoutComponent } from './nz-layout.component';
export type NzBreakPoinit = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
export class NzSiderComponent {
private _collapsed = false;
private _collapsible = false;
private _trigger = true;
_dimensionMap = {
xl: '1600px',
lg: '1200px',
md: '992px',
sm: '768px',
xs: '480px',
};
_below = false;
nzWidth = '200';
nzCollapsedWidth = 64;
nzBreakpoint: NzBreakPoinit;
set nzTrigger(value: boolean) {
this._trigger = toBoolean(value);
}
get nzTrigger(): boolean {
return this._trigger;
}
set nzCollapsible(value: boolean) {
this._collapsible = toBoolean(value);
}
get nzCollapsible(): boolean {
return this._collapsible;
}
set nzCollapsed(value: boolean) {
this._collapsed = toBoolean(value);
}
get nzCollapsed(): boolean {
return this._collapsed;
}
nzCollapsedChange = new EventEmitter();
get setZeroClass(): boolean {
return this.nzCollapsed && (this.nzCollapsedWidth === 0);
}
get setFlex(): string {
if (this.nzCollapsed) {
return `0 0 ${this.nzCollapsedWidth}px`;
} else {
return `0 0 ${this.nzWidth}px`;
}
}
// TODO: unify the type of nzCollapsedWidth and nzWidth
get setWidth(): number | string {
if (this.nzCollapsed) {
return this.nzCollapsedWidth;
} else {
return this.nzWidth;
}
}
onWindowResize(e: UIEvent): void {
if (this.nzBreakpoint) {
const matchBelow = window.matchMedia(`(max-width: ${this._dimensionMap[ this.nzBreakpoint ]})`).matches;
this._below = matchBelow;
this.nzCollapsed = matchBelow;
this.nzCollapsedChange.emit(matchBelow);
}
}
toggleCollapse(): void {
this.nzCollapsed = !this.nzCollapsed;
this.nzCollapsedChange.emit(this.nzCollapsed);
}
constructor( private nzLayoutComponent: NzLayoutComponent) {
if (this.nzLayoutComponent) {
this.nzLayoutComponent.hasSider = true;
}
if (typeof window !== 'undefined') {
const matchMediaPolyfill = (mediaQuery: string): MediaQueryList => {
return {
media: mediaQuery,
matches: false,
addListener(): void { },
removeListener(): void { },
};
};
window.matchMedia = window.matchMedia || matchMediaPolyfill;
}
}
get _isZeroTrigger(): boolean {
return this.nzCollapsible && this.nzTrigger && (this.nzCollapsedWidth === 0) && ((this.nzBreakpoint && this._below) || (!this.nzBreakpoint));
}
get _isSiderTrgger(): boolean {
return this.nzCollapsible && this.nzTrigger && (this.nzCollapsedWidth !== 0);
}
}