Split preview box creation from preview generation.

This commit is contained in:
Aaron van Geffen 2016-11-13 13:29:17 +01:00
parent 6c0f6b06e6
commit 7f5ce1820d

View File

@ -17,6 +17,7 @@ UploadQueue.prototype.addEvents = function() {
that.hideSpinner(); that.hideSpinner();
that.submit.disabled = false; that.submit.disabled = false;
}; };
that.addPreviewBoxForQueueSlot(i);
that.addPreviewForFile(that.queue.files[i], i, callback); that.addPreviewForFile(that.queue.files[i], i, callback);
}; };
}); });
@ -33,6 +34,12 @@ UploadQueue.prototype.clearPreviews = function() {
this.current_upload_index = -1; 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) { UploadQueue.prototype.addPreviewForFile = function(file, index, callback) {
if (!file) { if (!file) {
return false; return false;
@ -42,30 +49,19 @@ UploadQueue.prototype.addPreviewForFile = function(file, index, callback) {
preview.title = file.name; preview.title = file.name;
preview.style.maxHeight = '150px'; preview.style.maxHeight = '150px';
var preview_box = document.createElement('div'); var preview_box = document.getElementById('upload_preview_' + index);
preview_box.id = 'upload_preview_' + index;
preview_box.appendChild(preview); preview_box.appendChild(preview);
var reader = new FileReader(); var reader = new FileReader();
var that = this; var that = this;
var appendMe = function() { reader.addEventListener('load', function() {
preview.src = reader.result; preview.src = reader.result;
if (callback) { if (callback) {
preview.addEventListener('load', function() { preview.addEventListener('load', function() {
callback(); callback();
}); });
} }
that.preview_area.appendChild(preview_box); }, false);
};
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);
reader.readAsDataURL(file); reader.readAsDataURL(file);
}; };