class-jetpack-subscribe-modal.php
2 years ago
subscribe-modal.css
2 years ago
subscribe-modal.js
2 years ago
subscribe-modal.js
68 lines
| 1 | /* global Jetpack_Subscriptions */ |
| 2 | const { domReady } = wp; |
| 3 | |
| 4 | domReady( function () { |
| 5 | const modal = document.getElementsByClassName( 'jetpack-subscribe-modal' )[ 0 ]; |
| 6 | const modalDismissedCookie = 'jetpack_post_subscribe_modal_dismissed'; |
| 7 | const hasModalDismissedCookie = |
| 8 | document.cookie && document.cookie.indexOf( modalDismissedCookie ) > -1; |
| 9 | |
| 10 | if ( ! modal || hasModalDismissedCookie ) { |
| 11 | return; |
| 12 | } |
| 13 | |
| 14 | const close = document.getElementsByClassName( 'jetpack-subscribe-modal__close' )[ 0 ]; |
| 15 | let hasLoaded = false; |
| 16 | let isScrolling; |
| 17 | |
| 18 | window.onscroll = function () { |
| 19 | window.clearTimeout( isScrolling ); |
| 20 | |
| 21 | isScrolling = setTimeout( function () { |
| 22 | if ( ! hasLoaded ) { |
| 23 | openModal(); |
| 24 | } |
| 25 | }, Jetpack_Subscriptions.modalLoadTime ); |
| 26 | }; |
| 27 | |
| 28 | // User can edit modal, and could remove close link. |
| 29 | if ( close ) { |
| 30 | close.onclick = function ( event ) { |
| 31 | event.preventDefault(); |
| 32 | closeModal(); |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | window.onclick = function ( event ) { |
| 37 | if ( event.target === modal ) { |
| 38 | closeModal(); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | function closeModalOnEscapeKeydown( event ) { |
| 43 | if ( event.key === 'Escape' ) { |
| 44 | closeModal(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | function openModal() { |
| 49 | modal.classList.add( 'open' ); |
| 50 | document.body.classList.add( 'jetpack-subscribe-modal-open' ); |
| 51 | hasLoaded = true; |
| 52 | setModalDismissedCookie(); |
| 53 | window.addEventListener( 'keydown', closeModalOnEscapeKeydown ); |
| 54 | } |
| 55 | |
| 56 | function closeModal() { |
| 57 | modal.classList.remove( 'open' ); |
| 58 | document.body.classList.remove( 'jetpack-subscribe-modal-open' ); |
| 59 | window.removeEventListener( 'keydown', closeModalOnEscapeKeydown ); |
| 60 | } |
| 61 | |
| 62 | function setModalDismissedCookie() { |
| 63 | // Expires in 1 day |
| 64 | const expires = new Date( Date.now() + 86400 * 1000 ).toUTCString(); |
| 65 | document.cookie = `${ modalDismissedCookie }=true; expires=${ expires };path=/;`; |
| 66 | } |
| 67 | } ); |
| 68 |