ceaba3c18760aee272d820bd3d9527e61b2d7cf8
[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
21 'use strict';
22 import {ModalsHandler} from "app/utils";
23 import {SharingService} from "app/services";
24 import {IAppConfigurtaion, ArtifactModel, IFileDownload} from "app/models";
25 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
26 import {ComponentServiceNg2} from "../../../../ng2/services/component-services/component.service";
27 import {ArtifactGroupModel} from "../../../../models/artifacts";
28 import {ComponentGenericResponse} from "../../../../ng2/services/responses/component-generic-response";
29
30 export interface IInformationArtifactsScope extends IWorkspaceViewModelScope {
31     artifacts:Array<ArtifactModel>;
32     tableHeadersList:Array<any>;
33     artifactType:string;
34     isResourceInstance:boolean;
35     downloadFile:IFileDownload;
36     isLoading:boolean;
37     sortBy:string;
38     reverse:boolean;
39
40     getTitle():string;
41     addOrUpdate(artifact:ArtifactModel):void;
42     delete(artifact:ArtifactModel):void;
43     download(artifact:ArtifactModel):void;
44     clickArtifactName(artifact:any):void;
45     openEditEnvParametersModal(artifactResource:ArtifactModel):void;
46     sort(sortBy:string):void;
47     showNoArtifactMessage():boolean;
48 }
49
50 export class InformationArtifactsViewModel {
51
52     static '$inject' = [
53         '$scope',
54         '$filter',
55         '$state',
56         'sdcConfig',
57         'ModalsHandler',
58         'ComponentServiceNg2'
59     ];
60
61     constructor(private $scope:IInformationArtifactsScope,
62                 private $filter:ng.IFilterService,
63                 private $state:any,
64                 private sdcConfig:IAppConfigurtaion,
65                 private ModalsHandler:ModalsHandler,
66                 private ComponentServiceNg2: ComponentServiceNg2) {
67         this.initInformationalArtifacts();
68         this.$scope.updateSelectedMenuItem();
69     }
70
71     private initInformationalArtifacts = ():void => {
72         if(!this.$scope.component.artifacts) {
73             this.$scope.isLoading = true;
74             this.ComponentServiceNg2.getComponentInformationalArtifacts(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
75                 this.$scope.component.artifacts = response.artifacts;
76                 this.initScope();
77                 this.$scope.isLoading = false;
78             });
79         } else {
80             this.initScope();
81         }
82     }
83
84     private initScope = ():void => {
85
86         this.$scope.isLoading = false;
87         this.$scope.sortBy = 'artifactDisplayName';
88         this.$scope.reverse = false;
89         this.$scope.setValidState(true);
90         this.$scope.artifactType = 'informational';
91         this.$scope.getTitle = ():string => {
92             return this.$filter("resourceName")(this.$scope.component.name) + ' Artifacts';
93
94         };
95
96         this.$scope.tableHeadersList = [
97             {title: 'Name', property: 'artifactDisplayName'},
98             {title: 'Type', property: 'artifactType'},
99             {title: 'Version', property: 'artifactVersion'},
100             {title: 'UUID', property: 'artifactUUID'}
101         ];
102
103         this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.artifacts);
104         this.$scope.sort = (sortBy:string):void => {
105             this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
106             this.$scope.sortBy = sortBy;
107         };
108
109
110         this.$scope.addOrUpdate = (artifact:ArtifactModel):void => {
111             artifact.artifactGroupType = 'INFORMATIONAL';
112             this.ModalsHandler.openArtifactModal(artifact, this.$scope.component).then(() => {
113                 this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.artifacts);
114             });
115         };
116
117         this.$scope.showNoArtifactMessage = ():boolean => {
118             let artifacts:any = [];
119             artifacts = _.filter(this.$scope.artifacts, (artifact:ArtifactModel)=> {
120                 return artifact.esId;
121             });
122
123             if (artifacts.length === 0) {
124                 return true;
125             }
126             return false;
127         };
128
129         this.$scope.delete = (artifact:ArtifactModel):void => {
130
131             let onOk = ():void => {
132                 this.$scope.isLoading = true;
133                 let onSuccess = ():void => {
134                     this.$scope.isLoading = false;
135                     this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.artifacts);
136                 };
137
138                 let onFailed = (error:any):void => {
139                     console.log('Delete artifact returned error:', error);
140                     this.$scope.isLoading = false;
141                 };
142
143                 this.$scope.component.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(onSuccess, onFailed);
144             };
145
146             let title:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
147             let message:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
148             this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
149         };
150
151         this.$scope.clickArtifactName = (artifact:any) => {
152             if (!artifact.esId) {
153                 this.$scope.addOrUpdate(artifact);
154             }
155
156         };
157     }
158 }