2816c312f99c1fab6e8e9526fce1956a13977a5e
[sdc.git] /
1 //@require "./*.html"
2 'use strict';
3 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
4 import {ArtifactModel, ArtifactGroupModel, Resource} from "app/models";
5 import {ArtifactsUtils, ModalsHandler, ValidationUtils} from "app/utils";
6 import {ComponentServiceNg2} from "app/ng2/services/component-services/component.service";
7 import {ComponentGenericResponse} from "../../../../ng2/services/responses/component-generic-response";
8
9 interface IDeploymentArtifactsViewModelScope extends IWorkspaceViewModelScope {
10     tableHeadersList:Array<any>;
11     reverse:boolean;
12     sortBy:string;
13     artifacts:Array<ArtifactModel>;
14     editForm:ng.IFormController;
15     isLoading:boolean;
16     artifactDescriptions:any;
17     selectedArtifactId:string;
18     popoverTemplate:string;
19
20     addOrUpdate(artifact:ArtifactModel):void;
21     updateSelectedArtifact():void;
22     delete(artifact:ArtifactModel):void;
23     sort(sortBy:string):void;
24     noArtifactsToShow():boolean;
25     getValidationPattern(validationType:string, parameterType?:string):RegExp;
26     validateJson(json:string):boolean;
27     resetValue(parameter:any):void;
28     viewModeOrCsarComponent():boolean;
29     isLicenseArtifact(artifact:ArtifactModel):void;
30     getEnvArtifact(heatArtifact:ArtifactModel):ArtifactModel;
31     getEnvArtifactName(artifact:ArtifactModel):string;
32     openEditEnvParametersModal(artifact:ArtifactModel):void;
33     openDescriptionPopover(artifactId:string):void;
34     closeDescriptionPopover():void;
35 }
36
37 export class DeploymentArtifactsViewModel {
38
39     static '$inject' = [
40         '$scope',
41         '$templateCache',
42         '$filter',
43         'ValidationUtils',
44         'ArtifactsUtils',
45         'ModalsHandler',
46         'ComponentServiceNg2'
47     ];
48
49     constructor(private $scope:IDeploymentArtifactsViewModelScope,
50                 private $templateCache:ng.ITemplateCacheService,
51                 private $filter:ng.IFilterService,
52                 private validationUtils:ValidationUtils,
53                 private artifactsUtils:ArtifactsUtils,
54                 private ModalsHandler:ModalsHandler,
55                 private ComponentServiceNg2: ComponentServiceNg2) {
56         this.initScope();
57         this.$scope.updateSelectedMenuItem();
58     }
59
60     private initDescriptions = ():void => {
61         this.$scope.artifactDescriptions = {};
62         _.forEach(this.$scope.component.deploymentArtifacts, (artifact:ArtifactModel):void => {
63             this.$scope.artifactDescriptions[artifact.artifactLabel] = artifact.description;
64         });
65     };
66
67     private setArtifact = (artifact:ArtifactModel):void => {
68         if (!artifact.description || !this.$scope.getValidationPattern('string').test(artifact.description)) {
69             artifact.description = this.$scope.artifactDescriptions[artifact.artifactLabel];
70         }
71     };
72
73     private initScopeArtifacts = ()=> {
74         this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
75         _.forEach(this.$scope.artifacts, (artifact:ArtifactModel):void => {
76             artifact.envArtifact = this.getEnvArtifact(artifact);
77         });
78     };
79
80     private initArtifacts = (loadFromServer:boolean):void => {
81         if (loadFromServer) {
82             this.$scope.isLoading = true;
83             this.ComponentServiceNg2.getComponentDeploymentArtifacts(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
84                 this.$scope.component.deploymentArtifacts = response.deploymentArtifacts;
85                 this.initScopeArtifacts();
86                 this.$scope.isLoading = false;
87             });
88         } else {
89             this.initScopeArtifacts();
90         }
91
92     };
93
94     private getEnvArtifact = (heatArtifact:ArtifactModel):ArtifactModel=> {
95         return _.find(this.$scope.artifacts, (item:ArtifactModel)=> {
96             return item.generatedFromId === heatArtifact.uniqueId;
97         });
98     };
99
100     private getCurrentArtifact = ():ArtifactModel => {
101         if (!this.$scope.selectedArtifactId) {
102             return null;
103         }
104         let artifact:ArtifactModel = this.$scope.artifacts.filter((art) => {
105             return art.uniqueId == this.$scope.selectedArtifactId;
106         })[0];
107         return artifact;
108     }
109
110     private initScope = ():void => {
111         let self = this;
112         this.$scope.isLoading = false;
113         this.$scope.selectedArtifactId = null;
114         this.initDescriptions();
115         if(this.$scope.component.deploymentArtifacts) {
116             this.initArtifacts(false);
117         } else {
118             this.initArtifacts(true);
119         }
120         this.$scope.setValidState(true);
121
122         this.$scope.tableHeadersList = [
123             {title: 'Name', property: 'artifactDisplayName'},
124             {title: 'Type', property: 'artifactType'},
125             {title: 'Deployment timeout', property: 'timeout'},
126             {title: 'Version', property: 'artifactVersion'},
127             {title: 'UUID', property: 'artifactUUID'}
128         ];
129
130         this.$templateCache.put("deployment-artifacts-description-popover.html", require('app/view-models/workspace/tabs/deployment-artifacts/deployment-artifacts-description-popover.html'));
131         this.$scope.popoverTemplate = "deployment-artifacts-description-popover.html";
132
133         this.$scope.isLicenseArtifact = (artifact:ArtifactModel):boolean => {
134             let isLicense:boolean = false;
135             if (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
136
137                 isLicense = this.artifactsUtils.isLicenseType(artifact.artifactType);
138             }
139
140             return isLicense;
141         };
142
143         this.$scope.sort = (sortBy:string):void => {
144             this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
145             this.$scope.sortBy = sortBy;
146         };
147
148         this.$scope.getValidationPattern = (validationType:string, parameterType?:string):RegExp => {
149             return this.validationUtils.getValidationPattern(validationType, parameterType);
150         };
151
152         this.$scope.validateJson = (json:string):boolean => {
153             if (!json) {
154                 return true;
155             }
156             return this.validationUtils.validateJson(json);
157         };
158
159         this.$scope.viewModeOrCsarComponent = ():boolean => {
160             return this.$scope.isViewMode() || (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent());
161         };
162
163         this.$scope.addOrUpdate = (artifact:ArtifactModel):void => {
164             artifact.artifactGroupType = 'DEPLOYMENT';
165             let artifactCopy = new ArtifactModel(artifact);
166
167             let success = (response:any):void => {
168                 this.$scope.artifactDescriptions[artifactCopy.artifactLabel] = artifactCopy.description;
169                 this.initArtifacts(true);
170                 //  this.$scope.artifacts = _.values(this.$scope.component.deploymentArtifacts);
171             };
172
173             let error = (err:any):void => {
174                 console.log(err);
175                 this.initArtifacts(true);
176                 //  self.$scope.artifacts = _.values(self.$scope.component.deploymentArtifacts);
177             };
178
179             this.ModalsHandler.openArtifactModal(artifactCopy, this.$scope.component).then(success, error);
180         };
181
182         this.$scope.noArtifactsToShow = ():boolean => {
183             return !_.some(this.$scope.artifacts, 'esId');
184         };
185
186         this.$scope.resetValue = (parameter:any):void => {
187             if (!parameter.currentValue && parameter.defaultValue) {
188                 parameter.currentValue = parameter.defaultValue;
189             }
190             else if ('boolean' == parameter.type) {
191                 parameter.currentValue = parameter.currentValue.toUpperCase();
192             }
193         };
194
195         this.$scope.$watch('editForm.$valid', ():void => {
196             if (this.$scope.editForm) {
197                 //    this.$scope.setValidState(this.$scope.editForm.$valid);
198             }
199         });
200
201         this.$scope.updateSelectedArtifact = ():void => {
202             if (!this.$scope.isViewMode() && !this.$scope.isLoading) {
203                 let artifact:ArtifactModel = this.getCurrentArtifact();
204                 this.setArtifact(artifact); //resets artifact description to original value if invalid.
205                 if (artifact && artifact.originalDescription != artifact.description) {
206                     this.$scope.isLoading = true;
207                     let onSuccess = (responseArtifact:ArtifactModel):void => {
208                         this.$scope.artifactDescriptions[responseArtifact.artifactLabel] = responseArtifact.description;
209                         // this.$scope.artifacts = _.values(this.$scope.component.deploymentArtifacts);
210                         this.initArtifacts(true);
211                         this.$scope.isLoading = false;
212                     };
213
214                     let onFailed = (error:any):void => {
215                         console.log('Delete artifact returned error:', error);
216                         this.$scope.isLoading = false;
217                     };
218
219                     this.$scope.component.addOrUpdateArtifact(artifact).then(onSuccess, onFailed);
220                 }
221             }
222         };
223
224         this.$scope.delete = (artifact:ArtifactModel):void => {
225             let onOk = ():void => {
226                 this.$scope.isLoading = true;
227                 let onSuccess = ():void => {
228                     this.$scope.isLoading = false;
229                     this.initArtifacts(true);
230                     //this.$scope.artifacts = _.values(this.$scope.component.deploymentArtifacts);
231                 };
232
233                 let onFailed = (error:any):void => {
234                     this.$scope.isLoading = false;
235                     console.log('Delete artifact returned error:', error);
236                 };
237
238                 this.$scope.component.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(onSuccess, onFailed);
239             };
240
241             let title:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
242             let message:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
243             this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
244         };
245
246         this.$scope.getEnvArtifactName = (artifact:ArtifactModel):string => {
247             let envArtifact = this.$scope.getEnvArtifact(artifact);
248             if (envArtifact) {
249                 return envArtifact.artifactDisplayName;
250             }
251         };
252
253         this.$scope.openEditEnvParametersModal = (artifact:ArtifactModel):void => {
254             this.ModalsHandler.openEditEnvParametersModal(artifact, this.$scope.component).then(()=> {
255                 this.initArtifacts(true);
256             }, ()=> {
257                 this.initArtifacts(true);
258             });
259         };
260
261         this.$scope.openDescriptionPopover = (artifactId:string):void => {
262             if (this.$scope.selectedArtifactId && this.$scope.selectedArtifactId != artifactId) {
263                 this.$scope.updateSelectedArtifact();
264             }
265             this.$scope.selectedArtifactId = artifactId;
266
267         };
268
269         this.$scope.closeDescriptionPopover = ():void => {
270             if (this.$scope.selectedArtifactId) {
271                 this.$scope.updateSelectedArtifact();
272                 this.$scope.selectedArtifactId = null;
273             }
274         };
275     };
276 }