Skip to content
security.component.ts 5.22 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import { Component, OnInit } from '@angular/core';
import {TreeNodeInterface} from "../basic/basic.component";
import {
    FormBuilder,
    FormGroup,
    Validators
} from '@angular/forms';

@Component({
  selector: 'jhi-security',
  templateUrl: './security.component.html',
  styles: []
})
export class SecurityComponent implements OnInit {

    isBasicEdit = false;

    constructor(private fb: FormBuilder) { }

    data = [
        {
            key  : 1,
            key1 : '名称',
            key2 : '状态',
            key3 : '类型',
            key4 : '监控点',
            key5 : 'CPU利用率',
            children: [
                {
                    key    : 11,
                    key1 : '名称',
                    key2 : '状态',
                    key3 : '类型',
                    key4 : '监控点',
                    key5 : 'CPU利用率',
                },
                {
                    key     : 12,
                    key1 : '名称',
                    key2 : '状态',
                    key3 : '类型',
                    key4 : '监控点',
                    key5 : 'CPU利用率',
                    children: [ {
                        key    : 121,
                        key1 : '名称',
                        key2 : '状态',
                        key3 : '类型',
                        key4 : '监控点',
                        key5 : 'CPU利用率',
                    } ]
                },
                {
                    key     : 13,
                    key1 : '名称',
                    key2 : '状态',
                    key3 : '类型',
                    key4 : '监控点',
                    key5 : 'CPU利用率',
                    children: [
                        {
                            key     : 131,
                            key1 : '名称',
                            key2 : '状态',
                            key3 : '类型',
                            key4 : '监控点',
                            key5 : 'CPU利用率',
                            children: [
                                {
                                    key    : 1311,
                                    key1 : '名称',
                                    key2 : '状态',
                                    key3 : '类型',
                                    key4 : '监控点',
                                    key5 : 'CPU利用率',
                                },
                                {
                                    key    : 1312,
                                    key1 : '名称',
                                    key2 : '状态',
                                    key3 : '类型',
                                    key4 : '监控点',
                                    key5 : 'CPU利用率',
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            key    : 2,
            key1 : '名称',
            key2 : '状态',
            key3 : '类型',
            key4 : '监控点',
            key5 : 'CPU利用率',
        }
    ];
    expandDataCache = {};

    collapse(array: TreeNodeInterface[], data: TreeNodeInterface, $event: boolean): void {
        if ($event === false) {
            if (data.children) {
                data.children.forEach(d => {
                    const target = array.find(a => a.host === d.host);
                    target.expand = false;
                    this.collapse(array, target, false);
                });
            } else {
                return;
            }
        }
    }

    convertTreeToList(root: object): TreeNodeInterface[] {
        const stack = [];
        const array = [];
        const hashMap = {};
        stack.push({ ...root, level: 0, expand: false });

        while (stack.length !== 0) {
            const node = stack.pop();
            this.visitNode(node, hashMap, array);
            if (node.children) {
                for (let i = node.children.length - 1; i >= 0; i--) {
                    stack.push({ ...node.children[ i ], level: node.level + 1, expand: false, parent: node });
                }
            }
        }

        return array;
    }

    visitNode(node: TreeNodeInterface, hashMap: object, array: TreeNodeInterface[]): void {
        if (!hashMap[ node.host ]) {
            hashMap[ node.host ] = true;
            array.push(node);
        }
    }

    validateForm: FormGroup;

    submitForm(): void {
        for (const i in this.validateForm.controls) {
            this.validateForm.controls[ i ].markAsDirty();
            this.validateForm.controls[ i ].updateValueAndValidity();
        }
    }

    ngOnInit(): void {
        this.data.forEach(item => {
            this.expandDataCache[ item.key ] = this.convertTreeToList(item);
        });
        this.validateForm = this.fb.group({
            userName: [ null, [ Validators.required ] ],
            password: [ null, [ Validators.required ] ],
            remember: [ true ]
        });
    }

    showBasicEditModal(){
        this.isBasicEdit = true;
    }

    handleOk(): void {
        this.isBasicEdit = false;
    }

    handleCancel(): void {
        this.isBasicEdit = false;
    }
}