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";
9 templateUrl: './link-row.component.html',
10 styleUrls: ['./link-row.component.less']
14 export class LinkRowComponent {
15 @Input() data:Array<ServicePathMapItem>;
17 @Input() removeRow:Function;
18 source: Array<DropdownValue> = [];
19 target: Array<DropdownValue> = [];
20 srcCP: Array<DropdownValue> = [];
21 targetCP: Array<DropdownValue> = [];
25 this.parseInitialData(this.data);
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);
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;
51 console.warn('no option was found to match selection of Node/CP with id:' + nodeOrCPId);
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);
60 return result.sort((a, b) => a.label.localeCompare(b.label));
63 onSourceSelected(id) {
65 let srcCPOptions = this.findOptions(this.data, id);
66 this.srcCP = this.convertValuesToDropDownOptions(srcCPOptions);
67 this.link.fromCP = '';
68 this.link.toNode = '';
75 onSrcCPSelected (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 = '';
88 onTargetSelected(id) {
90 let targetCPOptions = this.findOptions(this.data, id);
91 this.targetCP = this.convertValuesToDropDownOptions(targetCPOptions);
97 onTargetCPSelected(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;