app-viewlogic-service.ts 9.4 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
import {
    getPSUIActionByModelObject,
    IPSAppDEUIAction,
    IPSAppViewLogic,
    IPSAppViewUIAction
} from '@ibiz/dynamic-model-api';
import { LogUtil, UIServiceHelp, Util } from 'ibiz-core';
import { AppGlobalService } from './app-global-action-service';

/**
 * 视图逻辑服务
 *
 * @export
 * @class AppViewLogicService
 */
export class AppViewLogicService {
    /**
     * 单例变量声明
     *
     * @private
     * @static
     * @type {AppViewLogicService}
     * @memberof AppViewLogicService
     */
    private static appViewLogicService: AppViewLogicService;

    /**
     * 获取 AppViewLogicService 单例对象
     *
     * @static
     * @returns {AppViewLogicService}
     * @memberof AppViewLogicService
     */
    public static getInstance(): AppViewLogicService {
        if (!AppViewLogicService.appViewLogicService) {
            AppViewLogicService.appViewLogicService = new AppViewLogicService();
        }
        return this.appViewLogicService;
    }

    /**
     * 构造AppViewLogicService对象
     *
     * @memberof AppViewLogicService
     */
    constructor(opts?: any) { }

    /**
     * 执行视图逻辑
     *
     * @param itemTag 触发标识
     * @param $event 触发事件源
     * @param actionContext 操作上下文环境
     * @param params 附加参数
     * @param viewlogicData 当前容器视图逻辑集合
     * @memberof AppViewLogicService
     */
    public async executeViewLogic(
        itemTag: string,
        $event: any,
        actionContext: any,
        params: any = {},
        viewlogicData: Array<IPSAppViewLogic> | null,
    ) {
        if (!viewlogicData) {
            return;
        }
        let targetViewLogic: any = viewlogicData.find((item: any) => {
            return item.name === itemTag;
        });
        await this.prepareActionParams(targetViewLogic, $event, actionContext, params);
    }

    /**
     * 执行逻辑
     *
     * @param {IPSAppViewLogic} logic
     * @param {*} $event
     * @param {*} actionContext
     * @param {*} [params]
     */
    public async executeLogic(logic: IPSAppViewLogic, $event: any, actionContext: any, params?: any) {
        await this.prepareActionParams(logic, $event, actionContext, params);
    }

    /**
     * 准备界面行为参数
     *
     * @param viewLogic  视图逻辑
     * @param $event 触发事件源
     * @param actionContext 操作上下文环境
     * @param params 附加参数
     *
     * @memberof AppViewLogicService
     */
    private async prepareActionParams(viewLogic: IPSAppViewLogic, $event: any, actionContext: any, params: any = {}) {
        if (!viewLogic) {
            LogUtil.warn('无事件参数未支持');
            return;
        }
        if (!Object.is(viewLogic.logicType, 'APPVIEWUIACTION') || !viewLogic.getPSAppViewUIAction) {
            return;
        }
        const targetViewAction: IPSAppViewUIAction | null = viewLogic.getPSAppViewUIAction();
        if (!targetViewAction) {
            LogUtil.warn('视图界面行为不存在');
            return;
        }
        await (targetViewAction as IPSAppViewUIAction).fill();
        // 取数
        let datas: any[] = [];
        let xData: any = null;
        if (actionContext) {
            if (!targetViewAction.xDataControlName) {
                if (actionContext.getDatas && actionContext.getDatas instanceof Function) {
                    datas = [...actionContext.getDatas()];
                }
            } else {
                //  逻辑事件源为自定义视图布局面板
                if (Object.is(actionContext.type, 'VIEWLAYOUT')) {
                    if ($event && $event.srfid) {
                        xData = actionContext.layoutDetailsModel[$event.srfid];
                    } else {
                        xData = this.getXDataForCustomViewLayoutPanel(actionContext);
                    }
                } else if (!Object.is(viewLogic.getParentPSModelObject().name.toLocaleLowerCase(), targetViewAction.xDataControlName.toLocaleLowerCase()) && actionContext.$refs[targetViewAction.xDataControlName.toLowerCase()]) {
                    // 界面行为数据部件与控件容器不同时,即逻辑事件在视图里时
                    xData = actionContext.$refs[targetViewAction.xDataControlName.toLowerCase()].ctrl;
                } else {
                    xData = actionContext;
                }
                if (xData && xData.getDatas && xData.getDatas instanceof Function) {
                    datas = [...xData.getDatas()];
                }
            }
            if (params && Object.keys(params).length > 0) {
                datas = [params];
            }
        } else {
            if (params && Object.keys(params).length > 0) {
                datas = [params];
            }
        }
        // 执行行为
        await this.executeAction(viewLogic, targetViewAction, datas, xData, $event, actionContext);
    }

