statewizardpanel-control-base.tsx 21.5 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 268 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 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 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 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 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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
import { IPSAppDEField, IPSControlAction, IPSDEEditForm, IPSDEStateWizardPanel, IPSDEWizard, IPSDEWizardEditForm, IPSDEWizardForm, IPSDEWizardStep } from '@ibiz/dynamic-model-api';
import { StateWizardPanelControlInterface, ViewState } from 'ibiz-core';
import { Subject, Subscription } from 'rxjs';
import { AppWizardPanelService } from '..';
import { MainControlBase } from './main-control-base';

export class StateWizardPanelControlBase extends MainControlBase implements StateWizardPanelControlInterface {

    /**
     * 向导面板部件实例
     * 
     * @memberof StateWizardPanelControlBase
     */
    public declare controlInstance: IPSDEStateWizardPanel;

    /**
     * 部件行为--init
     *
     * @type {string}
     * @memberof StateWizardPanelControlBase
     */
    public initAction!: string;

    /**
     * 部件行为--finish
     *
     * @type {string}
     * @memberof StateWizardPanelControlBase
     */
    public finishAction!: string;

    /**
     * 向导表单参数
     *
     * @type {*}
     * @memberof StateWizardPanelControlBase
     */
    public formParam: any = {};

    /**
     * 执行过的表单
     *
     * @public
     * @type {Array<string>}
     * @memberof StateWizardPanelControlBase
     */
    public historyForms: Array<string> = [];

    /**
     * 步骤行为集合
     *
     * @type {*}
     * @memberof StateWizardPanelControlBase
     */
    public stepActions: any = {};

    /**
     * 向导表单集合
     *
     * @type {Array<any>}
     * @memberof StateWizardPanelControlBase
     */
    public wizardForms: Array<any> = [];

    /**
     * 当前状态
     *
     * @memberof StateWizardPanelControlBase
     */
    public curState = '';

    /**
     * 当前激活表单
     *
     * @type {string}
     * @memberof StateWizardPanelControlBase
     */
    public activeForm: string = '';

    /**
     * 首表单
     *
     * @type {string}
     * @memberof StateWizardPanelControlBase
     */
    public firstForm: string = '';

    /**
     * 状态属性
     *
     * @type {string}
     * @memberof StateWizardPanelControlBase
     */
    public stateField: string = '';

    /**
     * 步骤标识集合
     *
     * @type {*}
     * @memberof StateWizardPanelControlBase
     */
    public stepTags: any = {};

    /**
     * 视图状态订阅对象
     *
     * @public
     * @type {Subject<{action: string, data: any}>}
     * @memberof StateWizardPanelControlBase
     */
    public wizardState: Subject<ViewState> = new Subject();

    /**
     * 步骤是否显示集合
     *
     * @type {*}
     * @memberof StateWizardPanelControlBase
     */
    public stepVisiable: any = {};

    /**
     * 当前显示表单
     *
     * @type {string}
     * @memberof StateWizardPanelControlBase
     */
    public curShow: string = "";

    /**
     * 抽屉状态
     *
     * @memberof StateWizardPanelControlBase
     */
    public drawerOpenStatus: any = {
        isOpen: false,
        formName: ""
    }

    /**
     * @description 状态向导面板部件事件
     * @type {(Subscription | undefined)}
     * @memberof StateWizardPanelControlBase
     */
    public stateWizardControlEvent: Subscription | undefined;

    /**
     * 部件模型数据初始化实例
     *
     * @param {*} [args]
     * @memberof StateWizardPanelControlBase
     */
    public async ctrlModelInit(args?: any) {
        await super.ctrlModelInit(args);
        this.stateField = (this.controlInstance.getStatePSAppDEField() as IPSAppDEField)?.codeName?.toLowerCase();
        //TODO行为首字母大写
        this.stateField = (this.controlInstance.getStatePSAppDEField() as IPSAppDEField)?.codeName?.toLowerCase();
        this.initAction = (this.controlInstance.getInitPSControlAction() as IPSControlAction)?.getPSAppDEMethod()?.codeName || 'Get';
        this.finishAction = (this.controlInstance.getFinishPSControlAction() as IPSControlAction)?.getPSAppDEMethod()?.codeName || 'Update';
        if (!(this.Environment && this.Environment.isPreviewMode)) {
            this.service = new AppWizardPanelService(this.controlInstance, this.context);
            await this.service.loaded();
        }
        this.initFirstForm();
    }

