mpickup-view2-engine.ts 11.7 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
import { Util } from '../utils';
import { ViewEngine } from './view-engine';

/**
 * 实体数据多项选择视图(左右关系)引擎
 *
 * @export
 * @class MPickupView2Engine
 * @extends {ViewEngine}
 */
export class MPickupView2Engine extends ViewEngine {

    /**
     * 选择视图面板
     *
     * @type {*}
     * @memberof MPickupView2Engine
     */
    public pickupViewPanel: any = null;

    /**
     * 树导航
     *
     * @type {*}
     * @memberof MPickupView2Engine
     */
    public treeExpBar: any = null;

    /**
     * Creates an instance of MPickupView2Engine.
     * 
     * @memberof MPickupView2Engine
     */
    constructor() {
        super();
    }

    /**
     * 初始化引擎
     *
     * @param {*} options
     * @memberof MPickupView2Engine
     */
    public init(options: any): void {
        this.pickupViewPanel = options.pickupViewPanel;
        this.treeExpBar = options.treeExpBar;
        if (options.view.viewdata) {
            const isStr: boolean = typeof options.view.viewdata == 'string';
            let viewdata: any = isStr ? JSON.parse(options.view.viewdata) : options.view.viewdata;
            if (viewdata['selectedData']) {
                options.view.viewSelections = [...viewdata['selectedData']];
                delete viewdata['selectedData'];
            }
            options.view.viewdata = isStr ? JSON.stringify(viewdata) : viewdata;
        }
        super.init(options);
    }


    /**
     * 引擎加载
     *
     * @memberof MPickupView2Engine
     */
    public load(): void {
        super.load();
        if (this.getTreeExpBar()) {
            const tag = this.getTreeExpBar().name;
            this.setViewState2({ tag: tag, action: 'load', viewdata: this.view.viewparams });
        }
    }

    /**
     * 引擎事件
     *
     * @param {string} ctrlName
     * @param {string} eventName
     * @param {*} args
     * @memberof MPickupView2Engine
     */
    public onCtrlEvent(ctrlName: string, eventName: string, args: any): void {
        super.onCtrlEvent(ctrlName, eventName, args);
        const treeExpBar = this.getTreeExpBar();
        if (treeExpBar && Object.is(ctrlName, treeExpBar.name)) {
            this.handleTreeExpBarEvents(eventName, args);
        }
        const panel = this.getPickupViewPanel();
        if (panel && Object.is(ctrlName, panel.name)) {
            this.handlePickupViewPanelEvents(ctrlName, eventName, args);
        }

    }

    /**
     * 处理树导航栏事件
     *
     * @protected
     * @param {string} eventName
     * @param {*} args
     * @memberof MPickupView2Engine
     */
    protected handleTreeExpBarEvents(eventName: string, args: any) {
        if (eventName === 'selectionchange') {
            this.view.navItem = args;
            this.view.$forceUpdate();
            this.view.$nextTick(() => {
                if (this.getPickupViewPanel()) {
                    this.setViewState2({ tag: this.getPickupViewPanel().name, action: 'load', viewdata: args?.srfnavdata?.viewparams });
                }
            })
        }
        if (Object.is(eventName, 'activated')) {
            this.emitViewEvent('viewdatasactivated', args);
        }
    }

    /**
     * 处理选择视图面板事件
     *
     * @protected
     * @param {string} ctrlName
     * @param {*} eventName
     * @param {*} args
     * @memberof MPickupView2Engine
     */
    protected handlePickupViewPanelEvents(ctrlName: string, eventName: any, args: any) {
        if (Object.is(eventName, 'selectionchange')) {
            this.onSelectionChange(ctrlName, args);
        }
        if (Object.is(eventName, 'load')) {
            this.onLoad(ctrlName, args);
        }
        if (Object.is(eventName, 'activated')) {
            this.onSelectionChange(ctrlName, args);
            this.view.onCLickRight();
        }
    }

