mob-appmenu-control-base.tsx 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
import { AppControlBase } from "./app-control-base";
import { AppFuncService } from '../app-service';
import { IPSAppMenu, IPSAppMenuItem } from '@ibiz/dynamic-model-api';
import { AuthService, MobMenuControlInterface, Util } from "ibiz-core";
import { AppMobMenuService } from "../ctrl-service";
/**
 * 菜单部件基类
 *
 * @export
 * @class AppControlBase
 * @extends {MobAppMenuControlBase}
 */
export class MobAppMenuControlBase extends AppControlBase implements MobMenuControlInterface {

    /**
     * 菜单部件实例
     * 
     * @memberof MobAppMenuControlBase
     */
    public declare controlInstance: IPSAppMenu;

    /**
     * 部件服务对象
     *
     * @type {*}
     * @memberof AppControlBase
     */
    public declare service: AppMobMenuService;

    /**
     * 部件样式
     *
     * @protected
     * @type {(string | 'ICONVIEW' | 'LISTVIEW' | 'SWIPERVIEW' | 'LISTVIEW2' | 'LISTVIEW3' | 'LISTVIEW4')}   默认空字符串 | 图标视图 | 列表视图 | 图片滑动视图 | 列表视图(无刷新) | 列表视图(无滑动) | 列表视图(无背景)
     * @memberof MobAppMenuControlBase
     */
    public controlStyle!: string | 'ICONVIEW' | 'LISTVIEW' | 'SWIPERVIEW' | 'LISTVIEW2' | 'LISTVIEW3' | 'LISTVIEW4';

    /**
     * 菜单数据
     *
     * @private
     * @type {any[]}
     * @memberof MobAppMenuControlBase
     */
    public menus: any[] = [];

    /**
     * 当前菜单是否在默认视图上
     *
     * @type {*}
     * @memberof MobAppMenuControlBase
     */
    public isDefaultPage?: boolean;

    /**
     * 菜单选中项
     *
     * @type {*}
     * @memberof MobAppMenuControlBase
     */
    public selectValue: any = null;


    /**
     * 计数器数据
     *
     * @type {*}
     * @memberof MobAppMenuControlBase
     */
    protected counterdata: any = {};

    /**
     * 
     *
     * @type {AuthService}
     * @memberof MobAppMenuControlBase
     */
    public authService!: AuthService;

    /**
     * 菜单项选中处理
     *
     * @param {*} index
     * @param {any[]} indexs
     * @returns
     * @memberof MobAppMenuControlBase
     */
    public select(menuName: any) {
        if (!menuName) {
            return;
        }
        let item = this.compute(this.menus, menuName);
        if (Object.keys(item).length === 0) {
            return;
        }
        this.click(item, menuName);
    }

    /**
     * 默认选中
     *
     * @param {*} menuName
     * @memberof MobAppMenuControlBase
     */
    public defaultSelect(menuName: any) {
        this.selectValue = menuName;
        this.select(this.selectValue)
    }

    /**
     * 获取菜单项数据
     *
     * @private
     * @param {any[]} items
     * @param {string} name
     * @returns
     * @memberof MobAppMenuControlBase
     */
    public compute(items: any[], name: string) {
        return items.find((_item: IPSAppMenuItem) => {
            return _item.name == name;
        })
    }

    /**
     * 菜单点击
     *  
     * @param menuItem
     * @memberof MobAppMenuControlBase
     */
    public async click(item: any, menuName: string) {
        if (!(await this.handleCtrlEvents('onclick', { action: 'onclick', data: item }))) {
            return;
        }
        let tempContext: any = Util.deepCopy(this.context);
        if (item.getPSNavigateContexts) {
            const localContext = Util.formatNavParam(item.getPSNavigateContexts);
            Object.assign(tempContext, localContext);
        }
        if (item.getPSAppFunc) {
            const appFuncs = this.service.getAllFuncs();
            const appFunc = appFuncs?.find((element: any) => {
                return element.appfunctag === item.getPSAppFunc?.codeName;
            });
            if (appFunc) {
                if (appFunc.appFuncType === 'APPVIEW' && this.isDefaultPage) {
                    const tempContext = { viewpath: appFunc.getPSAppView.modelPath };
                    Object.assign(tempContext, this.context);
                    const args = {
                        srfnavdata: {
                            context: tempContext,
                            viewparams: this.viewparams
                        }
                    };
                    Object.assign(args, appFunc);
                    this.selectValue = menuName;
                    this.ctrlEvent({ controlname: this.controlInstance.name, action: 'selectionchange', data: args });
                } else {
                    AppFuncService.getInstance().executeApplication(appFunc, tempContext);
                }
            }
        } else {
            console.warn(this.$t('app.commonWords.noAssign'));
        }
    }

    /**
     * 计算有效菜单项
     *
     * @param {*} data
     * @memberof MobAppMenuControlBase
     */
    public computedEffectiveMenus(inputMenus: Array<any>) {
        inputMenus.forEach((_item: any) => {
            if (!this.authService.getMenusPermission(_item)) {
                _item.hidden = true;
                if (_item.items && _item.items.length > 0) {
                    this.computedEffectiveMenus(_item.items);
                }
            }
            if (Object.is(_item.id, 'setting')) {
                _item.hidden = false;
            }
        })
    }

    /**
     * 数据处理
     *
     * @private
     * @param {any[]} items
     * @memberof MobAppMenuControlBase
     */
    private dataProcess(items: any[]): void {
        items.forEach((_item: any) => {
            if (_item.items && _item.items.length > 0) {
                this.dataProcess(_item.items)
            }
        });
    }


    /**
     * 部件挂载
     *
     * @memberof AppControlBase
     */
    public ctrlInit(args?: any) {
        super.ctrlInit();
        this.controlStyle = this.staticProps.controlStyle ? this.staticProps.controlStyle : "";
        this.isDefaultPage = this.staticProps.isDefaultPage;
        this.authService = new AuthService();
        this.handleMenusResource(this.service.getAllMenuItems());
        this.calcMenuName();
        this.viewState.subscribe(({ tag, action }: { tag: string; action: string }) => {
            if (!Object.is(this.controlInstance.name, tag)) {
                return;
            }
            if (Object.is(action, 'init')) {
                this.select(this.selectValue);
            }
        })

    }

    /**
     * 计算菜单名称
     *
     * @memberof MobAppMenuControlBase
     */
    public calcMenuName() {
        this.menus.forEach((item: any) => {
            item.caption = this.$tl(item.captionTag, item.caption);
        });
    }

    /**
     * 部件模型数据初始化实例
     *
     * @memberof AppDefaultMobForm
     */
    public async ctrlModelInit(args?: any) {
        await super.ctrlModelInit(args)
        this.service = new AppMobMenuService(this.controlInstance);
        await this.service.loaded(this.context, this.controlInstance)
    }


    /**
     * 通过统一资源标识计算菜单
     *
     * @param {*} data
     * @memberof AppMenuControlBase
     */
    public handleMenusResource(inputMenus: Array<any> | undefined) {
        if (!inputMenus) {
            return;
        }
        const Environment = (window as any).Environment;
        if (Environment.enablePermissionValid) {
            this.computedEffectiveMenus(inputMenus);
        }
        this.dataProcess(inputMenus);
        this.menus = inputMenus;
    }
}