    /**
     * 部件初始化
     *
     * @memberof StateWizardPanelControlBase
     */
    public ctrlInit() {
        super.ctrlInit();
        this.regFormActions();
        this.doInit();
        if (this.viewState) {
            this.stateWizardControlEvent = this.viewState.subscribe(({ tag, action, data }: any) => {
                if (Object.is(tag, this.name)) {
                    if (Object.is('load', action)) {
                        this.doInit(data);
                    }
                }
            });
        }
    }

    /**
     * 注册表单步骤行为
     *
     * @memberof StateWizardPanelControlBase
     */
    public regFormActions() {
        const wizard: IPSDEWizard | null = this.controlInstance.getPSDEWizard();
        const wizardForms: Array<IPSDEWizardForm> = wizard?.getPSDEWizardForms() || [];
        if (wizard && wizardForms.length > 0) {
            wizardForms.forEach((form: IPSDEWizardForm) => {
                this.regFormAction(`${this.controlInstance.name}_form_${form.formTag.toLowerCase()}`, form.getStepActions() || [], this.getStepTag(wizard.getPSDEWizardSteps() || [], (form?.getPSDEWizardStep()?.codeName) as string))
            })
        }
    }

    /**
     * 初始化当前激活表单
     * 
     * @memberof StateWizardPanelControlBase
     */
    public initFirstForm() {
        const wizard: IPSDEWizard | null = this.controlInstance.getPSDEWizard();
        const wizardForms: Array<IPSDEWizardForm> = wizard?.getPSDEWizardForms() || [];
        if (wizard && wizardForms.length > 0) {
            //TODO IPSDEWizard getFirstPSDEWizardForm()无返回值
            const firstForm = wizardForms.find((form: any) => { return form.firstForm; })
            if (firstForm) {
                this.firstForm = `${this.controlInstance.name}_form_${firstForm.formTag?.toLowerCase()}`;
            }
        }
    }

    /**
     * 状态表单加载完成
     *
     * @param {*} args 表单参数
     * @param {string} name 名称
     * @memberof StateWizardPanelControlBase
     */
    public wizardpanelFormload(args: any, name: string) {
        if (args) {
            Object.assign(this.formParam, args);
        }
    }

    /**
    * 向导表单保存完成
    *
    * @param {*} args 表单参数
    * @param {string} name 名称
    * @memberof StateWizardPanelControlBase
    */
    public wizardpanelFormsave(args: any, name: string) {
        Object.assign(this.formParam, args);
        if (Object.is(this.curState, 'NEXT')) {
            if (this.historyForms.indexOf(name) === -1) {
                this.historyForms.push(name);
            }
            this.setPopVisiable(name, false);
            if (this.getNextForm(name)) {
                this.activeForm = this.getNextForm(name);
                this.setPopVisiable(this.activeForm, true);
                setTimeout(() => {
                    this.formLoad(this.activeForm);
                }, 1);
            } else {
                this.doFinish();
            }
        } else if (Object.is(this.curState, 'FINISH')) {
            this.doFinish();
        }
    }

    /**
     * 步骤标题点击
     *
     * @param {string} name 步骤名称
     * @return {*} 
     * @memberof StateWizardPanelControlBase
     */
    public stepTitleClick(name: string) {
        const that = this;
        let activeIndex: number = that.wizardForms.indexOf(that.activeForm);
        let curIndex: number = that.wizardForms.indexOf(name);
        if (curIndex > activeIndex) {
            setTimeout(() => {
                (that.$refs[name + '_popover'] as any).showPopper = false;
                that.curShow = "";
            }, 0)
            return;
        }
        that.stepVisiable[name] = !that.stepVisiable[name];
        if (that.stepVisiable[name]) {
            that.curShow = name;
            that.formLoad(name);
        } else {
            that.curShow = "";
        }
    }

