aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/view-models/forms/artifact-form/artifact-form-view-model.ts
blob: 3e912706e03a9296e721a31955d3362cb857c423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
'use strict';
import {ArtifactModel, Resource, Component} from "app/models";
import {ArtifactsUtils, FormState, ValidationUtils, ArtifactType} from "app/utils";
import {CacheService} from "app/services";

export interface IEditArtifactModel {
    artifactResource:ArtifactModel;
    artifactTypes:Array<string>;
    artifactFile:any;
}

export interface IArtifactResourceFormViewModelScope extends ng.IScope {
    forms:any;
    $$childTail:any;
    isNew:boolean;
    isLoading:boolean;
    validationPattern:RegExp;
    urlValidationPattern:RegExp;
    labelValidationPattern:RegExp;
    integerValidationPattern:RegExp;
    commentValidationPattern:RegExp;
    artifactType:string;
    editArtifactResourceModel:IEditArtifactModel;
    defaultHeatTimeout:number;
    validExtensions:any;
    originalArtifactName:string;
    editForm:ng.IFormController;
    footerButtons:Array<any>;
    modalInstanceArtifact:ng.ui.bootstrap.IModalServiceInstance;

    fileExtensions():string;
    save(doNotCloseModal?:boolean):void;
    saveAndAnother():void;
    close():void;
    getOptions():Array<string>;
    isDeploymentHeat():boolean;
    onFileChange():void;
    setDefaultTimeout():void;
    openEditEnvParametersModal(artifact:ArtifactModel):void;
    getFormTitle():string;
    fileUploadRequired():string;
    isArtifactOwner():boolean;
}

export class ArtifactResourceFormViewModel {

    static '$inject' = [
        '$scope',
        '$uibModalInstance',
        'artifact',
        'Sdc.Services.CacheService',
        'ValidationPattern',
        'UrlValidationPattern',
        'LabelValidationPattern',
        'IntegerValidationPattern',
        'CommentValidationPattern',
        'ValidationUtils',
        '$base64',
        '$state',
        'ArtifactsUtils',
        '$uibModal',
        'component'
    ];

    private formState:FormState;
    private entityId:string;

    constructor(private $scope:IArtifactResourceFormViewModelScope,
                private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
                private artifact:ArtifactModel,
                private cacheService:CacheService,
                private ValidationPattern:RegExp,
                private UrlValidationPattern:RegExp,
                private LabelValidationPattern:RegExp,
                private IntegerValidationPattern:RegExp,
                private CommentValidationPattern:RegExp,
                private ValidationUtils:ValidationUtils,
                private $base64:any,
                private $state:any,
                private artifactsUtils:ArtifactsUtils,
                private $uibModal:ng.ui.bootstrap.IModalService,
                private component:Component) {


        this.entityId = this.component.uniqueId;
        this.formState = angular.isDefined(artifact.artifactLabel) ? FormState.UPDATE : FormState.CREATE;
        this.initScope();
    }

    private initEntity = ():void => {
        this.$scope.editArtifactResourceModel.artifactResource = this.artifact;
        this.$scope.originalArtifactName = this.artifact.artifactName;
    };


    private initFooterButtons = ():void => {

        this.$scope.footerButtons = [
            {'name': 'Done', 'css': 'blue', 'callback': this.$scope.save}
        ];
        if (this.$scope.isNew) {
            this.$scope.footerButtons.push({
                'name': 'Add Another',
                'css': 'grey',
                'disabled': !this.$scope.isNew && 'deployment' === this.$scope.artifactType,
                'callback': this.$scope.saveAndAnother
            });
        }
    };

    private filterDeploymentArtifactTypeByResourceType = (resourceType:string):any => {
        let result = {};
        _.each(this.$scope.validExtensions, function (typeSettings:any, typeName:string) {
            if (!typeSettings.validForResourceTypes || typeSettings.validForResourceTypes.indexOf(resourceType) > -1) {
                result[typeName] = typeSettings;
            }
        });

        return result;
    };

