Skip to content

Commit

Permalink
Correction to Trongate MX - clicks outside the space defined by an op…
Browse files Browse the repository at this point in the history
…en modal will only close the modals defined by Trongate MX itself.
  • Loading branch information
David Connelly committed Dec 11, 2024
1 parent c083df3 commit 69e9de6
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions public/js/trongate-mx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,18 @@ let trongateMXOpeningModal = false;
}
},

handleTrongateMXModalClick(event, modalElement) {
if (trongateMXOpeningModal === true) {
return;
}

if (!modalElement.contains(event.target)) {
closeModal();
// Remove this specific event listener since we're closing the modal
document.removeEventListener('click', modalElement._boundClickHandler);
}
},

openModal(modalId, modalData) {
trongateMXOpeningModal = true;

Expand All @@ -1041,7 +1053,6 @@ let trongateMXOpeningModal = false;
var overlay = document.createElement("div");
overlay.setAttribute("id", "overlay");
overlay.setAttribute("style", "z-index: 2");

body.prepend(overlay);

var targetModal = document.getElementById(modalId);
Expand All @@ -1051,7 +1062,6 @@ let trongateMXOpeningModal = false;
var newModal = document.createElement("div");
newModal.setAttribute("class", "modal");
newModal.setAttribute("id", modalId);

newModal.style.zIndex = 4;
newModal.innerHTML = targetModalContent;
modalContainer.appendChild(newModal);
Expand All @@ -1060,6 +1070,12 @@ let trongateMXOpeningModal = false;
(modalData.marginTop || modalData['margin-top'] || '12vh') :
'12vh';

// Create a bound version of the click handler specifically for this modal
newModal._boundClickHandler = (event) => this.handleTrongateMXModalClick(event, newModal);

// Add the click event listener with the bound handler
document.addEventListener('click', newModal._boundClickHandler);

setTimeout(() => {
newModal.style.opacity = 1;
newModal.style.marginTop = marginTop;
Expand Down Expand Up @@ -1508,31 +1524,12 @@ let trongateMXOpeningModal = false;
}
}

function handleModalClick(event) {

if (trongateMXOpeningModal === true) {
return;
}

const modalContainerEl = document.getElementById("modal-container");
if (modalContainerEl) {
const modal = modalContainerEl.querySelector(".modal");
if (modal && !modal.contains(event.target)) {
closeModal();
}
}

}

// Initialize Trongate MX when the DOM is loaded
document.addEventListener('DOMContentLoaded', Main.initializeTrongateMX);

// Initialize closing of modals upon pressing 'Escape' key.
document.addEventListener("keydown", handleEscapeKey);

// Invoke handleModalClick on every click event
document.addEventListener("click", handleModalClick);

// Expose necessary functions to the global scope
window.TrongateMX = {
init: Main.initializeTrongateMX,
Expand Down

0 comments on commit 69e9de6

Please sign in to comment.