Initial release of KGV Cookie plugin v1.0.0
This commit is contained in:
102
assets/js/kgv-cookie.js
Normal file
102
assets/js/kgv-cookie.js
Normal file
@@ -0,0 +1,102 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var config = window.kgvCookieConfig || {};
|
||||
var cookieName = config.cookieName || 'kgv_cookie_consent';
|
||||
|
||||
function setCookie(name, value, days) {
|
||||
var expires = '';
|
||||
if (days) {
|
||||
var date = new Date();
|
||||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
||||
expires = '; expires=' + date.toUTCString();
|
||||
}
|
||||
document.cookie = name + '=' + encodeURIComponent(value) + expires + '; path=/; SameSite=Lax';
|
||||
}
|
||||
|
||||
function getCookie(name) {
|
||||
var nameEQ = name + '=';
|
||||
var ca = document.cookie.split(';');
|
||||
for (var i = 0; i < ca.length; i++) {
|
||||
var c = ca[i];
|
||||
while (c.charAt(0) === ' ') {
|
||||
c = c.substring(1, c.length);
|
||||
}
|
||||
if (c.indexOf(nameEQ) === 0) {
|
||||
return decodeURIComponent(c.substring(nameEQ.length, c.length));
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
function publishConsent(value) {
|
||||
window.KGVCookieConsent = value;
|
||||
window.dispatchEvent(new CustomEvent('kgvCookieConsentChanged', { detail: { value: value } }));
|
||||
}
|
||||
|
||||
function hasConsent(type) {
|
||||
var current = getCookie(cookieName);
|
||||
if (!current) {
|
||||
return false;
|
||||
}
|
||||
if (current === 'all') {
|
||||
return true;
|
||||
}
|
||||
return type === 'essential' && current === 'essential';
|
||||
}
|
||||
|
||||
function init() {
|
||||
var banner = document.getElementById('kgv-cookie-banner');
|
||||
if (!banner) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.KGVCookie = {
|
||||
getConsent: function () {
|
||||
return getCookie(cookieName);
|
||||
},
|
||||
hasConsent: hasConsent,
|
||||
reopen: function () {
|
||||
banner.hidden = false;
|
||||
banner.classList.add('is-visible');
|
||||
}
|
||||
};
|
||||
|
||||
var current = getCookie(cookieName);
|
||||
if (!current) {
|
||||
banner.hidden = false;
|
||||
banner.classList.add('is-visible');
|
||||
} else {
|
||||
publishConsent(current);
|
||||
}
|
||||
|
||||
document.addEventListener('click', function (event) {
|
||||
var trigger = event.target.closest('[data-kgv-cookie]');
|
||||
if (!trigger) {
|
||||
return;
|
||||
}
|
||||
|
||||
var action = trigger.getAttribute('data-kgv-cookie');
|
||||
if (action === 'accept-all') {
|
||||
setCookie(cookieName, 'all', 365);
|
||||
publishConsent('all');
|
||||
banner.classList.remove('is-visible');
|
||||
banner.hidden = true;
|
||||
}
|
||||
|
||||
if (action === 'essential-only') {
|
||||
setCookie(cookieName, 'essential', 365);
|
||||
publishConsent('essential');
|
||||
banner.classList.remove('is-visible');
|
||||
banner.hidden = true;
|
||||
}
|
||||
|
||||
if (action === 'reopen') {
|
||||
banner.hidden = false;
|
||||
banner.classList.add('is-visible');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
})();
|
||||
Reference in New Issue
Block a user