handle uploads from the upload button

This commit is contained in:
ellotheth
2023-08-17 21:43:24 +02:00
committed by Gunwant Jain
parent fc821be03f
commit 341da99c36

View File

@@ -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,21 +118,13 @@ 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) upload(blob).then(() => {
.then(data => { // remove the jazz for if user returns to the prev page
window.location.href = data + "." + ext; upload_card.classList.remove('show');
grid_form.classList.remove('hidden');
// remove the jazz for if user returns to the prev page form.classList.remove('highlight');
upload_card.classList.remove('show'); });
grid_form.classList.remove('hidden');
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);
} }
} }