    /**
     * 左右按钮点击
     *
     * @param {*} mode 左右标识
     * @return {*} 
     * @memberof StateWizardPanelControlBase
     */
    public handleClick(mode: any) {
        if (Object.is(this.curShow, "")) {
            return;
        }
        let curIndex: number = this.wizardForms.indexOf(this.curShow);
        if (Object.is(mode, "PRE") && (curIndex !== 0)) {
            this.setPopVisiable(this.wizardForms[curIndex], false);
            setTimeout(() => {
                this.setPopVisiable(this.wizardForms[curIndex - 1], true);
                this.formLoad(this.wizardForms[curIndex - 1]);
            }, 0);
        }
        if (Object.is(mode, "NEXT") && (curIndex < (this.wizardForms.length - 1) && this.historyForms.includes(this.wizardForms[curIndex + 1]))) {
            this.setPopVisiable(this.wizardForms[curIndex], false);
            setTimeout(() => {
                this.setPopVisiable(this.wizardForms[curIndex + 1], true);
                this.formLoad(this.wizardForms[curIndex + 1]);
            }, 0);
        }
    }

    /**
     * 打开链接
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    public handleOpen(name: string) {
        this.handleClose(name);
        this.drawerOpenStatus.isOpen = true;
        this.drawerOpenStatus.formName = name;
    }

    /**
     * 关闭
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    public handleClose(name: string) {
        this.setPopVisiable(name, false);
    }

    /**
     * 上一步
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    public clickPrev(name: string) {
        const length = this.historyForms.length;
        if (length > 0) {
            this.curState = 'PREV';
            let curIndex: number = this.wizardForms.indexOf(name);
            this.handleCtrlEvents('onbeforeprev', { action: 'load', data: this.formParam }).then((res: boolean) => {
                if (res) {
                    this.setPopVisiable(name, false);
                    setTimeout(() => {
                        this.setPopVisiable(this.historyForms[curIndex - 1], true);
                        this.formLoad(this.historyForms[curIndex - 1]);
                    }, 1);
                }
            })
        }
    }

    /**
     * 下一步
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    public clickNext(name: string) {
        if (name) {
            if (this.$refs && this.$refs[name]) {
                let form: any = (this.$refs[name] as any).ctrl;
                if (form.formValidateStatus()) {
                    this.curState = 'NEXT';
                    this.handleCtrlEvents('onbeforenext', { action: 'save', data: this.formParam }).then((res: boolean) => {
                        if (res) {
                            this.wizardState.next({ tag: name, action: 'save', data: this.formParam });
                        }
                    })
                } else {
                    this.$throw((this.$t('app.commonwords.rulesexception') as string), 'onClickNext');
                }
            }
        }

    }

    /**
     * 完成
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    public clickFinish(name: string) {
        if (name) {
            if (this.$refs && this.$refs[name]) {
                let form: any = (this.$refs[name] as any).ctrl;
                if (form.formValidateStatus()) {
                    this.curState = 'FINISH';
                    this.handleCtrlEvents('onbeforefinish', { action: 'save', data: this.formParam }).then((res: boolean) => {
                        if (res) {
                            this.wizardState.next({ tag: name, action: 'save', data: this.formParam });
                        }
                    })
                } else {
                    this.$throw((this.$t('app.commonwords.rulesexception') as string), 'onClickFinish');
                }
            }
        }
    }

    /**
     * 抽屉状态改变
     *
     * @param {*} value  值
     * @memberof StateWizardPanelControlBase
     */
    public onVisibleChange(value: any) {
        if (!value) {
            this.drawerOpenStatus.isOpen = false;
        }
    }

