|
|
|
@ -1,41 +1,44 @@ |
|
|
|
|
export class Modal { |
|
|
|
|
export class Popup { |
|
|
|
|
constructor() { |
|
|
|
|
this.triggerModal = this.triggerModal.bind(this); |
|
|
|
|
this.triggerPopup = this.triggerPopup.bind(this); |
|
|
|
|
this.onDOMLoaded = this.onDOMLoaded.bind(this); |
|
|
|
|
this.handleCloseModal = this.handleCloseModal.bind(this); |
|
|
|
|
this.handleClosePopup = this.handleClosePopup.bind(this); |
|
|
|
|
this.handleKeydown = this.handleKeydown.bind(this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Triggers the modal on target elements |
|
|
|
|
* Triggers the popup on target elements |
|
|
|
|
* @param {Event} e |
|
|
|
|
*/ |
|
|
|
|
triggerModal(e) { |
|
|
|
|
const modalToShow = e?.target?.closest('[data-modal]')?.dataset?.modal || 'unknown-modal'; |
|
|
|
|
const modalEl = document.querySelector(`#${modalToShow}`); |
|
|
|
|
triggerPopup(e) { |
|
|
|
|
const popupToShow = |
|
|
|
|
e?.target?.closest('[data-popup]')?.dataset?.popup || 'unknown-popup'; |
|
|
|
|
const popupEl = document.querySelector(`#${popupToShow}`); |
|
|
|
|
|
|
|
|
|
if (!modalEl) { |
|
|
|
|
if (!popupEl) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
modalEl.classList.remove('hidden'); |
|
|
|
|
const focusEl = modalEl.querySelector('[autofocus]'); |
|
|
|
|
popupEl.classList.remove('hidden'); |
|
|
|
|
popupEl.classList.add('flex'); |
|
|
|
|
const focusEl = popupEl.querySelector('[autofocus]'); |
|
|
|
|
if (focusEl) { |
|
|
|
|
focusEl.focus(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
handleCloseModal(e) { |
|
|
|
|
handleClosePopup(e) { |
|
|
|
|
const target = e.target; |
|
|
|
|
const modalBody = target.closest('.modal-body'); |
|
|
|
|
const closestModal = target.closest('.modal'); |
|
|
|
|
const popupBody = target.closest('.popup-body'); |
|
|
|
|
const closestPopup = target.closest('.popup'); |
|
|
|
|
|
|
|
|
|
if (modalBody) { |
|
|
|
|
if (popupBody) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (closestModal) { |
|
|
|
|
closestModal.classList.add('hidden'); |
|
|
|
|
if (closestPopup) { |
|
|
|
|
closestPopup.classList.add('hidden'); |
|
|
|
|
closestPopup.classList.remove('flex'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -44,15 +47,16 @@ export class Modal { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const modal = document.querySelector('.modal:not(.hidden)'); |
|
|
|
|
if (modal) { |
|
|
|
|
modal.classList.add('hidden'); |
|
|
|
|
const popup = document.querySelector('.popup:not(.hidden)'); |
|
|
|
|
if (popup) { |
|
|
|
|
popup.classList.add('hidden'); |
|
|
|
|
popup.classList.remove('flex'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
onDOMLoaded() { |
|
|
|
|
document.addEventListener('click', this.triggerModal); |
|
|
|
|
document.addEventListener('click', this.handleCloseModal); |
|
|
|
|
document.addEventListener('click', this.triggerPopup); |
|
|
|
|
document.addEventListener('click', this.handleClosePopup); |
|
|
|
|
document.addEventListener('keydown', this.handleKeydown); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -61,5 +65,5 @@ export class Modal { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const modalRef = new Modal(); |
|
|
|
|
modalRef.init();
|
|
|
|
|
const popupRef = new Popup(); |
|
|
|
|
popupRef.init(); |
|
|
|
|