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