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

130 lines
4.1 KiB
JavaScript
Raw Normal View History

document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll(".custom-file-chooser").forEach(setupFileInput);
2023-08-02 22:49:43 +01:00
});
2023-06-02 20:15:10 +01:00
2023-08-02 22:49:43 +01:00
function setupFileInput(chooser) {
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 = [];
let overlay;
let dragCounter = 0;
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();
const dt = e.dataTransfer;
const files = dt.files;
const fileInput = document.getElementById(elementId);
if (fileInput?.hasAttribute("multiple")) {
files.forEach(file => allFiles.push(file));
} else if (fileInput) {
allFiles = [files[0]];
}
const dataTransfer = new DataTransfer();
allFiles.forEach((file) => dataTransfer.items.add(file));
fileInput.files = dataTransfer.files;
2023-07-13 22:03:23 +01:00
if (overlay) {
overlay.remove();
overlay = null;
2023-07-13 22:03:23 +01:00
}
dragCounter = 0;
Fix: input file overwrite in merge (#2335) * Fix input files being overwritten by newly uploaded files - Fix a bug that caused existing selected/uploaded files to be overwritten when a new input file is uploaded through input element. - Add source property to change event to differentiate between uploaded files using input element and drag/drop uploads to avoid processing drag/drop files more than once, thus avoiding file duplication (file duplication resulting from copying drop/drop files to input files on each 'change' event). * Dispatch and use file-input-change instead of change event for merging - Dispatch "file-input-change" event after each "change" event in file upload, to notify other functions/components relying on the files provided by the \<input\> element. - Use "file-input-change" instead of "change" event to display the latest version of uploaded files. # FAQ: - Why use "file-input-change" instead of "change" in merge.js? = "change" event is automatically triggered when a file is uploaded through \<input\> element which would replace all the existing selected/uploaded files including the drag/drop files. ## Example: Let's say that the user wants to upload/select the x.pdf, y.pdf and z.pdf all together: - user selects "x.pdf" -> file selected successfully. = selected files: x.pdf - user drags and drops "y.pdf" -> file dropped successfully = selected files: x.pdf, y.pdf - user selects again using \<input\> "z.pdf" -> file selected succesfully overwriting selected files. = selected files: z.pdf
2024-11-26 22:41:08 +02:00
fileInput.dispatchEvent(new CustomEvent("change", { bubbles: true, detail: {source: 'drag-drop'} }));
};
["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);
$("#" + elementId).on("change", function (e) {
Fix: input file overwrite in merge (#2335) * Fix input files being overwritten by newly uploaded files - Fix a bug that caused existing selected/uploaded files to be overwritten when a new input file is uploaded through input element. - Add source property to change event to differentiate between uploaded files using input element and drag/drop uploads to avoid processing drag/drop files more than once, thus avoiding file duplication (file duplication resulting from copying drop/drop files to input files on each 'change' event). * Dispatch and use file-input-change instead of change event for merging - Dispatch "file-input-change" event after each "change" event in file upload, to notify other functions/components relying on the files provided by the \<input\> element. - Use "file-input-change" instead of "change" event to display the latest version of uploaded files. # FAQ: - Why use "file-input-change" instead of "change" in merge.js? = "change" event is automatically triggered when a file is uploaded through \<input\> element which would replace all the existing selected/uploaded files including the drag/drop files. ## Example: Let's say that the user wants to upload/select the x.pdf, y.pdf and z.pdf all together: - user selects "x.pdf" -> file selected successfully. = selected files: x.pdf - user drags and drops "y.pdf" -> file dropped successfully = selected files: x.pdf, y.pdf - user selects again using \<input\> "z.pdf" -> file selected succesfully overwriting selected files. = selected files: z.pdf
2024-11-26 22:41:08 +02:00
let element = e.target;
const isDragAndDrop = e.detail?.source == 'drag-drop';
Fix: input file overwrite in merge (#2335) * Fix input files being overwritten by newly uploaded files - Fix a bug that caused existing selected/uploaded files to be overwritten when a new input file is uploaded through input element. - Add source property to change event to differentiate between uploaded files using input element and drag/drop uploads to avoid processing drag/drop files more than once, thus avoiding file duplication (file duplication resulting from copying drop/drop files to input files on each 'change' event). * Dispatch and use file-input-change instead of change event for merging - Dispatch "file-input-change" event after each "change" event in file upload, to notify other functions/components relying on the files provided by the \<input\> element. - Use "file-input-change" instead of "change" event to display the latest version of uploaded files. # FAQ: - Why use "file-input-change" instead of "change" in merge.js? = "change" event is automatically triggered when a file is uploaded through \<input\> element which would replace all the existing selected/uploaded files including the drag/drop files. ## Example: Let's say that the user wants to upload/select the x.pdf, y.pdf and z.pdf all together: - user selects "x.pdf" -> file selected successfully. = selected files: x.pdf - user drags and drops "y.pdf" -> file dropped successfully = selected files: x.pdf, y.pdf - user selects again using \<input\> "z.pdf" -> file selected succesfully overwriting selected files. = selected files: z.pdf
2024-11-26 22:41:08 +02:00
if (element instanceof HTMLInputElement && element.hasAttribute("multiple")) {
allFiles = isDragAndDrop ? allFiles : [... allFiles, ... element.files];
} else {
allFiles = Array.from(isDragAndDrop ? allFiles : [element.files[0]]);
}
Fix: input file overwrite in merge (#2335) * Fix input files being overwritten by newly uploaded files - Fix a bug that caused existing selected/uploaded files to be overwritten when a new input file is uploaded through input element. - Add source property to change event to differentiate between uploaded files using input element and drag/drop uploads to avoid processing drag/drop files more than once, thus avoiding file duplication (file duplication resulting from copying drop/drop files to input files on each 'change' event). * Dispatch and use file-input-change instead of change event for merging - Dispatch "file-input-change" event after each "change" event in file upload, to notify other functions/components relying on the files provided by the \<input\> element. - Use "file-input-change" instead of "change" event to display the latest version of uploaded files. # FAQ: - Why use "file-input-change" instead of "change" in merge.js? = "change" event is automatically triggered when a file is uploaded through \<input\> element which would replace all the existing selected/uploaded files including the drag/drop files. ## Example: Let's say that the user wants to upload/select the x.pdf, y.pdf and z.pdf all together: - user selects "x.pdf" -> file selected successfully. = selected files: x.pdf - user drags and drops "y.pdf" -> file dropped successfully = selected files: x.pdf, y.pdf - user selects again using \<input\> "z.pdf" -> file selected succesfully overwriting selected files. = selected files: z.pdf
2024-11-26 22:41:08 +02:00
if (!isDragAndDrop) {
let dataTransfer = new DataTransfer();
allFiles.forEach(file => dataTransfer.items.add(file));
element.files = dataTransfer.files;
Fix: input file overwrite in merge (#2335) * Fix input files being overwritten by newly uploaded files - Fix a bug that caused existing selected/uploaded files to be overwritten when a new input file is uploaded through input element. - Add source property to change event to differentiate between uploaded files using input element and drag/drop uploads to avoid processing drag/drop files more than once, thus avoiding file duplication (file duplication resulting from copying drop/drop files to input files on each 'change' event). * Dispatch and use file-input-change instead of change event for merging - Dispatch "file-input-change" event after each "change" event in file upload, to notify other functions/components relying on the files provided by the \<input\> element. - Use "file-input-change" instead of "change" event to display the latest version of uploaded files. # FAQ: - Why use "file-input-change" instead of "change" in merge.js? = "change" event is automatically triggered when a file is uploaded through \<input\> element which would replace all the existing selected/uploaded files including the drag/drop files. ## Example: Let's say that the user wants to upload/select the x.pdf, y.pdf and z.pdf all together: - user selects "x.pdf" -> file selected successfully. = selected files: x.pdf - user drags and drops "y.pdf" -> file dropped successfully = selected files: x.pdf, y.pdf - user selects again using \<input\> "z.pdf" -> file selected succesfully overwriting selected files. = selected files: z.pdf
2024-11-26 22:41:08 +02:00
}
handleFileInputChange(this);
Fix: input file overwrite in merge (#2335) * Fix input files being overwritten by newly uploaded files - Fix a bug that caused existing selected/uploaded files to be overwritten when a new input file is uploaded through input element. - Add source property to change event to differentiate between uploaded files using input element and drag/drop uploads to avoid processing drag/drop files more than once, thus avoiding file duplication (file duplication resulting from copying drop/drop files to input files on each 'change' event). * Dispatch and use file-input-change instead of change event for merging - Dispatch "file-input-change" event after each "change" event in file upload, to notify other functions/components relying on the files provided by the \<input\> element. - Use "file-input-change" instead of "change" event to display the latest version of uploaded files. # FAQ: - Why use "file-input-change" instead of "change" in merge.js? = "change" event is automatically triggered when a file is uploaded through \<input\> element which would replace all the existing selected/uploaded files including the drag/drop files. ## Example: Let's say that the user wants to upload/select the x.pdf, y.pdf and z.pdf all together: - user selects "x.pdf" -> file selected successfully. = selected files: x.pdf - user drags and drops "y.pdf" -> file dropped successfully = selected files: x.pdf, y.pdf - user selects again using \<input\> "z.pdf" -> file selected succesfully overwriting selected files. = selected files: z.pdf
2024-11-26 22:41:08 +02:00
this.dispatchEvent(new CustomEvent("file-input-change", { bubbles: true }));
});
function handleFileInputChange(inputElement) {
const files = allFiles;
const fileNames = files.map((f) => f.name);
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);
2023-08-02 22:49:43 +01:00
}
}
//Listen for event of file being removed and the filter it out of the allFiles array
document.addEventListener("fileRemoved", function (e) {
const fileName = e.detail;
allFiles = allFiles.filter(file => file.name !== fileName);
});
2023-08-02 22:49:43 +01:00
}