deindex-view-engine.ts 6.6 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
import { ViewEngine } from './view-engine';

/**
 * @description 实体首页视图引擎
 * @export
 * @class DeIndexViewEngine
 * @extends {ViewEngine}
 */
export class DeIndexViewEngine extends ViewEngine {
    /**
     * @description 表单部件
     * @private
     * @type {*}
     * @memberof DeIndexViewEngine
     */
    private form: any;

    /**
     * @description 数据关系栏部件
     * @private
     * @type {*}
     * @memberof DeIndexViewEngine
     */
    private drBar: any;

    /**
     * @description 引擎初始化
     * @param {*} opts
     * @memberof DeIndexViewEngine
     */
    public init(opts: any) {
        this.form = opts.form;
        this.drBar = opts.drbar;
        super.init(opts);
    }

    /**
     * @description 引擎加载
     * @memberof DeIndexViewEngine
     */
    public load() {
        super.load();
        const form = this.getForm();
        if (form && this.isLoadDefault) {
            const tag = form.name;
            let action: string = 'loaddraft';
            if (
                this.keyPSDEField &&
                this.view.context[this.keyPSDEField] &&
                !Object.is(this.view.context[this.keyPSDEField], '')
            ) {
                action = 'load';
            }
            this.setViewState2({ tag: tag, action: action, viewdata: this.view.viewparams });
        }
    }

    /**
     * @description 处理部件事件
     * @param {string} ctrlName 部件名称
     * @param {string} eventName 事件名称
     * @param {*} args 事件数据
     * @memberof DeIndexViewEngine
     */
    public onCtrlEvent(ctrlName: string, eventName: string, args: any): void {
        if (Object.is(ctrlName, this.getForm()?.name)) {
            this.handleFormEvents(eventName, args);
        }
        if (Object.is(ctrlName, this.getDrBar()?.name)) {
            this.handleDrBarEvents(eventName, args);
        }
    }

    /**
     * @description 处理表单部件事件
     * @param {string} eventName 事件名
     * @param {*} args 事件数据
     * @memberof DeIndexViewEngine
     */
    public handleFormEvents(eventName: string, args: any) {
        if (Object.is(eventName, 'load')) {
            this.onFormLoad(args);
        }
        if (Object.is(eventName, 'save')) {
            this.onFormSave(args);
        }
    }

    /**
     * @description 处理数据关系栏部件事件
     * @param {string} eventName 事件名
     * @param {*} args 事件数据
     * @memberof DeIndexViewEngine
     */
    public handleDrBarEvents(eventName: string, args: any) {
        if (Object.is(eventName, 'selectionchange')) {
            this.drBarSelectionChange(args);
        }
    }

    /**
     * @description 表单加载完成
     * @param {*} [arg={}]
     * @memberof DeIndexViewEngine
     */
    public onFormLoad(arg: any = {}): void {
        this.view.model.dataInfo = Object.is(arg.srfuf, '1')
            ? this.majorPSDEField
                ? arg[this.majorPSDEField]
                : arg.srfmajortext
            : this.view.$t('app.local.new');

        this.setTabCaption(this.view.model.dataInfo, Object.is(arg.srfuf, '0'));
        const newdata: boolean = !Object.is(arg.srfuf, '1');
        this.calcToolbarItemState(newdata);
        this.calcToolbarItemAuthState(this.transformData(arg));
        this.setDataCtrlData(arg,true);
        this.emitViewEvent('load', arg);
        const drBar = this.getDrBar();
        if (drBar) {
            const tag = drBar.name;
            this.setViewState2({ tag: tag, action: 'state', viewdata: arg });
        }
    }

    /**
     * @description 表单保存完成
     * @param {*} [arg={}]
     * @memberof DeIndexViewEngine
     */
    public onFormSave(arg: any = {}): void {
        this.view.model.dataInfo = Object.is(arg.srfuf, '1')
            ? this.majorPSDEField
                ? arg[this.majorPSDEField]
                : arg.srfmajortext
            : this.view.$t('app.local.new');

        this.setTabCaption(this.view.model.dataInfo, Object.is(arg.srfuf, '0'));
        const newdata: boolean = !Object.is(arg.srfuf, '1');
        this.calcToolbarItemState(newdata);
        this.calcToolbarItemAuthState(this.transformData(arg));
        this.emitViewEvent('save', arg);
        this.emitViewEvent('viewdataschange',arg);
        const drBar = this.getDrBar();
        if (drBar) {
            const tag = drBar.name;
            this.setViewState2({ tag: tag, action: 'state', viewdata: arg });
        }
    }

    /**
     * @description 数据关系栏选中数据变化
     * @param {any[]} args 选中数据
     * @return {*}  {void}
     * @memberof DeIndexViewEngine
     */
    public drBarSelectionChange(args: any[]): void {
        const item = args[0];
        if (!item || Object.keys(item).length === 0) {
            return;
        }
        this.view.selection = {};
        Object.assign(this.view.selection, JSON.parse(JSON.stringify(item)));
    }

    /**
     * @description 设置分页标题
     * @param {string} info
     * @param {boolean} isNew
     * @memberof DeIndexViewEngine
     */
    public setTabCaption(info: string, isNew: boolean): void {
        let viewModel: any = this.view.model;
        if (viewModel && info && !Object.is(info, '')) {
            // 解决表格视图标题问题
            if (this.view.$tabPageExp && this.view.viewDefaultUsage) {
                this.view.$tabPageExp.setCurPageCaption({
                    caption: viewModel.srfCaption,
                    title: viewModel.srfCaption,
                    info: info,
                    viewtag: this.view.viewtag,
                    cacheRoutePath: this.view.cacheRoutePath
                }
                );
            }
            if (this.view.$route) {
                this.view.$route.meta.info = info;
            }
        }
    }

    /**
     * @description 转化数据
     * @param {*} arg 源数据
     * @return {*} 
     * @memberof DeIndexViewEngine
     */
    public transformData(arg: any) {
        const form = this.getForm();
        if (form && form.transformData && form.transformData instanceof Function) {
            return form.transformData(arg);
        }
        return null;
    }

    /**
     * @description 获取表单部件
     * @return {*} 
     * @memberof DeIndexViewEngine
     */
    public getForm() {
        return this.form;
    }

    /**
     * @description 获取数据关系栏部件
     * @return {*} 
     * @memberof DeIndexViewEngine
     */
    public getDrBar() {
        return this.drBar;
    }

    /**
     * @description 视图销毁
     * @memberof DeIndexViewEngine
     */
     public destroyed() {
        super.destroyed();
        this.form = null;
        this.drBar = null;
    }

}