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";
9 interface IDeploymentArtifactsViewModelScope extends IWorkspaceViewModelScope {
10 tableHeadersList:Array<any>;
13 artifacts:Array<ArtifactModel>;
14 editForm:ng.IFormController;
16 artifactDescriptions:any;
17 selectedArtifactId:string;
18 popoverTemplate:string;
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;
37 export class DeploymentArtifactsViewModel {
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) {
57 this.$scope.updateSelectedMenuItem();
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;
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];
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);
80 private initArtifacts = (loadFromServer:boolean):void => {
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;
89 this.initScopeArtifacts();
94 private getEnvArtifact = (heatArtifact:ArtifactModel):ArtifactModel=> {
95 return _.find(this.$scope.artifacts, (item:ArtifactModel)=> {
96 return item.generatedFromId === heatArtifact.uniqueId;
100 private getCurrentArtifact = ():ArtifactModel => {
101 if (!this.$scope.selectedArtifactId) {
104 let artifact:ArtifactModel = this.$scope.artifacts.filter((art) => {
105 return art.uniqueId == this.$scope.selectedArtifactId;
110 private initScope = ():void => {
112 this.$scope.isLoading = false;
113 this.$scope.selectedArtifactId = null;
114 this.initDescriptions();
115 if(this.$scope.component.deploymentArtifacts) {
116 this.initArtifacts(false);
118 this.initArtifacts(true);
120 this.$scope.setValidState(true);
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'}
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";
133 this.$scope.isLicenseArtifact = (artifact:ArtifactModel):boolean => {
134 let isLicense:boolean = false;
135 if (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
137 isLicense = this.artifactsUtils.isLicenseType(artifact.artifactType);
143 this.$scope.sort = (sortBy:string):void => {
144 this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
145 this.$scope.sortBy = sortBy;
148 this.$scope.getValidationPattern = (validationType:string, parameterType?:string):RegExp => {
149 return this.validationUtils.getValidationPattern(validationType, parameterType);
152 this.$scope.validateJson = (json:string):boolean => {
156 return this.validationUtils.validateJson(json);
159 this.$scope.viewModeOrCsarComponent = ():boolean => {
160 return this.$scope.isViewMode() || (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent());
163 this.$scope.addOrUpdate = (artifact:ArtifactModel):void => {
164 artifact.artifactGroupType = 'DEPLOYMENT';
165 let artifactCopy = new ArtifactModel(artifact);
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);
173 let error = (err:any):void => {
175 this.initArtifacts(true);
176 // self.$scope.artifacts = _.values(self.$scope.component.deploymentArtifacts);
179 this.ModalsHandler.openArtifactModal(artifactCopy, this.$scope.component).then(success, error);
182 this.$scope.noArtifactsToShow = ():boolean => {
183 return !_.some(this.$scope.artifacts, 'esId');
186 this.$scope.resetValue = (parameter:any):void => {
187 if (!parameter.currentValue && parameter.defaultValue) {
188 parameter.currentValue = parameter.defaultValue;
190 else if ('boolean' == parameter.type) {
191 parameter.currentValue = parameter.currentValue.toUpperCase();
195 this.$scope.$watch('editForm.$valid', ():void => {
196 if (this.$scope.editForm) {
197 // this.$scope.setValidState(this.$scope.editForm.$valid);
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;
214 let onFailed = (error:any):void => {
215 console.log('Delete artifact returned error:', error);
216 this.$scope.isLoading = false;
219 this.$scope.component.addOrUpdateArtifact(artifact).then(onSuccess, onFailed);
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);
233 let onFailed = (error:any):void => {
234 this.$scope.isLoading = false;
235 console.log('Delete artifact returned error:', error);
238 this.$scope.component.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(onSuccess, onFailed);
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);
246 this.$scope.getEnvArtifactName = (artifact:ArtifactModel):string => {
247 let envArtifact = this.$scope.getEnvArtifact(artifact);
249 return envArtifact.artifactDisplayName;
253 this.$scope.openEditEnvParametersModal = (artifact:ArtifactModel):void => {
254 this.ModalsHandler.openEditEnvParametersModal(artifact, this.$scope.component).then(()=> {
255 this.initArtifacts(true);
257 this.initArtifacts(true);
261 this.$scope.openDescriptionPopover = (artifactId:string):void => {
262 if (this.$scope.selectedArtifactId && this.$scope.selectedArtifactId != artifactId) {
263 this.$scope.updateSelectedArtifact();
265 this.$scope.selectedArtifactId = artifactId;
269 this.$scope.closeDescriptionPopover = ():void => {
270 if (this.$scope.selectedArtifactId) {
271 this.$scope.updateSelectedArtifact();
272 this.$scope.selectedArtifactId = null;