diff options
author | Morris Jobke <hey@morrisjobke.de> | 2015-07-14 17:18:31 +0200 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2015-07-15 11:14:20 +0200 |
commit | 43afa4c0e352fd5bbc46956b7ee62078ff5b6e45 (patch) | |
tree | c45b7bb46a01fef043ad43305f8a4eb79d8d338f | |
parent | 3f5aa27d494d56217980195534ee999b5e473ca5 (diff) | |
download | nextcloud-server-43afa4c0e352fd5bbc46956b7ee62078ff5b6e45.tar.gz nextcloud-server-43afa4c0e352fd5bbc46956b7ee62078ff5b6e45.zip |
refactor this to proper separation of concerns
-rw-r--r-- | apps/files/js/fileactions.js | 9 | ||||
-rw-r--r-- | apps/files/js/filelist.js | 7 | ||||
-rw-r--r-- | apps/files/js/files.js | 42 |
3 files changed, 39 insertions, 19 deletions
diff --git a/apps/files/js/fileactions.js b/apps/files/js/fileactions.js index 78f195be91f..6b8913e40c1 100644 --- a/apps/files/js/fileactions.js +++ b/apps/files/js/fileactions.js @@ -486,10 +486,13 @@ return; } - var randomString = OCA.Files.Files.handleDownloadSpinner(downloadFileaction); - if (url) { - OC.redirect(url + '&downloadStartSecret=' + randomString); + var disableLoadingState = function(){ + OCA.Files.Files.updateFileActionSpinner(downloadFileaction, false); + }; + + OCA.Files.Files.updateFileActionSpinner(downloadFileaction, true); + OCA.Files.Files.handleDownload(url, disableLoadingState); } }, t('files', 'Download')); } diff --git a/apps/files/js/filelist.js b/apps/files/js/filelist.js index c018777d8a8..2bc9233ba69 100644 --- a/apps/files/js/filelist.js +++ b/apps/files/js/filelist.js @@ -426,9 +426,12 @@ return; } - var randomString = OCA.Files.Files.handleDownloadSpinner(downloadFileaction); + var disableLoadingState = function(){ + OCA.Files.Files.updateFileActionSpinner(downloadFileaction, false); + }; - OC.redirect(this.getDownloadUrl(files, dir) + '&downloadStartSecret=' + randomString); + OCA.Files.Files.updateFileActionSpinner(downloadFileaction, true); + OCA.Files.Files.handleDownload(this.getDownloadUrl(files, dir), disableLoadingState); return false; }, diff --git a/apps/files/js/files.js b/apps/files/js/files.js index de372a71f74..3b7c892f5eb 100644 --- a/apps/files/js/files.js +++ b/apps/files/js/files.js @@ -279,34 +279,48 @@ }, /** - * Replaces the download icon with a loading spinner and returns token for the download check: + * Handles the download and calls the callback function once the download has started * - browser sends download request and adds parameter with a token * - server notices this token and adds a set cookie to the download response * - browser now adds this cookie for the domain - * - JS periodically checks for this cookie and then knows when the download has started to remove all the user feedback + * - JS periodically checks for this cookie and then knows when the download has started to call the callback * - * @param downloadButtonElement download fileaction - * @returns {string} random token that needs to be set as cookie + * @param {string} url download URL + * @param {function} callback function to call once the download has started */ - handleDownloadSpinner: function(downloadButtonElement) { - var randomString = Math.random().toString(36).substring(2), - icon = downloadButtonElement.find('img'), - sourceImage = icon.attr('src'), + handleDownload: function(url, callback) { + var randomToken = Math.random().toString(36).substring(2), checkForDownloadCookie = function() { - if (!OC.Util.isCookieSetToValue('ocDownloadStarted', randomString)){ + if (!OC.Util.isCookieSetToValue('ocDownloadStarted', randomToken)){ return false; } else { - icon.attr('src', sourceImage); - downloadButtonElement.removeClass('disabled'); + callback(); return true; } }; - downloadButtonElement.addClass('disabled'); - icon.attr('src', sourceImage.replace('actions/download.svg', 'loading-small.gif')); + OC.redirect(url + '&downloadStartSecret=' + randomToken); OC.Util.waitFor(checkForDownloadCookie, 500); + }, - return randomString; + /** + * Replaces the download icon with a loading spinner and vice versa + * - also adds the class disabled to the passed in element + * + * @param downloadButtonElement download fileaction + * @param {boolean} showIt whether to show the spinner(true) or to hide it(false) + */ + updateFileActionSpinner: function(downloadButtonElement, showIt) { + var icon = downloadButtonElement.find('img'), + sourceImage = icon.attr('src'); + + if(showIt) { + downloadButtonElement.addClass('disabled'); + icon.attr('src', sourceImage.replace('actions/download.svg', 'loading-small.gif')); + } else { + downloadButtonElement.removeClass('disabled'); + icon.attr('src', sourceImage.replace('loading-small.gif', 'actions/download.svg')); + } } }; |