CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / tosca-artifacts / tosca-artifacts-view-model.ts
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 * as _ from "lodash";
23 import {ArtifactModel, IFileDownload} from "app/models";
24 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
25 import {ComponentGenericResponse} from "../../../../ng2/services/responses/component-generic-response";
26 import {ComponentServiceNg2} from "../../../../ng2/services/component-services/component.service";
27
28 export interface IToscaArtifactsScope extends IWorkspaceViewModelScope {
29     artifacts:Array<ArtifactModel>;
30     tableHeadersList:Array<any>;
31     artifactType:string;
32     downloadFile:IFileDownload;
33     isLoading:boolean;
34     sortBy:string;
35     reverse:boolean;
36
37     getTitle():string;
38     download(artifact:ArtifactModel):void;
39     sort(sortBy:string):void;
40     showNoArtifactMessage():boolean;
41 }
42
43 export class ToscaArtifactsViewModel {
44
45     static '$inject' = [
46         '$scope',
47         '$filter',
48         'ComponentServiceNg2'
49     ];
50
51     constructor(private $scope:IToscaArtifactsScope,
52                 private $filter:ng.IFilterService,
53                 private ComponentServiceNg2:ComponentServiceNg2) {
54         this.initToscaArtifacts();
55     }
56
57     private initToscaArtifacts = (): void => {
58
59         if(!this.$scope.component.toscaArtifacts) {
60             this.$scope.isLoading = true;
61             this.ComponentServiceNg2.getComponentToscaArtifacts(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
62                 this.$scope.component.toscaArtifacts = response.toscaArtifacts;
63                 this.initScope();
64                 this.$scope.isLoading = false;
65             }, () => {
66                 this.$scope.isLoading = false;
67             });
68         } else {
69             this.initScope();
70         }
71     }
72
73     private initScope = ():void => {
74         this.$scope.isLoading = false;
75         this.$scope.sortBy = 'artifactDisplayName';
76         this.$scope.reverse = false;
77         this.$scope.setValidState(true);
78         this.$scope.artifactType = 'informational';
79         this.$scope.getTitle = ():string => {
80             return this.$filter("resourceName")(this.$scope.component.name) + ' Artifacts';
81
82         };
83
84         this.$scope.tableHeadersList = [
85             {title: 'Name', property: 'artifactDisplayName'},
86             {title: 'Type', property: 'artifactType'},
87             {title: 'Version', property: 'artifactVersion'}
88         ];
89
90         this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.toscaArtifacts);
91         this.$scope.sort = (sortBy:string):void => {
92             this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
93             this.$scope.sortBy = sortBy;
94         };
95
96
97         this.$scope.showNoArtifactMessage = ():boolean => {
98             if (this.$scope.artifacts.length === 0) {
99                 return true;
100             }
101             return false;
102         };
103
104     }
105 }