main-control-base.tsx 7.7 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
import { Subscription } from 'rxjs';
import { UIServiceHelp } from 'ibiz-core';
import { AppCenterService, AppLoadingService } from '../app-service';
import { AppControlBase } from './app-control-base';
import { MobMainControlInterface, ModelTool, Util } from 'ibiz-core';
import { IPSAppDataEntity, IPSAppDEField, IPSAppDEUIAction, IPSDEFormButton, IPSDEFormDetail, IPSDEFormGroupPanel, IPSUIActionGroupDetail } from '@ibiz/dynamic-model-api';

/**
 * 部件基础公共基类
 *
 * @export
 * @class MainControlBase
 * @extends {AppControlBase}
 */
export class MainControlBase extends AppControlBase implements MobMainControlInterface {

    /**
     * 编辑视图
     *
     * @type {*}
     * @memberof MainControlBase
     */
    public opendata?: any;

    /**
     * 新建视图
     *
     * @type {*}
     * @memberof MainControlBase
     */
    public newdata?: any;


    /**
     * 应用实体codeName
     *
     * @readonly
     * @memberof MainViewBase
     */
    get appDeCodeName() {
        return this.controlInstance?.getPSAppDataEntity?.()?.codeName || '';
    }

    /**
     * 应用实体主键属性codeName
     *
     * @readonly
     * @memberof MainViewBase
     */
    get appDeKeyFieldName() {
        return (ModelTool.getAppEntityKeyField(this.controlInstance?.getPSAppDataEntity() as IPSAppDataEntity) as IPSAppDEField)?.codeName || '';
    }

    /**
     * 应用实体主信息属性codeName
     *
     * @readonly
     * @memberof MainViewBase
     */
    get appDeMajorFieldName() {
        return (ModelTool.getAppEntityMajorField(this.controlInstance?.getPSAppDataEntity() as IPSAppDataEntity) as IPSAppDEField)?.codeName || '';
    }

    /**
     * 监听静态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof ListControlBase
     */
    public onStaticPropsChange(newVal: any, oldVal: any) {
        if (newVal && !Util.isFieldsSame(newVal, oldVal)) {
            this.newdata = newVal.newdata;
            this.opendata = newVal.opendata;
            super.onStaticPropsChange(newVal, oldVal);
        }
    }

    /**
     * 加载服务
     *
     * @type {AppLoading}
     * @memberof MainControlBase
     */
    public ctrlLoadingService: AppLoadingService = new AppLoadingService();

    /**
     * 开始加载
     */
    public ctrlBeginLoading() {
    }

    /**
     * 结束加载
     */
    public endLoading() {
    }

    /**
     * 应用状态事件
     *
     * @public
     * @type {(Subscription | undefined)}
     * @memberof MainControlBase
     */
    public appStateEvent: Subscription | undefined;

    /**
     * 界面UI服务对象
     *
     * @type {*}
     * @memberof MainControlBase
     */
    public appUIService: any;

    /**
     * 界面行为模型
     *
     * @type {*}
     * @memberof MainControlBase
     */
    public actionModel: any = {};

    /**
     * 部件模型数据初始化
     *
     * @memberof MainControlBase
     */
    public async ctrlModelInit(args?: any) {
        await super.ctrlModelInit();
        this.initCtrlActionModel();
        if (!(this.Environment?.isPreviewMode)) {
            this.appUIService = await UIServiceHelp.getInstance().getService(this.controlInstance.getPSAppDataEntity(),this.context);
        }
    }

