import { Vue, Component, Prop, Emit, Watch } from 'vue-property-decorator'; import { UIStateService } from '../../../../app-service'; import './app-content-left-nav-menu.less'; /** * 左侧导航菜单 * * @export * @class AppContentLeftNavMenu * @extends {Vue} */ @Component({}) export class AppContentLeftNavMenu extends Vue { /** * UI状态服务 * * @protected * @type {UIStateService} * @memberof AppContentLeftNavMenu */ protected uiState: UIStateService = new UIStateService(); /** * 当前激活项 * * @protected * @type {*} * @memberof AppContentLeftNavMenu */ protected activeItem: any = {}; /** * 菜单Map表 * * @protected * @type {Map} * @memberof AppContentLeftNavMenu */ protected menuMap: Map = new Map(); /** * 部件名称 * * @type {string} * @memberof AppContentLeftNavMenu */ @Prop() ctrlName!: string; /** * 菜单数据 * * @type {any[]} * @memberof AppContentLeftNavMenu */ @Prop({ default: () => [] }) menus!: any[]; /** * 模型服务对象 * * @memberof AppStyle2DefaultLayout */ @Prop() modelService!:any; /** * 监控菜单数据变更 * * @memberof AppContentLeftNavMenu */ @Watch('menus', { immediate: true }) watchMenus(): void { this.fillMenuMap(this.menus); } /** * 菜单项点击 * * @param {*} item * @returns {*} * @memberof AppContentLeftNavMenu */ @Emit('menu-click') menuClick(item: any): any {} /** * 底部绘制实例 * * @memberof AppContentLeftNavMenu */ footerRenderItem!: any; /** * 组件创建完毕 * * @memberof AppContentLeftNavMenu */ created(): void { if (this.$route && this.$route.matched.length === 1) { this.openDefault(); } this.footerRenderItem = this.$footerRenderService.registerLeftItem((h: any) => { return ( this.uiState.leftNavMenuCollapseChange()} /> ); }, 0); } /** * 组件销毁 * * @memberof AppContentLeftNavMenu */ destroyed(): void { this.footerRenderItem?.remove(); } /** * 打开默认菜单 * * @protected * @memberof AppContentLeftNavMenu */ protected openDefault(): void { let menu: any; for (const [key, item] of this.menuMap) { if (item.openDefault === true) { menu = item; break; } } if (menu) { this.itemClick(menu); } } /** * 菜单项点击 * * @protected * @param {*} item * @memberof AppContentLeftExp */ protected itemClick(item: any): void { const styleMode: any = this.$uiState.layoutState.styleMode; if ((Object.is(styleMode,'DEFAULT') && item.name !== this.activeItem.name) || Object.is(styleMode,'STYLE2')) { this.changeActiveItem(item); this.menuClick(item); } } /** * 改变激活项 * * @protected * @param {*} item * @memberof AppContentLeftExp */ protected changeActiveItem(item: any): void { this.activeItem = item; this.activeItem.isActivated = true; } /** * 菜单项选中 * * @protected * @param {string} name * @memberof AppContentLeftNavMenu */ protected select(name: string): void { const item = this.menuMap.get(name); if (item) { this.itemClick(item); } } /** * 填充菜单Map表 * * @protected * @param {any[]} menus * @returns {*} * @memberof AppContentLeftNavMenu */ protected fillMenuMap(menus: any[]): any { menus.forEach((item: any) => { this.menuMap.set(item.name, item); if (item.getPSAppMenuItems) { this.fillMenuMap(item.getPSAppMenuItems); } }); } /** * 展开菜单项 * * @protected * @param {string} name * @memberof AppContentLeftNavMenu */ protected open(name: string): void { const i: number = this.uiState.layoutState.leftNavOpenedMenus.findIndex((str: any) => Object.is(str, name)); if (i === -1) { this.uiState.layoutState.leftNavOpenedMenus.push(name); } } /** * 收起菜单项 * * @protected * @param {string} name * @memberof AppContentLeftNavMenu */ protected close(name: string): void { const i: number = this.uiState.layoutState.leftNavOpenedMenus.findIndex((str: any) => Object.is(str, name)); if (i !== -1) { this.uiState.layoutState.leftNavOpenedMenus.splice(i, 1); this.$forceUpdate(); } } /** * 绘制子菜单 * * @protected * @param {*} item * @returns {*} * @memberof AppContentLeftNavMenu */ protected renderGroup(item: any): any { return ( {this.renderItems(item.getPSAppMenuItems)} ); } /** * 绘制菜单项 * * @protected * @param {*} item * @returns {*} * @memberof AppContentLeftNavMenu */ protected renderItem(item: any): any { return ( {this.$tl(item.captionTag,item.caption)} ); } /** * 绘制菜单 * * @protected * @param {any[]} items * @returns {*} * @memberof AppContentLeftNavMenu */ protected renderItems(items: any[]): any { return items.map((item: any) => { if (item.hidden) { return; } if (item.getPSAppMenuItems) { return this.renderGroup(item); } return this.renderItem(item); }); } /** * 绘制内容 * * @returns {*} * @memberof AppContentLeftNavMenu */ render(): any { return (
this.select(i)} on-open={(i: any) => this.open(i)} on-close={(i: any) => this.close(i)} > {this.renderItems(this.menus)}
); } }