Skip to content
select-group.component.ts 3.81 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {NzFormatEmitEvent, NzMessageService, NzTreeNode} from 'ng-zorro-antd';
import {TopologyService} from '../../netTopology/topology.service';

@Component({
    selector: 'smart-select-group',
    templateUrl: './select-group.component.html',
    styles: []
})
export class SelectGroupComponent implements OnInit {

    @Output() done = new EventEmitter<any>();

    isValiaible;
    title;

    nodes;
    nodeList;
    selectList = [];

    constructor(private topologySer: TopologyService,
                private message: NzMessageService) {
    }

    ngOnInit() {
        this.initForm();
    }

    showAddModal(title) {
        this.title = title;
        this.isValiaible = true;
        this.initForm();
        this.findTree();
    }

    initForm() {
        this.nodes = null;
        this.nodeList = [];
        this.selectList = [];
    }

    //查询树--- 分组
    findTree() {
        const data = {
            id: '',
            type: 'group'
        };
        this.topologySer.findTree(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    let option = res.data;
                    option.forEach(res => {
                        res.title = res.name;
                        res.key = res.id;
                    });
                    this.nodeList = option;
                    this.toNode(option);
                } else {
                    this.message.info(res.errMsg);
                }
            }
        );
    }

    toNode(data) {
        this.nodes = data.map(res => {
            return new NzTreeNode(res);
        });
    }

    //获取下级
    mouseAction(name: string, event: NzFormatEmitEvent) {
wangqinghua's avatar
wangqinghua committed
        if (event.node.children.length > 0) {
wangqinghua's avatar
wangqinghua committed
            return false;
        }
wangqinghua's avatar
wangqinghua committed
        let type;
        if (event.node.level == 0) {
            type = 'host';   //主机
        }
        if (event.node.level == 1) {
            type = 'item';  //监控项
        }
        if (this.title == '选择资源' && event.node.level == 1) {
            return false;
        }
wangqinghua's avatar
wangqinghua committed

        if (this.title == '添加设备' && event.node.level == 1) {
            return false;
        }

wangqinghua's avatar
wangqinghua committed
        const index = <any>event.node.key - 1;
        const data = {
            'id': event.node.origin.id,
            'type': type
        };
        this.topologySer.findTree(data).subscribe(
            (res) => {
                if (res.data) {
                    const dataSet = res.data;
                    dataSet.forEach(res => {
                        res.title = res.name;
                        res.key = res.id;
wangqinghua's avatar
wangqinghua committed
                        res.isLeaf = true;
                        if (res.inventoryExtends > 0 && this.title == '关联资产') {
wangqinghua's avatar
wangqinghua committed
                            res.disabled = true;
wangqinghua's avatar
wangqinghua committed
                        } else {
wangqinghua's avatar
wangqinghua committed
                            res.disabled = false;
                        }
wangqinghua's avatar
wangqinghua committed
                    });
                    event.node.addChildren(dataSet);
                } else {
                    event.node.addChildren([]);
                    this.message.warning('该下级为空');
                }

            }
        );
    }

    //选择树节点
wangqinghua's avatar
wangqinghua committed
    selectItem(event, node) {
wangqinghua's avatar
wangqinghua committed
        if (node.isChecked) {
            this.selectList.push(node.origin.id);   //主机

        } else {
            const index = this.selectList.indexOf(node.origin.id);
            this.selectList.splice(index, 1);
        }
    }

    handEditleOk() {
wangqinghua's avatar
wangqinghua committed
        this.select();
wangqinghua's avatar
wangqinghua committed
    }

    select() {
wangqinghua's avatar
wangqinghua committed
        if (this.selectList.length == 0) {
            this.message.warning('请选择资源');
wangqinghua's avatar
wangqinghua committed
            return false;
        }
wangqinghua's avatar
wangqinghua committed
        this.done.emit(this.selectList);
        this.isValiaible = false;
    }

    handleEditCancel() {
        this.initForm();
        this.isValiaible = false;
    }

}