common-view-engine.ts 7.0 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
import { LogUtil, Util } from "../utils";
import { ViewEngine } from "./view-engine";

/**
 * 视图引擎基类
 *
 * @export
 * @class CommonViewEngine
 */
export class CommonViewEngine extends ViewEngine {

    /**
     * 部件引擎集合
     *
     * @memberof CommonViewEngine
     */
    public ctrlEngineArray: Array<any> = [];

    /**
     * 视图部件Map
     *
     * @memberof CommonViewEngine
     */
    public viewCtrlMap: Map<string, any> = new Map();

    /**
     * 引擎初始化
     *
     * @param {*} [options={}]
     * @memberof CommonViewEngine
     */
    public init(options: any = {}): void {
        this.initViewControlMap(options.ctrl);
        this.initCtrlEngineArray(options.engine);
        super.init(options);
    }

    /**
     * 初始化引擎Map
     *
     * @param {*} options
     * @memberof CommonViewEngine
     */
    public initCtrlEngineArray(options: any) {
        if (options && options.length > 0) {
            this.ctrlEngineArray = [];
            options.forEach((element: any) => {
                const result = this.handleViewEngineParams(element);
                this.ctrlEngineArray.push(result);
            });
        }
    }

    /**
     * 初始化视图部件Map
     *
     * @param {*} options
     * @memberof CommonViewEngine
     */
    public initViewControlMap(options: any) {
        if (options && options.length > 0) {
            options.forEach((element: any) => {
                this.viewCtrlMap.set(element.name, element.ctrl);
            });
        }
    }

    /**
     * 引擎加载
     *
     * @param {*} [opts={}]
     * @memberof CommonViewEngine
     */
    public load(opts: any = {}): void {
        // 处理搜索部件加载并搜索(参数可指定触发部件)
        if (this.ctrlEngineArray.length > 0) {
            for (let element of this.ctrlEngineArray) {
                if (element.triggerCtrlName && Object.is(element.triggerCtrlName, 'VIEW')) {
                    if (element.triggerType && Object.is(element.triggerType, 'CtrlLoadAndSearch')) {
                        this.setViewState2({ tag: element.targetCtrlName, action: 'loaddraft', viewdata: Util.deepCopy(opts) });
                    }
                }
            }
        }
        // 处理部件加载(参数可指定触发部件)无指定触发部件时由容器触发
        if (this.ctrlEngineArray.length > 0) {
            for (let element of this.ctrlEngineArray) {
                if (element.triggerType && Object.is(element.triggerType, 'CtrlLoad') && !element.triggerCtrlName) {
                    if (element.targetCtrlName && Object.is(element.targetCtrlName, 'form')) {
                        if (this.keyPSDEField &&
                            this.view.context[this.keyPSDEField] &&
                            !Object.is(this.view.context[this.keyPSDEField], '')) {
                            this.setViewState2({ tag: element.targetCtrlName, action: 'load', viewdata: Util.deepCopy(opts) });
                        } else {
                            this.setViewState2({ tag: element.targetCtrlName, action: 'loaddraft', viewdata: Util.deepCopy(opts) });
                        }
                    } else {
                        this.setViewState2({ tag: element.targetCtrlName, action: 'load', viewdata: Util.deepCopy(opts) });
                    }
                }
            }
        }
    }

    /**
     * 部件事件机制
     *
     * @param {string} ctrlName
     * @param {string} eventName
     * @param {*} args
     * @memberof CommonViewEngine
     */
    public onCtrlEvent(ctrlName: string, eventName: string, args: any): void {
        super.onCtrlEvent(ctrlName, eventName, args);
        // 处理部件加载(参数可指定触发部件)
        if (Object.is(eventName, 'search') ||
            Object.is(eventName, 'load') ||
            Object.is(eventName, 'selectionchange')) {
            if (this.ctrlEngineArray.length > 0) {
                for (let element of this.ctrlEngineArray) {
                    if (element.triggerCtrlName && Object.is(element.triggerCtrlName, ctrlName)) {
                        if (element.triggerType && Object.is(element.triggerType, 'CtrlLoad')) {
                            if (this.view) {
                                if (this.view.$refs[element.targetCtrlName] && this.view.$refs[element.targetCtrlName].ctrl) {
                                    this.view.$refs[element.targetCtrlName].ctrl.setNavdatas(Util.deepCopy(args));
                                }
                                if (Util.isExistData(args)) {
                                    this.setViewState2({ tag: element.targetCtrlName, action: 'load', viewdata: Util.deepCopy(args) });
                                } else {
                                    this.setViewState2({ tag: element.targetCtrlName, action: 'reset', viewdata: Util.deepCopy(args) });
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 处理视图引擎参数
     *
     * @param {*} args 引擎数据
     * @memberof CommonViewEngine
     */
    public handleViewEngineParams(args: any) {
        switch (args.engineType) {
            case 'CtrlLoadTrigger':
                return this.handleCtrlLoad(args.getPSUIEngineParams);
            case 'CtrlLoad':
                return this.handleCtrlLoad(args.getPSUIEngineParams);
            case 'CtrlLoadAndSearch':
                return this.CtrlLoadAndSearch(args.getPSUIEngineParams);
            default:
                LogUtil.warn(`${args.engineType}暂未支持`);
                break;
        }
    }

    /**
     * 处理搜索部件加载并搜索(参数可指定触发部件)
     *
     * @param {*} args 引擎参数
     * @memberof CommonViewEngine
     */
    public CtrlLoadAndSearch(args: any) {
        if (!args || args.length < 1) {
            return null;
        }
        const targetCtrl = args.find((item: any) => {
            return item.name === 'CTRL' && item.paramType === 'CTRL';
        })
        return { triggerCtrlName: 'VIEW', triggerType: 'CtrlLoadAndSearch', targetCtrlName: targetCtrl.ctrlName };
    }

    /**
     * 处理部件加载(参数可指定触发部件)
     *
     * @param {*} args 引擎参数
     * @memberof CommonViewEngine
     */
    public handleCtrlLoad(args: any) {
        if (!args || args.length < 1) {
            return null;
        }
        const triggerCtrl = args.find((item: any) => {
            return item.name === 'TRIGGER' && item.paramType === 'CTRL';
        })
        const targetCtrl = args.find((item: any) => {
            return item.name === 'CTRL' && item.paramType === 'CTRL';
        })
        return { triggerCtrlName: triggerCtrl?.ctrlName, triggerType: 'CtrlLoad', targetCtrlName: targetCtrl.ctrlName };
    }

    /**
     * @description 视图销毁
     * @memberof CommonViewEngine
     */
    public destroyed() {
        super.destroyed();
        this.viewCtrlMap.clear();
    }

}