CONTROL-BASE.vue.ftl 21.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
<template>
    <div class="design-tree-container">
        <context-menu-container>
            <el-tree
            v-if="inited"
            ref="${ctrl.name}"
            class="design-tree"
            node-key="id"
            lazy
            :show-checkbox="!isSingleSelect"
            :check-on-click-node="!isSingleSelect"
            :default-expanded-keys="expandedKeys"
            :props="{
                label: 'text',
                isLeaf: 'leaf',
                children: 'children'
            }"
            :load="load"
            :highlight-current="true"
            :expand-on-click-node="false"
            @check="onCheck"
            @current-change="selectionChange"
            :filter-node-method="filterNode"
            >
                <template slot-scope="{ node, data }">
                  <context-menu :contextMenuStyle="{width: '100%'}" :data="node" :renderContent="renderContextMenu">
                    <tooltip transfer style="width: 100%;" max-width="2000" placement="right">
                        <div class="tree-node" @dblclick="doDefaultAction(node)">
                            <span class="icon">
                                <i v-if=" data.iconcls && !Object.is(data.iconcls, '')" :class="data.iconcls"></i>
                                <img v-else-if="data.icon && !Object.is(data.icon, '')" :src="data.icon" />
                                <icon v-else-if="isOutputIconDefault" type="ios-paper-outline"></icon>&nbsp;
                            </span>
                            <span class="text">
                                <span v-if="data.html" v-html="data.html"></span>
                                <span v-else>{{ data.isUseLangRes ? $t(data.text) : data.text }}</span>
                            </span>
                        </div>
                        <template slot="content">
                            <span v-if="data.html" v-html="data.html"></span>
                            <span v-else>{{ data.isUseLangRes ? $t(data.text) : data.text }}</span>
                        </template>
                    </tooltip>
                 </context-menu>
                </template>
            </el-tree>
        </context-menu-container>
    </div>
