Sync Integ to Master
[sdc.git] / catalog-ui / src / app / ng2 / pages / service-path-creator / link-row / link-row.component.ts
1 import {Component, Input} from '@angular/core';
2 import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";
3 import {Link} from './link.model';
4 import {ServicePathMapItem} from "app/models/graph/nodes-and-links-map";
5
6 @Component({
7     selector: 'link-row',
8     templateUrl: './link-row.component.html',
9     styleUrls: ['./link-row.component.less']
10 })
11
12
13 export class LinkRowComponent {
14     @Input() data:Array<ServicePathMapItem>;
15     @Input() link:Link;
16     @Input() removeRow:Function;
17     source:Array<DropdownValue> = [];
18     target: Array<DropdownValue> = [];
19     srcCP: Array<DropdownValue> = [];
20     targetCP: Array<DropdownValue> = [];
21
22     ngOnChanges() {
23         if (this.data) {
24             this.parseInitialData(this.data);
25         }
26     }
27
28     parseInitialData(data: Array<ServicePathMapItem>) {
29         this.source = this.convertValuesToDropDownOptions(data);
30         if (this.link.fromNode) {
31             let srcCPOptions = this.findOptions(data, this.link.fromNode);
32             if (!srcCPOptions) { return; }
33             this.srcCP = this.convertValuesToDropDownOptions(srcCPOptions);
34             if (this.link.fromCP) {
35                 let targetOptions = this.findOptions(srcCPOptions, this.link.fromCP);
36                 if (!targetOptions) { return; }
37                 this.target = this.convertValuesToDropDownOptions(targetOptions);
38                 if (this.link.toNode) {
39                     let targetCPOptions = this.findOptions(targetOptions, this.link.toNode);
40                     if (!targetCPOptions) { return; }
41                     this.targetCP = this.convertValuesToDropDownOptions(targetCPOptions);
42                 }
43             }
44         }
45     }
46
47     private findOptions(items: Array<ServicePathMapItem>, nodeOrCPId: string) {
48         let item = items.find((dataItem)=> nodeOrCPId === dataItem.id);
49         if (item && item.data && item.data.options) {
50             return item.data.options;
51         }
52         console.warn('no option was found to match selection of Node/CP with id:' + nodeOrCPId);
53         return null;
54     }
55
56     private convertValuesToDropDownOptions(values: Array<ServicePathMapItem>) : Array<DropdownValue> {
57         let result = [];
58         for (let i = 0; i < values.length ; i++) {
59             result[result.length] =  new DropdownValue(values[i].id, values[i].data.name);
60         }
61         return result;
62     }
63
64     onSourceSelected(id) {
65         if (id) {
66             let srcCPOptions = this.findOptions(this.data, id);
67             this.srcCP = this.convertValuesToDropDownOptions(srcCPOptions);
68             this.link.fromCP = '';
69             this.link.toNode = '';
70             this.link.toCP = '';
71             this.target = [];
72             this.targetCP = [];
73         }
74     }
75
76     onSrcCPSelected (id) {
77         if (id) {
78             let srcCPData = this.data.find((dataItem)=> this.link.fromNode === dataItem.id).data;
79             let srcCPOptions = srcCPData.options;
80             let targetOptions = this.findOptions(srcCPOptions, id);
81             this.target = this.convertValuesToDropDownOptions(targetOptions);
82             this.link.fromCPOriginId = srcCPData.ownerId;
83             this.link.toNode = '';
84             this.link.toCP = '';
85             this.targetCP = [];
86         }
87
88     }
89
90     onTargetSelected(id) {
91         if (id) {
92             let srcCPOptions = this.findOptions(this.data, this.link.fromNode);
93             let targetOptions = this.findOptions(srcCPOptions, this.link.fromCP);
94             let targetCPOptions = this.findOptions(targetOptions, id);
95             this.targetCP = this.convertValuesToDropDownOptions(targetCPOptions);
96             this.link.toCP = '';
97         }
98
99     }
100
101     onTargetCPSelected(id) {
102         if (id) {
103             let srcCPOptions = this.findOptions(this.data, this.link.fromNode);
104             let targetOptions = this.findOptions(srcCPOptions, this.link.fromCP);
105             let targetCPOptions = this.findOptions(targetOptions, this.link.toNode);
106             let targetCPDataObj = targetCPOptions.find((dataItem)=> id === dataItem.id).data;
107             this.link.toCPOriginId = targetCPDataObj.ownerId;
108         }
109     }
110 }