Skip to content

Commit

Permalink
refactor this to proper separation of concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
MorrisJobke committed Jul 15, 2015
1 parent 3f5aa27 commit 43afa4c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
9 changes: 6 additions & 3 deletions apps/files/js/fileactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
7 changes: 5 additions & 2 deletions apps/files/js/filelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand Down
42 changes: 28 additions & 14 deletions apps/files/js/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
};

Expand Down

0 comments on commit 43afa4c

Please sign in to comment.