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;
25 export interface IArtifactsViewModelScope extends ICompositionViewModelScope {
26 artifacts: Array<Models.ArtifactModel>;
28 downloadFile:Models.IFileDownload;
32 addOrUpdate(artifact:Models.ArtifactModel): void;
33 delete(artifact:Models.ArtifactModel): void;
34 download(artifact:Models.ArtifactModel): void;
35 openEditEnvParametersModal(artifact:Models.ArtifactModel):void;
36 getEnvArtifact(heatArtifact:Models.ArtifactModel):any;
37 getEnvArtifactName(artifact:Models.ArtifactModel):string;
38 isLicenseArtifact(artifact:Models.ArtifactModel):boolean;
39 isVFiArtifact(artifact:Models.ArtifactModel):boolean;
42 export class ResourceArtifactsViewModel {
53 'Sdc.Services.CacheService'
56 constructor(private $scope:IArtifactsViewModelScope,
57 private $filter:ng.IFilterService,
58 private $modal:ng.ui.bootstrap.IModalService,
59 private $templateCache:ng.ITemplateCacheService,
61 private sdcConfig:Models.IAppConfigurtaion,
62 private artifactsUtils:Sdc.Utils.ArtifactsUtils,
63 private ModalsHandler: Utils.ModalsHandler,
64 private cacheService:Services.CacheService) {
70 private initArtifactArr = (artifactType:string):void => {
71 let artifacts:Array<Models.ArtifactModel> = [];
73 if (this.$scope.selectedComponent) {
74 if ('interface' == artifactType) {
75 let interfaces = this.$scope.selectedComponent.interfaces;
76 if (interfaces && interfaces.standard && interfaces.standard.operations) {
78 angular.forEach(interfaces.standard.operations, (operation:any, interfaceName:string):void => {
79 let item:Sdc.Models.ArtifactModel = <Sdc.Models.ArtifactModel>{};
80 if (operation.implementation) {
81 item = <Sdc.Models.ArtifactModel> operation.implementation;
83 item.artifactDisplayName = interfaceName;
84 item.artifactLabel = interfaceName;
85 item.mandatory = false;
90 //init normal artifacts, deployment or api artifacts
91 let artifactsObj:Models.ArtifactGroupModel;
92 switch (artifactType) {
94 artifactsObj = (<Models.Components.Service>this.$scope.selectedComponent).serviceApiArtifacts;
97 if (!this.$scope.isComponentInstanceSelected()) {
98 artifactsObj = this.$scope.selectedComponent.deploymentArtifacts;
100 artifactsObj = this.$scope.currentComponent.selectedInstance.deploymentArtifacts;
104 artifactsObj = this.$scope.selectedComponent.artifacts;
107 _.forEach(artifactsObj, (artifact:Models.ArtifactModel, key) => {
108 artifacts.push(artifact);
112 this.$scope.artifacts = artifacts;
115 private openEditArtifactModal = (artifact:Models.ArtifactModel):void => {
116 let viewModelsHtmlBasePath:string = '/app/scripts/view-models/';
118 let modalOptions:ng.ui.bootstrap.IModalSettings = {
119 template: this.$templateCache.get(viewModelsHtmlBasePath + 'forms/artifact-form/artifact-form-view.html'),
120 controller: 'Sdc.ViewModels.ArtifactResourceFormViewModel',
125 artifact: ():Models.ArtifactModel => {
128 component: (): Models.Components.Component => {
129 return this.$scope.currentComponent;
134 let modalInstance:ng.ui.bootstrap.IModalServiceInstance = this.$modal.open(modalOptions);
138 this.initArtifactArr(this.$scope.artifactType);
142 private initScope = ():void => {
144 this.$scope.isLoading= false;
145 this.$scope.artifactType = this.artifactsUtils.getArtifactTypeByState(this.$state.current.name);
146 this.initArtifactArr(this.$scope.artifactType);
148 this.$scope.getTitle = ():string => {
149 return this.artifactsUtils.getTitle(this.$scope.artifactType, this.$scope.selectedComponent);
152 let vfiArtifactTypes:any = this.cacheService.get('UIConfiguration').artifacts.deployment.resourceInstanceDeploymentArtifacts;
154 this.$scope.isVFiArtifact=(artifact:Models.ArtifactModel):boolean=>{
155 return vfiArtifactTypes[artifact.artifactType];
158 this.$scope.$watch('selectedComponent', (newResource:Models.Components.Component):void => {
160 this.initArtifactArr(this.$scope.artifactType);
165 this.$scope.$watch('currentComponent.selectedInstance', (newInstance:Models.ComponentsInstances.ComponentInstance):void => {
167 this.initArtifactArr(this.$scope.artifactType);
171 this.$scope.addOrUpdate = (artifact:Models.ArtifactModel):void => {
172 this.artifactsUtils.setArtifactType(artifact, this.$scope.artifactType);
173 let artifactCopy = new Models.ArtifactModel(artifact);
174 this.openEditArtifactModal(artifactCopy);
178 this.$scope.delete = (artifact:Models.ArtifactModel):void => {
180 let onOk = ():void => {
181 this.$scope.isLoading= true;
182 this.artifactsUtils.removeArtifact(artifact, this.$scope.artifacts);
184 let success = (responseArtifact:Models.ArtifactModel):void => {
185 this.initArtifactArr(this.$scope.artifactType);
186 this.$scope.isLoading= false;
189 let error =(error:any):void =>{
190 console.log('Delete artifact returned error:', error);
191 this.initArtifactArr(this.$scope.artifactType);
192 this.$scope.isLoading= false;
194 if(this.$scope.isComponentInstanceSelected()){
195 this.$scope.currentComponent.deleteInstanceArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);
197 this.$scope.currentComponent.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);//TODO simulate error (make sure error returns)
200 let title: string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
201 let message: string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
202 this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
206 this.$scope.getEnvArtifact = (heatArtifact:Models.ArtifactModel):any=>{
207 return _.find(this.$scope.artifacts, (item:Models.ArtifactModel)=>{
208 return item.generatedFromId === heatArtifact.uniqueId;
212 this.$scope.getEnvArtifactName = (artifact:Models.ArtifactModel):string =>{
213 let envArtifact = this.$scope.getEnvArtifact(artifact);
215 return envArtifact.artifactDisplayName;
219 this.$scope.isLicenseArtifact = (artifact:Models.ArtifactModel) :boolean => {
220 let isLicense:boolean = false;
221 if(this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
222 isLicense = this.artifactsUtils.isLicenseType(artifact.artifactType);
228 this.$scope.openEditEnvParametersModal = (artifact:Models.ArtifactModel):void => {
230 let modalOptions:ng.ui.bootstrap.IModalSettings = {
231 template: this.$templateCache.get('/app/scripts/view-models/forms/env-parameters-form/env-parameters-form.html'),
232 controller: 'Sdc.ViewModels.EnvParametersFormViewModel',
236 artifact: ():Models.ArtifactModel => {
239 component: (): Models.Components.Component => {
240 return this.$scope.currentComponent;
245 let modalInstance:ng.ui.bootstrap.IModalServiceInstance = this.$modal.open(modalOptions);
249 this.initArtifactArr(this.$scope.artifactType);