    private initArtifactTypes = ():void => {

        let artifactTypes:any = this.cacheService.get('UIConfiguration');

        if ('deployment' === this.$scope.artifactType) {


            if ('HEAT_ENV' == this.artifact.artifactType || this.component.selectedInstance) {
                this.$scope.validExtensions = artifactTypes.artifacts.deployment.resourceInstanceDeploymentArtifacts;
            } else if (this.component.isResource()) {
                this.$scope.validExtensions = artifactTypes.artifacts.deployment.resourceDeploymentArtifacts;
                this.$scope.validExtensions = this.filterDeploymentArtifactTypeByResourceType((<Resource>this.component).resourceType);
            } else {
                this.$scope.validExtensions = artifactTypes.artifacts.deployment.serviceDeploymentArtifacts;
            }

            if (this.$scope.validExtensions) {
                this.$scope.editArtifactResourceModel.artifactTypes = Object.keys(this.$scope.validExtensions);
            }
            this.$scope.defaultHeatTimeout = artifactTypes.defaultHeatTimeout;
            if (this.$scope.isNew) {
                let isHeat = 'HEAT_ENV' == this.artifact.artifactType;
                _.remove(this.$scope.editArtifactResourceModel.artifactTypes, (item:string)=> {
                    return 'HEAT' == item.substring(0, 4) || (!isHeat && item == "VF_MODULES_METADATA") ||
                        _.has(ArtifactType.THIRD_PARTY_RESERVED_TYPES, item);
                });
            }

        }
        if (this.$scope.artifactType === 'informational') {
            this.$scope.editArtifactResourceModel.artifactTypes = artifactTypes.artifacts.other.map((element:any)=> {
                return element.name;
            });
            _.remove(this.$scope.editArtifactResourceModel.artifactTypes, (item:string)=> {
                return _.has(ArtifactType.THIRD_PARTY_RESERVED_TYPES, item) ||
                    _.has(ArtifactType.TOSCA, item);
            })
        }

        if (this.component.isResource() && (<Resource>this.component).isCsarComponent()) {
            _.remove(this.$scope.editArtifactResourceModel.artifactTypes, (item:string) => {
                return this.artifactsUtils.isLicenseType(item);
            })
        }

    };

    private initEditArtifactResourceModel = ():void => {
        this.$scope.editArtifactResourceModel = {
            artifactResource: null,
            artifactTypes: null,
            artifactFile: {}
        };

        this.initEntity();
    };