</template>
<#ibizinclude>
../@MACRO/CONTROL/CONTROL_HEADER-BASE.vue.ftl
</#ibizinclude>

    /**
     * 获取多项数据
     *
     * @returns {any[]}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public getDatas(): any[] {
        return [this.currentselectedNode];
    }

    /**
     * 获取单项树
     *
     * @returns {*}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public getData(): any {
        return this.currentselectedNode;
    }

    /**
     * 是否单选
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
80
    @Prop({ default: true }) public isSingleSelect!: boolean;
81 82 83 84 85 86 87

    /**
     * 是否默认选中第一条数据
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
88
    @Prop({ default: false }) public isSelectFirstDefault!: boolean;
89 90 91 92 93 94 95

    /**
     * 枝干节点是否可用(具有数据能力,可抛出)
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
96
    @Prop({default:true}) public isBranchAvailable!: boolean;
97 98 99 100 101 102 103

    /**
     * 显示处理提示
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
104
    @Prop({ default: true }) public showBusyIndicator?: boolean;
105 106 107 108 109 110 111

    /**
     * 初始化完成
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
112
    public inited: boolean = false;
113 114 115 116 117 118 119

    /**
     * 已选中数据集合
     *
     * @type {*}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
120
    public selectedNodes: any = [];
121 122 123 124 125 126 127

    /**
     * 当前选中数据项
     *
     * @type {*}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
128
    public currentselectedNode: any = {};
129 130 131 132 133 134 135

    /**
     * 选中数据字符串
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
136
    @Prop() public selectedData?: string;
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

    /**
     * 选中值变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    @Watch('selectedData')
    public onValueChange(newVal: any, oldVal: any) {
        this.echoselectedNodes = newVal ? this.isSingleSelect ? JSON.parse(newVal)[0] : JSON.parse(newVal) : [];
        this.selectedNodes = [];
        if(this.echoselectedNodes.length > 0){
            let AllnodesObj = (this.$refs.treeexpbar_tree as any).store.nodesMap;
            let AllnodesArray : any[] = [];
            for (const key in AllnodesObj) {
              if (AllnodesObj.hasOwnProperty(key)) {
                AllnodesArray.push(AllnodesObj[key].data);
              }
            }
            this.setDefaultSelection(AllnodesArray);
        }
    }

    /**
     * 回显选中数据集合
     *
     * @type {*}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
167
    public echoselectedNodes:any[] = this.selectedData ? ( this.isSingleSelect ? [JSON.parse(this.selectedData)[0]] : JSON.parse(this.selectedData)) : [];
168 169 170 171 172 173 174

    /**
     * 部件行为--update
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
175
    @Prop() public updateAction!: string;
176 177 178 179 180 181 182

    /**
     * 部件行为--fetch
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
183
    @Prop() public fetchAction!: string;
184 185 186 187 188 189 190

    /**
     * 部件行为--remove
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
191
    @Prop() public removeAction!: string;
192 193 194 195 196 197 198

    /**
     * 部件行为--load
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
199
    @Prop() public loadAction!: string;
200 201 202 203 204 205 206

    /**
     * 部件行为--create
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
207
    @Prop() public createAction!: string;
208 209 210 211 212 213 214

    /**
     * 过滤属性
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
215
    public srfnodefilter: string = '';
216 217 218 219 220 221 222

    /**
     * 默认输出图标
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
223
    public isOutputIconDefault: boolean = ${ctrl.isOutputIconDefault()?c};
224 225 226 227 228 229 230 231

    <#--  /**
     * 树数据
     *
     * @type {any[]}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    @Provide()
232
    public nodes: any[] = [];  -->
233 234 235 236 237 238 239 240

    /**
     * 数据展开主键
     *
     * @type {string[]}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    @Provide()
241
    public expandedKeys: string[] = [];
242 243 244 245

    /**
     * 选中数据变更事件
     *
246
     * @public
247 248 249 250 251
     * @param {*} data
     * @param {*} data 当前节点对应传入对象
     * @param {*} checkedState 树目前选中状态对象
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
252
    public onCheck(data: any, checkedState: any) {
253 254 255 256 257 258 259 260 261 262 263
        // 处理多选数据
        if(!this.isSingleSelect){
            let leafNodes = checkedState.checkedNodes.filter((item:any) => item.leaf);
            this.selectedNodes = JSON.parse(JSON.stringify(leafNodes));
            this.$emit('selectionchange', this.selectedNodes);
        }
    }

    /**
     * 选中数据变更事件
     *
264
     * @public
265 266 267 268
     * @param {*} data 节点对应传入对象
     * @param {*} node 节点对应node对象
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
269
    public selectionChange(data: any, node: any) {
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        // 禁用项处理
        if (data.disabled){
            node.isCurrent = false;
            return;
        }
        // 只处理最底层子节点
        if(this.isBranchAvailable || data.leaf){
            this.currentselectedNode = JSON.parse(JSON.stringify(data));
            // 单选直接替换
            if(this.isSingleSelect){
                this.selectedNodes = [this.currentselectedNode];
                this.$emit('selectionchange', this.selectedNodes);
            }
            // 多选用check方法
        }
    }

    /**
     * Vue声明周期(处理组件的输入属性)
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
292
    public created() {
293 294 295 296 297 298 299 300
        this.afterCreated();
    }

    /**
     * 执行created后的逻辑
     *
     *  @memberof ${srfclassname('${ctrl.codeName}')}
     */    
301
    public afterCreated(){
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
        if (this.viewState) {
            this.viewStateEvent = this.viewState.subscribe(({ tag, action, data }) => {
                if (!Object.is(tag, this.name)) {
                    return;
                }
                if (Object.is('load', action)) {
                    this.inited = false;
                    this.$nextTick(() => {
                        this.inited = true;
                    });
                }
                if (Object.is('filter', action)) {
                    this.srfnodefilter  = data.srfnodefilter;
                    this.refresh_all();
                }
                if (Object.is('refresh_parent', action)) {
                    this.refresh_parent();
                }
            });
        }
    }

    /**
     * 对树节点进行筛选操作
     * @memberof OrderTree
     */
