Skip to content

Commit

Permalink
Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
komejo committed Oct 31, 2024
1 parent 8d61391 commit 552a371
Showing 1 changed file with 52 additions and 33 deletions.
85 changes: 52 additions & 33 deletions themes/custom/ts_wrin/js/components/wri_cta_banners.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
*/

export default function(context) {
const modals = context.querySelectorAll('.region-top-modals .block-modal');
const ctaCookie = 'wri_cta_displayed';
const thankYouCookie = 'wri_cta_thank_you';
const modals = context.querySelectorAll(".region-top-modals .block-modal");
const ctaCookie = "wri_cta_displayed";
const thankYouCookie = "wri_cta_thank_you";

// Accessibility: aria-live region for announcements
const announceRegion = document.createElement('div');
announceRegion.setAttribute('aria-live', 'assertive');
announceRegion.setAttribute('role', 'alert');
announceRegion.style.position = 'absolute';
announceRegion.style.left = '-9999px';
const announceRegion = document.createElement("div");
announceRegion.setAttribute("aria-live", "assertive");
announceRegion.setAttribute("role", "alert");
announceRegion.style.position = "absolute";
announceRegion.style.left = "-9999px";
document.body.appendChild(announceRegion);

function announce(message) {
announceRegion.textContent = '';
announceRegion.textContent = "";
// Timeout ensures the message is re-announced when modal changes.
setTimeout(() => {
announceRegion.textContent = message;
Expand All @@ -28,7 +28,12 @@ export default function(context) {
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
return parts.length === 2 ? parts.pop().split(';').shift() : null;
return parts.length === 2
? parts
.pop()
.split(";")
.shift()
: null;
}

function setCookie(name, value, days) {
Expand All @@ -38,40 +43,54 @@ export default function(context) {
}

function closeModal(modal) {
modal.style.display = 'none';
setCookie(getCookie(thankYouCookie) ? thankYouCookie : ctaCookie, 'true', 1);
document.removeEventListener('keydown', closeOnEscape);
document.removeEventListener('click', clickOutsideModal);
announce('Modal closed.');
modal.style.display = "none";
setCookie(
getCookie(thankYouCookie) ? thankYouCookie : ctaCookie,
"true",
1
);
document.removeEventListener("keydown", closeOnEscape);
document.removeEventListener("click", clickOutsideModal);
announce("Modal closed.");
}

function closeOnEscape(event) {
if (event.key === 'Escape') {
const visibleModal = context.querySelector('.block-modal[style*="display: block"]');
if (event.key === "Escape") {
const visibleModal = context.querySelector(
'.block-modal[style*="display: block"]'
);
if (visibleModal) closeModal(visibleModal);
}
}

function clickOutsideModal(event) {
const visibleModal = context.querySelector('.block-modal[style*="display: block"]');
if (visibleModal && !visibleModal.querySelector('.modal_inner').contains(event.target)) {
const visibleModal = context.querySelector(
'.block-modal[style*="display: block"]'
);
if (
visibleModal &&
!visibleModal.querySelector(".modal_inner").contains(event.target)
) {
closeModal(visibleModal);
}
}

function showModal(modal) {
modal.style.display = 'block';
modal.querySelector('.modal-close').addEventListener('click', function () {
modal.style.display = "block";
modal.querySelector(".modal-close").addEventListener("click", function() {
closeModal(modal);
});
document.addEventListener('keydown', closeOnEscape);
document.addEventListener('click', clickOutsideModal);
announce('A message for you: ' + modal.querySelector('.field--name-field-title').textContent);
document.addEventListener("keydown", closeOnEscape);
document.addEventListener("click", clickOutsideModal);
announce(
"A message for you: " +
modal.querySelector(".field--name-field-title").textContent
);
}

function displayThankYouModal() {
const thankYouMessage = document.createElement('div');
thankYouMessage.classList.add('block-modal', 'thank-you-modal');
const thankYouMessage = document.createElement("div");
thankYouMessage.classList.add("block-modal", "thank-you-modal");
thankYouMessage.innerHTML = `
<div class="modal_inner" role="dialog" aria-labelledby="thankYouTitle" aria-describedby="thankYouMessage">
<div class="modal_inner_wrapper">
Expand All @@ -92,16 +111,16 @@ export default function(context) {
if (!modals.length) return;
const randomIndex = Math.floor(Math.random() * modals.length);
const selectedModal = modals[randomIndex];
selectedModal.setAttribute('role', 'dialog');
selectedModal.setAttribute('aria-labelledby', 'modalTitle');
selectedModal.setAttribute('aria-describedby', 'modalMessage');
selectedModal.querySelector('.field--name-field-title').id = 'modalTitle';
selectedModal.querySelector('.field--name-field-intro').id = 'modalMessage';
selectedModal.setAttribute("role", "dialog");
selectedModal.setAttribute("aria-labelledby", "modalTitle");
selectedModal.setAttribute("aria-describedby", "modalMessage");
selectedModal.querySelector(".field--name-field-title").id = "modalTitle";
selectedModal.querySelector(".field--name-field-intro").id = "modalMessage";
showModal(selectedModal);
}

const classyCookie = getCookie('classy_donation');
const hasDonated = classyCookie && classyCookie === 'true';
const classyCookie = getCookie("classy_donation");
const hasDonated = classyCookie && classyCookie === "true";

if (!getCookie(thankYouCookie) && hasDonated) {
displayThankYouModal();
Expand Down

0 comments on commit 552a371

Please sign in to comment.