2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 import {ValidationUtils} from "app/utils";
23 import {ArtifactModel, HeatParameterModel, Component} from "app/models";
25 export interface IEnvParametersFormViewModelScope extends ng.IScope {
28 heatParameters:Array<HeatParameterModel>;
30 artifactResource:ArtifactModel;
32 envParametersModal:ng.ui.bootstrap.IModalServiceInstance;
33 tableHeadersList:Array<any>;
34 selectedParameter:HeatParameterModel;
35 templatePopover:string;
37 getValidationPattern(type:string):RegExp;
39 validateJson(json:string):boolean;
40 onValueChanged(parameter: HeatParameterModel):void;
43 openDescPopover(selectedParam:HeatParameterModel):void;
44 closeDescriptionPopover():void;
47 export class EnvParametersFormViewModel {
59 constructor(private $scope:IEnvParametersFormViewModelScope,
60 private $templateCache:ng.ITemplateCacheService,
62 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
63 private artifact:ArtifactModel,
64 private ValidationUtils:ValidationUtils,
65 private component:Component) {
71 private updateInstanceHeat = ():void => {
72 let success = (responseArtifact:ArtifactModel):void => {
73 this.$scope.isLoading = false;
74 this.$uibModalInstance.close();
77 let error = ():void => {
78 this.$scope.isLoading = false;
79 console.info('Failed to load save artifact');
82 this.component.addOrUpdateInstanceArtifact(this.$scope.artifactResource).then(success, error);
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;
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"}
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";
103 this.$scope.getValidationPattern = (validationType:string, parameterType?:string):RegExp => {
104 return this.ValidationUtils.getValidationPattern(validationType, parameterType);
107 this.$scope.validateJson = (json:string):boolean => {
111 return this.ValidationUtils.validateJson(json);
114 this.$scope.isInstance = ():boolean => {
115 return !!this.component.selectedInstance;
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;
131 if (this.$scope.isInstance()) {
132 this.updateInstanceHeat();
136 let success = (responseArtifact:ArtifactModel):void => {
137 this.$scope.isLoading = false;
138 this.$uibModalInstance.close();
142 let error = ():void => {
143 this.$scope.isLoading = false;
144 console.info('Failed to load save artifact');
147 this.component.addOrUpdateArtifact(this.$scope.artifactResource).then(success, error);
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));
157 this.$scope.close = ():void => {
158 //this.artifact.heatParameters.forEach((parameter:any):void => {
159 // if (!parameter.currentValue && parameter.defaultValue) {
160 // parameter.currentValue = parameter.defaultValue;
163 this.$uibModalInstance.dismiss();
166 this.$scope.openDescPopover = (selectedParam:HeatParameterModel):void => {
167 this.$scope.selectedParameter = selectedParam;
170 this.$scope.closeDescriptionPopover = ():void => {
171 this.$scope.selectedParameter = null;
174 this.$scope.buttons = [
175 {'name': 'Save', 'css': 'blue', 'callback': this.$scope.save},
176 {'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
179 this.$scope.$watch("forms.editForm.$invalid", (newVal, oldVal) => {
180 this.$scope.buttons[0].disabled = this.$scope.forms.editForm.$invalid;