328
    public filterNode(value:any, data:any) {
329 330 331 332 333 334 335 336 337 338
        if (!value) return true;
        return data.text.indexOf(value) !== -1;
    }


    /**
     * vue 生命周期
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
339
    public destroyed() {
340 341 342 343 344 345 346 347
        this.afterDestroy();
    }

    /**
     * 执行destroyed后的逻辑
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
348
    public afterDestroy() {
349 350 351 352 353 354 355 356 357 358 359 360 361
        if (this.viewStateEvent) {
            this.viewStateEvent.unsubscribe();
        }
        <#if destroyed_block??>
        ${destroyed_block}
        </#if>
    }

    /**
     * 刷新数据
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
362
    public refresh_all(): void {
363 364 365 366 367 368 369 370 371 372 373
        this.inited = false;
        this.$nextTick(() => {
            this.inited = true;
        });
    }

    /**
     * 刷新父节点
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
374
    public refresh_parent(): void {
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
        if (Object.keys(this.currentselectedNode).length === 0) {
            return;
        }
        const tree: any = this.$refs.${ctrl.name};
        const node: any = tree.getNode(this.currentselectedNode.id);
        if (!node || !node.parent) {
            return;
        }
        let curNode:any = {}; 
        const { parent: _parent } = node;
        curNode = Util.deepObjectMerge(curNode,_parent);
        let tempContext:any = {};
        if(curNode.data && curNode.data.srfappctx){
            Object.assign(tempContext,curNode.data.srfappctx);
        }else{
            Object.assign(tempContext,this.context);
        }
        const id: string = _parent.key ? _parent.key : '#';
        const param: any = { srfnodeid: id };
        this.refresh_node(tempContext,param, true);
    }

    /**
     * 数据加载
     *
     * @param {*} node
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
403
    public load(node: any = {}, resolve?: any) {
404 405 406 407 408 409 410 411
        if (node.data && node.data.children) {
            resolve(node.data.children);
            return;
        }
        const params: any = {
            srfnodeid: node.data && node.data.id ? node.data.id : "#",
            srfnodefilter: this.srfnodefilter
        };
412
        let tempViewParams:any = JSON.parse(JSON.stringify(this.viewparams));
413 414 415
        let curNode:any = {}; 
        curNode = Util.deepObjectMerge(curNode,node);
        let tempContext:any = this.computecurNodeContext(curNode);
416 417
        if(curNode.data && curNode.data.srfparentdename){
            Object.assign(tempContext,{srfparentdename:curNode.data.srfparentdename});
418
            Object.assign(tempViewParams,{srfparentdename:curNode.data.srfparentdename});
419 420 421
        }
        if(curNode.data && curNode.data.srfparentkey){
            Object.assign(tempContext,{srfparentkey:curNode.data.srfparentkey});
422
            Object.assign(tempViewParams,{srfparentkey:curNode.data.srfparentkey});
423
        }
424
        Object.assign(params,{viewparams:tempViewParams});
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        this.service.getNodes(tempContext,params).then((response: any) => {
            if (!response || response.status !== 200) {
                this.$Notice.error({ title: "错误", desc: response.info });
                resolve([]);
                return;
            }
            const _items = response.data;
            this.formatExpanded(_items);
            <#--  if (!node.level || node.level === 0) {
                this.nodes = [..._items];
            }
            this.nodes = response.data;  -->
            resolve([..._items]);
            let isRoot = Object.is(node.level,0);
            let isSelectedAll = node.checked;
            this.setDefaultSelection(_items, isRoot, isSelectedAll);
            this.$emit("load", _items);
        }).catch((response: any) => {
            resolve([]);
            if (response && response.status === 401) {
                return;
            }
            this.$Notice.error({ title: "错误", desc: response.info });
        });
    }

    /**
     * 计算当前节点的上下文
     *
     * @param {*} curNode 当前节点
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
457
    public computecurNodeContext(curNode:any){
458 459 460 461 462
        let tempContext:any = {};
        if(curNode && curNode.data && curNode.data.srfappctx){
            tempContext = JSON.parse(JSON.stringify(curNode.data.srfappctx));
        }else{
            tempContext = JSON.parse(JSON.stringify(this.context));
463 464 465 466 467 468 469 470 471 472
        }
        return tempContext;
    }

    /**
     * 刷新功能
     *
     * @param {any[]} args
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
473
    public refresh(args: any[]): void {
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
        if (Object.keys(this.currentselectedNode).length === 0) {
            return;
        }
        const id: string = this.currentselectedNode.id;
        const param: any = { srfnodeid: id };
        const tree: any = this.$refs.${ctrl.name};
        const node: any = tree.getNode(id);
        if (!node) {
            return;
        }
        let curNode:any = {}; 
        curNode = Util.deepObjectMerge(curNode,node);
        let tempContext:any;
        if(curNode.data && curNode.data.srfappctx){
            Object.assign(tempContext,curNode.data.srfappctx);
        }else{
            Object.assign(tempContext,this.context);
        }
        this.refresh_node(tempContext,param, false);
    }

    /**
     * 刷新节点
     *
498
     * @public
499 500 501 502 503
     * @param {*} [curContext] 当前节点上下文
     * @param {*} [arg={}] 当前节点附加参数
     * @param {boolean} parentnode 是否是刷新父节点
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
504
    public refresh_node(curContext:any,arg: any = {}, parentnode: boolean): void {
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
        const { srfnodeid: id } = arg;
        Object.assign(arg,{viewparams:this.viewparams});
        const get: Promise<any> = this.service.getNodes(JSON.parse(JSON.stringify(this.context)),arg);
        get.then((response: any) => {
            if (!response || response.status !== 200) {
                this.$Notice.error({ title: '错误', desc: response.info });
                return;
            }
            const _items = [...response.data];
            this.formatExpanded(_items);
            const tree: any = this.$refs.${ctrl.name};
            tree.updateKeyChildren(id, _items);
            if (parentnode) {
                this.currentselectedNode = {};
            }
            this.setDefaultSelection(_items);
        }).catch((response: any) => {
            if (response && response.status === 401) {
                return;
            }
            this.$Notice.error({ title: '错误', desc: response.info });
        });
    }

    /**
     * 默认展开节点
     *
532
     * @public
533 534 535 536
     * @param {any[]} items
     * @returns {any[]}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
537
    public formatExpanded(items: any[]): any[] {
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
        const data: any[] = [];
        items.forEach((item) => {
            if (item.expanded || (item.children && item.children.length > 0)) {
                this.expandedKeys.push(item.id);
            }
        });
        return data;
    }

    /**
     * 设置默认选中,回显数项,选中所有子节点
     *
     * @param {any[]} items 当前节点所有子节点集合
     * @param {boolean} isRoot 是否是加载根节点
     * @param {boolean} isSelectedAll 是否选中所有子节点
     * @memberof MainTree
     */
