From 7f5ce1820de1c039f067e8cbd813dc1aad9ac689 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sun, 13 Nov 2016 13:29:17 +0100 Subject: [PATCH] Split preview box creation from preview generation. --- public/js/upload_queue.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/public/js/upload_queue.js b/public/js/upload_queue.js index 224408b..dcec335 100644 --- a/public/js/upload_queue.js +++ b/public/js/upload_queue.js @@ -17,6 +17,7 @@ UploadQueue.prototype.addEvents = function() { that.hideSpinner(); that.submit.disabled = false; }; + that.addPreviewBoxForQueueSlot(i); that.addPreviewForFile(that.queue.files[i], i, callback); }; }); @@ -33,6 +34,12 @@ UploadQueue.prototype.clearPreviews = function() { this.current_upload_index = -1; } +UploadQueue.prototype.addPreviewBoxForQueueSlot = function(index) { + var preview_box = document.createElement('div'); + preview_box.id = 'upload_preview_' + index; + this.preview_area.appendChild(preview_box); +}; + UploadQueue.prototype.addPreviewForFile = function(file, index, callback) { if (!file) { return false; @@ -42,30 +49,19 @@ UploadQueue.prototype.addPreviewForFile = function(file, index, callback) { preview.title = file.name; preview.style.maxHeight = '150px'; - var preview_box = document.createElement('div'); - preview_box.id = 'upload_preview_' + index; + var preview_box = document.getElementById('upload_preview_' + index); preview_box.appendChild(preview); var reader = new FileReader(); var that = this; - var appendMe = function() { + reader.addEventListener('load', function() { preview.src = reader.result; if (callback) { preview.addEventListener('load', function() { callback(); }); } - that.preview_area.appendChild(preview_box); - }; - var waitForMe = function() { - var previews = that.preview_area.childNodes; - if (previews.length === 0 || previews[previews.length - 1].id === 'upload_preview_' + (index - 1)) { - appendMe(); - } else { - setTimeout(waitForMe, 10); - } - }; - reader.addEventListener('load', waitForMe, false); + }, false); reader.readAsDataURL(file); };