md-control-base.tsx 18.9 KB
Newer Older
1
import { Subscription } from 'rxjs';
2
import { CodeListService, throttle, MDControlInterface, ModelTool, DataServiceHelp, AppServiceBase, AppItemEngine } from 'ibiz-core'
3
import { MainControlBase } from './main-control-base';
4 5
import { AppCenterService, AppGlobalService, AppViewLogicService } from '../app-service';
import { IPSAppCodeList, IPSAppDataEntity, IPSControlLogic, IPSDEContextMenu, IPSDETBUIActionItem, IPSDEToolbar, IPSDEToolbarItem, IPSDEUIAction } from '@ibiz/dynamic-model-api';
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


/**
 * 多数据部件基类
 *
 * @export
 * @class MDControlBase
 * @extends {MainControlBase}
 */
export class MDControlBase extends MainControlBase implements MDControlInterface {
    /**
     * 代码表服务对象
     *
     * @type {CodeListService}
     * @memberof MDControlBase
     */
    public codeListService!: CodeListService;

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

    /**
     * 多数据部件实例
     *
     * @public
     * @type {(IPSMDControl)}
     * @memberof MDControlBase
     */
    public declare controlInstance: any;

    /**
     * 快速工具栏实例
     *
     * @type {IPSDEToolbar}
     * @memberof MDControlBase
     */
    public quickToolbarInstance!: IPSDEToolbar;

    /**
     * 批处理工具栏实例
     *
     * @type {IPSDEToolbar}
     * @memberof MDControlBase
     */
    public batchToolbarInstance!: IPSDEToolbar;

    /**
     * 快速行为模型数据
     *
     * @protected
     * @type {[]}
     * @memberof MDControlBase
     */
    public quickToolbarModels: Array<any> = [];

    /**
     * 批操作行为模型数据
     *
     * @protected
     * @type {[]}
     * @memberof MDControlBase
     */
    public batchToolbarModels: Array<any> = [];

    /**
     * 选中行数据
     *
     * @type {any[]}
     * @memberof MDControlBase
     */
    public selections: any[] = [];

    /**
     * 当前页
     *
     * @type {number}
     * @memberof MDControlBase
     */
    public curPage: number = 1;

    /**
     * 多数据部件数据激活模式
     * 0 不激活
     * 1 单击激活
     * 2 双击激活
     *
     * @type {(number | 0 | 1 | 2)}
     * @memberof GridControlBase
     */
    public mDCtrlActiveMode: number | 0 | 1 | 2 = 2;

    /**
     * 数据
     *
     * @type {any[]}
     * @memberof MDControlBase
     */
    public items: any[] = [];

    /**
     * 是否支持分页
     *
     * @type {boolean}
     * @memberof MDControlBase
     */
    public isEnablePagingBar: boolean = true;

    /**
     * 是否禁用排序
     *
     * @type {boolean}
     * @memberof MDControlBase
     */
    public isNoSort: boolean = false;

    /**
     * 分页条数
     *
     * @type {number}
     * @memberof MDControlBase
     */
    public limit: number = 20;

    /**
     * 总条数
     *
     * @type {number}
     * @memberof MDControlBase
     */
    public totalRecord: number = 0;

    /**
     * 是否单选
     *
     * @type {boolean}
     * @memberof MDControlBase
     */
    public isSingleSelect?: boolean;

    /**
     * 是否默认选中第一条数据
     *
     * @type {boolean}
     * @memberof MDControlBase
     */
    public isSelectFirstDefault: boolean = false;

    /**
     * 排序方向
     *
     * @type {string}
     * @memberof MDControlBase
     */
    public minorSortDir: string = "";

    /**
     * 排序字段
     *
     * @type {string}
     * @memberof MDControlBase
     */
    public minorSortPSDEF: string = "";

    /**
     * 建立数据行为
     *
     * @readonly
     * @memberof MDControlBase
     */
    public createAction: string = "";
    /**
     * 查询数据行为
     *
     * @readonly
     * @memberof MDControlBase
     */
    public fetchAction: string = "";

    /** 
     * 更新数据行为
     *
     * @readonly
     * @memberof MDControlBase
     */
    public updateAction: string = "";

    /**
     * 删除数据行为
     *
     * @readonly
     * @memberof MDControlBase
     */
    public removeAction: string = "";

    /**
     * 查询数据行为
     *
     * @readonly
     * @memberof MDControlBase
     */
    public loadAction: string = "";

    /**
     * 获取草稿数据行为
     *
     * @readonly
     * @memberof MDControlBase
     */
    public loaddraftAction: string = "";

    /**
     * 数据映射(数据项名称和UI名称的映射)
     * 
     * @memberof MDControlBase
     */
    public dataMap: Map<string, any> = new Map();