    /**
     * 值选中变化
     *
     * @param {string} ctrlName 选择视图面板名称
     * @param {any[]} args 选中数据
     * @memberof MPickupView2Engine
     */
    public onSelectionChange(ctrlName: any, args: any[]): void {
        if (this.view) {
            this.view.ctrlModel[ctrlName].selections = [...Util.deepCopy(args)];
            this.computeButtonStatus();
            if (!this.view.isShowButton) {
                this.emitViewEvent('viewdataschange', [...args]);
            }
        }
    }

    /**
     * 视图加载完成
     *
     * @param {string} ctrlName 选择视图面板名称
     * @param {any[]} args 选中数据
     * @memberof MPickupView2Engine
     */
    public onLoad(ctrlName: string, args: any[]): void {
        //  多次加载整合数据(适配树)
        const deDuplicate = (items: any[]) => {
            const back: any[] = [];
            const datas = this.view.ctrlModel[ctrlName].datas || [];
            if (datas?.length > 0) {
                datas.forEach((data: any) => {
                    back.push(data.srfkey);
                })
            }
            const filterArray = items.filter((item: any) => {
                if (item.srfkey && back.indexOf(item.srfkey) === -1) {
                    back.push(item.srfkey);
                    return true;
                } else {
                    return false;
                }
            })
            return filterArray.concat(datas);
        }
        const pickupViewPanel = this.getPickupViewPanel();
        //  特殊识别选择树视图
        if (pickupViewPanel?.embedViewType === 'DEPICKUPTREEVIEW') {
            this.view.ctrlModel[ctrlName].datas = [...Util.deepCopy(deDuplicate(args))];
        } else {
            this.view.ctrlModel[ctrlName].datas = [...Util.deepCopy(args)];
        }
        this.computeButtonStatus();
    }

    /**
     * 计算按钮状态
     *
     * @private
     * @memberof MPickupView2Engine
     */
    private computeButtonStatus() {
        const panel = this.getPickupViewPanel();
        const viewButtonModel = this.view.viewButtonModel;
        //  有选中数据时,全部到右侧按钮 (右侧有数据时启用,否则禁用)
        if (this.view.viewSelections && this.view.viewSelections.length) {
            viewButtonModel['view_allleftbtn'].disabled = false;
            //  到左侧按钮(右侧有选中数据时启用,否则禁用)
            if (this.view.viewSelections.some((selection: any) => selection._select)) {
                viewButtonModel['view_leftbtn'].disabled = false;
            } else {
                viewButtonModel['view_leftbtn'].disabled = true;
            }
        } else {
            viewButtonModel['view_allleftbtn'].disabled = true;
            //  到左侧按钮
            viewButtonModel['view_leftbtn'].disabled = true;
        }
        const ctrlModel = this.view.ctrlModel;
        if (panel && ctrlModel[panel.name]) {
            //  全部到右侧按钮(左侧选择面板有数据时启用,无数据禁用)
            if (ctrlModel[panel.name].datas && ctrlModel[panel.name].datas.length) {
                viewButtonModel['view_allrightbtn'].disabled = false;
            } else {
                viewButtonModel['view_allrightbtn'].disabled = true;
            }
            //  到右侧按钮 (面板存在数据或右侧有数据时启用)
            if ((ctrlModel[panel.name].selections && ctrlModel[panel.name].selections.length) || (this.view.viewSelections && this.view.viewSelections.length)) {
                viewButtonModel['view_rightbtn'].disabled = false;
            } else {
                viewButtonModel['view_rightbtn'].disabled = true;
            }
        }
    }

