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

124 lines
3.7 KiB
JavaScript
Raw Normal View History

2025-02-28 08:42:11 +01:00
/* global Jetpack_Subscriptions */
2023-09-26 10:24:36 +02:00
const { domReady } = wp;
2025-02-28 08:42:11 +01:00
domReady( () => {
const modal = document.querySelector( '.jetpack-subscribe-modal' );
2024-02-16 11:03:01 +01:00
const modalDismissedCookie = 'jetpack_post_subscribe_modal_dismissed';
2025-02-28 08:42:11 +01:00
const skipUrlParam = 'jetpack_skip_subscription_popup';
function hasEnoughTimePassed() {
const lastDismissed = localStorage.getItem( modalDismissedCookie );
return lastDismissed ? Date.now() - lastDismissed > Jetpack_Subscriptions.modalInterval : true;
}
// Subscriber ended up here e.g. from emails:
// we won't show the modal to them in future since they most likely are already a subscriber.
function skipModal() {
const url = new URL( window.location.href );
if ( url.searchParams.has( skipUrlParam ) ) {
url.searchParams.delete( skipUrlParam );
window.history.replaceState( {}, '', url );
storeCloseTimestamp();
return true;
}
return false;
}
2023-09-26 10:24:36 +02:00
2025-02-28 08:42:11 +01:00
if ( ! modal || ! hasEnoughTimePassed() || skipModal() ) {
2023-09-26 10:24:36 +02:00
return;
}
2025-02-28 08:42:11 +01:00
const modalLoadTimeout = setTimeout( openModal, Jetpack_Subscriptions.modalLoadTime );
const targetElement = (
document.querySelector( '.entry-content' ) || document.documentElement
).getBoundingClientRect();
2023-09-26 10:24:36 +02:00
2025-02-28 08:42:11 +01:00
function hasPassedScrollThreshold() {
const scrollPosition = window.scrollY + window.innerHeight / 2;
const scrollPositionThreshold =
targetElement.top +
( targetElement.height * Jetpack_Subscriptions.modalScrollThreshold ) / 100;
return scrollPosition > scrollPositionThreshold;
}
2023-09-26 10:24:36 +02:00
2025-02-28 08:42:11 +01:00
function onScroll() {
requestAnimationFrame( () => {
if ( hasPassedScrollThreshold() ) {
2024-02-16 11:03:01 +01:00
openModal();
2023-09-26 10:24:36 +02:00
}
2025-02-28 08:42:11 +01:00
} );
}
window.addEventListener( 'scroll', onScroll, { passive: true } );
// This take care of the case where the user has multiple tabs open.
function onLocalStorage( event ) {
if ( event.key === modalDismissedCookie ) {
closeModal();
removeEventListeners();
}
}
window.addEventListener( 'storage', onLocalStorage );
// When the form is submitted, and next modal loads, it'll fire "subscription-modal-loaded" signalling that this form can be hidden.
const form = modal.querySelector( 'form' );
if ( form ) {
form.addEventListener( 'subscription-modal-loaded', closeModal );
}
2023-09-26 10:24:36 +02:00
// User can edit modal, and could remove close link.
2025-02-28 08:42:11 +01:00
function onCloseButtonClick( event ) {
event.preventDefault();
closeModal();
}
const close = document.getElementsByClassName( 'jetpack-subscribe-modal__close' )[ 0 ];
2023-09-26 10:24:36 +02:00
if ( close ) {
2025-02-28 08:42:11 +01:00
close.addEventListener( 'click', onCloseButtonClick );
2023-09-26 10:24:36 +02:00
}
2025-02-28 08:42:11 +01:00
function closeOnWindowClick( event ) {
2023-09-26 10:24:36 +02:00
if ( event.target === modal ) {
2024-02-16 11:03:01 +01:00
closeModal();
2023-09-26 10:24:36 +02:00
}
2025-02-28 08:42:11 +01:00
}
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() {
2025-02-28 08:42:11 +01:00
// If the user is typing in a form, don't open the modal or has anything else focused.
if ( document.activeElement && document.activeElement.tagName !== 'BODY' ) {
return;
}
2024-02-16 11:03:01 +01:00
modal.classList.add( 'open' );
document.body.classList.add( 'jetpack-subscribe-modal-open' );
window.addEventListener( 'keydown', closeModalOnEscapeKeydown );
2025-02-28 08:42:11 +01:00
window.addEventListener( 'click', closeOnWindowClick );
removeEventListeners();
2024-02-16 11:03:01 +01:00
}
function closeModal() {
modal.classList.remove( 'open' );
document.body.classList.remove( 'jetpack-subscribe-modal-open' );
window.removeEventListener( 'keydown', closeModalOnEscapeKeydown );
2025-02-28 08:42:11 +01:00
window.removeEventListener( 'storage', onLocalStorage );
window.removeEventListener( 'click', closeOnWindowClick );
storeCloseTimestamp();
}
// Remove all event listeners. That would add the modal again.
function removeEventListeners() {
window.removeEventListener( 'scroll', onScroll );
clearTimeout( modalLoadTimeout );
2024-02-16 11:03:01 +01:00
}
2025-02-28 08:42:11 +01:00
function storeCloseTimestamp() {
localStorage.setItem( modalDismissedCookie, Date.now() );
2023-09-26 10:24:36 +02:00
}
} );