forked from rDrama/rDrama
fix submit.js
parent
399d32eaf5
commit
42ec3264d7
|
@ -216,14 +216,13 @@ if (location.href == '/submit') {
|
||||||
|
|
||||||
const filelist = document.getElementById('upload-filelist');
|
const filelist = document.getElementById('upload-filelist');
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', function () {
|
/**
|
||||||
/**
|
|
||||||
* Sets up the elements inside file upload rows.
|
* Sets up the elements inside file upload rows.
|
||||||
*
|
*
|
||||||
* @param {File} file
|
* @param {File} file
|
||||||
* @return {HTMLLIElement} row
|
* @return {HTMLLIElement} row
|
||||||
*/
|
*/
|
||||||
function addRow(file) {
|
function addRow(file) {
|
||||||
var row = document.createElement('li');
|
var row = document.createElement('li');
|
||||||
|
|
||||||
var name = document.createElement('span');
|
var name = document.createElement('span');
|
||||||
|
@ -246,14 +245,14 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||||
filelist.innerHTML = '';
|
filelist.innerHTML = '';
|
||||||
filelist.appendChild(row);
|
filelist.appendChild(row);
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the page while the file is being uploaded.
|
* Updates the page while the file is being uploaded.
|
||||||
*
|
*
|
||||||
* @param {ProgressEvent} evt
|
* @param {ProgressEvent} evt
|
||||||
*/
|
*/
|
||||||
function handleUploadProgress(evt) {
|
function handleUploadProgress(evt) {
|
||||||
var xhr = evt.target;
|
var xhr = evt.target;
|
||||||
var bar = xhr.bar;
|
var bar = xhr.bar;
|
||||||
var percentIndicator = xhr.percent;
|
var percentIndicator = xhr.percent;
|
||||||
|
@ -265,16 +264,16 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||||
bar.setAttribute('value', progressPercent);
|
bar.setAttribute('value', progressPercent);
|
||||||
percentIndicator.textContent = progressPercent + '%';
|
percentIndicator.textContent = progressPercent + '%';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Complete the uploading process by checking the response status and, if the
|
* Complete the uploading process by checking the response status and, if the
|
||||||
* upload was successful, writing the URL(s) and creating the copy element(s)
|
* upload was successful, writing the URL(s) and creating the copy element(s)
|
||||||
* for the files.
|
* for the files.
|
||||||
*
|
*
|
||||||
* @param {ProgressEvent} evt
|
* @param {ProgressEvent} evt
|
||||||
*/
|
*/
|
||||||
function handleUploadComplete(evt) {
|
function handleUploadComplete(evt) {
|
||||||
var xhr = evt.target;
|
var xhr = evt.target;
|
||||||
var bar = xhr.bar;
|
var bar = xhr.bar;
|
||||||
var row = xhr.row;
|
var row = xhr.row;
|
||||||
|
@ -332,15 +331,15 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||||
link.textContent = response.description;
|
link.textContent = response.description;
|
||||||
url.appendChild(link);
|
url.appendChild(link);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the page while the file is being uploaded.
|
* Updates the page while the file is being uploaded.
|
||||||
*
|
*
|
||||||
* @param {File} file
|
* @param {File} file
|
||||||
* @param {HTMLLIElement} row
|
* @param {HTMLLIElement} row
|
||||||
*/
|
*/
|
||||||
function uploadFile(file, row) {
|
function uploadFile(file, row) {
|
||||||
var bar = row.querySelector('.file-progress');
|
var bar = row.querySelector('.file-progress');
|
||||||
var percentIndicator = row.querySelector('.progress-percent');
|
var percentIndicator = row.querySelector('.progress-percent');
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
|
@ -358,58 +357,58 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||||
var form = new FormData();
|
var form = new FormData();
|
||||||
form.append('files[]', file);
|
form.append('files[]', file);
|
||||||
xhr.send(form);
|
xhr.send(form);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prevents the browser for allowing the normal actions associated with an event.
|
* Prevents the browser for allowing the normal actions associated with an event.
|
||||||
* This is used by event handlers to allow custom functionality without
|
* This is used by event handlers to allow custom functionality without
|
||||||
* having to worry about the other consequences of that action.
|
* having to worry about the other consequences of that action.
|
||||||
*
|
*
|
||||||
* @param {Event} evt
|
* @param {Event} evt
|
||||||
*/
|
*/
|
||||||
function stopDefaultEvent(evt) {
|
function stopDefaultEvent(evt) {
|
||||||
evt.stopPropagation();
|
evt.stopPropagation();
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds 1 to the state and changes the text.
|
* Adds 1 to the state and changes the text.
|
||||||
*
|
*
|
||||||
* @param {Object} state
|
* @param {Object} state
|
||||||
* @param {HTMLButtonElement} element
|
* @param {HTMLButtonElement} element
|
||||||
* @param {DragEvent} evt
|
* @param {DragEvent} evt
|
||||||
*/
|
*/
|
||||||
function handleDrag(state, element, evt) {
|
function handleDrag(state, element, evt) {
|
||||||
stopDefaultEvent(evt);
|
stopDefaultEvent(evt);
|
||||||
if (state.dragCount == 1) {
|
if (state.dragCount == 1) {
|
||||||
element.textContent = 'Drop it here~';
|
element.textContent = 'Drop it here~';
|
||||||
}
|
}
|
||||||
state.dragCount += 1;
|
state.dragCount += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subtracts 1 from the state and changes the text back.
|
* Subtracts 1 from the state and changes the text back.
|
||||||
*
|
*
|
||||||
* @param {Object} state
|
* @param {Object} state
|
||||||
* @param {HTMLButtonElement} element
|
* @param {HTMLButtonElement} element
|
||||||
* @param {DragEvent} evt
|
* @param {DragEvent} evt
|
||||||
*/
|
*/
|
||||||
function handleDragAway(state, element, evt) {
|
function handleDragAway(state, element, evt) {
|
||||||
stopDefaultEvent(evt);
|
stopDefaultEvent(evt);
|
||||||
state.dragCount -= 1;
|
state.dragCount -= 1;
|
||||||
if (state.dragCount == 0) {
|
if (state.dragCount == 0) {
|
||||||
element.textContent = 'Select or drop file(s)';
|
element.textContent = 'Select or drop file(s)';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepares files for uploading after being added via drag-drop.
|
* Prepares files for uploading after being added via drag-drop.
|
||||||
*
|
*
|
||||||
* @param {Object} state
|
* @param {Object} state
|
||||||
* @param {HTMLButtonElement} element
|
* @param {HTMLButtonElement} element
|
||||||
* @param {DragEvent} evt
|
* @param {DragEvent} evt
|
||||||
*/
|
*/
|
||||||
function handleDragDrop(state, element, evt) {
|
function handleDragDrop(state, element, evt) {
|
||||||
stopDefaultEvent(evt);
|
stopDefaultEvent(evt);
|
||||||
handleDragAway(state, element, evt);
|
handleDragAway(state, element, evt);
|
||||||
var len = evt.dataTransfer.files.length;
|
var len = evt.dataTransfer.files.length;
|
||||||
|
@ -418,14 +417,14 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||||
var row = addRow(file);
|
var row = addRow(file);
|
||||||
uploadFile(file, row);
|
uploadFile(file, row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepares the files to be uploaded when they're added to the <input> element.
|
* Prepares the files to be uploaded when they're added to the <input> element.
|
||||||
*
|
*
|
||||||
* @param {InputEvent} evt
|
* @param {InputEvent} evt
|
||||||
*/
|
*/
|
||||||
function uploadFiles(evt) {
|
function uploadFiles(evt) {
|
||||||
var len = evt.target.files.length;
|
var len = evt.target.files.length;
|
||||||
// For each file, make a row, and upload the file.
|
// For each file, make a row, and upload the file.
|
||||||
for (var i = 0; i < len; i++) {
|
for (var i = 0; i < len; i++) {
|
||||||
|
@ -433,43 +432,42 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||||
var row = addRow(file);
|
var row = addRow(file);
|
||||||
uploadFile(file, row);
|
uploadFile(file, row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens up a "Select files.." dialog window to allow users to select files to upload.
|
* Opens up a "Select files.." dialog window to allow users to select files to upload.
|
||||||
*
|
*
|
||||||
* @param {HTMLInputElement} target
|
* @param {HTMLInputElement} target
|
||||||
* @param {InputEvent} evt
|
* @param {InputEvent} evt
|
||||||
*/
|
*/
|
||||||
function selectFiles(target, evt) {
|
function selectFiles(target, evt) {
|
||||||
stopDefaultEvent(evt);
|
stopDefaultEvent(evt);
|
||||||
target.click();
|
target.click();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Handles the pasting function */
|
/* Handles the pasting function */
|
||||||
window.addEventListener("paste", e => {
|
window.addEventListener("paste", e => {
|
||||||
var len = e.clipboardData.files.length;
|
var len = e.clipboardData.files.length;
|
||||||
for (var i = 0; i < len; i++) {
|
for (var i = 0; i < len; i++) {
|
||||||
var file = e.clipboardData.files[i];
|
var file = e.clipboardData.files[i];
|
||||||
var row = addRow(file);
|
var row = addRow(file);
|
||||||
uploadFile(file, row);
|
uploadFile(file, row);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* Set-up the event handlers for the <button>, <input> and the window itself
|
/* Set-up the event handlers for the <button>, <input> and the window itself
|
||||||
and also set the "js" class on selector "#upload-form", presumably to
|
and also set the "js" class on selector "#upload-form", presumably to
|
||||||
allow custom styles for clients running javascript. */
|
allow custom styles for clients running javascript. */
|
||||||
var state = {dragCount: 0};
|
var state = {dragCount: 0};
|
||||||
var uploadButton = document.getElementById('image-upload-block');
|
var uploadButton = document.getElementById('image-upload-block');
|
||||||
window.addEventListener('dragenter', handleDrag.bind(this, state, uploadButton), false);
|
window.addEventListener('dragenter', handleDrag.bind(this, state, uploadButton), false);
|
||||||
window.addEventListener('dragleave', handleDragAway.bind(this, state, uploadButton), false);
|
window.addEventListener('dragleave', handleDragAway.bind(this, state, uploadButton), false);
|
||||||
window.addEventListener('drop', handleDragAway.bind(this, state, uploadButton), false);
|
window.addEventListener('drop', handleDragAway.bind(this, state, uploadButton), false);
|
||||||
window.addEventListener('dragover', stopDefaultEvent, false);
|
window.addEventListener('dragover', stopDefaultEvent, false);
|
||||||
|
|
||||||
|
|
||||||
var uploadInput = document.getElementById('file-upload');
|
var uploadInput = document.getElementById('file-upload');
|
||||||
uploadInput.addEventListener('change', uploadFiles);
|
uploadInput.addEventListener('change', uploadFiles);
|
||||||
// uploadButton.addEventListener('click', selectFiles.bind(this, uploadInput));
|
// uploadButton.addEventListener('click', selectFiles.bind(this, uploadInput));
|
||||||
uploadButton.addEventListener('drop', handleDragDrop.bind(this, state, uploadButton), false);
|
uploadButton.addEventListener('drop', handleDragDrop.bind(this, state, uploadButton), false);
|
||||||
});
|
|
||||||
|
|
Loading…
Reference in New Issue