2016-09-04 11:54:17 +02:00
|
|
|
function UploadQueue(options) {
|
|
|
|
this.queue = options.queue_element;
|
2016-09-04 13:14:06 +02:00
|
|
|
this.preview_area = options.preview_area;
|
|
|
|
this.upload_progress = [];
|
2016-09-04 14:15:38 +02:00
|
|
|
this.upload_url = options.upload_url;
|
2016-09-04 12:45:15 +02:00
|
|
|
this.submit = options.submit_button;
|
2016-09-04 11:54:17 +02:00
|
|
|
this.addEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
UploadQueue.prototype.addEvents = function() {
|
|
|
|
var that = this;
|
|
|
|
that.queue.addEventListener('change', function() {
|
2016-09-04 12:52:59 +02:00
|
|
|
that.showSpinner(that.queue, "Generating previews (not uploading yet!)");
|
2016-09-04 12:45:15 +02:00
|
|
|
that.clearPreviews();
|
2016-09-04 11:54:17 +02:00
|
|
|
for (var i = 0; i < that.queue.files.length; i++) {
|
2016-09-04 12:45:15 +02:00
|
|
|
var callback = (i !== that.queue.files.length - 1) ? null : function() {
|
|
|
|
that.hideSpinner();
|
2016-09-04 14:39:21 +02:00
|
|
|
that.submit.disabled = false;
|
2016-09-04 12:45:15 +02:00
|
|
|
};
|
2016-11-13 13:29:17 +01:00
|
|
|
that.addPreviewBoxForQueueSlot(i);
|
2016-09-04 12:45:15 +02:00
|
|
|
that.addPreviewForFile(that.queue.files[i], i, callback);
|
2016-09-04 11:54:17 +02:00
|
|
|
};
|
|
|
|
});
|
2016-09-04 12:45:15 +02:00
|
|
|
that.submit.addEventListener('click', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
that.process();
|
|
|
|
});
|
2016-09-04 14:39:21 +02:00
|
|
|
this.submit.disabled = true;
|
2016-09-04 11:54:17 +02:00
|
|
|
};
|
|
|
|
|
2016-09-04 12:45:15 +02:00
|
|
|
UploadQueue.prototype.clearPreviews = function() {
|
2016-09-04 13:14:06 +02:00
|
|
|
this.preview_area.innerHTML = '';
|
2016-09-04 14:39:21 +02:00
|
|
|
this.submit.disabled = true;
|
|
|
|
this.current_upload_index = -1;
|
2016-09-04 11:54:17 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 13:29:17 +01:00
|
|
|
UploadQueue.prototype.addPreviewBoxForQueueSlot = function(index) {
|
|
|
|
var preview_box = document.createElement('div');
|
|
|
|
preview_box.id = 'upload_preview_' + index;
|
|
|
|
this.preview_area.appendChild(preview_box);
|
|
|
|
};
|
|
|
|
|
2016-09-04 12:45:15 +02:00
|
|
|
UploadQueue.prototype.addPreviewForFile = function(file, index, callback) {
|
2016-09-04 11:54:17 +02:00
|
|
|
if (!file) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-08 20:35:26 +01:00
|
|
|
var preview = document.createElement('canvas');
|
2016-09-04 13:14:06 +02:00
|
|
|
preview.title = file.name;
|
2016-09-04 11:54:17 +02:00
|
|
|
|
2016-11-13 13:29:17 +01:00
|
|
|
var preview_box = document.getElementById('upload_preview_' + index);
|
2016-09-04 13:14:06 +02:00
|
|
|
preview_box.appendChild(preview);
|
|
|
|
|
2016-09-04 11:54:17 +02:00
|
|
|
var reader = new FileReader();
|
|
|
|
var that = this;
|
2016-11-13 13:29:17 +01:00
|
|
|
reader.addEventListener('load', function() {
|
2020-02-08 20:35:26 +01:00
|
|
|
var original = document.createElement('img');
|
|
|
|
original.src = reader.result;
|
|
|
|
|
|
|
|
original.addEventListener('load', function() {
|
|
|
|
// Preparation: make canvas size proportional to the original image.
|
|
|
|
preview.height = 150;
|
|
|
|
preview.width = preview.height * (original.width / original.height);
|
|
|
|
|
|
|
|
// First pass: resize to 50% on temp canvas.
|
|
|
|
var temp = document.createElement('canvas'),
|
|
|
|
tempCtx = temp.getContext('2d');
|
|
|
|
|
|
|
|
temp.width = original.width * 0.5;
|
|
|
|
temp.height = original.height * 0.5;
|
|
|
|
tempCtx.drawImage(original, 0, 0, temp.width, temp.height);
|
|
|
|
|
|
|
|
// Second pass: resize again on temp canvas.
|
|
|
|
tempCtx.drawImage(temp, 0, 0, temp.width * 0.5, temp.height * 0.5);
|
|
|
|
|
|
|
|
// Final pass: resize to desired size on preview canvas.
|
|
|
|
var context = preview.getContext('2d');
|
|
|
|
context.drawImage(temp, 0, 0, temp.width * 0.5, temp.height * 0.5,
|
|
|
|
0, 0, preview.width, preview.height);
|
|
|
|
|
|
|
|
if (callback) {
|
2016-09-04 12:45:15 +02:00
|
|
|
callback();
|
2020-02-08 20:35:26 +01:00
|
|
|
}
|
|
|
|
});
|
2016-11-13 13:29:17 +01:00
|
|
|
}, false);
|
2016-09-04 11:54:17 +02:00
|
|
|
reader.readAsDataURL(file);
|
|
|
|
};
|
2016-09-04 12:45:15 +02:00
|
|
|
|
|
|
|
UploadQueue.prototype.process = function() {
|
2016-09-04 13:14:06 +02:00
|
|
|
this.showSpinner(this.submit, "Preparing to upload files...");
|
2016-09-04 14:39:21 +02:00
|
|
|
if (this.queue.files.length > 0) {
|
|
|
|
this.submit.disabled = true;
|
|
|
|
this.nextFile();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
UploadQueue.prototype.nextFile = function() {
|
2016-09-04 13:14:06 +02:00
|
|
|
var files = this.queue.files;
|
2016-09-04 14:39:21 +02:00
|
|
|
var i = ++this.current_upload_index;
|
|
|
|
if (i === files.length) {
|
|
|
|
this.hideSpinner();
|
|
|
|
} else {
|
2016-09-04 13:14:06 +02:00
|
|
|
this.setSpinnerLabel("Uploading file " + (i + 1) + " out of " + files.length);
|
2016-09-04 14:39:21 +02:00
|
|
|
this.sendFile(files[i], i, function() {
|
|
|
|
this.nextFile();
|
|
|
|
});
|
|
|
|
}
|
2016-09-04 13:14:06 +02:00
|
|
|
};
|
|
|
|
|
2016-09-04 14:15:38 +02:00
|
|
|
UploadQueue.prototype.sendFile = function(file, index, callback) {
|
|
|
|
// Prepare the request.
|
|
|
|
var that = this;
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.addEventListener('error', function(event) {
|
|
|
|
that.updateProgress(index, -1);
|
|
|
|
});
|
|
|
|
request.addEventListener('progress', function(event) {
|
|
|
|
that.updateProgress(index, event.loaded / event.total);
|
|
|
|
});
|
|
|
|
request.addEventListener('load', function(event) {
|
|
|
|
that.updateProgress(index, 1);
|
|
|
|
if (request.responseText !== null && request.status === 200) {
|
|
|
|
var obj = JSON.parse(request.responseText);
|
|
|
|
if (obj.error) {
|
|
|
|
alert(obj.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (callback) {
|
|
|
|
callback.call(that, obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var data = new FormData();
|
|
|
|
data.append('uploads', file, file.name);
|
|
|
|
|
|
|
|
request.open('POST', this.upload_url, true);
|
|
|
|
request.send(data);
|
|
|
|
};
|
|
|
|
|
2016-09-04 13:14:06 +02:00
|
|
|
UploadQueue.prototype.addProgressBar = function(index) {
|
|
|
|
if (index in this.upload_progress) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var progress_container = document.createElement('div');
|
|
|
|
progress_container.className = 'progress';
|
|
|
|
|
|
|
|
var progress = document.createElement('div');
|
|
|
|
progress_container.appendChild(progress);
|
|
|
|
|
|
|
|
var preview_box = document.getElementById('upload_preview_' + index);
|
|
|
|
preview_box.appendChild(progress_container);
|
|
|
|
|
|
|
|
this.upload_progress[index] = progress;
|
|
|
|
};
|
|
|
|
|
|
|
|
UploadQueue.prototype.updateProgress = function(index, progress) {
|
|
|
|
if (!(index in this.upload_progress)) {
|
|
|
|
this.addProgressBar(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
var bar = this.upload_progress[index];
|
2016-09-04 14:15:38 +02:00
|
|
|
|
|
|
|
if (progress >= 0) {
|
|
|
|
bar.style.width = Math.ceil(progress * 100) + '%';
|
|
|
|
} else {
|
|
|
|
bar.style.width = "";
|
|
|
|
if (progress === -1) {
|
|
|
|
bar.className = "error";
|
|
|
|
}
|
|
|
|
}
|
2016-09-04 12:45:15 +02:00
|
|
|
};
|
|
|
|
|
2016-09-04 12:52:59 +02:00
|
|
|
UploadQueue.prototype.showSpinner = function(sibling, label) {
|
2016-09-04 12:45:15 +02:00
|
|
|
if (this.spinner) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.spinner = document.createElement('div');
|
|
|
|
this.spinner.className = 'spinner';
|
|
|
|
sibling.parentNode.appendChild(this.spinner);
|
2016-09-04 12:52:59 +02:00
|
|
|
|
|
|
|
if (label) {
|
|
|
|
this.spinner_label = document.createElement('span');
|
|
|
|
this.spinner_label.className = 'spinner_label';
|
|
|
|
this.spinner_label.innerHTML = label;
|
|
|
|
sibling.parentNode.appendChild(this.spinner_label);
|
|
|
|
}
|
2016-09-04 12:45:15 +02:00
|
|
|
};
|
|
|
|
|
2016-09-04 13:14:06 +02:00
|
|
|
UploadQueue.prototype.setSpinnerLabel = function(label) {
|
|
|
|
if (this.spinner_label) {
|
|
|
|
this.spinner_label.innerHTML = label;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 12:45:15 +02:00
|
|
|
UploadQueue.prototype.hideSpinner = function() {
|
2016-09-04 12:52:59 +02:00
|
|
|
if (this.spinner) {
|
|
|
|
this.spinner.parentNode.removeChild(this.spinner);
|
|
|
|
this.spinner = null;
|
|
|
|
}
|
|
|
|
if (this.spinner_label) {
|
|
|
|
this.spinner_label.parentNode.removeChild(this.spinner_label);
|
|
|
|
this.spinner_label = null;
|
|
|
|
}
|
2016-09-04 12:45:15 +02:00
|
|
|
};
|