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