555
    public setDefaultSelection(items: any[], isRoot: boolean = false, isSelectedAll: boolean = false): void {
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
        if(items.length == 0){
            return;
        }
        let defaultData: any;
        // 导航中选中第一条配置的默认选中,没有选中第一条
        if(this.isSelectFirstDefault){
            if(this.isSingleSelect){
                let index = items.findIndex((item: any) => item.selected);
                if(index === -1) {
                    if(isRoot){
                        index = 0;
                    }else{
                        return;
                    }
                }
                defaultData = items[index];
                this.setTreeNodeHighLight(defaultData);
                this.currentselectedNode = JSON.parse(JSON.stringify(defaultData));
                if(this.isBranchAvailable || defaultData.leaf){
                    this.selectedNodes = [this.currentselectedNode];
                    this.$emit('selectionchange', this.selectedNodes);
                } 
            }
        }
        // 已选数据的回显
        if(this.echoselectedNodes && this.echoselectedNodes.length > 0){
            let checkedNodes = items.filter((item:any)=>{
                return this.echoselectedNodes.some((val:any)=> {
                    if(Object.is(item.srfkey,val.srfkey) && Object.is(item.srfmajortext,val.srfmajortext)){
                        val.used = true;
                        return true;
                    }
                });
            });
            if(checkedNodes.length > 0){
                this.echoselectedNodes = this.echoselectedNodes.filter((item:any)=> !item.used);
                // 父节点选中时,不需要执行这段,会选中所有子节点
                if(!isSelectedAll){
                    if(this.isSingleSelect){
                        this.setTreeNodeHighLight(checkedNodes[0]);
                        this.currentselectedNode = JSON.parse(JSON.stringify(checkedNodes[0]));
                        this.selectedNodes = [this.currentselectedNode];
                    }else{
                        this.selectedNodes = this.selectedNodes.concat(checkedNodes);
                        const tree: any = this.$refs.treeexpbar_tree;
                        tree.setCheckedNodes(this.selectedNodes);
                    }
                }
            }
        }
        // 父节点选中时,选中所有子节点
        if(isSelectedAll){
            let leafNodes = items.filter((item:any) => item.leaf);
            this.selectedNodes = this.selectedNodes.concat(leafNodes);
            this.$emit('selectionchange', this.selectedNodes);
        }
    } 

    /**
     * 绘制右击菜单
     *
     * @param {*} node
     * @returns
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
621
    public renderContextMenu(node: any) {
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
        let content;
        if (node && node.data) {
            const data: any = JSON.parse(JSON.stringify(node.data));
            this.currentselectedNode = { ...data };
            const tags: string[] = data.id.split(';');
<#if ctrl.getPSControls()??>
<#list ctrl.getPSControls() as childCtrl>
<#if childCtrl.getControlType() == "CONTEXTMENU">
            if (tags[0] === "${childCtrl.getOwner().getNodeType()}") {
                content = this.renderContextMenu${srfclassname(childCtrl.getOwner().getNodeType()?lower_case)}();
            }
</#if>
</#list>
</#if>
        }
        return content;
    }
<#if ctrl.getPSControls()??>
<#list ctrl.getPSControls() as childCtrl>
<#if childCtrl.getControlType() == "CONTEXTMENU">

    /**
     * 绘制${childCtrl.getOwner().getNodeType()}类型右键菜单
     *
     * @param {*} node
     * @returns
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
650
    public renderContextMenu${srfclassname(childCtrl.getOwner().getNodeType()?lower_case)}() {
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
        return (
<@ibizindent blank=12>
${P.getCtrlCode(childCtrl, 'CONTROL.html').code}
</@ibizindent>
        );
    }
</#if>
</#list>
</#if>

    /**
     * 设置选中高亮
     *
     * @param {*} data
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
667
    public setTreeNodeHighLight(data: any): void {
668 669 670 671 672 673 674 675 676 677
        const tree: any = this.$refs.${ctrl.name};
        tree.setCurrentKey(data.id);
    }

    /**
     * 执行默认界面行为
     *
     * @param {*} node
     * @memberof AppView
     */
678
    public doDefaultAction(node: any) {
679 680 681 682 683 684 685 686 687 688 689 690 691 692
        if (node && node.data) {
            const data: any = node.data;
            const tags: string[] = data.id.split(';');
<#if ctrl.getPSDETreeNodes()??>
<#list ctrl.getPSDETreeNodes() as node>
<#if node.getPSDEContextMenu()?? && node.getDefaultPSUIAction()??>
<#assign contextMenu = node.getPSDEContextMenu()/>
            if (tags[0] === "${contextMenu.getOwner().getNodeType()}") {
                this.${contextMenu.name}_click({tag: '${node.getDefaultPSUIAction().name}'});
            }
</#if>
</#list>
</#if>
        }
693
        this.$emit('nodedblclick', this.selectedNodes);
694 695 696 697 698 699 700 701 702
    }

<#ibizinclude>
../@MACRO/CONTROL/CONTROL_BOTTOM-BASE.vue.ftl
</#ibizinclude>

<#ibizinclude>
../@MACRO/CONTROL/CONTROL-BASE.style.ftl
</#ibizinclude>