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=========================================================
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";
27 export class DOWNLOAD_CSS_CLASSES {
28 static DOWNLOAD_ICON = "table-download-btn tosca";
29 static LOADER_ICON = "tlv-loader small loader";
32 export interface IDownloadArtifactScope extends ng.IScope {
34 artifact:ArtifactModel;
39 downloadIconClass:string;
40 updateDownloadIcon:Function;
43 export class DownloadArtifactDirective implements ng.IDirective {
45 constructor(private $window:any, private cacheService:CacheService, private EventListenerService:EventListenerService, private fileUtils:FileUtils) {
53 downloadIconClass: '@'
57 link = (scope:IDownloadArtifactScope, element:any) => {
58 scope.$window = this.$window;
60 element.on("click", function () {
61 scope.download(scope.artifact);
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;
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);
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);
94 //replace the loader to download icon
95 scope.updateDownloadIcon = () => {
96 element[0].className = scope.downloadIconClass || DOWNLOAD_CSS_CLASSES.DOWNLOAD_ICON;
100 initDownloadLoader();
102 scope.download = (artifact:ArtifactModel):void => {
104 let onFaild = (response):void => {
105 console.info('onFaild', response);
106 removeDownloadedFileLoader();
109 let onSuccess = (data:IFileDownload):void => {
111 removeDownloadedFileLoader();
114 setDownloadedFileLoader();
116 if (scope.instance) {
117 scope.component.downloadInstanceArtifact(artifact.uniqueId).then(onSuccess, onFaild);
119 scope.component.downloadArtifact(artifact.uniqueId).then(onSuccess, onFaild);
123 let downloadFile = (file:IFileDownload):void => {
125 let blob = this.fileUtils.base64toBlob(file.base64Contents, '');
126 let fileName = file.artifactName;
127 this.fileUtils.downloadFile(blob, fileName);
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);
140 public static factory = ($window:any, cacheService:CacheService, EventListenerService:EventListenerService, fileUtils:FileUtils)=> {
141 return new DownloadArtifactDirective($window, cacheService, EventListenerService, fileUtils);
146 DownloadArtifactDirective.factory.$inject = ['$window', 'Sdc.Services.CacheService', 'EventListenerService', 'FileUtils'];