| 1 |
/** |
| 2 |
* Woo My Account Logout widget behavior. |
| 3 |
* |
| 4 |
* Shows a confirmation modal before logging out. |
| 5 |
*/ |
| 6 |
(function ($) { |
| 7 |
"use strict"; |
| 8 |
|
| 9 |
const MODAL_ID = 'ka-logout-modal'; |
| 10 |
const FLAG = "kaWooLogoutBound"; |
| 11 |
|
| 12 |
const createModal = () => { |
| 13 |
const modal = document.createElement('div'); |
| 14 |
modal.className = 'ka-logout-modal'; |
| 15 |
modal.id = MODAL_ID; |
| 16 |
modal.innerHTML = ` |
| 17 |
<div class="ka-logout-modal__dialog" role="dialog" aria-modal="true" aria-labelledby="${MODAL_ID}-title"> |
| 18 |
<h3 id="${MODAL_ID}-title" class="ka-logout-modal__title">Confirm logout</h3> |
| 19 |
<p class="ka-logout-modal__desc">Are you sure you want to log out?</p> |
| 20 |
<div class="ka-logout-modal__actions"> |
| 21 |
<a class="ka-logout-modal__btn ka-logout-modal__btn--primary" data-action="confirm" href="#">Yes, log out</a> |
| 22 |
<button class="ka-logout-modal__btn ka-logout-modal__btn--ghost" type="button" data-action="cancel">Cancel</button> |
| 23 |
</div> |
| 24 |
</div> |
| 25 |
`; |
| 26 |
document.body.appendChild(modal); |
| 27 |
return modal; |
| 28 |
}; |
| 29 |
|
| 30 |
const getModal = () => document.getElementById(MODAL_ID) || createModal(); |
| 31 |
|
| 32 |
const openModal = (logoutUrl) => { |
| 33 |
const modal = getModal(); |
| 34 |
const confirmBtn = modal.querySelector('[data-action="confirm"]'); |
| 35 |
confirmBtn.setAttribute('href', logoutUrl); |
| 36 |
modal.classList.add('is-visible'); |
| 37 |
confirmBtn.focus(); |
| 38 |
}; |
| 39 |
|
| 40 |
const closeModal = () => { |
| 41 |
const modal = document.getElementById(MODAL_ID); |
| 42 |
if (modal) { |
| 43 |
modal.classList.remove('is-visible'); |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
/** |
| 48 |
* Bind delegated listeners once per page. |
| 49 |
* |
| 50 |
* @returns {void} |
| 51 |
*/ |
| 52 |
const bindOnce = () => { |
| 53 |
if (window[FLAG]) { |
| 54 |
return; |
| 55 |
} |
| 56 |
window[FLAG] = true; |
| 57 |
|
| 58 |
document.addEventListener('click', (e) => { |
| 59 |
const target = e.target; |
| 60 |
if (!(target instanceof Element)) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
const trigger = target.closest('.ka-woo-account-logout[data-ka-logout-confirm="1"]'); |
| 65 |
if (!trigger) { |
| 66 |
const cancelBtn = target.closest(`#${MODAL_ID} [data-action="cancel"]`); |
| 67 |
if (cancelBtn) { |
| 68 |
e.preventDefault(); |
| 69 |
closeModal(); |
| 70 |
} |
| 71 |
return; |
| 72 |
} |
| 73 |
e.preventDefault(); |
| 74 |
const url = trigger.getAttribute('href'); |
| 75 |
if (!url) { |
| 76 |
return; |
| 77 |
} |
| 78 |
openModal(url); |
| 79 |
}); |
| 80 |
|
| 81 |
document.addEventListener('keydown', (e) => { |
| 82 |
if (e.key === 'Escape') { |
| 83 |
closeModal(); |
| 84 |
} |
| 85 |
}); |
| 86 |
}; |
| 87 |
|
| 88 |
document.addEventListener("DOMContentLoaded", bindOnce); |
| 89 |
|
| 90 |
$(window).on("elementor/frontend/init", function () { |
| 91 |
elementorFrontend.hooks.addAction( |
| 92 |
"frontend/element_ready/woo_my_account_logout.default", |
| 93 |
function () { |
| 94 |
bindOnce(); |
| 95 |
} |
| 96 |
); |
| 97 |
}); |
| 98 |
})(jQuery); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|