Skip to content
node.component.ts 3.88 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component, Input, OnInit} from '@angular/core';
wangqinghua's avatar
wangqinghua committed
import {TopologyService} from '../../topology.service';
import {LocalStorageService} from 'ngx-webstorage';
import {NzFormatEmitEvent, NzMessageService, NzTreeNode} from 'ng-zorro-antd';

declare let editor: any;
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
@Component({
    selector: 'smart-node',
    templateUrl: './node.component.html',
    styles: []
})
export class NodeComponent implements OnInit {
wangqinghua's avatar
wangqinghua committed
    @Input() dataSet;
wangqinghua's avatar
wangqinghua committed

    isNode = false;
    name;
wangqinghua's avatar
wangqinghua committed
    topoId;
wangqinghua's avatar
wangqinghua committed
    hostId;
wangqinghua's avatar
wangqinghua committed

    nodes;
    nodeList;
    selectList = [];

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

wangqinghua's avatar
wangqinghua committed
    ngOnInit() {
    }
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
    showModal(nodeObj) {
wangqinghua's avatar
wangqinghua committed
        console.log(nodeObj);
wangqinghua's avatar
wangqinghua committed
        this.hostId = nodeObj.hostIds;  //主机id;
        this.name = nodeObj.name;  //主机名称;
wangqinghua's avatar
wangqinghua committed
        this.topoId = nodeObj.topoId;  //关联拓扑图id;
wangqinghua's avatar
wangqinghua committed
        this.isNode = true;
        this.findTree();
    }

    //查询树--- 分组
    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);
                }
            }
        );
    }

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

    //获取下级
    mouseAction(name: string, event: NzFormatEmitEvent) {
        if (event.node.children.length > 0) {
            return false;
        }
        let type;
        if (event.node.level == 0) {
            type = 'host';   //主机
        }
wangqinghua's avatar
wangqinghua committed
        if (event.node.level == 1) {
wangqinghua's avatar
wangqinghua committed
            return false;
        }
        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;
                        res.isLeaf = true;
                    });
                    event.node.addChildren(dataSet);
                } else {
                    event.node.addChildren([]);
                    this.message.warning('该下级为空');
                }

            }
        );
    }

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

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

    //节点
    handleNodeCancel() {
        this.isNode = false;
        this.selectList = [];
    }

    handleNodeOk() {
wangqinghua's avatar
wangqinghua committed
        if (this.name) {
            const item = {
                name: this.name,
wangqinghua's avatar
wangqinghua committed
                topoId: this.topoId
wangqinghua's avatar
wangqinghua committed
            };
            this.isNode = false;
            editor.utils.setNode(item);
wangqinghua's avatar
wangqinghua committed
        }
wangqinghua's avatar
wangqinghua committed
        this.isNode = false;
wangqinghua's avatar
wangqinghua committed
        this.topoId = null;
wangqinghua's avatar
wangqinghua committed
        // const data = {
        //     hostIds:this.selectList
        // };
        // this.topologySer.findByHostIdOrWeb(data).subscribe(
        //     (res)=>{
        //         this.selectList = [];
        //         const item = {
        //             name:this.name,
        //             img:res.data[0].url
        //         };
        //         this.isNode = false;
        //         editor.utils.setNode(item);
        //         localStorage.setItem("node",'false');
        //     }
        // )