Catalog alignment
[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 } 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
32 @Component({
33     selector: 'inputs-table',
34     templateUrl: './inputs-table.component.html',
35     styleUrls: ['../inputs-table/inputs-table.component.less'],
36 })
37 export class InputsTableComponent {
38
39     @Input() inputs: Array<InputFEModel>;
40     @Input() instanceNamesMap: Map<string, InstanceFeDetails>;
41     @Input() readonly: boolean;
42     @Input() isLoading: boolean;
43     @Output() inputChanged: EventEmitter<any> = new EventEmitter<any>();
44     @Output() deleteInput: EventEmitter<any> = new EventEmitter<any>();
45
46     @Input() fePropertiesMap: InstanceFePropertiesMap;
47     
48     sortBy: String;
49     reverse: boolean;
50     selectedInputToDelete: InputFEModel;    
51
52     sort = (sortBy) => {
53         this.reverse = (this.sortBy === sortBy) ? !this.reverse : true;
54         let reverse = this.reverse ? 1 : -1;
55         this.sortBy = sortBy;
56         let instanceNameMapTemp = this.instanceNamesMap;
57         let itemIdx1Val = "";
58         let itemIdx2Val = "";
59         this.inputs.sort(function (itemIdx1, itemIdx2) {
60             if (sortBy == 'instanceUniqueId') {
61                 itemIdx1Val = (itemIdx1[sortBy] && instanceNameMapTemp[itemIdx1[sortBy]] !== undefined) ? instanceNameMapTemp[itemIdx1[sortBy]].name : "";
62                 itemIdx2Val = (itemIdx2[sortBy] && instanceNameMapTemp[itemIdx2[sortBy]] !== undefined) ? instanceNameMapTemp[itemIdx2[sortBy]].name : "";
63             }
64             else {
65                 itemIdx1Val = itemIdx1[sortBy];
66                 itemIdx2Val = itemIdx2[sortBy];
67             }            
68             if (itemIdx1Val < itemIdx2Val) {
69                 return -1 * reverse;
70             }
71             else if (itemIdx1Val > itemIdx2Val) {
72                 return 1 * reverse;
73             }
74             else {
75                 return 0;
76             }
77         });
78     };
79
80
81     constructor(private modalService: ModalService, private dataTypeService: DataTypeService){
82         var x = 5
83     }
84
85
86     onInputChanged = (input, event) => {
87         input.updateDefaultValueObj(event.value, event.isValid);
88         this.inputChanged.emit(input);
89     };
90
91     onDeleteInput = () => {
92         this.deleteInput.emit(this.selectedInputToDelete);
93         this.modalService.closeCurrentModal();
94     };
95
96     openDeleteModal = (input: InputFEModel) => {
97         console.log('exist inputs: ' + this.inputs)
98         
99         
100         this.selectedInputToDelete = input;
101         this.modalService.createActionModal("Delete Input", "Are you sure you want to delete this input?", "Delete", this.onDeleteInput, "Close").instance.open();
102     }
103
104     getConstraints(input:InputFEModel): string[]{
105         
106         if (input.inputPath){
107             const pathValuesName = input.inputPath.split('#');
108             const rootPropertyName = pathValuesName[0];
109             const propertyName = pathValuesName[1];
110             let filterredRootPropertyType = _.values(this.fePropertiesMap)[0].filter(property => 
111                 property.name == rootPropertyName);
112             if (filterredRootPropertyType.length > 0){
113                 let rootPropertyType = filterredRootPropertyType[0].type;
114                 return this.dataTypeService.getConstraintsByParentTypeAndUniqueID(rootPropertyType, propertyName);
115             }else{
116                 return null;
117             }
118                 
119         }
120         // else if(input.constraints.length > 0){
121         //     return input.constraints[0].validValues
122         // }
123         else{
124             return null;
125         }
126     }
127
128     checkInstanceFePropertiesMapIsFilled(){
129         return _.keys(this.fePropertiesMap).length > 0
130     }
131 }
132
133