working UGC content upload with multi-upload support
This commit is contained in:
@@ -1,153 +1,9 @@
|
||||
// J621-style multi-upload for the gallery "add media" page. Files are
|
||||
// uploaded one at a time to /api/uploads/ (kind=media) and previewed; on
|
||||
// submit the pending uploads are moved into the project's gallery.
|
||||
// Gallery "add media" page: uses the shared uploads.js engine to upload files
|
||||
// to /api/uploads/ (kind=media). On submit the pending uploads are moved into
|
||||
// the project's gallery.
|
||||
(function () {
|
||||
const zone = document.querySelector('.upload-zone[data-upload="media"]');
|
||||
if (!zone) return;
|
||||
|
||||
const csrf = getCookie('csrftoken');
|
||||
const csrfHeader = csrf ? { 'X-CSRFToken': csrf } : {};
|
||||
const preview = document.getElementById('media-preview');
|
||||
const batch = document.getElementById('media-batch');
|
||||
const input = document.getElementById('media-input');
|
||||
const drop = zone.querySelector('.drop-zone');
|
||||
|
||||
const pending = JSON.parse(
|
||||
(document.getElementById('packs-pending-uploads') || { textContent: '[]' }).textContent
|
||||
);
|
||||
|
||||
pending.forEach(renderCard);
|
||||
|
||||
function renderCard(upload) {
|
||||
const card = document.createElement('div');
|
||||
card.className = 'file-preview-card';
|
||||
card.dataset.uuid = upload.uuid;
|
||||
|
||||
if ((upload.content_type || '').startsWith('image/')) {
|
||||
const img = document.createElement('img');
|
||||
img.src = upload.url;
|
||||
img.alt = upload.filename;
|
||||
card.appendChild(img);
|
||||
} else if ((upload.content_type || '').startsWith('video/')) {
|
||||
const video = document.createElement('video');
|
||||
video.src = upload.url;
|
||||
video.muted = true;
|
||||
video.autoplay = true;
|
||||
video.loop = true;
|
||||
card.appendChild(video);
|
||||
} else {
|
||||
const icon = document.createElement('div');
|
||||
icon.className = 'file-preview-icon';
|
||||
icon.innerHTML = '<i class="fas fa-file"></i>';
|
||||
card.appendChild(icon);
|
||||
}
|
||||
|
||||
const name = document.createElement('span');
|
||||
name.className = 'file-preview-name';
|
||||
name.textContent = upload.filename;
|
||||
card.appendChild(name);
|
||||
|
||||
const remove = document.createElement('button');
|
||||
remove.type = 'button';
|
||||
remove.className = 'file-preview-remove';
|
||||
remove.innerHTML = '<i class="fas fa-times"></i>';
|
||||
remove.addEventListener('click', () => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/api/uploads/' + upload.uuid + '/delete/');
|
||||
for (const k in csrfHeader) xhr.setRequestHeader(k, csrfHeader[k]);
|
||||
xhr.onload = () => { if (card.parentNode) card.parentNode.removeChild(card); };
|
||||
xhr.onerror = () => alert('Network error.');
|
||||
xhr.send();
|
||||
});
|
||||
card.appendChild(remove);
|
||||
preview.appendChild(card);
|
||||
}
|
||||
|
||||
function uploadFile(file) {
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
fd.append('kind', 'media');
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/api/uploads/');
|
||||
for (const k in csrfHeader) xhr.setRequestHeader(k, csrfHeader[k]);
|
||||
xhr.onload = function () {
|
||||
if (xhr.status !== 200) {
|
||||
try {
|
||||
alert(JSON.parse(xhr.responseText).error || 'Upload failed.');
|
||||
} catch (e) {
|
||||
alert('Upload failed.');
|
||||
}
|
||||
} else {
|
||||
renderCard(JSON.parse(xhr.responseText));
|
||||
}
|
||||
batchDone++;
|
||||
updateBatch();
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
alert('Network error during upload.');
|
||||
batchDone++;
|
||||
updateBatch();
|
||||
};
|
||||
xhr.send(fd);
|
||||
}
|
||||
|
||||
let batchTotal = 0;
|
||||
let batchDone = 0;
|
||||
function updateBatch() {
|
||||
if (batchTotal === 0) { batch.hidden = true; return; }
|
||||
batch.hidden = false;
|
||||
document.getElementById('media-batch-status').textContent =
|
||||
'Uploading ' + batchDone + ' of ' + batchTotal + ' files';
|
||||
if (batchDone >= batchTotal && batchTotal > 0) {
|
||||
setTimeout(() => { batch.hidden = true; batchTotal = 0; batchDone = 0; }, 2500);
|
||||
}
|
||||
}
|
||||
|
||||
function handleFiles(files) {
|
||||
batchTotal += files.length;
|
||||
updateBatch();
|
||||
files.forEach((file) => uploadFile(file));
|
||||
}
|
||||
|
||||
drop.addEventListener('click', () => input.click());
|
||||
drop.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); input.click(); }
|
||||
});
|
||||
['dragenter', 'dragover'].forEach((evt) => drop.addEventListener(evt, (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
drop.classList.add('dragover');
|
||||
}));
|
||||
['dragleave', 'drop'].forEach((evt) => drop.addEventListener(evt, (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
drop.classList.remove('dragover');
|
||||
}));
|
||||
drop.addEventListener('drop', (e) => {
|
||||
if (e.dataTransfer && e.dataTransfer.files.length) {
|
||||
handleFiles(Array.from(e.dataTransfer.files));
|
||||
}
|
||||
});
|
||||
input.addEventListener('change', () => {
|
||||
handleFiles(Array.from(input.files));
|
||||
input.value = '';
|
||||
});
|
||||
|
||||
['dragover', 'drop'].forEach((evt) => window.addEventListener(evt, (e) => e.preventDefault()));
|
||||
|
||||
function getCookie(name) {
|
||||
let cookieValue = null;
|
||||
if (document.cookie && document.cookie !== '') {
|
||||
const cookies = document.cookie.split(';');
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
const cookie = cookies[i].trim();
|
||||
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
||||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return cookieValue;
|
||||
}
|
||||
PacksUploads.init({ pending: pending });
|
||||
})();
|
||||
|
||||
+20
-200
@@ -1,197 +1,17 @@
|
||||
// J621-style upload + real-time autosave for the "create project" form.
|
||||
// Each selected file is uploaded individually (XHR + FormData) to /api/uploads/
|
||||
// and stored under media/uploads/user_<id>/. Typed fields autosave to the
|
||||
// ProjectDraft via /api/draft/ (debounced, no explicit Save Draft button).
|
||||
// "Create project" page: autosaves typed fields to the ProjectDraft via
|
||||
// /api/draft/ (debounced, no explicit Save Draft button). File uploads are
|
||||
// handled by the shared uploads.js engine (per-file XHR to /api/uploads/).
|
||||
(function () {
|
||||
const form = document.getElementById('project-form');
|
||||
if (!form) return;
|
||||
|
||||
const csrf = getCookie('csrftoken');
|
||||
const csrfHeader = csrf ? { 'X-CSRFToken': csrf } : {};
|
||||
const savedUploads = JSON.parse(
|
||||
const pending = JSON.parse(
|
||||
(document.getElementById('packs-draft-uploads') || { textContent: '[]' }).textContent
|
||||
);
|
||||
|
||||
// ----- Multi-upload (per-file XHR, thumbnails) -----
|
||||
|
||||
const zones = {};
|
||||
document.querySelectorAll('.upload-zone').forEach((zone) => {
|
||||
const kind = zone.dataset.upload;
|
||||
zones[kind] = {
|
||||
zone,
|
||||
drop: zone.querySelector('.drop-zone'),
|
||||
input: zone.querySelector('input[type=file]'),
|
||||
preview: zone.querySelector('.upload-preview, #media-preview'),
|
||||
};
|
||||
if (kind === 'media') zones[kind].batch = document.getElementById('media-batch');
|
||||
});
|
||||
|
||||
// Re-render the pending uploads that survived from a previous session.
|
||||
savedUploads.forEach((u) => {
|
||||
const z = zones[u.kind];
|
||||
if (z && !z.current) renderUpload(u, z);
|
||||
});
|
||||
|
||||
function renderUpload(upload, z) {
|
||||
if (z.kind === 'thumbnail' || z.kind === 'version') {
|
||||
z.preview.innerHTML = '';
|
||||
z.preview.appendChild(previewCard(upload));
|
||||
z.current = upload;
|
||||
} else {
|
||||
z.preview.appendChild(previewCard(upload));
|
||||
}
|
||||
}
|
||||
|
||||
function previewCard(upload) {
|
||||
const card = document.createElement('div');
|
||||
card.className = 'file-preview-card';
|
||||
card.dataset.uuid = upload.uuid;
|
||||
|
||||
if ((upload.content_type || '').startsWith('image/')) {
|
||||
const img = document.createElement('img');
|
||||
img.src = upload.url;
|
||||
img.alt = upload.filename;
|
||||
card.appendChild(img);
|
||||
} else if ((upload.content_type || '').startsWith('video/')) {
|
||||
const video = document.createElement('video');
|
||||
video.src = upload.url;
|
||||
video.muted = true;
|
||||
video.autoplay = true;
|
||||
video.loop = true;
|
||||
card.appendChild(video);
|
||||
} else {
|
||||
const icon = document.createElement('div');
|
||||
icon.className = 'file-preview-icon';
|
||||
icon.innerHTML = '<i class="fas fa-file-archive"></i>';
|
||||
card.appendChild(icon);
|
||||
}
|
||||
|
||||
const name = document.createElement('span');
|
||||
name.className = 'file-preview-name';
|
||||
name.textContent = upload.filename;
|
||||
card.appendChild(name);
|
||||
|
||||
const remove = document.createElement('button');
|
||||
remove.type = 'button';
|
||||
remove.className = 'file-preview-remove';
|
||||
remove.innerHTML = '<i class="fas fa-times"></i>';
|
||||
remove.addEventListener('click', () => deleteUpload(upload.uuid, card));
|
||||
card.appendChild(remove);
|
||||
return card;
|
||||
}
|
||||
|
||||
function uploadFile(file, kind, z) {
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
fd.append('kind', kind);
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/api/uploads/');
|
||||
for (const k in csrfHeader) xhr.setRequestHeader(k, csrfHeader[k]);
|
||||
|
||||
xhr.onload = function () {
|
||||
z.busy = false;
|
||||
if (xhr.status !== 200) {
|
||||
try {
|
||||
alert(JSON.parse(xhr.responseText).error || 'Upload failed.');
|
||||
} catch (e) {
|
||||
alert('Upload failed.');
|
||||
}
|
||||
return;
|
||||
}
|
||||
const data = JSON.parse(xhr.responseText);
|
||||
renderUpload(data, z);
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
z.busy = false;
|
||||
alert('Network error during upload.');
|
||||
};
|
||||
xhr.send(fd);
|
||||
}
|
||||
|
||||
function deleteUpload(uuid, card) {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/api/uploads/' + uuid + '/delete/');
|
||||
for (const k in csrfHeader) xhr.setRequestHeader(k, csrfHeader[k]);
|
||||
xhr.onload = function () {
|
||||
if (card && card.parentNode) card.parentNode.removeChild(card);
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
alert('Network error.');
|
||||
};
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
Object.keys(zones).forEach((kind) => {
|
||||
const z = zones[kind];
|
||||
z.drop.addEventListener('click', () => z.input.click());
|
||||
z.drop.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); z.input.click(); }
|
||||
});
|
||||
|
||||
['dragenter', 'dragover'].forEach((evt) => z.drop.addEventListener(evt, (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
z.drop.classList.add('dragover');
|
||||
}));
|
||||
['dragleave', 'drop'].forEach((evt) => z.drop.addEventListener(evt, (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
z.drop.classList.remove('dragover');
|
||||
}));
|
||||
z.drop.addEventListener('drop', (e) => {
|
||||
if (e.dataTransfer && e.dataTransfer.files.length) {
|
||||
handleFiles(Array.from(e.dataTransfer.files), kind, z);
|
||||
}
|
||||
});
|
||||
z.input.addEventListener('change', () => {
|
||||
handleFiles(Array.from(z.input.files), kind, z);
|
||||
z.input.value = '';
|
||||
});
|
||||
});
|
||||
|
||||
// Prevent the browser opening files dropped outside a zone.
|
||||
['dragover', 'drop'].forEach((evt) => window.addEventListener(evt, (e) => e.preventDefault()));
|
||||
|
||||
let batchTotal = 0;
|
||||
let batchDone = 0;
|
||||
|
||||
function handleFiles(files, kind, z) {
|
||||
// Thumbnail/version are single slots: take one file, replace any
|
||||
// current upload, and ignore further drops while one is in flight.
|
||||
if (kind === 'thumbnail' || kind === 'version') {
|
||||
if (z.busy) return;
|
||||
const file = files[0];
|
||||
if (z.current) deleteUpload(z.current.uuid, null);
|
||||
z.busy = true;
|
||||
uploadFile(file, kind, z);
|
||||
return;
|
||||
}
|
||||
|
||||
batchTotal += files.length;
|
||||
updateBatch();
|
||||
files.forEach((file) => uploadFile(file, kind, z));
|
||||
}
|
||||
|
||||
function updateBatch() {
|
||||
const z = zones.media;
|
||||
if (!z || !z.batch) return;
|
||||
if (batchTotal === 0) { z.batch.hidden = true; return; }
|
||||
z.batch.hidden = false;
|
||||
document.getElementById('media-batch-status').textContent =
|
||||
'Uploading ' + batchDone + ' of ' + batchTotal + ' files';
|
||||
if (batchDone >= batchTotal && batchTotal > 0) {
|
||||
setTimeout(() => {
|
||||
z.batch.hidden = true;
|
||||
batchTotal = 0;
|
||||
batchDone = 0;
|
||||
}, 2500);
|
||||
}
|
||||
}
|
||||
|
||||
// ----- Real-time draft autosave -----
|
||||
|
||||
const autosaveStatus = document.getElementById('autosave-status');
|
||||
const csrf = getCookie('csrftoken');
|
||||
const csrfHeader = csrf ? { 'X-CSRFToken': csrf } : {};
|
||||
const FIELDS = ['title', 'summary', 'category', 'description', 'caption', 'version_name', 'changelog'];
|
||||
|
||||
function setStatus(text, state) {
|
||||
@@ -200,7 +20,6 @@
|
||||
}
|
||||
|
||||
let saveTimer = null;
|
||||
let lastSavedAt = null;
|
||||
|
||||
function scheduleSave() {
|
||||
setStatus('<i class="fas fa-circle-notch fa-spin"></i> Saving…', 'saving');
|
||||
@@ -223,8 +42,6 @@
|
||||
for (const k in csrfHeader) xhr.setRequestHeader(k, csrfHeader[k]);
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
const r = JSON.parse(xhr.responseText);
|
||||
lastSavedAt = r.updated_at;
|
||||
setStatus('<i class="fas fa-check"></i> Saved just now', 'saved');
|
||||
} else {
|
||||
setStatus('<i class="fas fa-exclamation-triangle"></i> Save failed', 'error');
|
||||
@@ -236,18 +53,21 @@
|
||||
xhr.send(JSON.stringify({ data }));
|
||||
}
|
||||
|
||||
FIELDS.forEach((name) => {
|
||||
const el = document.getElementById('id_' + name);
|
||||
if (el) {
|
||||
el.addEventListener('input', scheduleSave);
|
||||
if (el.tagName === 'SELECT') el.addEventListener('change', scheduleSave);
|
||||
}
|
||||
});
|
||||
document.addEventListener('tags-changed', scheduleSave);
|
||||
function initAutosave() {
|
||||
FIELDS.forEach((name) => {
|
||||
const el = document.getElementById('id_' + name);
|
||||
if (el) {
|
||||
el.addEventListener('input', scheduleSave);
|
||||
if (el.tagName === 'SELECT') el.addEventListener('change', scheduleSave);
|
||||
}
|
||||
});
|
||||
document.addEventListener('tags-changed', scheduleSave);
|
||||
|
||||
// Autosave is always live; indicate it once the page settles.
|
||||
setStatus('<i class="fas fa-cloud-upload-alt"></i> Autosave on', 'idle');
|
||||
setTimeout(() => setStatus('<i class="fas fa-check"></i> Saved just now', 'saved'), 2000);
|
||||
setStatus('<i class="fas fa-cloud-upload-alt"></i> Autosave on', 'idle');
|
||||
setTimeout(() => setStatus('<i class="fas fa-check"></i> Saved just now', 'saved'), 2000);
|
||||
}
|
||||
|
||||
PacksUploads.init({ pending: pending, onReady: initAutosave });
|
||||
|
||||
function getCookie(name) {
|
||||
let cookieValue = null;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Edit project" page: uses the shared uploads.js engine for the thumbnail
|
||||
// (single slot). The existing thumbnail is shown as a read-only preview until
|
||||
// a replacement is uploaded.
|
||||
(function () {
|
||||
const pending = JSON.parse(
|
||||
(document.getElementById('packs-pending-uploads') || { textContent: '[]' }).textContent
|
||||
);
|
||||
PacksUploads.init({ pending: pending });
|
||||
})();
|
||||
@@ -9,6 +9,13 @@
|
||||
const chips = document.getElementById('tag-chips');
|
||||
const hidden = document.getElementById('id_tags');
|
||||
|
||||
// category slug -> color, provided by the server (json_script).
|
||||
const colorMap = {};
|
||||
try {
|
||||
const el = document.getElementById('packs-tag-categories');
|
||||
if (el) JSON.parse(el.textContent).forEach(c => { colorMap[c.slug] = c.color; });
|
||||
} catch (e) { /* ignore */ }
|
||||
|
||||
const tags = new Map(); // "category:name" -> {category, name, color}
|
||||
|
||||
function render() {
|
||||
@@ -23,6 +30,8 @@
|
||||
x.type = 'button';
|
||||
x.className = 'tag-chip-remove';
|
||||
x.textContent = '×';
|
||||
x.title = 'Remove tag';
|
||||
x.setAttribute('aria-label', 'Remove tag ' + tag.name);
|
||||
x.addEventListener('click', () => { tags.delete(tag.category + ':' + tag.name); render(); });
|
||||
chip.appendChild(x);
|
||||
chips.appendChild(chip);
|
||||
@@ -33,7 +42,8 @@
|
||||
|
||||
function addTag(category, name, color) {
|
||||
const canonical = category.toLowerCase();
|
||||
tags.set(canonical + ':' + name, { category: canonical, name, color: color || '#666666' });
|
||||
color = color || colorMap[canonical] || '#666666';
|
||||
tags.set(canonical + ':' + name, { category: canonical, name, color });
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
// Shared J621-style uploader for UGC pages.
|
||||
// Wires every `.upload-zone[data-upload]` on the page: dropzone click/drag,
|
||||
// per-file XHR to /api/uploads/, preview cards, removal, batch progress.
|
||||
// Single slots (thumbnail/version) hold one file; media is multi.
|
||||
(function () {
|
||||
function getCookie(name) {
|
||||
let cookieValue = null;
|
||||
if (document.cookie && document.cookie !== '') {
|
||||
const cookies = document.cookie.split(';');
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
const cookie = cookies[i].trim();
|
||||
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
||||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return cookieValue;
|
||||
}
|
||||
|
||||
function init(options) {
|
||||
const opts = options || {};
|
||||
const pending = opts.pending || [];
|
||||
const csrf = getCookie('csrftoken');
|
||||
const csrfHeader = csrf ? { 'X-CSRFToken': csrf } : {};
|
||||
|
||||
const zones = {};
|
||||
document.querySelectorAll('.upload-zone[data-upload]').forEach((zone) => {
|
||||
const kind = zone.dataset.upload;
|
||||
zones[kind] = {
|
||||
zone,
|
||||
kind,
|
||||
single: kind === 'thumbnail' || kind === 'version',
|
||||
drop: zone.querySelector('.drop-zone'),
|
||||
input: zone.querySelector('input[type=file]'),
|
||||
preview: zone.querySelector('.upload-preview, #media-preview'),
|
||||
batch: zone.querySelector('.upload-batch'),
|
||||
current: null,
|
||||
busy: false,
|
||||
batchTotal: 0,
|
||||
batchDone: 0,
|
||||
};
|
||||
});
|
||||
|
||||
// Render uploads that survived from a previous session.
|
||||
pending.forEach((u) => {
|
||||
const z = zones[u.kind];
|
||||
if (z) addCard(u, z);
|
||||
});
|
||||
|
||||
function previewCard(upload, z) {
|
||||
const card = document.createElement('div');
|
||||
card.className = 'file-preview-card';
|
||||
card.dataset.uuid = upload.uuid;
|
||||
|
||||
const ct = upload.content_type || '';
|
||||
if (ct.startsWith('image/')) {
|
||||
const img = document.createElement('img');
|
||||
img.src = upload.url;
|
||||
img.alt = upload.filename || '';
|
||||
card.appendChild(img);
|
||||
} else if (ct.startsWith('video/')) {
|
||||
const video = document.createElement('video');
|
||||
video.src = upload.url;
|
||||
video.muted = true;
|
||||
video.autoplay = true;
|
||||
video.loop = true;
|
||||
card.appendChild(video);
|
||||
} else {
|
||||
const icon = document.createElement('div');
|
||||
icon.className = 'file-preview-icon';
|
||||
icon.innerHTML = '<i class="fas fa-file-archive"></i>';
|
||||
card.appendChild(icon);
|
||||
}
|
||||
|
||||
const name = document.createElement('span');
|
||||
name.className = 'file-preview-name';
|
||||
name.textContent = upload.filename || '';
|
||||
card.appendChild(name);
|
||||
|
||||
if (upload.removable !== false) {
|
||||
const remove = document.createElement('button');
|
||||
remove.type = 'button';
|
||||
remove.className = 'file-preview-remove';
|
||||
remove.innerHTML = '<i class="fas fa-times"></i>';
|
||||
remove.addEventListener('click', () => deleteUpload(upload.uuid, card, z));
|
||||
card.appendChild(remove);
|
||||
}
|
||||
return card;
|
||||
}
|
||||
|
||||
function addCard(upload, z) {
|
||||
if (z.single) {
|
||||
z.preview.innerHTML = '';
|
||||
z.preview.appendChild(previewCard(upload, z));
|
||||
if (upload.removable !== false) z.current = upload;
|
||||
} else {
|
||||
z.preview.appendChild(previewCard(upload, z));
|
||||
}
|
||||
}
|
||||
|
||||
function uploadFile(file, z) {
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
fd.append('kind', z.kind);
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/api/uploads/');
|
||||
for (const k in csrfHeader) xhr.setRequestHeader(k, csrfHeader[k]);
|
||||
|
||||
xhr.onload = function () {
|
||||
z.busy = false;
|
||||
if (!z.single) { z.batchDone++; updateBatch(z); }
|
||||
if (xhr.status !== 200) {
|
||||
try { alert(JSON.parse(xhr.responseText).error || 'Upload failed.'); }
|
||||
catch (e) { alert('Upload failed.'); }
|
||||
return;
|
||||
}
|
||||
addCard(JSON.parse(xhr.responseText), z);
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
z.busy = false;
|
||||
if (!z.single) { z.batchDone++; updateBatch(z); }
|
||||
alert('Network error during upload.');
|
||||
};
|
||||
xhr.send(fd);
|
||||
}
|
||||
|
||||
function deleteUpload(uuid, card, z) {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/api/uploads/' + uuid + '/delete/');
|
||||
for (const k in csrfHeader) xhr.setRequestHeader(k, csrfHeader[k]);
|
||||
xhr.onload = function () {
|
||||
if (card && card.parentNode) card.parentNode.removeChild(card);
|
||||
if (z && z.current && z.current.uuid === uuid) z.current = null;
|
||||
if (opts.onRemove) opts.onRemove(uuid);
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
alert('Network error.');
|
||||
};
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function handleFiles(files, z) {
|
||||
// Single slots: take one file, replace the current upload, and
|
||||
// ignore further drops while one is in flight.
|
||||
if (z.single) {
|
||||
if (z.busy) return;
|
||||
const file = files[0];
|
||||
if (z.current) deleteUpload(z.current.uuid, null, z);
|
||||
z.busy = true;
|
||||
uploadFile(file, z);
|
||||
return;
|
||||
}
|
||||
z.batchTotal += files.length;
|
||||
updateBatch(z);
|
||||
files.forEach((file) => uploadFile(file, z));
|
||||
}
|
||||
|
||||
function updateBatch(z) {
|
||||
if (!z.batch) return;
|
||||
if (z.batchTotal === 0) { z.batch.hidden = true; return; }
|
||||
z.batch.hidden = false;
|
||||
const status = z.batch.querySelector('.upload-batch-status');
|
||||
if (status) {
|
||||
status.textContent = 'Uploading ' + z.batchDone + ' of ' + z.batchTotal + ' files';
|
||||
}
|
||||
if (z.batchDone >= z.batchTotal && z.batchTotal > 0) {
|
||||
setTimeout(() => {
|
||||
z.batch.hidden = true;
|
||||
z.batchTotal = 0;
|
||||
z.batchDone = 0;
|
||||
}, 2500);
|
||||
}
|
||||
}
|
||||
|
||||
Object.keys(zones).forEach((kind) => {
|
||||
const z = zones[kind];
|
||||
if (!z.drop || !z.input) return;
|
||||
|
||||
z.drop.addEventListener('click', () => z.input.click());
|
||||
z.drop.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); z.input.click(); }
|
||||
});
|
||||
|
||||
['dragenter', 'dragover'].forEach((evt) => z.drop.addEventListener(evt, (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
z.drop.classList.add('dragover');
|
||||
}));
|
||||
['dragleave', 'drop'].forEach((evt) => z.drop.addEventListener(evt, (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
z.drop.classList.remove('dragover');
|
||||
}));
|
||||
z.drop.addEventListener('drop', (e) => {
|
||||
if (e.dataTransfer && e.dataTransfer.files.length) {
|
||||
handleFiles(Array.from(e.dataTransfer.files), z);
|
||||
}
|
||||
});
|
||||
z.input.addEventListener('change', () => {
|
||||
handleFiles(Array.from(z.input.files), z);
|
||||
z.input.value = '';
|
||||
});
|
||||
});
|
||||
|
||||
// Prevent the browser opening files dropped outside a zone.
|
||||
['dragover', 'drop'].forEach((evt) => window.addEventListener(evt, (e) => e.preventDefault()));
|
||||
|
||||
if (opts.onReady) opts.onReady();
|
||||
}
|
||||
|
||||
window.PacksUploads = { init: init };
|
||||
})();
|
||||
@@ -0,0 +1,8 @@
|
||||
// "New version" page: uses the shared uploads.js engine for the version file
|
||||
// (single slot). On submit the pending version temp is moved into the project.
|
||||
(function () {
|
||||
const pending = JSON.parse(
|
||||
(document.getElementById('packs-pending-uploads') || { textContent: '[]' }).textContent
|
||||
);
|
||||
PacksUploads.init({ pending: pending });
|
||||
})();
|
||||
Reference in New Issue
Block a user