ng-ytl-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
182 lines (154 loc) • 5.07 kB
text/typescript
import {
AfterViewInit, Component, HostBinding, Input, OnChanges, SimpleChanges, ViewEncapsulation
} from '@angular/core';
import { toBoolean } from '../util/convert';
import { NzMenuItemComponent } from './nz-menu-item.component';
import { NzSubMenuComponent } from './nz-submenu.component';
export type NzMode = 'vertical' | 'horizontal' | 'inline';
export class NzMenuComponent implements OnChanges, AfterViewInit {
private _clickActive = true;
private _inlineCollapsed = false;
/** set when has submenu component */
hasSubMenu = false;
/** set when in dropdown component */
isInDropDown = false;
/** collection of menu item */
menuItems: NzMenuItemComponent[] = [];
/** collection of sub menu */
subMenus: NzSubMenuComponent[] = [];
/** view init flat */
isInit = false;
/** temporary mode */
_tempMode: NzMode;
/** opened index of array */
_subMenusOpenIndex = [];
nzMode: NzMode = 'vertical';
nzTheme: 'light' | 'dark' = 'light';
set nzClickActive(value: boolean) {
this._clickActive = toBoolean(value);
}
get nzClickActive(): boolean {
return this._clickActive;
}
set nzInlineCollapsed(value: boolean) {
this._inlineCollapsed = toBoolean(value);
if (this.isInit) {
this.updateInlineCollapse();
}
}
get nzInlineCollapsed(): boolean {
return this._inlineCollapsed;
}
updateInlineCollapse(): void {
if (this._inlineCollapsed) {
this.hideSubMenus();
// after the animation is over
setTimeout(() => this.nzMode = 'vertical', 150);
} else {
this.reductionSubMenus();
this.nzMode = this._tempMode;
}
}
/** define host class */
get _isInDropDownClass(): boolean {
return this.isInDropDown;
}
get _isNotInDropDownClass(): boolean {
return !this.isInDropDown;
}
get setDropDownThemeLightClass(): boolean {
return this.isInDropDown && (this.nzTheme === 'light');
}
get setDropDownThemeDarkClass(): boolean {
return this.isInDropDown && (this.nzTheme === 'dark');
}
get setMenuThemeLightClass(): boolean {
return (!this.isInDropDown) && (this.nzTheme === 'light');
}
get setMenuThemeDarkClass(): boolean {
return (!this.isInDropDown) && (this.nzTheme === 'dark');
}
get setMenuVerticalClass(): boolean {
return (!this.isInDropDown) && (this.nzMode === 'vertical');
}
get setMenuHorizontalClass(): boolean {
return (!this.isInDropDown) && (this.nzMode === 'horizontal');
}
get setMenuInlineClass(): boolean {
return (!this.isInDropDown) && (this.nzMode === 'inline');
}
get setMenuInlineCollapsedClass(): boolean {
return (!this.isInDropDown) && (this.nzMode !== 'horizontal') && this.nzInlineCollapsed;
}
ngOnChanges(changes: SimpleChanges): void {
for (const propName in changes) {
if (propName === 'nzMode') {
if (this.isInit) {
this.subMenus.forEach(submenu => {
submenu.nzOpen = false;
submenu.nzOpenChange.emit(false);
});
}
}
}
}
ngAfterViewInit(): void {
this.isInit = true;
this._tempMode = this.nzMode;
this.updateInlineCollapse();
}
/** trigger when menu item clicked */
clearAllSelected(): void {
this.menuItems.forEach(menu => menu.nzSelected = false);
}
hideSubMenus(): void {
this._subMenusOpenIndex = [];
this.subMenus.forEach((submenu, index) => {
if (submenu.nzOpen) {
this._subMenusOpenIndex.push(index);
}
submenu.nzOpen = false;
});
}
reductionSubMenus(): void {
this._subMenusOpenIndex.forEach(i => this.subMenus[ i ].nzOpen = true);
this._subMenusOpenIndex = [];
}
/** api for dropdown or navigation to set isInDropDown status */
setDropDown(value: boolean): void {
setTimeout(_ => {
this.isInDropDown = value;
this.menuItems.forEach(menu => menu.isInDropDown = value);
this.subMenus.forEach(subMenu => subMenu.isInDropDown = value);
});
}
setHasSubMenu(value: boolean): void {
setTimeout(_ => {
this.hasSubMenu = value;
});
}
}