bfile/filed/static/dragndrop-form.js

55 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-09-29 17:09:24 +02:00
(
() => {
// display the js only text
2023-09-29 17:45:12 +02:00
const text = document.getElementById('drag-n-drop-jsonly');
text.style.display = 'initial';
2023-09-29 17:09:24 +02:00
2023-09-29 17:45:12 +02:00
/** @type {HTMLElement} */
const root_drag_rop = document.getElementsByClassName('file-drag-n-drop')[0];
/** @type {HTMLElement} */
2023-09-29 17:09:24 +02:00
const drag_rop = document.getElementsByClassName('file-drag-n-drop-inside')[0];
const dr_rop_t = document.getElementsByClassName('file-drag-n-drop-inside-text')[0];
2023-09-29 17:45:12 +02:00
function drag_end() {
root_drag_rop.style.background = 'var(--header-sec-color)'
drag_rop.style.borderColor = '#656565';
}
async function fill_bg() {
for (let i = 100; i != 0; i--) {
await delay(Math.random());
root_drag_rop.style.background = `linear-gradient(#353535 ${i}%, var(--header-sec-color) ${i + 0.01}%)`;
}
root_drag_rop.style.background = `var(--header-sec-color)`;
}
let drdrop_enabled = true;
drag_rop.addEventListener('drop', async e => {
e.preventDefault();
if (!drdrop_enabled) return;
if (e.dataTransfer.items) {
for (let i = 0; i != e.dataTransfer.items.length; i++) {
let file = e.dataTransfer.items[i].getAsFile();
text.innerText = 'Processing...';
drdrop_enabled = false;
await fill_bg();
drdrop_enabled = true;
text.innerText = `Selected file: ${file.name.substring(0,16)}${file.name.length > 16 ? '...' : ''}\nYou can drop another file to replace this one`;
}
}
});
drag_rop.addEventListener('dragover', e => {
e.preventDefault();
if (!drdrop_enabled) return;
root_drag_rop.style.background = '#393939'
drag_rop.style.borderColor = '#959595';
});
2023-09-29 17:09:24 +02:00
2023-09-29 17:45:12 +02:00
drag_rop.addEventListener('mouseleave', drag_end);
drag_rop.addEventListener('dragend', drag_end)
2023-09-29 17:09:24 +02:00
}
)()