kollapsminoriteten/wp-content/plugins/jetpack/modules/subscriptions/subscribe-modal/subscribe-modal.js

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-09-26 10:24:36 +02:00
const { domReady } = wp;
domReady( function () {
const modal = document.getElementsByClassName( 'jetpack-subscribe-modal' )[ 0 ];
2024-02-16 11:03:01 +01:00
const modalDismissedCookie = 'jetpack_post_subscribe_modal_dismissed';
const hasModalDismissedCookie =
document.cookie && document.cookie.indexOf( modalDismissedCookie ) > -1;
2023-09-26 10:24:36 +02:00
2024-02-16 11:03:01 +01:00
if ( ! modal || hasModalDismissedCookie ) {
2023-09-26 10:24:36 +02:00
return;
}
const close = document.getElementsByClassName( 'jetpack-subscribe-modal__close' )[ 0 ];
let hasLoaded = false;
let isScrolling;
window.onscroll = function () {
window.clearTimeout( isScrolling );
isScrolling = setTimeout( function () {
2024-02-16 11:03:01 +01:00
if ( ! hasLoaded ) {
openModal();
2023-09-26 10:24:36 +02:00
}
}, 300 );
};
// User can edit modal, and could remove close link.
if ( close ) {
2024-02-16 11:03:01 +01:00
close.onclick = function ( event ) {
event.preventDefault();
closeModal();
2023-09-26 10:24:36 +02:00
};
}
window.onclick = function ( event ) {
if ( event.target === modal ) {
2024-02-16 11:03:01 +01:00
closeModal();
2023-09-26 10:24:36 +02:00
}
};
2024-02-16 11:03:01 +01:00
function closeModalOnEscapeKeydown( event ) {
if ( event.key === 'Escape' ) {
closeModal();
}
}
function openModal() {
modal.classList.add( 'open' );
document.body.classList.add( 'jetpack-subscribe-modal-open' );
hasLoaded = true;
setModalDismissedCookie();
window.addEventListener( 'keydown', closeModalOnEscapeKeydown );
}
function closeModal() {
modal.classList.remove( 'open' );
document.body.classList.remove( 'jetpack-subscribe-modal-open' );
window.removeEventListener( 'keydown', closeModalOnEscapeKeydown );
}
2023-09-26 10:24:36 +02:00
function setModalDismissedCookie() {
// Expires in 1 day
const expires = new Date( Date.now() + 86400 * 1000 ).toUTCString();
2023-12-07 09:44:11 +01:00
document.cookie = `${ modalDismissedCookie }=true; expires=${ expires };path=/;`;
2023-09-26 10:24:36 +02:00
}
} );