| 1 |
(function () { |
| 2 |
function toArray(nl) { |
| 3 |
return Array.prototype.slice.call(nl || []); |
| 4 |
} |
| 5 |
|
| 6 |
function setCookie(name, value, days) { |
| 7 |
var expires = ''; |
| 8 |
if (days) { |
| 9 |
var date = new Date(); |
| 10 |
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); |
| 11 |
expires = '; expires=' + date.toUTCString(); |
| 12 |
} |
| 13 |
document.cookie = name + '=' + encodeURIComponent(value) + expires + '; path=/; SameSite=Lax'; |
| 14 |
} |
| 15 |
|
| 16 |
function getCookie(name) { |
| 17 |
var match = document.cookie.match(new RegExp('(?:^|; )' + name.replace(/[()[\]{}*+?^$|#\\]/g, '\\$&') + '=([^;]*)')); |
| 18 |
return match ? decodeURIComponent(match[1]) : null; |
| 19 |
} |
| 20 |
|
| 21 |
function pushGTM(categories) { |
| 22 |
if (typeof window.dataLayer !== 'undefined') { |
| 23 |
window.dataLayer.push({ |
| 24 |
event: 'bkbg_cookie_consent', |
| 25 |
cookie_consent: categories |
| 26 |
}); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
function initCookieConsent(banner) { |
| 31 |
var cookieName = banner.getAttribute('data-cookie-name') || 'bkbg_cookie_consent'; |
| 32 |
var cookieDays = parseInt(banner.getAttribute('data-cookie-days'), 10) || 365; |
| 33 |
var position = banner.getAttribute('data-position') || 'bottom'; |
| 34 |
var animation = banner.getAttribute('data-animation') || 'slide'; |
| 35 |
|
| 36 |
// Already consented? Skip. |
| 37 |
if (getCookie(cookieName) || localStorage.getItem(cookieName)) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
var acceptBtn = banner.querySelector('[data-bkbg-cc-accept]'); |
| 42 |
var declineBtn = banner.querySelector('[data-bkbg-cc-decline]'); |
| 43 |
var customizeBtn = banner.querySelector('[data-bkbg-cc-customize]'); |
| 44 |
var saveBtn = banner.querySelector('[data-bkbg-cc-save]'); |
| 45 |
var categoriesPanel = banner.querySelector('.bkbg-cc-categories'); |
| 46 |
|
| 47 |
function hideBanner() { |
| 48 |
banner.classList.remove('is-visible'); |
| 49 |
banner.addEventListener('transitionend', function handler() { |
| 50 |
banner.remove(); |
| 51 |
banner.removeEventListener('transitionend', handler); |
| 52 |
}); |
| 53 |
} |
| 54 |
|
| 55 |
function acceptAll() { |
| 56 |
var categories = { |
| 57 |
necessary: true, |
| 58 |
analytics: true, |
| 59 |
marketing: true, |
| 60 |
functional: true |
| 61 |
}; |
| 62 |
var value = JSON.stringify(categories); |
| 63 |
setCookie(cookieName, value, cookieDays); |
| 64 |
localStorage.setItem(cookieName, value); |
| 65 |
pushGTM(categories); |
| 66 |
hideBanner(); |
| 67 |
} |
| 68 |
|
| 69 |
function declineAll() { |
| 70 |
var categories = { |
| 71 |
necessary: true, |
| 72 |
analytics: false, |
| 73 |
marketing: false, |
| 74 |
functional: false |
| 75 |
}; |
| 76 |
var value = JSON.stringify(categories); |
| 77 |
setCookie(cookieName, value, cookieDays); |
| 78 |
localStorage.setItem(cookieName, value); |
| 79 |
pushGTM(categories); |
| 80 |
hideBanner(); |
| 81 |
} |
| 82 |
|
| 83 |
function savePreferences() { |
| 84 |
var checkboxes = toArray((categoriesPanel || banner).querySelectorAll('[data-bkbg-cc-category]')); |
| 85 |
var categories = { necessary: true }; |
| 86 |
checkboxes.forEach(function (cb) { |
| 87 |
categories[cb.getAttribute('data-bkbg-cc-category')] = cb.checked; |
| 88 |
}); |
| 89 |
var value = JSON.stringify(categories); |
| 90 |
setCookie(cookieName, value, cookieDays); |
| 91 |
localStorage.setItem(cookieName, value); |
| 92 |
pushGTM(categories); |
| 93 |
hideBanner(); |
| 94 |
} |
| 95 |
|
| 96 |
if (acceptBtn) acceptBtn.addEventListener('click', acceptAll); |
| 97 |
if (declineBtn) declineBtn.addEventListener('click', declineAll); |
| 98 |
|
| 99 |
if (customizeBtn && categoriesPanel) { |
| 100 |
customizeBtn.addEventListener('click', function () { |
| 101 |
var isHidden = categoriesPanel.hidden; |
| 102 |
categoriesPanel.hidden = !isHidden; |
| 103 |
}); |
| 104 |
} |
| 105 |
|
| 106 |
if (saveBtn) saveBtn.addEventListener('click', savePreferences); |
| 107 |
|
| 108 |
// Show banner after short delay |
| 109 |
setTimeout(function () { |
| 110 |
banner.classList.add('is-visible'); |
| 111 |
}, 400); |
| 112 |
} |
| 113 |
|
| 114 |
function init() { |
| 115 |
// Only run on frontend (not in block editor) |
| 116 |
if (document.body.classList.contains('block-editor-page')) return; |
| 117 |
|
| 118 |
toArray(document.querySelectorAll('.bkbg-cc-banner')).forEach(initCookieConsent); |
| 119 |
} |
| 120 |
|
| 121 |
if (document.readyState === 'loading') { |
| 122 |
document.addEventListener('DOMContentLoaded', init); |
| 123 |
} else { |
| 124 |
init(); |
| 125 |
} |
| 126 |
})(); |
| 127 |
|