3a048c187954d9c603f98d182e3d889339671f18
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 /// <reference path="../../../../references"/>
21 module Sdc.ViewModels {
22     'use strict';
23     import ArtifactModel = Sdc.Models.ArtifactModel;
24
25     export interface IInformationArtifactsScope extends IWorkspaceViewModelScope {
26         artifacts: Array<Models.ArtifactModel>;
27         tableHeadersList: Array<any>;
28         artifactType: string;
29         isResourceInstance:boolean;
30         downloadFile:Models.IFileDownload;
31         isLoading:boolean;
32         sortBy:string;
33         reverse:boolean;
34
35         getTitle(): string;
36         addOrUpdate(artifact:Models.ArtifactModel): void;
37         delete(artifact:Models.ArtifactModel): void;
38         download(artifact:Models.ArtifactModel): void;
39         clickArtifactName(artifact:any):void;
40         openEditEnvParametersModal(artifactResource:Models.ArtifactModel):void;
41         sort(sortBy:string): void;
42         showNoArtifactMessage():boolean;
43     }
44
45     export class InformationArtifactsViewModel {
46
47         static '$inject' = [
48             '$scope',
49             '$filter',
50             '$modal',
51             '$templateCache',
52             'Sdc.Services.SharingService',
53             '$state',
54             'sdcConfig',
55             'ModalsHandler'
56         ];
57
58         constructor(private $scope:IInformationArtifactsScope,
59                     private $filter:ng.IFilterService,
60                     private $modal:ng.ui.bootstrap.IModalService,
61                     private $templateCache:ng.ITemplateCacheService,
62                     private sharingService:Sdc.Services.SharingService,
63                     private $state:any,
64                     private sdcConfig:Models.IAppConfigurtaion,
65                     private ModalsHandler:Utils.ModalsHandler) {
66             this.initScope();
67             this.$scope.updateSelectedMenuItem();
68         }
69
70
71         private getMappedObjects():any {
72             return {
73                 normal: this.$scope.component.artifacts
74             };
75         }
76
77         private initScope = ():void => {
78             let self = this;
79             this.$scope.isLoading = false;
80             this.$scope.sortBy = 'artifactDisplayName';
81             this.$scope.reverse = false;
82             this.$scope.setValidState(true);
83             this.$scope.artifactType = 'normal';
84             this.$scope.getTitle = ():string => {
85                 return this.$filter("resourceName")(this.$scope.component.name) + ' Artifacts';
86
87             };
88
89             this.$scope.tableHeadersList = [
90                 {title: 'Name', property: 'artifactDisplayName'},
91                 {title: 'Type', property: 'artifactType'}
92             ];
93
94             this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.artifacts);
95             this.$scope.sort = (sortBy:string):void => {
96                 this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
97                 this.$scope.sortBy = sortBy;
98             };
99
100
101             this.$scope.addOrUpdate = (artifact:Models.ArtifactModel):void => {
102                 artifact.artifactGroupType = 'INFORMATIONAL';
103                 this.ModalsHandler.openWizardArtifactModal(artifact, this.$scope.component).then(() => {
104                     this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.artifacts);
105                 });
106             };
107
108             this.$scope.showNoArtifactMessage = ():boolean => {
109                 let artifacts:any = [];
110                 artifacts = _.filter(this.$scope.artifacts, (artifact:Models.ArtifactModel)=> {
111                     return artifact.esId;
112                 });
113
114                 if (artifacts.length === 0) {
115                     return true;
116                 }
117                 return false;
118             };
119
120             this.$scope.delete = (artifact:Models.ArtifactModel):void => {
121
122                 let onOk = ():void => {
123                     this.$scope.isLoading = true;
124                     let onSuccess = ():void => {
125                         this.$scope.isLoading = false;
126                         this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.artifacts);
127                     };
128
129                     let onFailed = (error:any):void => {
130                         console.log('Delete artifact returned error:', error);
131                         this.$scope.isLoading = false;
132                     };
133
134                     this.$scope.component.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(onSuccess, onFailed);
135                 };
136
137                 let title:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
138                 let message:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
139                 this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
140             };
141
142             this.$scope.clickArtifactName = (artifact:any) => {
143                 if (!artifact.esId) {
144                     this.$scope.addOrUpdate(artifact);
145                 }
146
147             };
148         }
149     }
150 }