    /**
     * 分组模式
     * 
     * @type {string}
     * @memberof MDControlBase
     */
    public groupMode: string = '';

    /**
     * 是否开启分组
     * 
     * @type {boolean}
     * @memberof MDControlBase
     */
    public isEnableGroup: boolean = false;

    /**
     * 分组属性
     * 
     * @type {string}
     * @memberof MDControlBase
     */
    public groupField: string = '';

    /**
     * 分组代码表
     * 
     * @type {IPSAppCodeList}
     * @memberof MDControlBase
     */
    public groupCodeList?: IPSAppCodeList;

261 262 263 264 265 266 267 268
    /**
     * 上下文菜单逻辑map
     *
     * @type {Map<string, AppItemEngine[]>}
     * @memberof MDControlBase
     */
    public contextMenuLogicMap: Map<string, AppItemEngine[]> = new Map();

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    /**
     * 获取视图样式
     *
     * @readonly
     * @memberof MDControlBase
     */
    get viewStyle() {
        const parentModel: any = this.controlInstance?.getParentPSModelObject?.();
        if (parentModel && parentModel.viewStyle) {
            return parentModel.viewStyle;
        } else {
            if (parentModel && parentModel.controlType) {
                return parentModel.getParentPSModelObject?.()?.viewStyle || 'DEFAULT';
            }
            return 'DEFAULT';
        }
    }

    /**
     * 监听静态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof MDControlBase
     */
    public onStaticPropsChange(newVal: any, oldVal: any) {
        this.isSingleSelect = newVal.isSingleSelect !== false;
        this.isSelectFirstDefault = newVal.isSelectFirstDefault === true;
        this.mDCtrlActiveMode = newVal.mDCtrlActiveMode;
        super.onStaticPropsChange(newVal, oldVal);
    }

    /**
     * 部件模型数据初始化
     *
     * @memberof MDControlBase
     */
    public async ctrlModelInit(args?: any) {
        await super.ctrlModelInit();
        const { name } = this.controlInstance;
        if (this.controlInstance?.getPSAppDataEntity?.() && !(this.Environment && this.Environment.isPreviewMode)) {
            if (!this.controlInstance?.getPSAppDataEntity()?.isFill) {
                await this.controlInstance?.getPSAppDataEntity().fill();
            }
            this.appEntityService = await DataServiceHelp.getInstance().getService((this.controlInstance.getPSAppDataEntity() as IPSAppDataEntity), { context: this.context });
        }
        this.loaddraftAction = this.controlInstance?.getGetDraftPSControlAction?.()?.getPSAppDEMethod?.()?.codeName || "GetDraft";
        this.loadAction = this.controlInstance?.getGetPSControlAction?.()?.getPSAppDEMethod?.()?.codeName || "Get";
        this.removeAction = this.controlInstance?.getRemovePSControlAction?.()?.getPSAppDEMethod?.()?.codeName || "Remove";
        this.updateAction = this.controlInstance?.getUpdatePSControlAction?.()?.getPSAppDEMethod?.()?.codeName || "Update";
        this.fetchAction = this.controlInstance?.getFetchPSControlAction?.()?.getPSAppDEMethod?.()?.codeName || "FetchDefault";
        this.createAction = this.controlInstance?.getCreatePSControlAction?.()?.getPSAppDEMethod?.()?.codeName || "Create";
        if (this.controlInstance?.getPSControls?.()) {
            this.quickToolbarInstance = ModelTool.findPSControlByName(`${name}_quicktoolbar`, this.controlInstance.getPSControls())
            this.batchToolbarInstance = ModelTool.findPSControlByName(`${name}_batchtoolbar`, this.controlInstance.getPSControls())
        }
        this.initToolBarModels();
        this.initDataMap();
        this.initGroupOptions();
    }

