aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/composition/graph/service-path-creator/link-row/link-row.component.ts
blob: 83c30b1a60a6a11df14073ab57ab67e31a6747d3 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import {Component, Input} from '@angular/core';
import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";
import {Link} from './link.model';
import {ServicePathMapItem} from "app/models/graph/nodes-and-links-map";
import * as _ from "lodash";

@Component({
    selector: 'link-row',
    templateUrl: './link-row.component.html',
    styleUrls: ['./link-row.component.less']
})


export class LinkRowComponent {
    @Input() data:Array<ServicePathMapItem>;
    @Input() link:Link;
    @Input() removeRow:Function;
    source: Array<DropdownValue> = [];
    target: Array<DropdownValue> = [];
    srcCP: Array<DropdownValue> = [];
    targetCP: Array<DropdownValue> = [];

    ngOnChanges() {
        if (this.data) {
            this.parseInitialData(this.data);
        }
    }

    parseInitialData(data: Array<ServicePathMapItem>) {
        this.source = this.convertValuesToDropDownOptions(data);
        if (this.link.fromNode) {
            let srcCPOptions = this.findOptions(data, this.link.fromNode);
            if (!srcCPOptions) { return; }
            this.srcCP = this.convertValuesToDropDownOptions(srcCPOptions);
            if (this.link.fromCP) {
                this.target = this.convertValuesToDropDownOptions(data);
                if (this.link.toNode) {
                    let targetCPOptions = this.findOptions(data, this.link.toNode);
                    if (!targetCPOptions) { return; }
                    this.targetCP = this.convertValuesToDropDownOptions(targetCPOptions);
                }
            }
        }
    }

    private findOptions(items: Array<ServicePathMapItem>, nodeOrCPId: string) {
        let item = _.find(items, (dataItem) => nodeOrCPId === dataItem.id);
        if (item && item.data && item.data.options) {
            return item.data.options;
        }
        console.warn('no option was found to match selection of Node/CP with id:' + nodeOrCPId);
        return null;
    }

    private convertValuesToDropDownOptions(values: Array<ServicePathMapItem>): Array<DropdownValue> {
        let result:Array<DropdownValue> = [];
        for (let i = 0; i < values.length ; i++) {
            result[result.length] =  new DropdownValue(values[i].id, values[i].data.name);
        }
        return result.sort((a, b) => a.label.localeCompare(b.label));
    }

    onSourceSelected(id) {
        if (id) {
            let srcCPOptions = this.findOptions(this.data, id);
            this.srcCP = this.convertValuesToDropDownOptions(srcCPOptions);
            this.link.fromCP = '';
            this.link.toNode = '';
            this.link.toCP = '';
            this.target = [];
            this.targetCP = [];
        }
    }

    onSrcCPSelected (id) {
        if (id) {
            let srcCPOptions = this.findOptions(this.data, this.link.fromNode);
            let srcCPData = srcCPOptions.find(option => id === option.id).data;
            this.target = this.convertValuesToDropDownOptions(this.data);
            this.link.fromCPOriginId = srcCPData.ownerId;
            this.link.toNode = '';
            this.link.toCP = '';
            this.targetCP = [];
        }

    }

    onTargetSelected(id) {
        if (id) {
            let targetCPOptions = this.findOptions(this.data, id);
            this.targetCP = this.convertValuesToDropDownOptions(targetCPOptions);
            this.link.toCP = '';
        }

    }

    onTargetCPSelected(id) {
        if (id) {
            let targetCPOptions = this.findOptions(this.data, this.link.toNode);
            let targetCPDataObj = targetCPOptions.find(option => id === option.id).data;
            this.link.toCPOriginId = targetCPDataObj.ownerId;
        }
    }
}