1/** 2 * @file 3 * Modifies the behaviour of the newsletter_popup defined in the simple-popup-blocks module's settings. 4 * The popup won't show when the user has closed it or signed up. 5 * Do not modify delay value on the module's settings page, use variable delay instead. 6 * @see https://iisd.ddev.site/admin/config/media/simple-popup-blocks/manage/newsletter_popup 7 */ 8 9(function ($, Drupal, cookies) { 10 11 'use strict'; 12 13 Drupal.behaviors.newsletterPopup = { 14 attach: function (context) { 15 // popup delay in seconds. 16 const delay = 45; 17 18 const $newsletterPopup = $('#spb-c-newsletter-popup'); 19 const $newsletterCloseButton = $('#spb-c-newsletter-popup .spb_close', context); 20 const $signUpButton = $('.c-newsletter-popup__form-submit', context); 21 // Delay cookie expiry date. 22 // see https://github.com/js-cookie/js-cookie/wiki/Frequently-Asked-Questions#expire-cookies-in-less-than-a-day 23 const delay_expiry = new Date(new Date().getTime() + delay * 1000); 24 25 if ((!cookies.get('newsletter_popup_closed')) && (!cookies.get('newsletter_delay_expired'))) { 26 if (!cookies.get('newsletter_delay')) { 27 cookies.set('newsletter_delay', delay_expiry, {expires: delay_expiry}); 28 } 29 30 // This is defined to make sure we don't 'reset' the delay time 31 // for the user session. The delay is saved inside the 'newsletter_delay' cookie. 32 // The timeout variable is the remaining delay value for the session. 33 var timeout = (Date.parse(cookies.get('newsletter_delay')) - new Date().getTime()); 34 35 // Show the popup after a timeout. 36 setTimeout(function () { 37 $newsletterPopup.show(); 38 cookies.set('newsletter_delay_expired', 1, {expires: 1825}); 39 }, timeout); 40 } 41 42 $(once('newsletterPopupClose',$newsletterCloseButton)).click(function () { 43 setCookieClosed(); 44 }); 45 46 $(once('newsletterPopupSignup',$signUpButton)).click(function () { 47 setCookieClosed(); 48 }); 49 50 // If the popup was never closed before or delay cookie has expired, show the popup. 51 if ((!cookies.get('newsletter_popup_closed') && (!cookies.get('newsletter_delay')))) { 52 $newsletterPopup.show(); 53 } 54 55 function setCookieClosed() { 56 // Set a cookie (365 * 5 = 1825) 57 cookies.set('newsletter_popup_closed', 1, {expires: 1825}); 58 } 59 } 60 }; 61}) 62(jQuery, Drupal, window.Cookies);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.