diff options
Diffstat (limited to 'plugins/jetpack/modules/widgets/eu-cookie-law/eu-cookie-law.js')
-rw-r--r-- | plugins/jetpack/modules/widgets/eu-cookie-law/eu-cookie-law.js | 75 |
1 files changed, 46 insertions, 29 deletions
diff --git a/plugins/jetpack/modules/widgets/eu-cookie-law/eu-cookie-law.js b/plugins/jetpack/modules/widgets/eu-cookie-law/eu-cookie-law.js index 7989ee69..3fc02533 100644 --- a/plugins/jetpack/modules/widgets/eu-cookie-law/eu-cookie-law.js +++ b/plugins/jetpack/modules/widgets/eu-cookie-law/eu-cookie-law.js @@ -1,44 +1,57 @@ -( function( $ ) { +( function () { var cookieValue = document.cookie.replace( /(?:(?:^|.*;\s*)eucookielaw\s*\=\s*([^;]*).*$)|^.*$/, '$1' ), - overlay = $( '#eu-cookie-law' ), + overlay = document.getElementById( 'eu-cookie-law' ), + widget = document.querySelector( '.widget_eu_cookie_law_widget' ), + inCustomizer = widget && widget.hasAttribute( 'data-customize-widget-id' ), + getScrollTop, initialScrollPosition, scrollFunction; - if ( overlay.hasClass( 'top' ) ) { - $( '.widget_eu_cookie_law_widget' ).addClass( 'top' ); + if ( null === widget || null === overlay ) { + return; } - if ( overlay.hasClass( 'ads-active' ) ) { + /** + * Gets the amount that the window is scrolled. + * + * @return int The distance from the top of the document. + */ + getScrollTop = function () { + return Math.abs( document.body.getBoundingClientRect().y ); + }; + + if ( overlay.classList.contains( 'top' ) ) { + widget.classList.add( 'top' ); + } + + if ( overlay.classList.contains( 'ads-active' ) ) { var adsCookieValue = document.cookie.replace( /(?:(?:^|.*;\s*)personalized-ads-consent\s*\=\s*([^;]*).*$)|^.*$/, '$1' ); - if ( '' !== cookieValue && '' !== adsCookieValue ) { - overlay.remove(); + if ( '' !== cookieValue && '' !== adsCookieValue && ! inCustomizer ) { + overlay.parentNode.removeChild( overlay ); } - } else if ( '' !== cookieValue ) { - overlay.remove(); + } else if ( '' !== cookieValue && ! inCustomizer ) { + overlay.parentNode.removeChild( overlay ); } - $( '.widget_eu_cookie_law_widget' ) - .appendTo( 'body' ) - .fadeIn(); - - overlay.find( 'form' ).on( 'submit', accept ); + document.body.appendChild( widget ); + overlay.querySelector( 'form' ).addEventListener( 'submit', accept ); - if ( overlay.hasClass( 'hide-on-scroll' ) ) { - initialScrollPosition = $( window ).scrollTop(); - scrollFunction = function() { - if ( Math.abs( $( window ).scrollTop() - initialScrollPosition ) > 50 ) { + if ( overlay.classList.contains( 'hide-on-scroll' ) ) { + initialScrollPosition = getScrollTop(); + scrollFunction = function () { + if ( Math.abs( getScrollTop() - initialScrollPosition ) > 50 ) { accept(); } }; - $( window ).on( 'scroll', scrollFunction ); - } else if ( overlay.hasClass( 'hide-on-time' ) ) { - setTimeout( accept, overlay.data( 'hide-timeout' ) * 1000 ); + window.addEventListener( 'scroll', scrollFunction ); + } else if ( overlay.classList.contains( 'hide-on-time' ) ) { + setTimeout( accept, overlay.getAttribute( 'data-hide-timeout' ) * 1000 ); } var accepted = false; @@ -52,18 +65,21 @@ event.preventDefault(); } - if ( overlay.hasClass( 'hide-on-scroll' ) ) { - $( window ).off( 'scroll', scrollFunction ); + if ( overlay.classList.contains( 'hide-on-scroll' ) ) { + window.removeEventListener( 'scroll', scrollFunction ); } var expireTime = new Date(); expireTime.setTime( - expireTime.getTime() + overlay.data( 'consent-expiration' ) * 24 * 60 * 60 * 1000 + expireTime.getTime() + overlay.getAttribute( 'data-consent-expiration' ) * 24 * 60 * 60 * 1000 ); document.cookie = 'eucookielaw=' + expireTime.getTime() + ';path=/;expires=' + expireTime.toGMTString(); - if ( overlay.hasClass( 'ads-active' ) && overlay.hasClass( 'hide-on-button' ) ) { + if ( + overlay.classList.contains( 'ads-active' ) && + overlay.classList.contains( 'hide-on-button' ) + ) { document.cookie = 'personalized-ads-consent=' + expireTime.getTime() + @@ -71,10 +87,11 @@ expireTime.toGMTString(); } - overlay.fadeOut( 400, function() { - overlay.remove(); + overlay.classList.add( 'hide' ); + setTimeout( function () { + overlay.parentNode.removeChild( overlay ); var widgetSection = document.querySelector( '.widget.widget_eu_cookie_law_widget' ); widgetSection.parentNode.removeChild( widgetSection ); - } ); + }, 400 ); } -} )( jQuery ); +} )(); |