    /**
     * 获取步表单
     *
     * @param {IPSDEWizardStep} step 步骤模型
     * @return {*} 
     * @memberof StateWizardPanelControlBase
     */
    public getStepForm(step: IPSDEWizardStep) {
        const editForms: Array<IPSDEEditForm> = this.controlInstance.getPSDEEditForms() || [];
        let stepForm: any;
        if (editForms.length > 0) {
            stepForm = editForms.find((form: IPSDEEditForm) => {
                const wizardForm: IPSDEWizardForm | null = (form as IPSDEWizardEditForm)?.getPSDEWizardForm();
                if (wizardForm) {
                    const wizardStep: string | undefined = wizardForm.getPSDEWizardStep()?.codeName;
                    if (wizardStep && wizardStep == step.codeName) {
                        return true;
                    }
                }
                return false;
            });
        }
        return stepForm ? stepForm.name : "";
    }

    /**
     * 设置popover是否显示
     *
     * @param {string} name 表单名称
     * @param {boolean} isVisiable 是否显示
     * @memberof StateWizardPanelControlBase
     */
    public setPopVisiable(name: string, isVisiable: boolean) {
        this.stepVisiable[name] = isVisiable;
        const refFrom = (this.$refs[name + '_popover'] as any);
        if (refFrom) {
            refFrom.showPopper = isVisiable;
            this.curShow = isVisiable ? name : "";
        }
    }

