handle uploads from the upload button
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
const body = document.querySelector('body');
|
const body = document.querySelector('body');
|
||||||
const form = document.querySelector('form');
|
const form = document.querySelector('form');
|
||||||
const grid_form = document.querySelector('.grid_form');
|
const grid_form = document.querySelector('.grid_form');
|
||||||
const fileInput = document.querySelector('.fileUpload');
|
const fileUpload = document.querySelector('.fileUpload');
|
||||||
|
const fileInput = document.querySelector('input#file-upload');
|
||||||
const upload_card = document.querySelector('#upload_card');
|
const upload_card = document.querySelector('#upload_card');
|
||||||
const textarea = document.querySelector('textarea');
|
const textarea = document.querySelector('textarea');
|
||||||
const select = document.querySelector('select');
|
const select = document.querySelector('select');
|
||||||
@@ -19,7 +20,7 @@ window.onload = () => {
|
|||||||
const onInput = () => {
|
const onInput = () => {
|
||||||
submitButton.classList.toggle('hidden', !textarea.value);
|
submitButton.classList.toggle('hidden', !textarea.value);
|
||||||
select.classList.toggle('hidden', !textarea.value);
|
select.classList.toggle('hidden', !textarea.value);
|
||||||
fileInput.classList.toggle('hidden', textarea.value);
|
fileUpload.classList.toggle('hidden', textarea.value);
|
||||||
}
|
}
|
||||||
textarea.addEventListener('input', onInput);
|
textarea.addEventListener('input', onInput);
|
||||||
onInput();
|
onInput();
|
||||||
@@ -93,6 +94,19 @@ function unhighlight(e) {
|
|||||||
grid_form.classList.remove('hidden');
|
grid_form.classList.remove('hidden');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function upload(file) {
|
||||||
|
const ext = file.name.split(".")[1];
|
||||||
|
var url = window.location.href;
|
||||||
|
|
||||||
|
return postData(url, file)
|
||||||
|
.then(data => {
|
||||||
|
window.location.href = data + (ext ? "." + ext : '');
|
||||||
|
})
|
||||||
|
.catch(function (err) {
|
||||||
|
console.info(err + " url: " + url);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Files are dropped
|
// Files are dropped
|
||||||
function dropHandler(ev) {
|
function dropHandler(ev) {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
@@ -104,20 +118,12 @@ function dropHandler(ev) {
|
|||||||
if (ev.dataTransfer.items) {
|
if (ev.dataTransfer.items) {
|
||||||
var item = ev.dataTransfer.items[0];
|
var item = ev.dataTransfer.items[0];
|
||||||
var blob = item.getAsFile();
|
var blob = item.getAsFile();
|
||||||
const ext = blob.name.split(".")[1];
|
|
||||||
var url = window.location.href;
|
|
||||||
|
|
||||||
postData(url, blob)
|
|
||||||
.then(data => {
|
|
||||||
window.location.href = data + "." + ext;
|
|
||||||
|
|
||||||
|
upload(blob).then(() => {
|
||||||
// remove the jazz for if user returns to the prev page
|
// remove the jazz for if user returns to the prev page
|
||||||
upload_card.classList.remove('show');
|
upload_card.classList.remove('show');
|
||||||
grid_form.classList.remove('hidden');
|
grid_form.classList.remove('hidden');
|
||||||
form.classList.remove('highlight');
|
form.classList.remove('highlight');
|
||||||
})
|
|
||||||
.catch(function (err) {
|
|
||||||
console.info(err + " url: " + url);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,14 +135,15 @@ document.onpaste = function (pasteEvent) {
|
|||||||
var blob = item.getAsFile();
|
var blob = item.getAsFile();
|
||||||
|
|
||||||
if (blob !== null && blob !== '') {
|
if (blob !== null && blob !== '') {
|
||||||
var url = window.location.href;
|
upload(blob);
|
||||||
|
}
|
||||||
postData(url, blob)
|
}
|
||||||
.then(data => {
|
|
||||||
window.location.href = data;
|
// upload from the button
|
||||||
})
|
fileInput.onchange = function (e) {
|
||||||
.catch(function (err) {
|
const file = e.target.files[0];
|
||||||
console.info(err + " url: " + url);
|
|
||||||
});
|
if (file) {
|
||||||
|
upload(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user