Catalog alignment
[sdc.git] / catalog-ui / src / app / directives / download-artifact / download-artifact.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 {IFileDownload, Component, ArtifactModel} from "app/models";
23 import {EventListenerService} from "app/services";
24 import {CacheService} from "app/services-ng2";
25 import {EVENTS, FileUtils} from "app/utils";
26
27 export class DOWNLOAD_CSS_CLASSES {
28     static DOWNLOAD_ICON = "table-download-btn tosca";
29     static LOADER_ICON = "tlv-loader small loader";
30 }
31
32 export interface IDownloadArtifactScope extends ng.IScope {
33     $window:any;
34     artifact:ArtifactModel;
35     component:Component;
36     instance:boolean;
37     download:Function;
38     showLoader:boolean;
39     downloadIconClass:string;
40     updateDownloadIcon:Function;
41 }
42
43 export class DownloadArtifactDirective implements ng.IDirective {
44
45     constructor(private $window:any, private cacheService:CacheService, private EventListenerService:EventListenerService, private fileUtils:FileUtils) {
46     }
47
48     scope = {
49         artifact: '=',
50         component: '=',
51         instance: '=',
52         showLoader: '=',
53         downloadIconClass: '@'
54     };
55     restrict = 'EA';
56
57     link = (scope:IDownloadArtifactScope, element:any) => {
58         scope.$window = this.$window;
59
60         element.on("click", function () {
61             scope.download(scope.artifact);
62         });
63
64
65         let initDownloadLoader = ()=> {
66             //if the artifact is in a middle of download progress register form callBack & change icon from download to loader
67             if (scope.showLoader && this.cacheService.get(scope.artifact.uniqueId)) {
68                 this.EventListenerService.registerObserverCallback(EVENTS.DOWNLOAD_ARTIFACT_FINISH_EVENT + scope.artifact.uniqueId, scope.updateDownloadIcon);
69                 window.setTimeout(():void => {
70                     if (this.cacheService.get(scope.artifact.uniqueId)) {
71                         element[0].className = DOWNLOAD_CSS_CLASSES.LOADER_ICON;
72                     }
73                 }, 1000);
74
75             }
76         };
77
78         let setDownloadedFileLoader = ()=> {
79             if (scope.showLoader) {
80                 //set in cache service thet the artifact is in download progress
81                 this.cacheService.set(scope.artifact.uniqueId, true);
82                 initDownloadLoader();
83             }
84         };
85
86         let removeDownloadedFileLoader = ()=> {
87             if (scope.showLoader) {
88                 this.cacheService.set(scope.artifact.uniqueId, false);
89                 this.EventListenerService.notifyObservers(EVENTS.DOWNLOAD_ARTIFACT_FINISH_EVENT + scope.artifact.uniqueId);
90             }
91         };
92
93
94         //replace the loader to download icon
95         scope.updateDownloadIcon = () => {
96             element[0].className = scope.downloadIconClass || DOWNLOAD_CSS_CLASSES.DOWNLOAD_ICON;
97         };
98
99
100         initDownloadLoader();
101
102         scope.download = (artifact:ArtifactModel):void => {
103
104             let onFaild = (response):void => {
105                 console.info('onFaild', response);
106                 removeDownloadedFileLoader();
107             };
108
109             let onSuccess = (data:IFileDownload):void => {
110                 downloadFile(data);
111                 removeDownloadedFileLoader();
112             };
113
114             setDownloadedFileLoader();
115
116             if (scope.instance) {
117                 scope.component.downloadInstanceArtifact(artifact.uniqueId).then(onSuccess, onFaild);
118             } else {
119                 scope.component.downloadArtifact(artifact.uniqueId).then(onSuccess, onFaild);
120             }
121         };
122
123         let downloadFile = (file:IFileDownload):void => {
124             if (file) {
125                 let blob = this.fileUtils.base64toBlob(file.base64Contents, '');
126                 let fileName = file.artifactName;
127                 this.fileUtils.downloadFile(blob, fileName);
128             }
129         };
130
131         element.on('$destroy', ()=> {
132             //remove listener of download event
133             if (scope.artifact && scope.artifact.uniqueId) {
134                 this.EventListenerService.unRegisterObserver(EVENTS.DOWNLOAD_ARTIFACT_FINISH_EVENT + scope.artifact.uniqueId);
135             }
136         });
137
138     };
139
140     public static factory = ($window:any, cacheService:CacheService, EventListenerService:EventListenerService, fileUtils:FileUtils)=> {
141         return new DownloadArtifactDirective($window, cacheService, EventListenerService, fileUtils);
142     };
143
144 }
145
146 DownloadArtifactDirective.factory.$inject = ['$window', 'Sdc.Services.CacheService', 'EventListenerService', 'FileUtils'];