import { AuthUtil, deepCopy } from '@/core'; import { DrbarActionType, EditView2ActionType, EditFormActionType, IEditView2Ability, IParam, IViewActionResult, ICtrlActionResult, INavigateParam, } from '@/core/interface'; import { IEditView2Controller } from '@/core/interface/view/controller'; import { IEditView2ControllerParams } from '@/core/interface/view/controller-params'; import { IEditView2Store } from '@/core/interface/view/store'; import { IDrbarAbility, IEditFormAbility, } from '@/core/interface/widgets/ability'; import { DEViewController } from './de-view-controller'; /** * 编辑视图2控制器 * * @export * @class EditView2Controller * @extends {DEViewController} */ export class EditView2Controller extends DEViewController< EditView2ActionType, IEditView2Store, IEditView2Ability > implements IEditView2Controller { /** * 处理视图初始化 * * @protected * @param {IEditView2ControllerParams} params * @memberof EditView2Controller */ protected processViewInit( params: IEditView2ControllerParams ) { super.processViewInit(params); Object.assign(this.store, { dataInfo: '', navParam: {} }); } /** * @description 获取表单部件能力 * @protected * @return {*} {(IEditFormAbility | undefined)} * @memberof EditView2Controller */ protected getMainCtrlAbility(): IEditFormAbility | undefined { const form = this.model.ctrls.find( (ctrl: IParam) => ctrl.controlType === 'FORM' ); if (form) { return this.getSubAbility(form.name); } } /** * @description 获取drbar部件能力 * @protected * @return {*} {(IDrbarAbility | undefined)} * @memberof EditView2Controller */ protected getDrbarAbility(): IDrbarAbility | undefined { const drbar = this.model.ctrls.find( (ctrl: IParam) => ctrl.controlType === 'DRBAR' ); if (drbar) { return this.getSubAbility(drbar.name); } } /** * 视图加载 * * @param {IParam} [opts={}] * @return {*} * @memberof EditView2Controller */ public viewMounted(opts: IParam = {}) { super.viewMounted(opts); if (this.model.useDefaultLayout) { if (!this.isLoadDefault) { this.isLoadDefault = true; return; } this.load(); } else { this.initLayout().then(() => { if (!this.isLoadDefault) { this.isLoadDefault = true; return; } this.load(); }); } } /** * 视图加载 * * @param {IParam} [args] * @return {*} {Promise} * @memberof EditView2Controller */ public load(args?: IParam): Promise { const form = this.getMainCtrlAbility(); if (form) { let action: 'loadDraft' | 'load' = 'loadDraft'; if ( this.model.entityCodeName && this.store.context[this.model.entityCodeName.toLowerCase()] ) { action = 'load'; } return form[action](Object.assign(this.store.viewParams, args)); } else { return Promise.reject({ ok: false, data: null, rowData: { status: 500 }, }); } } /** * 视图保存 * * @param {IParam} [args] * @return {*} {Promise} * @memberof EditView2Controller */ public save(args?: IParam): Promise { return new Promise((resolve, reject) => { const form = this.getMainCtrlAbility(); if (form) { const tempViewParams = deepCopy(this.store.viewParams); form .save(Object.assign(tempViewParams, args)) .then((res: ICtrlActionResult) => { const data = res.data ? res.data : []; this.emit('viewDataSave', data); resolve({ ok: false, data: data, rowData: { status: 200 } }); }) .catch((error) => { this.emit('viewDataSave', []); reject({ ok: false, data: error, rowData: { status: 500 } }); }); } else { this.emit('viewDataSave', []); reject({ ok: false, data: [], rowData: { status: 500 } }); } }); } /** * 处理部件行为 * * @param {string} name 部件标识 * @param {EditFormActionType | DrbarActionType} action 部件行为 * @param {IParam[]} data 行为数据 * @memberof EditView2Controller */ public handleCtrlAction( name: string, action: EditFormActionType | DrbarActionType, data: IParam[] ): void { super.handleCtrlAction(name, action, data); const form = this.getMainCtrlAbility(); const drbar = this.getDrbarAbility(); if (form && name === form.name) { this.handleFormCtrlAction(action as EditFormActionType, data); return; } if (drbar && name === drbar.name) { this.handleDrtabAction( action as DrbarActionType, data as INavigateParam[] ); return; } } /** * 表单部件行为 * * @protected * @template T * @param {(T | EditFormActionType)} action * @param {IParam[]} data * @memberof EditView2Controller */ protected handleFormCtrlAction( action: T | EditFormActionType, data: IParam[] ) { if (action === 'load' || action === 'loadDraft') { this.handleFormLoad(data); } if (action === 'save') { this.handleFormSave(data); } if (action === 'remove') { this.handleFormRemove(data); } } /** * drtab部件行为 * * @protected * @param {DrbarActionType} action * @param {INavigateParam[]} data * @memberof EditView2Controller */ protected handleDrtabAction(action: DrbarActionType, data: INavigateParam[]) { if (Object.is(action, 'selectionChange') && data.length > 0) { this.store.navParam = data[0]; if (!this.model.useDefaultLayout) { const navPos: any = Object.values(this.store.layoutModelDetails).find( (item: any) => { return item.predefinedType === 'NAV_POS'; } ); if (navPos) { navPos.setNavData(data[0]); } } this.emit('viewDataChange', data); } } /** * 处理表单加载完成 * * @param {IParam[]} data 表单数据 * @memberof EditView2Controller */ public async handleFormLoad(data: IParam[]) { await this.handleFormData(data); this.handleDrbarData(data); this.emit('viewDataChange', data); } /** * 处理表单保存 * * @param {IParam[]} data * @memberof EditView2Controller */ protected async handleFormSave(data: IParam[]) { await this.handleFormData(data); this.handleDrbarData(data); this.emit('viewDataChange', data); } /** * 处理表单删除 * * @param {IParam[]} data * @memberof EditView2Controller */ protected handleFormRemove(data: IParam[]) { this.emit('viewDataChange', data); } /** * @description 处理drbar数据 * @param {IParam[]} data * @memberof EditView2Controller */ protected handleDrbarData(data: IParam[]) { const drbar = this.getDrbarAbility(); if (drbar) { const arg = data[0]; if (this.model.entityCodeName) { Object.assign(this.store.context, { srfparentkey: this.model.entityCodeName, }); Object.assign(this.store.viewParams, { srfparentkey: this.model.entityCodeName, }); } if (arg.srfkey) { Object.assign(this.store.context, { srfparentdename: arg.srfkey }); Object.assign(this.store.viewParams, { srfparentdename: arg.srfkey }); } drbar.handleFormChange(arg); } } /** * 处理表单数据 * * @param {IParam[]} data * @memberof EditView2Controller */ protected async handleFormData(data: IParam[]) { const arg = data[0]; this.store.dataInfo = Object.is(arg.srfuf, '1') ? this.model.majorPSAppDEField ? arg[this.model.majorPSAppDEField.toLowerCase()] : arg.srfmajortext : App.ts("app.common.newlybuild","新建"); AuthUtil.calcToolbarItemState( Object.is(arg.srfuf, '0'), this.store.toolbarItems ); await this.calcToolbarItemAuthState(arg); } }