Stirling-PDF/src/main/resources/static/js/fileInput.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

105 lines
3.5 KiB
JavaScript
Raw Normal View History

2023-06-02 21:15:10 +02:00
document.addEventListener('DOMContentLoaded', function() {
2023-08-02 23:49:43 +02:00
document.querySelectorAll('.custom-file-chooser').forEach(setupFileInput);
});
2023-06-02 21:15:10 +02:00
2023-08-02 23:49:43 +02:00
function setupFileInput(chooser) {
2023-08-20 22:57:19 +02:00
const elementId = chooser.getAttribute('data-bs-element-id');
const filesSelected = chooser.getAttribute('data-bs-files-selected');
const pdfPrompt = chooser.getAttribute('data-bs-pdf-prompt');
let allFiles = [];
2023-07-13 23:03:23 +02:00
let overlay;
let dragCounter = 0;
2023-06-02 21:15:10 +02:00
2023-07-13 23:03:23 +02:00
const dragenterListener = function() {
dragCounter++;
if (!overlay) {
overlay = document.createElement('div');
overlay.style.position = 'fixed';
overlay.style.top = 0;
overlay.style.left = 0;
overlay.style.width = '100%';
overlay.style.height = '100%';
overlay.style.background = 'rgba(0, 0, 0, 0.5)';
overlay.style.color = '#fff';
overlay.style.zIndex = '1000';
overlay.style.display = 'flex';
overlay.style.alignItems = 'center';
overlay.style.justifyContent = 'center';
overlay.style.pointerEvents = 'none';
overlay.innerHTML = '<p>Drop files anywhere to upload</p>';
document.getElementById('content-wrap').appendChild(overlay);
}
};
const dragleaveListener = function() {
dragCounter--;
if (dragCounter === 0) {
if (overlay) {
overlay.remove();
overlay = null;
}
}
};
const dropListener = function(e) {
e.preventDefault();
2023-07-13 23:03:23 +02:00
const dt = e.dataTransfer;
const files = dt.files;
for (let i = 0; i < files.length; i++) {
allFiles.push(files[i]);
}
const dataTransfer = new DataTransfer();
allFiles.forEach(file => dataTransfer.items.add(file));
2023-08-02 23:49:43 +02:00
const fileInput = document.getElementById(elementId);
fileInput.files = dataTransfer.files;
2023-07-13 23:03:23 +02:00
if (overlay) {
overlay.remove();
overlay = null;
}
dragCounter = 0;
2023-07-18 23:04:18 +02:00
fileInput.dispatchEvent(new Event('change', { bubbles: true }));
2023-07-13 23:03:23 +02:00
};
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
document.body.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
document.body.addEventListener('dragenter', dragenterListener);
document.body.addEventListener('dragleave', dragleaveListener);
document.body.addEventListener('drop', dropListener);
2023-06-02 21:15:10 +02:00
2023-08-06 15:16:56 +02:00
$("#" + elementId).on("change", function(e) {
allFiles = Array.from(e.target.files);
handleFileInputChange(this);
});
2023-07-13 23:03:23 +02:00
2023-08-02 23:49:43 +02:00
function handleFileInputChange(inputElement) {
const files = allFiles;
const fileNames = files.map(f => f.name);
2023-08-02 23:49:43 +02:00
const selectedFilesContainer = $(inputElement).siblings(".selected-files");
selectedFilesContainer.empty();
fileNames.forEach(fileName => {
selectedFilesContainer.append("<div>" + fileName + "</div>");
});
if (fileNames.length === 1) {
$(inputElement).siblings(".custom-file-label").addClass("selected").html(fileNames[0]);
} else if (fileNames.length > 1) {
$(inputElement).siblings(".custom-file-label").addClass("selected").html(fileNames.length + " " + filesSelected);
} else {
$(inputElement).siblings(".custom-file-label").addClass("selected").html(pdfPrompt);
}
}
}