    private initScope = ():void => {

        this.$scope.validationPattern = this.ValidationPattern;
        this.$scope.urlValidationPattern = this.UrlValidationPattern;
        this.$scope.labelValidationPattern = this.LabelValidationPattern;
        this.$scope.integerValidationPattern = this.IntegerValidationPattern;
        this.$scope.commentValidationPattern = this.CommentValidationPattern;
        this.$scope.isLoading = false;
        this.$scope.isNew = (this.formState === FormState.CREATE);
        this.$scope.artifactType = this.artifactsUtils.getArtifactTypeByState(this.$state.current.name);
        this.$scope.modalInstanceArtifact = this.$uibModalInstance;

        this.initEditArtifactResourceModel();
        this.initArtifactTypes();

        // In case of edit, show the file name in browse.
        if (this.artifact.artifactName !== "" && 'HEAT_ENV' !== this.artifact.artifactType) {
            this.$scope.editArtifactResourceModel.artifactFile = {};
            this.$scope.editArtifactResourceModel.artifactFile.filename = this.artifact.artifactName;
        }

        //scope methods
        this.$scope.isDeploymentHeat = ():boolean => {
            return !this.$scope.isNew && this.$scope.artifactType === 'deployment'
                && this.$scope.editArtifactResourceModel.artifactResource.isHEAT();

        };
        this.$scope.onFileChange = ():void => {
            if (this.$scope.editArtifactResourceModel.artifactFile && this.$scope.editArtifactResourceModel.artifactFile.filename) {
                this.$scope.editArtifactResourceModel.artifactResource.artifactName = this.$scope.editArtifactResourceModel.artifactFile.filename;
            } else {
                this.$scope.editArtifactResourceModel.artifactResource.artifactName = this.$scope.originalArtifactName;
            }
        };
        this.$scope.setDefaultTimeout = ():void => {
            if (this.$scope.isDeploymentHeat() && !this.$scope.editArtifactResourceModel.artifactResource.timeout) {
                this.$scope.editArtifactResourceModel.artifactResource.timeout = this.$scope.defaultHeatTimeout;
            }

            if (this.$scope.editArtifactResourceModel.artifactFile.filename) {
                this.$scope.editArtifactResourceModel.artifactFile = {};
                this.$scope.forms.editForm.myArtifactFile.$setValidity('required', false);
            }
        };

        this.$scope.fileExtensions = ():string => {
            let type:string = this.$scope.editArtifactResourceModel.artifactResource.artifactType;
            return type && this.$scope.validExtensions && this.$scope.validExtensions[type].acceptedTypes ?
                this.$scope.validExtensions[type].acceptedTypes.join(',') : "";
        };

        this.$scope.save = (doNotCloseModal?:boolean):void => {
            this.$scope.isLoading = true;
            this.$scope.editArtifactResourceModel.artifactResource.description = this.ValidationUtils.stripAndSanitize(this.$scope.editArtifactResourceModel.artifactResource.description);

            if (!this.$scope.isDeploymentHeat()) {
                this.$scope.editArtifactResourceModel.artifactResource.timeout = null;
            }

            if (this.$scope.editArtifactResourceModel.artifactFile) {
                this.$scope.editArtifactResourceModel.artifactResource.payloadData = this.$scope.editArtifactResourceModel.artifactFile.base64;
                this.$scope.editArtifactResourceModel.artifactResource.artifactName = this.$scope.editArtifactResourceModel.artifactFile.filename;
            }

            let onFaild = (response):void => {
                this.$scope.isLoading = false;
                console.info('onFaild', response);
            };

            let onSuccess = (artifactResource:ArtifactModel):void => {
                this.$scope.isLoading = false;
                this.$scope.originalArtifactName = "";

                if (this.$scope.isDeploymentHeat()) {
                    if (artifactResource.heatParameters) {
                        this.$scope.openEditEnvParametersModal(artifactResource);
                    }
                }

                if (!doNotCloseModal) {
                    this.$uibModalInstance.close();
                } else {
                    this.$scope.editArtifactResourceModel.artifactFile = null;
                    angular.element("input[type='file']").val(null); //  for support chrome when upload the same file
                    this.artifactsUtils.addAnotherAfterSave(this.$scope);
                }

            };

            if ('HEAT_ENV' == this.artifact.artifactType) {
                if (this.component.selectedInstance) {
                    this.component.uploadInstanceEnvFile(this.$scope.editArtifactResourceModel.artifactResource).then(onSuccess, onFaild);
                } else {
                    this.component.addOrUpdateArtifact(this.$scope.editArtifactResourceModel.artifactResource).then(onSuccess, onFaild);

                }
            } else if (this.$scope.isArtifactOwner()) {
                this.component.addOrUpdateInstanceArtifact(this.$scope.editArtifactResourceModel.artifactResource).then(onSuccess, onFaild);
            } else {
                this.component.addOrUpdateArtifact(this.$scope.editArtifactResourceModel.artifactResource).then(onSuccess, onFaild);
            }
        };

        this.$scope.isArtifactOwner = ():boolean=> {
            return this.component.isService() && !!this.component.selectedInstance;
        };

        this.$scope.saveAndAnother = ():void => {
            this.$scope.save(true);
        };

        this.$scope.close = ():void => {
            this.$uibModalInstance.close();
        };

        this.$scope.fileUploadRequired = ():string => {
            if (this.$scope.editArtifactResourceModel.artifactFile.filename) {
                // This is edit mode
                return 'false';
            } else {
                return 'true';
            }
        };

        this.$scope.getFormTitle = ():string => {
            if ('HEAT_ENV' == this.artifact.artifactType) {
                return 'Update HEAT ENV';
            }
            if (this.$scope.isDeploymentHeat()) {
                if (!this.$scope.editArtifactResourceModel.artifactResource.artifactChecksum) {
                    return 'Add HEAT Template';
                }
                return 'Update HEAT Template';
            }
            if (this.$scope.isNew) {
                return 'Add Artifact';
            }
            return 'Update Artifact';
        };

        this.$scope.openEditEnvParametersModal = (artifactResource:ArtifactModel):void => {

            let modalOptions:ng.ui.bootstrap.IModalSettings = {
                templateUrl: '../env-parameters-form/env-parameters-form.html',
                controller: 'Sdc.ViewModels.EnvParametersFormViewModel',
                size: 'sdc-md',
                backdrop: 'static',
                resolve: {
                    artifact: ():ArtifactModel => {
                        return artifactResource;
                    },
                    component: ():Component => {
                        return this.component;
                    }
                }
            };

            let modalInstance:ng.ui.bootstrap.IModalServiceInstance = this.$uibModal.open(modalOptions);
            modalInstance
                .result
                .then(():void => {
                });
        };

        this.$scope.forms = {};

        this.initFooterButtons();


        this.$scope.$watch("forms.editForm.$invalid", (newVal, oldVal) => {
            if(this.$scope.forms.editForm) {
                this.$scope.footerButtons[0].disabled = this.$scope.forms.editForm.$invalid;
                if (this.$scope.isNew) {
                    this.$scope.footerButtons[1].disabled = this.$scope.forms.editForm.$invalid;
                }
            }
        });

    }
}