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