CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / forms / env-parameters-form / env-parameters-form.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 'use strict';
22 import {ValidationUtils} from "app/utils";
23 import {ArtifactModel, HeatParameterModel, Component} from "app/models";
24
25 export interface IEnvParametersFormViewModelScope extends ng.IScope {
26     isLoading:boolean;
27     type:string;
28     heatParameters:Array<HeatParameterModel>;
29     forms:any;
30     artifactResource:ArtifactModel;
31     buttons:Array<any>;
32     envParametersModal:ng.ui.bootstrap.IModalServiceInstance;
33     tableHeadersList:Array<any>;
34     selectedParameter:HeatParameterModel;
35     templatePopover:string;
36
37     getValidationPattern(type:string):RegExp;
38     isInstance():boolean;
39     validateJson(json:string):boolean;
40     onValueChanged(parameter: HeatParameterModel):void;
41     close():void;
42     save():void;
43     openDescPopover(selectedParam:HeatParameterModel):void;
44     closeDescriptionPopover():void;
45 }
46
47 export class EnvParametersFormViewModel {
48
49     static '$inject' = [
50         '$scope',
51         '$templateCache',
52         '$state',
53         '$uibModalInstance',
54         'artifact',
55         'ValidationUtils',
56         'component'
57     ];
58
59     constructor(private $scope:IEnvParametersFormViewModelScope,
60                 private $templateCache:ng.ITemplateCacheService,
61                 private $state:any,
62                 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
63                 private artifact:ArtifactModel,
64                 private ValidationUtils:ValidationUtils,
65                 private component:Component) {
66
67
68         this.initScope();
69     }
70
71     private updateInstanceHeat = ():void => {
72         let success = (responseArtifact:ArtifactModel):void => {
73             this.$scope.isLoading = false;
74             this.$uibModalInstance.close();
75         };
76
77         let error = ():void => {
78             this.$scope.isLoading = false;
79             console.info('Failed to load save artifact');
80         };
81
82         this.component.addOrUpdateInstanceArtifact(this.$scope.artifactResource).then(success, error);
83     };
84
85     private initScope = ():void => {
86         this.$scope.forms = {};
87         this.$scope.envParametersModal = this.$uibModalInstance;
88         this.$scope.artifactResource = this.artifact;
89         this.$scope.heatParameters = angular.copy(this.artifact.heatParameters);
90         //if param does not have a value - display the default
91         this.$scope.heatParameters.forEach((heatParam) => {
92             heatParam.currentValue = heatParam.currentValue || heatParam.defaultValue;
93         });
94         this.$scope.tableHeadersList = [
95             {title: "Parameter", property: "name"},
96             {title: "Default Value", property: "defaultValue", info: "DEFAULT_VALUE_INFO"},
97             {title: "Current Value", property: "currentValue", info: "CURRENT_VALUE_INFO"}
98         ];
99
100         this.$templateCache.put("env-parametr-description-popover.html", require('app/view-models/forms/env-parameters-form/env-parametr-description-popover.html'));
101         this.$scope.templatePopover = "env-parametr-description-popover.html";
102
103         this.$scope.getValidationPattern = (validationType:string, parameterType?:string):RegExp => {
104             return this.ValidationUtils.getValidationPattern(validationType, parameterType);
105         };
106
107         this.$scope.validateJson = (json:string):boolean => {
108             if (!json) {
109                 return true;
110             }
111             return this.ValidationUtils.validateJson(json);
112         };
113
114         this.$scope.isInstance = ():boolean => {
115             return !!this.component.selectedInstance;
116         };
117
118         this.$scope.save = ():void => {
119             this.$scope.buttons[0].disabled = true;//prevent double click (DE246266)
120             this.$scope.isLoading = true;
121             this.artifact.heatParameters = angular.copy(this.$scope.heatParameters);
122             this.artifact.heatParameters.forEach((parameter:any):void => {
123                 if ("" === parameter.currentValue) {
124                     //[Bug 154465] - Update and erase current value field in Env parameters form return empty String ("") instead of null.
125                     parameter.currentValue = null;
126                 } else if (parameter.defaultValue && parameter.defaultValue == parameter.currentValue) {
127                     parameter.currentValue = undefined;
128                 }
129             });
130
131             if (this.$scope.isInstance()) {
132                 this.updateInstanceHeat();
133                 return;
134             }
135
136             let success = (responseArtifact:ArtifactModel):void => {
137                 this.$scope.isLoading = false;
138                 this.$uibModalInstance.close();
139
140             };
141
142             let error = ():void => {
143                 this.$scope.isLoading = false;
144                 console.info('Failed to load save artifact');
145             };
146
147             this.component.addOrUpdateArtifact(this.$scope.artifactResource).then(success, error);
148         };
149
150         this.$scope.onValueChanged = (parameter: HeatParameterModel):void => {
151             parameter.filterTerm = parameter.name + ' ' + parameter.currentValue + ' ' + parameter.defaultValue + ' ' +parameter.description
152             if('json'==parameter.type){
153                 this.$scope.forms.editForm[parameter.name].$setValidity('pattern', this.$scope.validateJson(parameter.currentValue));
154             }
155         }
156
157         this.$scope.close = ():void => {
158             //this.artifact.heatParameters.forEach((parameter:any):void => {
159             //    if (!parameter.currentValue && parameter.defaultValue) {
160             //        parameter.currentValue = parameter.defaultValue;
161             //    }
162             //});
163             this.$uibModalInstance.dismiss();
164         };
165
166         this.$scope.openDescPopover = (selectedParam:HeatParameterModel):void => {
167             this.$scope.selectedParameter = selectedParam;
168         };
169
170         this.$scope.closeDescriptionPopover = ():void => {
171             this.$scope.selectedParameter = null;
172         };
173
174         this.$scope.buttons = [
175             {'name': 'Save', 'css': 'blue', 'callback': this.$scope.save},
176             {'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
177         ];
178
179         this.$scope.$watch("forms.editForm.$invalid", (newVal, oldVal) => {
180             this.$scope.buttons[0].disabled = this.$scope.forms.editForm.$invalid;
181         });
182
183     };
184 }