    /**
     * 是否显示
     *
     * @param {string} name 表单名称
     * @param {string} type 类型
     * @return {*} 
     * @memberof StateWizardPanelControlBase
     */
    public isVisiable(name: string, type: string) {
        const actions: Array<string> = this.stepActions[name];
        if (actions && actions.indexOf(type) !== -1 && Object.is(name, this.activeForm)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 部件事件
     * @param ctrl 部件 
     * @param action  行为
     * @param data 数据
     * 
     * @memberof StateWizardPanelControlBase
     */
    public onCtrlEvent(controlname: string, action: string, data: any) {
        if (Object.is(action, "save")) {
            this.wizardpanelFormsave(data, controlname);
        } else if (Object.is(action, "load")) {
            this.wizardpanelFormload(data, controlname);
        }
    }

    /**
     * 获取下一步向导表单
     *
     * @memberof StateWizardPanelControlBase
     */
    public getNextForm(name: string) {
        let index = this.wizardForms.indexOf(name);
        if (index >= 0) {
            if (this.wizardForms[index + 1]) {
                return this.wizardForms[index + 1];
            }
        }
        return undefined;
    }

    /**
     * 完成行为
     *
     * @memberof StateWizardPanelControlBase
     */
    public doFinish() {
        let arg: any = {};
        Object.assign(arg, this.formParam);
        Object.assign(arg, { viewparams: this.viewparams });
        let tempContext: any = JSON.parse(JSON.stringify(this.context));
        this.onControlRequset('doFinish', tempContext, arg);
        const post: Promise<any> = this.service.finish(this.finishAction, tempContext, arg, this.showBusyIndicator);
        post.then((response: any) => {
            this.onControlResponse('doFinish', response);
            if (response && response.status === 200) {
                const data = response.data;
                this.handleCtrlEvents('onfinishsuccess', { action: this.finishAction, navParam: arg, data: data }).then((res: boolean) => {
                    if (res) {
                        this.ctrlEvent({
                            controlname: this.controlInstance.name,
                            action: 'finish',
                            data: data,
                        });
                    }
                })
            } else {
                this.handleCtrlEvents('onfinisherror', { action: this.finishAction, navParam: arg, data: response?.data }).then((res: boolean) => {
                    if (res) {
                        this.$throw(response, 'doFinish');
                    }
                })
            }
        }).catch((response: any) => {
            this.onControlResponse('doFinish', response);
            this.handleCtrlEvents('onfinisherror', { action: this.finishAction, navParam: arg, data: response?.data }).then((res: boolean) => {
                if (res) {
                    this.$throw(response, 'doFinish');
                }
            })
        });
    }

    /**
     * 获取步骤标识
     *
     * @param {Array<any>} wizardSteps 步骤数组
     * @param {string} tag 标识
     * @return {*} 
     * @memberof StateWizardPanelControlBase
     */
    public getStepTag(wizardSteps: Array<any>, tag: string) {
        if (wizardSteps && (wizardSteps.length > 0) && tag) {
            let curStep: any = wizardSteps.find((step: any) => {
                return step.title === tag;
            })
            return curStep.stepTag || tag;
        } else {
            return tag;
        }
    }

    /**
     * 注册表单
     *
     * @param {string} name 名称
     * @param {Array<string>} actions 步骤集合
     * @param {*} stepTag 步骤标识
     * @memberof StateWizardPanelControlBase
     */
    public regFormAction(name: string, actions: Array<string>, stepTag: any) {
        this.stepActions[name] = actions;
        this.stepTags[name] = stepTag;
        this.stepVisiable[name] = false;
        this.wizardForms.push(name);
    }

    /**
     * 计算激活表单
     *
     * @param {*} data 数据
     * @memberof StateWizardPanelControlBase
     */
    public computedActiveForm(data: any) {
        if (data[this.stateField]) {
            if (Object.keys(this.stepTags).length > 0) {
                Object.keys(this.stepTags).forEach((name: string) => {
                    if (this.stepTags[name] === data[this.stateField]) {
                        this.activeForm = name;
                    }
                })
            }
            if (!this.activeForm) {
                this.activeForm = this.firstForm;
            }
        } else {
            this.activeForm = this.firstForm;
        }
        if (this.activeForm) {
            let index = this.wizardForms.indexOf(this.activeForm);
            this.wizardForms.forEach((item: any, inx: number) => {
                if (inx <= index) {
                    this.historyForms.push(item);
                }
            })
        }
    }

    /**
     * 初始化
     *
     * @param {*} [opt={}] 额外参数
     * @memberof StateWizardPanelControlBase
     */
    public doInit(opt: any = {}) {
        const arg: any = { ...opt };
        Object.assign(arg, { viewparams: this.viewparams });
        let tempContext: any = JSON.parse(JSON.stringify(this.context));
        this.handleCtrlEvents('onbeforeinit', { action: this.initAction, navContext: tempContext, navParam: arg }).then((res: any) => {
            if (res) {
                this.onControlRequset('doInit', tempContext, arg);
                const post: Promise<any> = this.service.init(this.initAction, tempContext, arg, this.showBusyIndicator);
                post.then((response: any) => {
                    this.onControlResponse('doInit', response);
                    if (response && response.status === 200) {
                        this.handleCtrlEvents('oninitsuccess', { action: this.initAction, navParam: arg, data: this.formParam }).then((res: boolean) => {
                            if (res) {
                                this.formParam = response.data;
                                if (response.data[this.appDeCodeName.toLowerCase()]) {
                                    Object.assign(this.context, { [this.appDeCodeName.toLowerCase()]: response.data[this.appDeCodeName.toLowerCase()] });
                                }
                                this.computedActiveForm(this.formParam);
                            }
                        })
                    } else {
                        this.handleCtrlEvents('oniniterror', { action: this.initAction, navParam: arg, data: response?.data }).then((res: boolean) => {
                            if (res) {
                                this.$throw(response, 'doInit');
                            }
                        })
                    }
                }).catch((response: any) => {
                    this.onControlResponse('doInit', response);
                    this.handleCtrlEvents('oniniterror', { action: this.initAction, navParam: arg, data: response?.data }).then((res: boolean) => {
                        if (res) {
                            this.$throw(response, 'doInit');
                        }
                    })
                })
            }
        })
    }

    /**
     * 表单加载
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    public formLoad(name: string) {
        if (name) {
            this.wizardState.next({ tag: name, action: 'load', data: this.formParam });
        }
    }

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