Skip to content

Commit

Permalink
Fix: prevent fileInput.js from adding event listeners more than once (#…
Browse files Browse the repository at this point in the history
…2365)

Fix fileInput.js adding event listeners more than once

- Fix a bug that caused fileInput.js to add event listeners more than once per HTML file as it's included in fileSelector fragment in fragments/common.html thus it's being loaded N times where N is the number of file selectors / custom file chooser / file input elements per HTML file, which resulted in each event actions being executed N times as well, which was prevalent in drag and drop operations such as dragging and dropping a file called y.png, it would be duplicated N times (as in /sign path).
  • Loading branch information
omar-ahmed42 authored Dec 2, 2024
1 parent db02fba commit 04ccdf6
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/resources/static/js/fileInput.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll(".custom-file-chooser").forEach(setupFileInput);
});
let isScriptExecuted = false;
if (!isScriptExecuted) {
isScriptExecuted = true;
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll(".custom-file-chooser").forEach(setupFileInput);
});
}

function setupFileInput(chooser) {
const elementId = chooser.getAttribute("data-bs-element-id");
Expand Down Expand Up @@ -85,7 +89,7 @@ function setupFileInput(chooser) {
$("#" + elementId).on("change", function (e) {
let element = e.target;
const isDragAndDrop = e.detail?.source == 'drag-drop';

if (element instanceof HTMLInputElement && element.hasAttribute("multiple")) {
allFiles = isDragAndDrop ? allFiles : [... allFiles, ... element.files];
} else {
Expand Down

0 comments on commit 04ccdf6

Please sign in to comment.