re base code
[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                 this.target = this.convertValuesToDropDownOptions(data);
36                 if (this.link.toNode) {
37                     let targetCPOptions = this.findOptions(data, this.link.toNode);
38                     if (!targetCPOptions) { return; }
39                     this.targetCP = this.convertValuesToDropDownOptions(targetCPOptions);
40                 }
41             }
42         }
43     }
44
45     private findOptions(items: Array<ServicePathMapItem>, nodeOrCPId: string) {
46         let item = _.find(items, (dataItem) => nodeOrCPId === dataItem.id);
47         if (item && item.data && item.data.options) {
48             return item.data.options;
49         }
50         console.warn('no option was found to match selection of Node/CP with id:' + nodeOrCPId);
51         return null;
52     }
53
54     private convertValuesToDropDownOptions(values: Array<ServicePathMapItem>): Array<DropdownValue> {
55         let result:Array<DropdownValue> = [];
56         for (let i = 0; i < values.length ; i++) {
57             result[result.length] =  new DropdownValue(values[i].id, values[i].data.name);
58         }
59         return result.sort((a, b) => a.label.localeCompare(b.label));
60     }
61
62     onSourceSelected(id) {
63         if (id) {
64             let srcCPOptions = this.findOptions(this.data, id);
65             this.srcCP = this.convertValuesToDropDownOptions(srcCPOptions);
66             this.link.fromCP = '';
67             this.link.toNode = '';
68             this.link.toCP = '';
69             this.target = [];
70             this.targetCP = [];
71         }
72     }
73
74     onSrcCPSelected (id) {
75         if (id) {
76             let srcCPOptions = this.findOptions(this.data, this.link.fromNode);
77             let srcCPData = srcCPOptions.find(option => id === option.id).data;
78             this.target = this.convertValuesToDropDownOptions(this.data);
79             this.link.fromCPOriginId = srcCPData.ownerId;
80             this.link.toNode = '';
81             this.link.toCP = '';
82             this.targetCP = [];
83         }
84
85     }
86
87     onTargetSelected(id) {
88         if (id) {
89             let targetCPOptions = this.findOptions(this.data, id);
90             this.targetCP = this.convertValuesToDropDownOptions(targetCPOptions);
91             this.link.toCP = '';
92         }
93
94     }
95
96     onTargetCPSelected(id) {
97         if (id) {
98             let targetCPOptions = this.findOptions(this.data, this.link.toNode);
99             let targetCPDataObj = targetCPOptions.find(option => id === option.id).data;
100             this.link.toCPOriginId = targetCPDataObj.ownerId;
101         }
102     }
103 }