class-jetpack-subscribe-modal.php
2 years ago
subscribe-modal.css
2 years ago
subscribe-modal.js
2 years ago
subscribe-modal.js
109 lines
| 1 | /* global Jetpack_Subscriptions */ |
| 2 | const { domReady } = wp; |
| 3 | domReady( () => { |
| 4 | const modal = document.querySelector( '.jetpack-subscribe-modal' ); |
| 5 | const modalDismissedCookie = 'jetpack_post_subscribe_modal_dismissed'; |
| 6 | |
| 7 | function hasEnoughTimePassed() { |
| 8 | const lastDismissed = localStorage.getItem( modalDismissedCookie ); |
| 9 | return lastDismissed ? Date.now() - lastDismissed > Jetpack_Subscriptions.modalInterval : true; |
| 10 | } |
| 11 | |
| 12 | if ( ! modal || ! hasEnoughTimePassed() ) { |
| 13 | return; |
| 14 | } |
| 15 | |
| 16 | const modalLoadTimeout = setTimeout( openModal, Jetpack_Subscriptions.modalLoadTime ); |
| 17 | |
| 18 | const targetElement = ( |
| 19 | document.querySelector( '.entry-content' ) || document.documentElement |
| 20 | ).getBoundingClientRect(); |
| 21 | |
| 22 | function hasPassedScrollThreshold() { |
| 23 | const scrollPosition = window.scrollY + window.innerHeight / 2; |
| 24 | const scrollPositionThreshold = |
| 25 | targetElement.top + |
| 26 | ( targetElement.height * Jetpack_Subscriptions.modalScrollThreshold ) / 100; |
| 27 | return scrollPosition > scrollPositionThreshold; |
| 28 | } |
| 29 | |
| 30 | function onScroll() { |
| 31 | requestAnimationFrame( () => { |
| 32 | if ( hasPassedScrollThreshold() ) { |
| 33 | openModal(); |
| 34 | } |
| 35 | } ); |
| 36 | } |
| 37 | |
| 38 | window.addEventListener( 'scroll', onScroll, { passive: true } ); |
| 39 | |
| 40 | // This take care of the case where the user has multiple tabs open. |
| 41 | function onLocalStorage( event ) { |
| 42 | if ( event.key === modalDismissedCookie ) { |
| 43 | closeModal(); |
| 44 | removeEventListeners(); |
| 45 | } |
| 46 | } |
| 47 | window.addEventListener( 'storage', onLocalStorage ); |
| 48 | |
| 49 | // When the form is submitted, and next modal loads, it'll fire "subscription-modal-loaded" signalling that this form can be hidden. |
| 50 | const form = modal.querySelector( 'form' ); |
| 51 | if ( form ) { |
| 52 | form.addEventListener( 'subscription-modal-loaded', closeModal ); |
| 53 | } |
| 54 | |
| 55 | // User can edit modal, and could remove close link. |
| 56 | function onCloseButtonClick( event ) { |
| 57 | event.preventDefault(); |
| 58 | closeModal(); |
| 59 | } |
| 60 | const close = document.getElementsByClassName( 'jetpack-subscribe-modal__close' )[ 0 ]; |
| 61 | if ( close ) { |
| 62 | close.addEventListener( 'click', onCloseButtonClick ); |
| 63 | } |
| 64 | |
| 65 | function closeOnWindowClick( event ) { |
| 66 | if ( event.target === modal ) { |
| 67 | closeModal(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | function closeModalOnEscapeKeydown( event ) { |
| 72 | if ( event.key === 'Escape' ) { |
| 73 | closeModal(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | function openModal() { |
| 78 | // If the user is typing in a form, don't open the modal or has anything else focused. |
| 79 | if ( document.activeElement && document.activeElement.tagName !== 'BODY' ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | modal.classList.add( 'open' ); |
| 84 | document.body.classList.add( 'jetpack-subscribe-modal-open' ); |
| 85 | window.addEventListener( 'keydown', closeModalOnEscapeKeydown ); |
| 86 | window.addEventListener( 'click', closeOnWindowClick ); |
| 87 | removeEventListeners(); |
| 88 | } |
| 89 | |
| 90 | function closeModal() { |
| 91 | modal.classList.remove( 'open' ); |
| 92 | document.body.classList.remove( 'jetpack-subscribe-modal-open' ); |
| 93 | window.removeEventListener( 'keydown', closeModalOnEscapeKeydown ); |
| 94 | window.removeEventListener( 'storage', onLocalStorage ); |
| 95 | window.removeEventListener( 'click', closeOnWindowClick ); |
| 96 | storeCloseTimestamp(); |
| 97 | } |
| 98 | |
| 99 | // Remove all event listeners. That would add the modal again. |
| 100 | function removeEventListeners() { |
| 101 | window.removeEventListener( 'scroll', onScroll ); |
| 102 | clearTimeout( modalLoadTimeout ); |
| 103 | } |
| 104 | |
| 105 | function storeCloseTimestamp() { |
| 106 | localStorage.setItem( modalDismissedCookie, Date.now() ); |
| 107 | } |
| 108 | } ); |
| 109 |