    /**
     * 获取数据部件(数据源为自定义视图布局面板时)
     *
     * @private
     * @param {*} actionContext
     * @return {*} 
     * @memberof AppViewLogicService
     */
    private getXDataForCustomViewLayoutPanel(actionContext: any) {
        const xDataMap: string[] = ['GRID', 'LIST', 'FORM', 'TREEVIEW', 'DATAVIEW', 'CALENDAR' ];
        try {
            const controlRefs: any = actionContext.viewProxyMode ? actionContext.$refs : actionContext.$slots;
            const allControls: any[] = actionContext.viewProxyMode ? actionContext.viewLayoutPanel.getPSControls() : actionContext.viewInstance.getPSControls();
            if (controlRefs && allControls && (allControls.length > 0)) {
                const xDataControl = allControls.find((control: any) => xDataMap.indexOf(control.controlType) !== -1);
                // 非视图代理模式
                if (!actionContext.viewProxyMode) {
                    return (controlRefs && xDataControl) ? controlRefs[`layout-${xDataControl.name.toLowerCase()}`]?.[0]?.child?.ctrl : null;
                } else {
                    return (controlRefs && xDataControl) ? controlRefs[`${xDataControl.name.toLowerCase()}`]?.ctrl : null;
                }
            } else {
                return null;
            }
        } catch (error: any) {
            return null;
        }
    }

    /**
     * 执行行为
     *
     * @param viewLogic  视图逻辑
     * @param targetViewAction  视图界面行为
     * @param datas 参数
     * @param xData 数据部件
     * @param $event 触发事件源
     * @param actionContext 操作上下文环境
     *
     * @memberof AppViewLogicService
     */
    private async executeAction(viewLogic: IPSAppViewLogic, targetViewAction: IPSAppViewUIAction, datas: any, xData: any, $event: any, actionContext: any) {
        const contextJO: any = Util.deepCopy(actionContext.context ? actionContext.context : {});
        const paramJO: any = Util.deepCopy(actionContext.viewparams ? actionContext.viewparams : {});
        const targetParentObject: any = viewLogic.getParentPSModelObject();
        const targetUIAction = (await getPSUIActionByModelObject(targetViewAction)) as IPSAppDEUIAction;
        if (targetUIAction.predefinedType) {
            if (targetParentObject && targetParentObject?.getPSAppDataEntity()) {
                Object.assign(contextJO, { srfparentdemapname: targetParentObject?.getPSAppDataEntity()?.getPSDEName() });
                (AppGlobalService.getInstance() as any).executeGlobalAction(
                    targetUIAction.predefinedType,
                    datas,
                    contextJO,
                    paramJO,
                    $event,
                    xData,
                    actionContext,
                    targetParentObject?.getPSAppDataEntity()?.codeName.toLowerCase(),
                    targetUIAction
                );
            } else {
                (AppGlobalService.getInstance() as any).executeGlobalAction(
                    targetUIAction.predefinedType,
                    datas,
                    contextJO,
                    paramJO,
                    $event,
                    xData,
                    actionContext,
                    undefined,
                    targetUIAction
                );
            }
        } else {
            if (targetUIAction && targetUIAction.getPSAppDataEntity()) {
                Object.assign(contextJO, { srfparentdemapname: targetParentObject?.getPSAppDataEntity()?.getPSDEName() });
                const targetUIService: any = await UIServiceHelp.getInstance().getService(
                    targetUIAction.getPSAppDataEntity(),
                    {context:contextJO}
                );
                await targetUIService.loaded();
                targetUIService.excuteAction(
                    targetUIAction.uIActionTag,
                    datas,
                    contextJO,
                    paramJO,
                    $event,
                    xData,
                    actionContext,
                    targetParentObject?.getPSAppDataEntity()?.codeName.toLowerCase(),
                );
            }else{
                 // 全局界面行为插件
                (AppGlobalService.getInstance() as any).executeGlobalAction(
                    targetUIAction.codeName,
                    datas,
                    contextJO,
                    paramJO,
                    $event,
                    xData,
                    actionContext,
                    undefined,
                    targetUIAction
                );
            }
        }
    }

}