    /**
     * 初始化工具栏模型
     *
     * @memberof MDControlBase
     */
    public initToolBarModels() {
        const getModelData = (_item: IPSDEToolbarItem) => {
            const item: IPSDETBUIActionItem = _item as IPSDETBUIActionItem;
            const uiAction: IPSDEUIAction | null = item.getPSUIAction ? (item?.getPSUIAction() as IPSDEUIAction) : null;
            return {
                name: item.name,
                showCaption: item.showCaption,
                showIcon: item.showIcon,
                tooltip: this.$tl(item.getTooltipPSLanguageRes()?.lanResTag, item.tooltip),
                iconcls: item.getPSSysImage()?.cssClass,
                icon: item.getPSSysImage()?.imagePath,
                actiontarget: item.uIActionTarget,
                caption: this.$tl(item.getCapPSLanguageRes()?.lanResTag, item.caption),
                disabled: false,
                itemType: item.itemType,
                visabled: true,
                noprivdisplaymode: uiAction?.noPrivDisplayMode,
                actionLevel: (_item as any).actionLevel,
                dataaccaction: '',
                uiaction: {
                    tag: uiAction?.uIActionTag ? uiAction.uIActionTag : uiAction?.id ? uiAction.id : '',
                    target: uiAction?.actionTarget,
                },
            };
        };
        if (this.quickToolbarInstance) {
            let targetViewToolbarItems: any[] = [];
            this.quickToolbarInstance.getPSDEToolbarItems()?.forEach((item: IPSDEToolbarItem) => {
                targetViewToolbarItems.push(getModelData(item));
            });
            this.quickToolbarModels = targetViewToolbarItems;
        }
        if (this.batchToolbarInstance) {
            let targetViewToolbarItems: any[] = [];
            this.batchToolbarInstance.getPSDEToolbarItems()?.forEach((item: IPSDEToolbarItem) => {
                targetViewToolbarItems.push(getModelData(item));
            });
            this.batchToolbarModels = targetViewToolbarItems;
        }
    }

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
    /**
     * 初始化上下文菜单逻辑
     *
     * @param {IPSDEContextMenu} contextMenu
     * @memberof MDControlBase
     */
    public initContextMenuUILogic(contextMenu: IPSDEContextMenu) {
        if (AppServiceBase.getInstance().getEnableUIModelEx()) {
            const ToolbarItems = contextMenu.getPSDEToolbarItems();
            if (ToolbarItems && ToolbarItems.length > 0) {
                ToolbarItems.forEach((toolbarItem: IPSDEToolbarItem) => {
                    const itemLogics = toolbarItem.M.getPSControlLogics;
                    if (itemLogics && itemLogics.length > 0) {
                        itemLogics.forEach((logic: any) => {
                            if (
                                logic.triggerType === 'ITEMVISIBLE' ||
                                logic.triggerType === 'ITEMENABLE' ||
                                logic.triggerType === 'ITEMBLANK'
                            ) {
                                const logics = this.contextMenuLogicMap.get(`${contextMenu.name}`);
                                if (logics && logics.length > 0) {
                                    logics.push(new AppItemEngine(logic));
                                    this.contextMenuLogicMap.set(`${contextMenu.name}`, logics);
                                } else {
                                    this.contextMenuLogicMap.set(`${contextMenu.name}`, [new AppItemEngine(logic)]);
                                }
                            }
                        })
                    }
                })
            }
        } else {
            const contextMenuLogics = contextMenu.getPSControlLogics();
            if (contextMenuLogics && contextMenuLogics?.length > 0) {
                contextMenuLogics.forEach((logic: IPSControlLogic) => {
                    if (
                        logic.triggerType === 'ITEMVISIBLE' ||
                        logic.triggerType === 'ITEMENABLE' ||
                        logic.triggerType === 'ITEMBLANK'
                    ) {
                        const logics = this.contextMenuLogicMap.get(`${contextMenu.name}`);
                        if (logics && logics.length > 0) {
                            logics.push(new AppItemEngine(logic));
                            this.contextMenuLogicMap.set(`${contextMenu.name}`, logics);
                        } else {
                            this.contextMenuLogicMap.set(`${contextMenu.name}`, [new AppItemEngine(logic)]);
                        }
                    }
                })
            }
        }
    }
    
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    /**
     * 多数据部件初始化
     *
     * @memberof MDControlBase
     */
    public ctrlInit() {
        super.ctrlInit();
        let _this: any = this;
        // 全局刷新通知
        if (AppCenterService.getMessageCenter()) {
            _this.appStateEvent = AppCenterService.getMessageCenter().subscribe(({ name, action, data }: { name: string, action: string, data: any }) => {
                if (!_this.appDeCodeName || !Object.is(name, _this.appDeCodeName)) {
                    return;
                }
                if (Object.is(action, 'appRefresh')) {
                    _this.refresh(data);
                }
            })
        }
        _this.codeListService = new CodeListService({ $store: _this.$store });
    }

    /**
     * 执行destroyed后的逻辑
     *
     * @memberof MDControlBase
     */
    public ctrlDestroyed() {
        super.ctrlDestroyed();
        if (this.appStateEvent) {
            this.appStateEvent.unsubscribe();
        }
    }

    /**
     * 部件工具栏点击
     *
     * @param ctrl 部件
     * @param data 工具栏回传数据
     * @param $event 事件源对象
     * @memberof MDControlBase
     */
    public handleItemClick(ctrl: string, data: any, $event: any) {
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
        if (AppServiceBase.getInstance().getEnableUIModelEx()) {
            const uiAction = ModelTool.getToolBarUIActionByItemName(ctrl == 'quicktoolbar' ? this.quickToolbarInstance : this.batchToolbarInstance, data.tag);
            AppGlobalService.getInstance().executeGlobalUIAction(uiAction, $event, this, undefined, undefined);
        } else {
            AppViewLogicService.getInstance().executeViewLogic(
                this.getViewLogicTag(this.controlInstance.name, ctrl, data.tag),
                $event,
                this,
                undefined,
                this.controlInstance.getPSAppViewLogics() as Array<any>,
            );
        }
    }

