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=========================================================
20 /// <reference path="../../../references"/>
21 module Sdc.ViewModels.Wizard {
23 import ArtifactModel = Sdc.Models.ArtifactModel;
25 interface IArtifactDeploymentStepViewModelScope extends IWizardCreationStepScope {
26 tableHeadersList: Array<any>;
29 component: Models.Components.Component;
30 artifacts: Array<Models.ArtifactModel>;
31 editForm:ng.IFormController;
33 artifactDescriptions:any;
34 updateInProgress:boolean;
36 addOrUpdate(artifact:Models.ArtifactModel): void;
37 update(artifact:Models.ArtifactModel): void;
38 delete(artifact:Models.ArtifactModel): void;
39 sort(sortBy:string): void;
40 noArtifactsToShow():boolean;
41 getValidationPattern(validationType:string, parameterType?:string):RegExp;
42 validateJson(json:string):boolean;
43 resetValue(parameter:any):void;
46 export class ArtifactDeploymentStepViewModel implements IWizardCreationStep {
58 private $scope:IArtifactDeploymentStepViewModelScope,
59 private $filter:ng.IFilterService,
60 private $modal:ng.ui.bootstrap.IModalService,
61 private $templateCache:ng.ITemplateCacheService,
62 private validationUtils: Sdc.Utils.ValidationUtils,
63 private ModalsHandler: Utils.ModalsHandler
65 this.$scope.registerChild(this);
66 this.$scope.setValidState(true);
70 private initDescriptions = ():void =>{
71 this.$scope.artifactDescriptions = {};
72 _.forEach(this.$scope.component.deploymentArtifacts,(artifact:Models.ArtifactModel):void => {
73 this.$scope.artifactDescriptions[artifact.artifactLabel] = artifact.description;
78 private setArtifact = (artifact:Models.ArtifactModel):void =>{
79 if(artifact.heatParameters) {
80 artifact.heatParameters.forEach((parameter:any):void => {
81 if (!parameter.currentValue && parameter.defaultValue) {
82 parameter.currentValue = parameter.defaultValue;
83 } else if ("" === parameter.currentValue) {
84 parameter.currentValue = null;
88 if(!artifact.description || !this.$scope.getValidationPattern('string').test(artifact.description)){
89 artifact.description = this.$scope.artifactDescriptions[artifact.artifactLabel];
93 private updateAll = ():void =>{
94 let artifacts:Array<Models.ArtifactModel>= [];
95 _.forEach(this.$scope.component.deploymentArtifacts,(artifact:Models.ArtifactModel): void => {
96 if(artifact.selected) {
97 this.setArtifact(artifact);
98 artifacts.push(artifact);
101 this.$scope.component.updateMultipleArtifacts(artifacts);
106 private initScope = (): void => {
108 this.$scope.isLoading = false;
109 this.$scope.updateInProgress = false;
110 this.$scope.component = this.$scope.getComponent();
111 this.initDescriptions();
112 this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
115 this.$scope.tableHeadersList = [
116 {title:'Name', property: 'artifactDisplayName'},
117 {title:'Type', property: 'artifactType'},
118 {title:'Deployment timeout', property: 'timeout'}
121 this.$scope.sort = (sortBy:string):void => {
122 this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
123 this.$scope.sortBy = sortBy;
126 this.$scope.getValidationPattern = (validationType:string, parameterType?:string):RegExp => {
127 return this.validationUtils.getValidationPattern(validationType, parameterType);
130 this.$scope.validateJson = (json:string):boolean => {
134 return this.validationUtils.validateJson(json);
138 this.$scope.addOrUpdate = (artifact:Models.ArtifactModel): void => {
139 artifact.artifactGroupType = 'DEPLOYMENT';
140 let artifactCopy = new Models.ArtifactModel(artifact);
141 this.ModalsHandler.openWizardArtifactModal(artifactCopy, this.$scope.component).then(() => {
142 this.$scope.artifactDescriptions[artifactCopy.artifactLabel]= artifactCopy.description;
143 this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
147 this.$scope.noArtifactsToShow = ():boolean =>{
148 return !_.some(this.$scope.artifacts, 'esId');
151 this.$scope.resetValue = (parameter:any):void => {
152 if(!parameter.currentValue && parameter.defaultValue){
153 parameter.currentValue = parameter.defaultValue;
155 else if('boolean'==parameter.type){
156 parameter.currentValue = parameter.currentValue.toUpperCase();
161 this.$scope.$watch('editForm.$valid', ():void => {
162 if(this.$scope.editForm) {
163 this.$scope.setValidState(this.$scope.editForm.$valid);
167 this.$scope.update = (artifact:Models.ArtifactModel):void =>{
168 if(false == this.$scope.isLoading) {
169 if(artifact.selected) {
170 this.$scope.isLoading = true;
171 this.$scope.updateInProgress = true;
172 let onSuccess = (responseArtifact:Models.ArtifactModel):void => {
173 this.$scope.artifactDescriptions[responseArtifact.artifactLabel] = responseArtifact.description;
174 this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
175 this.$scope.isLoading = false;
176 this.$scope.updateInProgress = false;
177 artifact.selected = !artifact.selected;
180 let onFailed = (error:any):void => {
181 console.log('Delete artifact returned error:', error);
182 this.$scope.isLoading = false;
183 this.$scope.updateInProgress = false;
184 artifact.selected = !artifact.selected;
187 this.setArtifact(artifact);
188 this.$scope.component.addOrUpdateArtifact(artifact).then(onSuccess, onFailed);
190 artifact.selected = !artifact.selected;
195 this.$scope.delete = (artifact:Models.ArtifactModel):void => {
196 let onOk = ():void => {
197 this.$scope.isLoading = true;
198 let onSuccess = ():void => {
199 this.$scope.isLoading = false;
200 this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
203 let onFailed = (error:any):void => {
204 this.$scope.isLoading = false;
205 console.log('Delete artifact returned error:', error);
208 this.$scope.component.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(onSuccess, onFailed);
211 let title:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
212 let message:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
213 this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
217 public save = (callback:Function):void => {
219 this.$scope.setComponent(this.$scope.component);
223 public back = (callback:Function):void => {
224 this.$scope.setComponent(this.$scope.component);