summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2015-07-07 15:16:37 +0200
committerMorris Jobke <hey@morrisjobke.de>2015-07-07 15:16:37 +0200
commit3f5aa27d494d56217980195534ee999b5e473ca5 (patch)
tree619a559284179afbb680a6f08d8a6b3bf8855c94
parent3d8297c25473c5496fa7e27f600f1e23053df2fc (diff)
downloadnextcloud-server-3f5aa27d494d56217980195534ee999b5e473ca5.tar.gz
nextcloud-server-3f5aa27d494d56217980195534ee999b5e473ca5.zip
refactoring into proper methods
-rw-r--r--apps/files/js/fileactions.js28
-rw-r--r--apps/files/js/filelist.js30
-rw-r--r--apps/files/js/files.js33
-rw-r--r--core/js/js.js32
4 files changed, 65 insertions, 58 deletions
diff --git a/apps/files/js/fileactions.js b/apps/files/js/fileactions.js
index 6678c2d5b34..78f195be91f 100644
--- a/apps/files/js/fileactions.js
+++ b/apps/files/js/fileactions.js
@@ -486,36 +486,10 @@
return;
}
- downloadFileaction.addClass('disabled');
- var icon = downloadFileaction.find('img');
- var sourceImage = icon.attr('src');
- icon.attr('src', sourceImage.replace('actions/download.svg', 'loading-small.gif'));
-
- var randomString = Math.random().toString(36).substring(2);
-
- var isCookieSet = function(name, value) {
- var cookies = document.cookie.split(';');
- for (var i=0; i < cookies.length; i++) {
- var cookie = cookies[i].split('=');
- if (cookie[0].trim() === name && cookie[1].trim() === value) {
- return true;
- }
- }
- return false;
- };
-
- var checkForDownloadCookie = function() {
- if (!isCookieSet('ocDownloadStarted', randomString)){
- setTimeout(checkForDownloadCookie, 500);
- } else {
- icon.attr('src', sourceImage);
- downloadFileaction.removeClass('disabled');
- }
- };
+ var randomString = OCA.Files.Files.handleDownloadSpinner(downloadFileaction);
if (url) {
OC.redirect(url + '&downloadStartSecret=' + randomString);
- checkForDownloadCookie();
}
}, t('files', 'Download'));
}
diff --git a/apps/files/js/filelist.js b/apps/files/js/filelist.js
index 86c6e225115..c018777d8a8 100644
--- a/apps/files/js/filelist.js
+++ b/apps/files/js/filelist.js
@@ -426,37 +426,9 @@
return;
}
- downloadFileaction.addClass('disabled');
- var icon = downloadFileaction.find('img');
- var sourceImage = icon.attr('src');
- icon.attr('src', sourceImage.replace('actions/download.svg', 'loading-small.gif'));
-
- var randomString = Math.random().toString(36).substring(2);
-
- var isCookieSet = function(name, value) {
- var cookies = document.cookie.split(';');
- for (var i=0; i < cookies.length; i++) {
- var cookie = cookies[i].split('=');
- if (cookie[0].trim() === name && cookie[1].trim() === value) {
- return true;
- }
- }
- return false;
- };
-
- var checkForDownloadCookie = function() {
- console.log('check');
- if (!isCookieSet('ocDownloadStarted', randomString)){
- setTimeout(checkForDownloadCookie, 500);
- } else {
- console.log('boom');
- icon.attr('src', sourceImage);
- downloadFileaction.removeClass('disabled');
- }
- };
+ var randomString = OCA.Files.Files.handleDownloadSpinner(downloadFileaction);
OC.redirect(this.getDownloadUrl(files, dir) + '&downloadStartSecret=' + randomString);
- checkForDownloadCookie();
return false;
},
diff --git a/apps/files/js/files.js b/apps/files/js/files.js
index 44868e78bd0..de372a71f74 100644
--- a/apps/files/js/files.js
+++ b/apps/files/js/files.js
@@ -276,8 +276,39 @@
FileList.scrollTo(getURLParameter('scrollto'));
}
*/
+ },
+
+ /**
+ * Replaces the download icon with a loading spinner and returns token for the download check:
+ * - 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
+ *
+ * @param downloadButtonElement download fileaction
+ * @returns {string} random token that needs to be set as cookie
+ */
+ handleDownloadSpinner: function(downloadButtonElement) {
+ var randomString = Math.random().toString(36).substring(2),
+ icon = downloadButtonElement.find('img'),
+ sourceImage = icon.attr('src'),
+ checkForDownloadCookie = function() {
+ if (!OC.Util.isCookieSetToValue('ocDownloadStarted', randomString)){
+ return false;
+ } else {
+ icon.attr('src', sourceImage);
+ downloadButtonElement.removeClass('disabled');
+ return true;
+ }
+ };
+
+ downloadButtonElement.addClass('disabled');
+ icon.attr('src', sourceImage.replace('actions/download.svg', 'loading-small.gif'));
+ OC.Util.waitFor(checkForDownloadCookie, 500);
+
+ return randomString;
}
- }
+ };
Files._updateStorageStatisticsDebounced = _.debounce(Files._updateStorageStatistics, 250);
OCA.Files.Files = Files;
diff --git a/core/js/js.js b/core/js/js.js
index e0adc3591ac..cce5a47bdd3 100644
--- a/core/js/js.js
+++ b/core/js/js.js
@@ -1513,8 +1513,38 @@ OC.Util = {
}
}
return aa.length - bb.length;
+ },
+ /**
+ * Calls the callback in a given interval until it returns true
+ * @param {function} callback
+ * @param {integer} interval in milliseconds
+ */
+ waitFor: function(callback, interval) {
+ var internalCallback = function() {
+ if(callback() !== true) {
+ setTimeout(internalCallback, interval);
+ }
+ };
+
+ internalCallback();
+ },
+ /**
+ * Checks if a cookie with the given name is present and is set to the provided value.
+ * @param {string} name name of the cookie
+ * @param {string} value value of the cookie
+ * @return {boolean} true if the cookie with the given name has the given value
+ */
+ isCookieSetToValue: function(name, value) {
+ var cookies = document.cookie.split(';');
+ for (var i=0; i < cookies.length; i++) {
+ var cookie = cookies[i].split('=');
+ if (cookie[0].trim() === name && cookie[1].trim() === value) {
+ return true;
+ }
+ }
+ return false;
}
-}
+};
/**
* Utility class for the history API,