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

@Component({
wangqinghua's avatar
wangqinghua committed
    selector: 'smart-device',
    templateUrl: './device.component.html',
    styles: [
        ``
    ]
wangqinghua's avatar
wangqinghua committed
})
export class DeviceComponent implements OnInit {
wangqinghua's avatar
wangqinghua committed
    @Output() done = new EventEmitter<any>();
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
    isValiaible;
wangqinghua's avatar
wangqinghua committed
    title;
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
    nodes;
    nodeList;
    selectTreeList = {
        hostIds:<any>[],
        httpIds:<any>[]
    };

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

    ngOnInit() {
        this.initForm();
    }

wangqinghua's avatar
wangqinghua committed
    showAddModal(title) {
        this.title = title;
wangqinghua's avatar
wangqinghua committed
        this.isValiaible = true;
        this.initForm();
        this.findTree();
    }

    initForm(){
        this.nodes = null;
        this.nodeList = [];
        this.selectTreeList = {
            hostIds:<any>[],
            httpIds:<any>[]
        };
    }

wangqinghua's avatar
wangqinghua committed
    //查询树--- 分组
wangqinghua's avatar
wangqinghua committed
    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;
                        res.disabled = true;
                    });
                    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) {
        let type;
wangqinghua's avatar
wangqinghua committed
        setTimeout(_ => {
        }, 1000);
wangqinghua's avatar
wangqinghua committed
        if(event.node.level == 0){
            type = "host";   //主机
        }
        if(event.node.level == 1){
            type = "item";  //监控项
        }
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
                        if(this.title == "选择资源"){
                            res.isLeaf = true;
                        }
                        if(res.inventoryExtends == 1){
                            res.disabled = true;
                        }
wangqinghua's avatar
wangqinghua committed
                    });
                    event.node.addChildren(dataSet);
                    this.nodeList[index].children = dataSet;
                    this.nodeList[index].expanded = true;
                } else {
                    event.node.addChildren([]);
                    this.message.warning('该下级为空');
                }

            }
        );
    }

    //选择树节点
wangqinghua's avatar
wangqinghua committed
    /**
     * inventoryExtends 1:有资产  0:无资产
     * @param {NzFormatEmitEvent} event
     */
wangqinghua's avatar
wangqinghua committed
    selectCheckTree(event: NzFormatEmitEvent) {
        if (event.node.isChecked) {
            if(event.node.level == 1){
                this.selectTreeList.hostIds.push(event.node.origin.id);   //主机
            }
            if(event.node.level == 2){
                this.selectTreeList.httpIds.push(event.node.origin.id)  //监控项
            }
        } else {
            if(event.node.level == 1){
                const index = this.selectTreeList.hostIds.indexOf(event.node.origin.id);
                this.selectTreeList.hostIds.splice(index, 1);
            }
            if(event.node.level == 2){
                const index = this.selectTreeList.httpIds.indexOf(event.node.origin.id);
                this.selectTreeList.httpIds.splice(index, 1);
            }

        }
    }

    handEditleOk(){
wangqinghua's avatar
wangqinghua committed
        if(this.title == "添加设备"){
            this.topology();
        }
        if(this.title == "选择资源"){
            this.select();
        }

    }

    topology(){
wangqinghua's avatar
wangqinghua committed
        this.topologySer.findDefaultIcon(this.selectTreeList).subscribe(
            (res)=>{
wangqinghua's avatar
wangqinghua committed
                if(res.data.length > 0){
                    this.done.emit(res.data);
                    this.isValiaible = false;
                }else{
                    this.message.warning("该项无默认图标")
                }
wangqinghua's avatar
wangqinghua committed
            }
        )
    }

wangqinghua's avatar
wangqinghua committed
    select(){
        this.done.emit(this.selectTreeList);
        this.isValiaible = false;
    }

wangqinghua's avatar
wangqinghua committed
    handleEditCancel(){
        this.initForm();
        this.isValiaible = false;
    }
wangqinghua's avatar
wangqinghua committed

}