Add metadata to topology inputs
[sdc.git] / catalog-ui / src / app / ng2 / components / logic / inputs-table / inputs-table.component.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 Huawei Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 /**
23  * Created by rc2122 on 5/4/2017.
24  */
25 import { Component, Input, Output, EventEmitter, ViewChildren, QueryList } from "@angular/core";
26 import { InputFEModel } from "app/models";
27 import { ModalService } from "../../../services/modal.service";
28 import { InstanceFeDetails } from "app/models/instance-fe-details";
29 import { InstanceFePropertiesMap } from "../../../../models/properties-inputs/property-fe-map";
30 import { DataTypeService } from "../../../services/data-type.service";
31 import { MetadataEntry } from "app/models/metadataEntry";
32 import { DynamicElementComponent } from "../../ui/dynamic-element/dynamic-element.component";
33
34 @Component({
35     selector: 'inputs-table',
36     templateUrl: './inputs-table.component.html',
37     styleUrls: ['../inputs-table/inputs-table.component.less'],
38 })
39 export class InputsTableComponent {
40
41     @Input() inputs: Array<InputFEModel>;
42     @Input() instanceNamesMap: Map<string, InstanceFeDetails>;
43     @Input() readonly: boolean;
44     @Input() isLoading: boolean;
45     @Input() componentType: string;
46     
47     @Output() inputChanged: EventEmitter<any> = new EventEmitter<any>();
48     @Output() deleteInput: EventEmitter<any> = new EventEmitter<any>();
49
50     @Input() fePropertiesMap: InstanceFePropertiesMap;
51
52     @ViewChildren('metadataViewChildren') public metadataViewChildren: QueryList<DynamicElementComponent>;
53     
54     sortBy: String;
55     reverse: boolean;
56     selectedInputToDelete: InputFEModel;
57
58     sort = (sortBy) => {
59         this.reverse = (this.sortBy === sortBy) ? !this.reverse : true;
60         let reverse = this.reverse ? 1 : -1;
61         this.sortBy = sortBy;
62         let instanceNameMapTemp = this.instanceNamesMap;
63         let itemIdx1Val = "";
64         let itemIdx2Val = "";
65         this.inputs.sort(function (itemIdx1, itemIdx2) {
66             if (sortBy == 'instanceUniqueId') {
67                 itemIdx1Val = (itemIdx1[sortBy] && instanceNameMapTemp[itemIdx1[sortBy]] !== undefined) ? instanceNameMapTemp[itemIdx1[sortBy]].name : "";
68                 itemIdx2Val = (itemIdx2[sortBy] && instanceNameMapTemp[itemIdx2[sortBy]] !== undefined) ? instanceNameMapTemp[itemIdx2[sortBy]].name : "";
69             }
70             else {
71                 itemIdx1Val = itemIdx1[sortBy];
72                 itemIdx2Val = itemIdx2[sortBy];
73             }            
74             if (itemIdx1Val < itemIdx2Val) {
75                 return -1 * reverse;
76             }
77             else if (itemIdx1Val > itemIdx2Val) {
78                 return 1 * reverse;
79             }
80             else {
81                 return 0;
82             }
83         });
84     };
85
86
87     constructor(private modalService: ModalService, private dataTypeService: DataTypeService){
88         var x = 5
89     }
90
91
92     onInputChanged = (input, event) => {
93         input.updateDefaultValueObj(event.value, event.isValid);
94         this.inputChanged.emit(input);
95     };
96
97     onRequiredChanged = (input: InputFEModel, event) => {
98         this.inputChanged.emit(input);
99     }
100
101     onMetadataKeyChanged = (input: InputFEModel, event, metadataEntry: MetadataEntry) => {
102         let dynamicElementComponent = this.metadataViewChildren.filter(element => element.name == input.name + "_" + metadataEntry.key).pop();
103
104         input.updateMetadataKey(metadataEntry, event.value);
105         this.inputChanged.emit(input);
106
107         var mapKeyError = input.metadataMapKeyError;
108         if(input.metadataMapKeyError){
109             dynamicElementComponent.cmpRef.instance.control.setErrors({mapKeyError});
110         } 
111     };
112
113     onMetadataValueChanged = (input: InputFEModel, event, metadataEntry: MetadataEntry) => {
114         input.updateMetadataValue(metadataEntry, event.value);
115         this.inputChanged.emit(input);
116     };
117     
118     
119     createNewMetadataEntry = (input: InputFEModel): void => {
120         let metadataEntry = new MetadataEntry("", "");
121         input.addMetadataEntry(metadataEntry);
122         this.inputChanged.emit(input);
123     }
124
125     deleteMetadataEntry = (input: InputFEModel, metadataEntry: MetadataEntry) => {
126         input.deleteMetadataEntry(metadataEntry);
127         this.inputChanged.emit(input);
128     }
129     
130     onDeleteInput = () => {
131         this.deleteInput.emit(this.selectedInputToDelete);
132         this.modalService.closeCurrentModal();
133     };
134
135     openDeleteModal = (input: InputFEModel) => {
136         console.log('exist inputs: ' + this.inputs)
137         this.selectedInputToDelete = input;
138         this.modalService.createActionModal("Delete Input", "Are you sure you want to delete this input?", "Delete", this.onDeleteInput, "Close").instance.open();
139     }
140
141     getConstraints(input:InputFEModel): string[]{
142         
143         if (input.inputPath){
144             const pathValuesName = input.inputPath.split('#');
145             const rootPropertyName = pathValuesName[0];
146             const propertyName = pathValuesName[1];
147             let filterredRootPropertyType = _.values(this.fePropertiesMap)[0].filter(property => 
148                 property.name == rootPropertyName);
149             if (filterredRootPropertyType.length > 0){
150                 let rootPropertyType = filterredRootPropertyType[0].type;
151                 return this.dataTypeService.getConstraintsByParentTypeAndUniqueID(rootPropertyType, propertyName);
152             }else{
153                 return null;
154             }
155                 
156         }
157         // else if(input.constraints.length > 0){
158         //     return input.constraints[0].validValues
159         // }
160         else{
161             return null;
162         }
163     }
164
165     checkInstanceFePropertiesMapIsFilled(){
166         return _.keys(this.fePropertiesMap).length > 0
167     }
168
169     hasInputMetadata(){
170         for(let input of this.inputs){
171             if (input.metadataEntries.length > 0){
172                 return true;
173             }
174         }
175         return false;
176     }
177
178 }
179
180