diff options
author | Michael Lando <ml636r@att.com> | 2017-06-09 03:19:04 +0300 |
---|---|---|
committer | Michael Lando <ml636r@att.com> | 2017-06-09 03:19:04 +0300 |
commit | ed64b5edff15e702493df21aa3230b81593e6133 (patch) | |
tree | a4cb01fdaccc34930a8db403a3097c0d1e40914b /catalog-ui/src/app/directives/download-artifact/download-artifact.ts | |
parent | 280f8015d06af1f41a3ef12e8300801c7a5e0d54 (diff) |
[SDC-29] catalog 1707 rebase commit.
Change-Id: I43c3dc5cf44abf5da817649bc738938a3e8388c1
Signed-off-by: Michael Lando <ml636r@att.com>
Diffstat (limited to 'catalog-ui/src/app/directives/download-artifact/download-artifact.ts')
-rw-r--r-- | catalog-ui/src/app/directives/download-artifact/download-artifact.ts | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/catalog-ui/src/app/directives/download-artifact/download-artifact.ts b/catalog-ui/src/app/directives/download-artifact/download-artifact.ts new file mode 100644 index 0000000000..7c817935cf --- /dev/null +++ b/catalog-ui/src/app/directives/download-artifact/download-artifact.ts @@ -0,0 +1,125 @@ +'use strict'; +import {IFileDownload, Component, ArtifactModel} from "app/models"; +import {EventListenerService, CacheService} from "app/services"; +import {EVENTS, FileUtils} from "app/utils"; + +export class DOWNLOAD_CSS_CLASSES { + static DOWNLOAD_ICON = "table-download-btn tosca"; + static LOADER_ICON = "tlv-loader small loader"; +} + +export interface IDownloadArtifactScope extends ng.IScope { + $window:any; + artifact:ArtifactModel; + component:Component; + instance:boolean; + download:Function; + showLoader:boolean; + downloadIconClass:string; + updateDownloadIcon:Function; +} + +export class DownloadArtifactDirective implements ng.IDirective { + + constructor(private $window:any, private cacheService:CacheService, private EventListenerService:EventListenerService, private fileUtils:FileUtils) { + } + + scope = { + artifact: '=', + component: '=', + instance: '=', + showLoader: '=', + downloadIconClass: '@' + }; + restrict = 'EA'; + + link = (scope:IDownloadArtifactScope, element:any) => { + scope.$window = this.$window; + + element.on("click", function () { + scope.download(scope.artifact); + }); + + + let initDownloadLoader = ()=> { + //if the artifact is in a middle of download progress register form callBack & change icon from download to loader + if (scope.showLoader && this.cacheService.get(scope.artifact.uniqueId)) { + this.EventListenerService.registerObserverCallback(EVENTS.DOWNLOAD_ARTIFACT_FINISH_EVENT + scope.artifact.uniqueId, scope.updateDownloadIcon); + window.setTimeout(():void => { + if (this.cacheService.get(scope.artifact.uniqueId)) { + element[0].className = DOWNLOAD_CSS_CLASSES.LOADER_ICON; + } + }, 1000); + + } + }; + + let setDownloadedFileLoader = ()=> { + if (scope.showLoader) { + //set in cache service thet the artifact is in download progress + this.cacheService.set(scope.artifact.uniqueId, true); + initDownloadLoader(); + } + }; + + let removeDownloadedFileLoader = ()=> { + if (scope.showLoader) { + this.cacheService.set(scope.artifact.uniqueId, false); + this.EventListenerService.notifyObservers(EVENTS.DOWNLOAD_ARTIFACT_FINISH_EVENT + scope.artifact.uniqueId); + } + }; + + + //replace the loader to download icon + scope.updateDownloadIcon = () => { + element[0].className = scope.downloadIconClass || DOWNLOAD_CSS_CLASSES.DOWNLOAD_ICON; + }; + + + initDownloadLoader(); + + scope.download = (artifact:ArtifactModel):void => { + + let onFaild = (response):void => { + console.info('onFaild', response); + removeDownloadedFileLoader(); + }; + + let onSuccess = (data:IFileDownload):void => { + downloadFile(data); + removeDownloadedFileLoader(); + }; + + setDownloadedFileLoader(); + + if (scope.instance) { + scope.component.downloadInstanceArtifact(artifact.uniqueId).then(onSuccess, onFaild); + } else { + scope.component.downloadArtifact(artifact.uniqueId).then(onSuccess, onFaild); + } + }; + + let downloadFile = (file:IFileDownload):void => { + if (file) { + let blob = this.fileUtils.base64toBlob(file.base64Contents, ''); + let fileName = file.artifactName; + this.fileUtils.downloadFile(blob, fileName); + } + }; + + element.on('$destroy', ()=> { + //remove listener of download event + if (scope.artifact && scope.artifact.uniqueId) { + this.EventListenerService.unRegisterObserver(EVENTS.DOWNLOAD_ARTIFACT_FINISH_EVENT + scope.artifact.uniqueId); + } + }); + + }; + + public static factory = ($window:any, cacheService:CacheService, EventListenerService:EventListenerService, fileUtils:FileUtils)=> { + return new DownloadArtifactDirective($window, cacheService, EventListenerService, fileUtils); + }; + +} + +DownloadArtifactDirective.factory.$inject = ['$window', 'Sdc.Services.CacheService', 'EventListenerService', 'FileUtils']; |