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 {
23 import Resource = Sdc.Models.Components.Resource;
24 import ArtifactModel = Sdc.Models.ArtifactModel;
26 interface IDeploymentArtifactsViewModelScope extends IWorkspaceViewModelScope {
27 tableHeadersList: Array<any>;
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;
44 viewModeOrCsarComponent():boolean;
45 isLicenseArtifact(artifact:Models.ArtifactModel): boolean;
49 export class DeploymentArtifactsViewModel {
61 constructor(private $scope:IDeploymentArtifactsViewModelScope,
62 private $filter:ng.IFilterService,
63 private $modal:ng.ui.bootstrap.IModalService,
64 private $templateCache:ng.ITemplateCacheService,
65 private validationUtils:Sdc.Utils.ValidationUtils,
66 private artifactsUtils:Sdc.Utils.ArtifactsUtils,
67 private ModalsHandler:Utils.ModalsHandler) {
69 this.$scope.updateSelectedMenuItem();
72 private initDescriptions = ():void => {
73 this.$scope.artifactDescriptions = {};
74 _.forEach(this.$scope.component.deploymentArtifacts, (artifact:Models.ArtifactModel):void => {
75 this.$scope.artifactDescriptions[artifact.artifactLabel] = artifact.description;
80 private setArtifact = (artifact:Models.ArtifactModel):void => {
81 if (artifact.heatParameters) {
82 artifact.heatParameters.forEach((parameter:any):void => {
83 if (!parameter.currentValue && parameter.defaultValue) {
84 parameter.currentValue = parameter.defaultValue;
85 } else if ("" === parameter.currentValue) {
86 parameter.currentValue = null;
90 if (!artifact.description || !this.$scope.getValidationPattern('string').test(artifact.description)) {
91 artifact.description = this.$scope.artifactDescriptions[artifact.artifactLabel];
95 private updateAll = ():void => {
96 let artifacts:Array<Models.ArtifactModel> = [];
97 _.forEach(this.$scope.component.deploymentArtifacts, (artifact:Models.ArtifactModel):void => {
98 if (artifact.selected) {
99 this.setArtifact(artifact);
100 artifacts.push(artifact);
103 this.$scope.component.updateMultipleArtifacts(artifacts);
107 private initScope = ():void => {
109 this.$scope.isLoading = false;
110 this.$scope.updateInProgress = false;
111 this.initDescriptions();
112 this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
113 this.$scope.setValidState(true);
115 this.$scope.tableHeadersList = [
116 {title: 'Name', property: 'artifactDisplayName'},
117 {title: 'Type', property: 'artifactType'},
118 {title: 'Deployment timeout', property: 'timeout'}
121 this.$scope.isLicenseArtifact = (artifact:Models.ArtifactModel) :boolean => {
122 let isLicense:boolean = false;
123 if(this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
125 isLicense = this.artifactsUtils.isLicenseType(artifact.artifactType);
131 this.$scope.sort = (sortBy:string):void => {
132 this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
133 this.$scope.sortBy = sortBy;
136 this.$scope.getValidationPattern = (validationType:string, parameterType?:string):RegExp => {
137 return this.validationUtils.getValidationPattern(validationType, parameterType);
140 this.$scope.validateJson = (json:string):boolean => {
144 return this.validationUtils.validateJson(json);
147 this.$scope.viewModeOrCsarComponent = ():boolean => {
148 return this.$scope.isViewMode() || (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent());
152 this.$scope.addOrUpdate = (artifact:Models.ArtifactModel):void => {
153 artifact.artifactGroupType = 'DEPLOYMENT';
154 let artifactCopy = new Models.ArtifactModel(artifact);
156 let success = (response:any):void => {
157 self.$scope.artifactDescriptions[artifactCopy.artifactLabel] = artifactCopy.description;
158 self.$scope.artifacts = <ArtifactModel[]>_.values(self.$scope.component.deploymentArtifacts);
161 let error = (err:any):void =>{
163 self.$scope.artifacts = <ArtifactModel[]>_.values(self.$scope.component.deploymentArtifacts);
167 this.ModalsHandler.openWizardArtifactModal(artifactCopy, self.$scope.component).then(success, error);
170 this.$scope.noArtifactsToShow = ():boolean => {
171 return !_.some(this.$scope.artifacts, 'esId');
175 this.$scope.resetValue = (parameter:any):void => {
176 if (!parameter.currentValue && parameter.defaultValue) {
177 parameter.currentValue = parameter.defaultValue;
179 else if ('boolean' == parameter.type) {
180 parameter.currentValue = parameter.currentValue.toUpperCase();
185 this.$scope.$watch('editForm.$valid', ():void => {
186 if (this.$scope.editForm) {
187 // this.$scope.setValidState(this.$scope.editForm.$valid);
191 this.$scope.update = (artifact:Models.ArtifactModel):void => {
192 if (false == this.$scope.isLoading) {
193 if (artifact.selected && !this.$scope.isViewMode()) {
194 this.$scope.isLoading = true;
195 this.$scope.updateInProgress = true;
196 let onSuccess = (responseArtifact:Models.ArtifactModel):void => {
197 this.$scope.artifactDescriptions[responseArtifact.artifactLabel] = responseArtifact.description;
198 this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
199 this.$scope.isLoading = false;
200 artifact.selected = !artifact.selected;
201 this.$scope.updateInProgress = false;
204 let onFailed = (error:any):void => {
205 console.log('Delete artifact returned error:', error);
206 this.$scope.isLoading = false;
207 artifact.selected = !artifact.selected;
208 this.$scope.updateInProgress = false;
211 this.setArtifact(artifact);
212 this.$scope.component.addOrUpdateArtifact(artifact).then(onSuccess, onFailed);
214 artifact.selected = !artifact.selected;
220 this.$scope.delete = (artifact:Models.ArtifactModel):void => {
221 let onOk = ():void => {
222 this.$scope.isLoading = true;
223 let onSuccess = ():void => {
224 this.$scope.isLoading = false;
225 this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
228 let onFailed = (error:any):void => {
229 this.$scope.isLoading = false;
230 console.log('Delete artifact returned error:', error);
233 this.$scope.component.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(onSuccess, onFailed);
236 let title:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
237 let message:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
238 this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
242 public save = (callback:Function):void => {
244 this.$scope.setComponent(this.$scope.component);
248 public back = (callback:Function):void => {
249 this.$scope.setComponent(this.$scope.component);