import { IParam, IMDCtrlAbility, IMDViewAbility, MDViewActionType, IMDViewController, IMDViewControllerParams, SearchFormActionType, MDCtrlActionType, ISearchFormAbility, IMDViewModel, IViewActionResult, ICtrlActionResult, } from '@/core/interface'; import { IMDViewStore } from '@/core/interface/view/store/i-md-view-store'; import { AuthUtil, deepCopy } from '@/core/utils'; import { DEViewController } from './de-view-controller'; export class MDViewController< T, S extends IMDViewStore, A extends IMDViewAbility > extends DEViewController implements IMDViewController { /** * 视图模型 * * @protected * @type {IMDViewModel} * @memberof MDViewController */ protected declare readonly model: IMDViewModel; /** * 查询参数 * * @protected * @type {IParam} * @memberof MDViewController */ protected queryParams: IParam = {}; /** * 是否抛出快速分组值 * * @private * @type {boolean} * @memberof MDViewController */ protected isEmitQuickGroupValue = false; /** * 处理视图初始化 * * @protected * @param {(IMDViewControllerParams)} params * @memberof MDViewController */ protected processViewInit( params: IMDViewControllerParams ) { super.processViewInit(params); Object.assign(this.store, { expandSearchForm: this.model.expandSearchForm ? true : false, }); } /** * 获取多数据部件能力(具体多数据视图实现) * * @protected * @param {string} name * @return {*} {IMDCtrlAbility} * @memberof MDViewController */ protected getMainCtrlAbility(): IMDCtrlAbility | undefined { throw new Error('Method is no'); } /** * 获取搜索表单能力 * * @protected * @return {*} {(ISearchFormAbility | undefined)} * @memberof MDViewController */ protected getSearchFormAbility(): ISearchFormAbility | undefined { const searchForm = this.model.ctrls.find( (ctrl: IParam) => ctrl.controlType === 'SEARCHFORM' && ctrl.codeName !== 'QUICKSEARCHFORM' ); if (searchForm) { return this.getSubAbility(searchForm.name); } return undefined; } /** * 视图挂载 * * @param {(IParam | undefined)} [opts] * @return {*} {void} * @memberof MDViewController */ public viewMounted(opts: IParam = {}): void { super.viewMounted(opts); App.getGlobalNotificationHelper().setAbility( (this.getModel() as IMDViewModel).entityCodeName || '', this.getAbility() ); if (this.model.useDefaultLayout) { if (!this.isLoadDefault) { this.isLoadDefault = true; return; } if ( this.model.enableQuickGroup && this.model.quickGroupCodeList && !this.isEmitQuickGroupValue ) { return; } const searchForm = this.getSearchFormAbility(); if (searchForm) { const tempViewParams = deepCopy(this.store.viewParams); searchForm.loadDraft(tempViewParams); return; } this.load(); } else { this.initLayout().then(() => { if (!this.isLoadDefault) { this.isLoadDefault = true; return; } const searchForm = this.getSearchFormAbility(); if (searchForm) { const tempViewParams = deepCopy(this.store.viewParams); searchForm.loadDraft(tempViewParams); return; } this.load(); }); } } /** * 加载数据 * * @param {IParam} [args] * @return {*} {Promise} * @memberof MDViewController */ public load(args?: IParam): Promise { const mdCtrl = this.getMainCtrlAbility(); if (mdCtrl) { const tempViewParams = deepCopy(this.store.viewParams); return mdCtrl.load(Object.assign(tempViewParams, args)); } else { return Promise.reject({ ok: false, data: null, rowData: { status: 500 }, }); } } /** * 刷新 * * @param {(IParam | undefined)} [args] * @return {*} {Promise} * @memberof MDViewController */ public refresh(args?: IParam | undefined): Promise { const mdCtrl = this.getMainCtrlAbility(); if (!mdCtrl) { return Promise.resolve(false); } const tempViewParams = deepCopy(this.store.viewParams); return mdCtrl.refresh(Object.assign(tempViewParams, args)); } /** * 视图保存 * * @param {IParam} [args] * @return {*} {Promise} * @memberof MDViewController */ public save(args?: IParam): Promise { return new Promise((resolve, reject) => { const mdCtrl = this.getMainCtrlAbility(); if (mdCtrl && mdCtrl.save) { const tempViewParams = deepCopy(this.store.viewParams); mdCtrl .save(Object.assign(tempViewParams, args)) .then((res: ICtrlActionResult) => { const data = res.data ? res.data : []; this.emit('viewDataSave', data); resolve({ ok: true, 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 {(MDCtrlActionType | SearchFormActionType)} action * @param {IParam[]} data * @memberof GridViewController */ public handleCtrlAction( name: string, action: MDCtrlActionType | SearchFormActionType, data: IParam[] ): void { const mdCtrl = this.getMainCtrlAbility(); if (mdCtrl && name === mdCtrl.name) { this.handleMDCtrlAction(action as MDCtrlActionType, data); return; } const searchForm = this.getSearchFormAbility(); if (searchForm && name === searchForm.name) { this.handleSearchFormAction(action as SearchFormActionType, data); return; } } /** * 处理搜索表单部件行为 * * @param {SearchFormActionType} action * @param {IParam[]} data * @memberof MDViewController */ protected handleSearchFormAction( action: SearchFormActionType, data: IParam[] ) { const mdCtrl = this.getMainCtrlAbility(); if (mdCtrl) { if (action === 'load') { const tempViewParams = deepCopy(this.store.viewParams); mdCtrl.load(tempViewParams); } if (action === 'search') { const tempViewParams = deepCopy(this.store.viewParams); mdCtrl.load(tempViewParams); } } } /** * 处理多数据部件行为 * * @template T * @param {(T | MDCtrlEventActionType)} action * @param {IParam[]} data * @memberof MDViewController */ protected handleMDCtrlAction( action: T | MDCtrlActionType, data: IParam[] ) { if (action === 'beforeLoad') { this.handleMDCtrlBeforeLoad(data); } if (action === 'load') { this.handleMDCtrlLoad(data); } if (action === 'selectionChange') { this.handleSelectionChange(data); } if (action === 'dataActive') { this.handleDataActive(data); } } /** * 处理多数据部件加载之前 * * @protected * @param {IParam[]} data * @memberof MDViewController */ protected handleMDCtrlBeforeLoad(data: IParam[]) { const param = data && data.length > 0 ? data[0] : {}; const searchForm = this.getSearchFormAbility(); if (searchForm) { Object.assign(param, searchForm.getData()[0]); } if (this.queryParams && Object.keys(this.queryParams).length > 0) { if (this.queryParams['quickGroupData']) { Object.assign(param, this.queryParams['quickGroupData']); } if (this.queryParams['query'] && !this.model.expandSearchForm) { Object.assign(param, { query: this.queryParams['query'] }); } } } /** * 处理多数据部件加载 * * @protected * @param {IParam[]} data * @memberof MDViewController */ protected handleMDCtrlLoad(data: IParam[]) { AuthUtil.calcToolbarItemState(true, this.store.toolbarItems); this.calcToolbarItemAuthState(data[0]); this.emit('viewDataLoaded', data); } /** * 处理选中 * * @protected * @param {IParam[]} data * @memberof MDViewController */ protected handleSelectionChange(data: IParam[]) { this.emit('viewDataChange', data); // TODO 属性面板 const state = data.length > 0 && !Object.is(data[0].srfkey, '') ? false : true; AuthUtil.calcToolbarItemState(state, this.store.toolbarItems); if (data && data.length > 0) { this.calcToolbarItemAuthState(data[0]); } else { this.calcToolbarItemAuthState({}); } } /** * 处理数据激活 * * @protected * @param {IParam[]} data * @return {*} * @memberof MDViewController */ protected handleDataActive(data: IParam[]) { if (!data || data.length === 0) { return; } const { context, viewParams } = this.store; this.openData(context, viewParams, data); } /** * 快速分组值变化 * * @param {IParam} item * @memberof MDViewController */ public handleQuickGroupValueChange(item: IParam) { if (item) { this.queryParams.quickGroupData = item.data; const mdCtrl = this.getMainCtrlAbility(); if (mdCtrl) { const tempViewParams = deepCopy(this.store.viewParams); mdCtrl.load(tempViewParams); } } this.isEmitQuickGroupValue = true; } /** * 搜索 * * @param {string} searchValue * @memberof MDViewController */ public search(searchValue: string) { this.queryParams.query = searchValue; const mdCtrl = this.getMainCtrlAbility(); if (mdCtrl) { mdCtrl.load({ query: searchValue, srfpagereset: true }); } } /** * 切换过滤器 * * @memberof MDViewController */ public toggleFilter(): void { this.store.expandSearchForm = !this.store.expandSearchForm; } /** * 视图销毁 * * @memberof MDViewController */ public viewDestroy(): void { super.viewDestroy(); App.getGlobalNotificationHelper().destroyAbility( (this.getModel() as IMDViewModel).entityCodeName || '', this.getAbility() ); } /** * 获取能力 * * @return {*} {A} * @memberof MDViewController */ public getAbility(): A { return { ...super.getAbility(), toggleFilter: this.toggleFilter.bind(this), }; } }