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