    /**
     * 初始化界面行为模型
     *
     * @type {*}
     * @memberof MainControlBase
     */
    public initCtrlActionModel() { 
        let allFormDetails: IPSDEFormDetail[] = ModelTool.getAllFormDetails(this.controlInstance);
        if (allFormDetails?.length > 0) {
            allFormDetails.forEach((detail: IPSDEFormDetail) => {
                if (detail?.detailType == 'BUTTON' && (detail as IPSDEFormButton).getPSUIAction()) {
                    // 添加表单按钮的actionModel
                    const appUIAction: IPSAppDEUIAction = ((detail as IPSDEFormButton).getPSUIAction() as IPSAppDEUIAction);
                    const appUIAction_M = Util.deepCopy(appUIAction.M);
                    this.actionModel[appUIAction.uIActionTag] = Object.assign(appUIAction_M, { disabled: false, visabled: true, getNoPrivDisplayMode: appUIAction.noPrivDisplayMode ? appUIAction.noPrivDisplayMode : 6 });
                } else if (detail?.detailType == 'GROUPPANEL' && (detail as IPSDEFormGroupPanel).getPSUIActionGroup()?.getPSUIActionGroupDetails()?.length) {
                    // 添加表单分组界面行为组的actionModel
                    (detail as IPSDEFormGroupPanel).getPSUIActionGroup()?.getPSUIActionGroupDetails()?.forEach((actionDetail: IPSUIActionGroupDetail) => {
                        if (actionDetail?.getPSUIAction()) {
                            const appUIAction: IPSAppDEUIAction = (actionDetail.getPSUIAction() as IPSAppDEUIAction);
                            const appUIAction_M = Util.deepCopy(appUIAction.M);
                            this.actionModel[appUIAction.uIActionTag] = Object.assign(appUIAction_M, { disabled: false, visabled: true, getNoPrivDisplayMode: appUIAction.noPrivDisplayMode ? appUIAction.noPrivDisplayMode : 6 });
                        }
                    })
                }
            })
        }
    }

    /**
     * 转化数据
     *
     * @param {*} args
     * @memberof  MainControlBase
     */
    public transformData(args: any) {
        if (!this.appDeCodeName) {
            return;
        }
        let _this: any = this;
        if (_this.service && _this.service.handleRequestData instanceof Function && _this.service.handleRequestData('transform', _this.context, args)) {
            return _this.service.handleRequestData('transform', _this.context, args)['data'];
        }
    }

    /**
     * 部件初始化
     *
     * @param {*} [args]
     * @memberof MainControlBase
     */
    public ctrlInit(args?: any) {
        super.ctrlInit(args);
        // 全局刷新通知
        if (AppCenterService?.getMessageCenter()) {
            this.appStateEvent = AppCenterService.getMessageCenter().subscribe(({ name, action, data }) => {
                const appDataEntity = this.controlInstance?.getPSAppDataEntity?.();
                if (!appDataEntity || !Object.is(name, appDataEntity?.codeName)) {
                    return;
                }
                if (Object.is(action, 'appRefresh')) {
                    this.refresh(data);
                }
            })
        }
    }

    /**
     * 部件销毁
     *
     * @memberof MainControlBase
     */
    public ctrlDestroyed() {
        super.ctrlDestroyed();
        if (this.appStateEvent) {
            this.appStateEvent.unsubscribe();
        }
    }


    /**
     * 部件刷新数据
     *
     * @param {*} args
     * @memberof  MainControlBase
     */
    public refresh(args?: any): void { }

    /**
     * 获取视图逻辑标识(拼合逻辑:部件标识_列标识_成员标识_click)
     *
     * @param {*} args
     * @memberof  MainControlBase
     */
    public getViewLogicTag(ctrlTag: string, columnTag: any, detailTag: string) {
        return `${ctrlTag}_${columnTag}_${detailTag}_click`;
    }

    /**
     * 处理部件UI请求
     *
     * @memberof MainControlBase
     */
    public onControlRequset(action: string, context: any, viewparam: any) {
        this.ctrlBeginLoading();
    }

    /**
     * 处理部件UI响应
     *
     * @memberof MainControlBase
     */
    public onControlResponse(action: string, response: any) {
        this.endLoading();
        if (Object.is(action, 'load')) {
            this.controlIsLoaded = true;
        }
        if (response?.status == 403) {
            this.enableControlUIAuth = false;
            this.ctrlEvent({
                controlname: this.controlInstance.name,
                action: 'authlimit',
                data: response,
            });
        }
    }
}