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=========================================================
22 import {ArtifactModel, Resource, Component} from "app/models";
23 import {ArtifactsUtils, FormState, ValidationUtils, ArtifactType} from "app/utils";
24 import {CacheService} from "app/services";
26 export interface IEditArtifactModel {
27 artifactResource:ArtifactModel;
28 artifactTypes:Array<string>;
32 export interface IArtifactResourceFormViewModelScope extends ng.IScope {
37 validationPattern:RegExp;
38 urlValidationPattern:RegExp;
39 labelValidationPattern:RegExp;
40 integerValidationPattern:RegExp;
41 commentValidationPattern:RegExp;
43 editArtifactResourceModel:IEditArtifactModel;
44 defaultHeatTimeout:number;
46 originalArtifactName:string;
47 editForm:ng.IFormController;
48 footerButtons:Array<any>;
49 modalInstanceArtifact:ng.ui.bootstrap.IModalServiceInstance;
51 fileExtensions():string;
52 save(doNotCloseModal?:boolean):void;
53 saveAndAnother():void;
55 getOptions():Array<string>;
56 isDeploymentHeat():boolean;
58 setDefaultTimeout():void;
59 openEditEnvParametersModal(artifact:ArtifactModel):void;
60 getFormTitle():string;
61 fileUploadRequired():string;
62 isArtifactOwner():boolean;
65 export class ArtifactResourceFormViewModel {
71 'Sdc.Services.CacheService',
73 'UrlValidationPattern',
74 'LabelValidationPattern',
75 'IntegerValidationPattern',
76 'CommentValidationPattern',
85 private formState:FormState;
86 private entityId:string;
88 constructor(private $scope:IArtifactResourceFormViewModelScope,
89 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
90 private artifact:ArtifactModel,
91 private cacheService:CacheService,
92 private ValidationPattern:RegExp,
93 private UrlValidationPattern:RegExp,
94 private LabelValidationPattern:RegExp,
95 private IntegerValidationPattern:RegExp,
96 private CommentValidationPattern:RegExp,
97 private ValidationUtils:ValidationUtils,
100 private artifactsUtils:ArtifactsUtils,
101 private $uibModal:ng.ui.bootstrap.IModalService,
102 private component:Component) {
105 this.entityId = this.component.uniqueId;
106 this.formState = angular.isDefined(artifact.artifactLabel) ? FormState.UPDATE : FormState.CREATE;
110 private initEntity = ():void => {
111 this.$scope.editArtifactResourceModel.artifactResource = this.artifact;
112 this.$scope.originalArtifactName = this.artifact.artifactName;
116 private initFooterButtons = ():void => {
118 this.$scope.footerButtons = [
119 {'name': 'Done', 'css': 'blue', 'callback': this.$scope.save}
121 if (this.$scope.isNew) {
122 this.$scope.footerButtons.push({
123 'name': 'Add Another',
125 'disabled': !this.$scope.isNew && 'deployment' === this.$scope.artifactType,
126 'callback': this.$scope.saveAndAnother
131 private filterDeploymentArtifactTypeByResourceType = (resourceType:string):any => {
133 _.each(this.$scope.validExtensions, function (typeSettings:any, typeName:string) {
134 if (!typeSettings.validForResourceTypes || typeSettings.validForResourceTypes.indexOf(resourceType) > -1) {
135 result[typeName] = typeSettings;
142 private initArtifactTypes = ():void => {
144 let artifactTypes:any = this.cacheService.get('UIConfiguration');
146 if ('deployment' === this.$scope.artifactType) {
149 if ('HEAT_ENV' == this.artifact.artifactType || this.component.selectedInstance) {
150 this.$scope.validExtensions = artifactTypes.artifacts.deployment.resourceInstanceDeploymentArtifacts;
151 } else if (this.component.isResource()) {
152 this.$scope.validExtensions = artifactTypes.artifacts.deployment.resourceDeploymentArtifacts;
153 this.$scope.validExtensions = this.filterDeploymentArtifactTypeByResourceType((<Resource>this.component).resourceType);
155 this.$scope.validExtensions = artifactTypes.artifacts.deployment.serviceDeploymentArtifacts;
158 if (this.$scope.validExtensions) {
159 this.$scope.editArtifactResourceModel.artifactTypes = Object.keys(this.$scope.validExtensions);
161 this.$scope.defaultHeatTimeout = artifactTypes.defaultHeatTimeout;
162 if (this.$scope.isNew) {
163 let isHeat = 'HEAT_ENV' == this.artifact.artifactType;
164 _.remove(this.$scope.editArtifactResourceModel.artifactTypes, (item:string)=> {
165 return 'HEAT' == item.substring(0, 4) || (!isHeat && item == "VF_MODULES_METADATA") ||
166 _.has(ArtifactType.THIRD_PARTY_RESERVED_TYPES, item);
171 if (this.$scope.artifactType === 'informational') {
172 this.$scope.editArtifactResourceModel.artifactTypes = artifactTypes.artifacts.other.map((element:any)=> {
175 _.remove(this.$scope.editArtifactResourceModel.artifactTypes, (item:string)=> {
176 return _.has(ArtifactType.THIRD_PARTY_RESERVED_TYPES, item) ||
177 _.has(ArtifactType.TOSCA, item);
181 if (this.component.isResource() && (<Resource>this.component).isCsarComponent()) {
182 _.remove(this.$scope.editArtifactResourceModel.artifactTypes, (item:string) => {
183 return this.artifactsUtils.isLicenseType(item);
189 private initEditArtifactResourceModel = ():void => {
190 this.$scope.editArtifactResourceModel = {
191 artifactResource: null,
199 private initScope = ():void => {
201 this.$scope.validationPattern = this.ValidationPattern;
202 this.$scope.urlValidationPattern = this.UrlValidationPattern;
203 this.$scope.labelValidationPattern = this.LabelValidationPattern;
204 this.$scope.integerValidationPattern = this.IntegerValidationPattern;
205 this.$scope.commentValidationPattern = this.CommentValidationPattern;
206 this.$scope.isLoading = false;
207 this.$scope.isNew = (this.formState === FormState.CREATE);
208 this.$scope.artifactType = this.artifactsUtils.getArtifactTypeByState(this.$state.current.name);
209 this.$scope.modalInstanceArtifact = this.$uibModalInstance;
211 this.initEditArtifactResourceModel();
212 this.initArtifactTypes();
214 // In case of edit, show the file name in browse.
215 if (this.artifact.artifactName !== "" && 'HEAT_ENV' !== this.artifact.artifactType) {
216 this.$scope.editArtifactResourceModel.artifactFile = {};
217 this.$scope.editArtifactResourceModel.artifactFile.filename = this.artifact.artifactName;
221 this.$scope.isDeploymentHeat = ():boolean => {
222 return !this.$scope.isNew && this.$scope.artifactType === 'deployment'
223 && this.$scope.editArtifactResourceModel.artifactResource.isHEAT();
226 this.$scope.onFileChange = ():void => {
227 if (this.$scope.editArtifactResourceModel.artifactFile && this.$scope.editArtifactResourceModel.artifactFile.filename) {
228 this.$scope.editArtifactResourceModel.artifactResource.artifactName = this.$scope.editArtifactResourceModel.artifactFile.filename;
230 this.$scope.editArtifactResourceModel.artifactResource.artifactName = this.$scope.originalArtifactName;
233 this.$scope.setDefaultTimeout = ():void => {
234 if (this.$scope.isDeploymentHeat() && !this.$scope.editArtifactResourceModel.artifactResource.timeout) {
235 this.$scope.editArtifactResourceModel.artifactResource.timeout = this.$scope.defaultHeatTimeout;
238 if (this.$scope.editArtifactResourceModel.artifactFile.filename) {
239 this.$scope.editArtifactResourceModel.artifactFile = {};
240 this.$scope.forms.editForm.myArtifactFile.$setValidity('required', false);
244 this.$scope.fileExtensions = ():string => {
245 let type:string = this.$scope.editArtifactResourceModel.artifactResource.artifactType;
246 return type && this.$scope.validExtensions && this.$scope.validExtensions[type].acceptedTypes ?
247 this.$scope.validExtensions[type].acceptedTypes.join(',') : "";
250 this.$scope.save = (doNotCloseModal?:boolean):void => {
251 this.$scope.isLoading = true;
252 this.$scope.editArtifactResourceModel.artifactResource.description = this.ValidationUtils.stripAndSanitize(this.$scope.editArtifactResourceModel.artifactResource.description);
254 if (!this.$scope.isDeploymentHeat()) {
255 this.$scope.editArtifactResourceModel.artifactResource.timeout = null;
258 if (this.$scope.editArtifactResourceModel.artifactFile) {
259 this.$scope.editArtifactResourceModel.artifactResource.payloadData = this.$scope.editArtifactResourceModel.artifactFile.base64;
260 this.$scope.editArtifactResourceModel.artifactResource.artifactName = this.$scope.editArtifactResourceModel.artifactFile.filename;
263 let onFaild = (response):void => {
264 this.$scope.isLoading = false;
265 console.info('onFaild', response);
268 let onSuccess = (artifactResource:ArtifactModel):void => {
269 this.$scope.isLoading = false;
270 this.$scope.originalArtifactName = "";
272 if (this.$scope.isDeploymentHeat()) {
273 if (artifactResource.heatParameters) {
274 this.$scope.openEditEnvParametersModal(artifactResource);
278 if (!doNotCloseModal) {
279 this.$uibModalInstance.close();
281 this.$scope.editArtifactResourceModel.artifactFile = null;
282 angular.element("input[type='file']").val(null); // for support chrome when upload the same file
283 this.artifactsUtils.addAnotherAfterSave(this.$scope);
288 if ('HEAT_ENV' == this.artifact.artifactType) {
289 if (this.component.selectedInstance) {
290 this.component.uploadInstanceEnvFile(this.$scope.editArtifactResourceModel.artifactResource).then(onSuccess, onFaild);
292 this.component.addOrUpdateArtifact(this.$scope.editArtifactResourceModel.artifactResource).then(onSuccess, onFaild);
295 } else if (this.$scope.isArtifactOwner()) {
296 this.component.addOrUpdateInstanceArtifact(this.$scope.editArtifactResourceModel.artifactResource).then(onSuccess, onFaild);
298 this.component.addOrUpdateArtifact(this.$scope.editArtifactResourceModel.artifactResource).then(onSuccess, onFaild);
302 this.$scope.isArtifactOwner = ():boolean=> {
303 return this.component.isService() && !!this.component.selectedInstance;
306 this.$scope.saveAndAnother = ():void => {
307 this.$scope.save(true);
310 this.$scope.close = ():void => {
311 this.$uibModalInstance.close();
314 this.$scope.fileUploadRequired = ():string => {
315 if (this.$scope.editArtifactResourceModel.artifactFile.filename) {
323 this.$scope.getFormTitle = ():string => {
324 if ('HEAT_ENV' == this.artifact.artifactType) {
325 return 'Update HEAT ENV';
327 if (this.$scope.isDeploymentHeat()) {
328 if (!this.$scope.editArtifactResourceModel.artifactResource.artifactChecksum) {
329 return 'Add HEAT Template';
331 return 'Update HEAT Template';
333 if (this.$scope.isNew) {
334 return 'Add Artifact';
336 return 'Update Artifact';
339 this.$scope.openEditEnvParametersModal = (artifactResource:ArtifactModel):void => {
341 let modalOptions:ng.ui.bootstrap.IModalSettings = {
342 templateUrl: '../env-parameters-form/env-parameters-form.html',
343 controller: 'Sdc.ViewModels.EnvParametersFormViewModel',
347 artifact: ():ArtifactModel => {
348 return artifactResource;
350 component: ():Component => {
351 return this.component;
356 let modalInstance:ng.ui.bootstrap.IModalServiceInstance = this.$uibModal.open(modalOptions);
363 this.$scope.forms = {};
365 this.initFooterButtons();
368 this.$scope.$watch("forms.editForm.$invalid", (newVal, oldVal) => {
369 if(this.$scope.forms.editForm) {
370 this.$scope.footerButtons[0].disabled = this.$scope.forms.editForm.$invalid;
371 if (this.$scope.isNew) {
372 this.$scope.footerButtons[1].disabled = this.$scope.forms.editForm.$invalid;