    /**
     * 到右侧
     *
     * @memberof MPickupView2Engine
     */
    public toRight() {
        const panel: any = Object.values(this.view.ctrlModel).find((model: any) => Object.is(model.type, 'PICKUPVIEWPANEL'));
        if (!panel) {
            return;
        }
        const newSelections: any[] = [];
        const backSelections: any[] = this.view.viewSelections || [];
        panel.selections?.forEach((item: any) => {
            const index = this.view.viewSelections.findIndex((select: any) => Object.is(item.srfkey, select.srfkey));
            if (index === -1) {
                let _item = Util.deepCopy(item);
                //  添加右侧区域选中标识
                Object.assign(_item, { _select: false });
                newSelections.push(_item);
            } else {
                newSelections.push(this.view.viewSelections[index]);
            }
        });
        this.view.viewSelections = this.duplicates([...newSelections, ...backSelections]);
        this.computeButtonStatus();
    }

    /**
     * 全部到右侧
     *
     * @memberof MPickupView2Engine
     */
    public toAllRight() {
        const panel: any = Object.values(this.view.ctrlModel).find((model: any) => Object.is(model.type, 'PICKUPVIEWPANEL'));
        if (!panel || !panel.datas || panel.datas.length === 0) {
            return;
        }
        let flag: boolean = false;
        panel.datas.forEach((data: any) => {
            const index = this.view.viewSelections.findIndex((select: any) => Object.is(data.srfkey, select.srfkey));
            if (index > 0) {
                flag = true;
            }
        })
        if (flag && panel.datas.length > 0) {
            return;
        }
        if (this.getPickupViewPanel()) {
            this.getPickupViewPanel().selectAll(panel.datas);
        }
        panel.datas.forEach((data: any) => {
            if (!data.srfmajortext) {
                Object.assign(data, { srfmajortext: data[this.majorPSDEField as string] });
            }
            const index = this.view.viewSelections.findIndex((selection: any) => Object.is(data.srfkey, selection.srfkey));
            if (index === -1) {
                let _item = Util.deepCopy(data);
                //  添加右侧区域选中标识
                Object.assign(_item, { _select: false });
                this.view.viewSelections.push(_item);
            }
        });
        this.view.selectedData = JSON.stringify(this.view.viewSelections);
        panel.selections = [...this.view.viewSelections];
        this.computeButtonStatus();
    }

    /**
     * 到左侧
     *
     * @memberof MPickupView2Engine
     */
    public toLeft() {
        const selections = Util.deepCopy(this.view.viewSelections);
        selections.forEach((item: any) => {
            if (!item._select) {
                return;
            }
            const index = this.view.viewSelections.findIndex((selection: any) => Object.is(item.srfkey, selection.srfkey));
            if (index !== -1) {
                this.view.viewSelections.splice(index, 1);
            }
        });
        this.view.selectedData = JSON.stringify(this.view.viewSelections);
        this.computeButtonStatus();
    }

    /**
     * 全部到左侧
     *
     * @memberof MPickupView2Engine
     */
    public toAllLeft() {
        this.view.viewSelections = [];
        this.view.selectedData = JSON.stringify(this.view.viewSelections);
        this.computeButtonStatus();
    }

    /**
    * 去重
    *
    * @param {any[]} data
    * @return {*}  {any[]}
    * @memberof MPickupView2Engine
    */
    public duplicates(data: any[]): any[] {
        const uniqueSet = new Set(data);
        return [...uniqueSet];
    }

    /**
     * 获取选择视图面板
     *
     * @returns {*}
     * @memberof MPickupView2Engine
     */
    public getPickupViewPanel(): any {
        return this.pickupViewPanel;
    }

    /**
     * 获取树导航
     *
     * @returns {*}
     * @memberof MPickupView2Engine
     */
    public getTreeExpBar(): any {
        return this.treeExpBar;
    }
362 363 364 365 366 367 368 369 370 371

    /**
     * @description 视图销毁
     * @memberof MPickupView2Engine
     */
    public destroyed() {
        super.destroyed();
        this.pickupViewPanel = null;
        this.treeExpBar = null;
    }
372
}