    /**
     * 处理上下文菜单逻辑
     *
     * @param {any[]} data
     * @param {*} actionModel
     * @memberof MDControlBase
     */
    public handleContextMenuLogic(data: any[], actionModel: any) {
        if (this.contextMenuLogicMap.size > 0 && Object.keys(actionModel).length > 0) {
            let contextMenuName: string = '';
            Object.values(actionModel).forEach((action: any) => {
                contextMenuName = action.ctrlname;
            })
            const logics = this.contextMenuLogicMap.get(contextMenuName);
            if (logics && logics.length > 0) {
                logics.forEach((logic: AppItemEngine) => {
                    logic.executeUILogic({ arg: { sender: this, navContext: this.context, navParam: this.viewparams, navData: this.navdatas, data: data, itemModels: actionModel }, utils: this.viewCtx, app: this.viewCtx.app, view: this.viewCtx.view, ctrl: this });
                })
            }
        }
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
    }

    /**
     * 获取多项数据
     *
     * @returns {any[]}
     * @memberof MDControlBase
     */
    public getDatas(): any[] {
        return this.selections;
    }

    /**
     * 获取单项数据
     *
     * @returns {*}
     * @memberof MDControlBase
     */
    public getData(): any {
        return this.selections[0];
    }

    /**
      * 绘制加载数据提示信息
      *
      * @return {*} 
      * @memberof MDControlBase
      */
    public renderLoadDataTip() {
        return <div class="control__loading">
            <img class="loading__img" src={__webpack_public_path__ + "./assets/img/load-data.gif"} />
            <span class="loading__text">{this.$t('app.warn.load')}</span>
        </div>
    }

    /**
     * 绘制无数据提示信息
     *
     * @return {*} 
     * @memberof MDControlBase
     */
    public renderEmptyDataTip() {
        return <div class="control__empty">
            <img class="empty__img" src={__webpack_public_path__ + "./assets/img/empty-data.svg"} />
            <span class="empty__text">{this.$t('app.warn.nofind')}</span>
            {this.renderQuickToolbar()}
        </div>
    }

    /**
     * 绘制快速工具栏
     *
     * @return {*} 
     * @memberof MDControlBase
     */
    public renderQuickToolbar(): any {
        return <view-toolbar
                class="app-quick-toolbar"
                toolbarModels={this.quickToolbarModels}
                on-item-click={(data: any, $event: any) => {
                    throttle(this.handleItemClick, ['quicktoolbar', data, $event], this);
                }}
            ></view-toolbar>
    }

    /**
     * 绘制批处理工具栏
     *
     * @return {*} 
     * @memberof MDControlBase
     */
    public renderBatchToolbar(): any {
        for (let index = 0; index < this.batchToolbarModels.length; index++) {
            this.batchToolbarModels[index].disabled = this.selections.length <= 0;
        }
        return <template v-show={this.selections.length > 0 || this.viewStyle == 'DEFAULT'}>
            <view-toolbar
                class="app-batch-toolbar"
                toolbarModels={this.batchToolbarModels}
                on-item-click={(data: any, $event: any) => {
                    throttle(this.handleItemClick, ['batchtoolbar', data, $event], this);
                }}
            ></view-toolbar>
        </template>
    }

    /**
     * 初始化数据映射
     * 
     * @memberof MDControlBase
     */
    public initDataMap() { }

    /**
     * 初始化分组配置
     * 
     * @memberof MDControlBase
     */
    public initGroupOptions() {
        this.isEnableGroup = (this.controlInstance.enableGroup && this.controlInstance.getGroupPSAppDEField?.()) ? true : false;
        if (this.isEnableGroup) {
            this.groupMode = this.controlInstance.groupMode;
            this.groupField = this.controlInstance.getGroupPSAppDEField().codeName?.toLowerCase();
            this.groupCodeList = this.controlInstance.getGroupPSCodeList?.() as IPSAppCodeList;
        }
    }

    /**
     * 分组
     * 
     * @memberof MDControlBase
     */
    public group() {
        if (Object.is(this.groupMode, "AUTO")) {
            this.drawGroup();
        } else if (Object.is(this.groupMode, "CODELIST")) {
            this.drawCodelistGroup();
        }
    }

    /**
     * 自动分组
     * 
     * @memberof MDControlBase
     */
    public drawGroup() { }

    /**
     * 代码表分组
     * 
     * @memberof MDControlBase
     */
    public drawCodelistGroup() { }
}