Initial release of KGV Cookie plugin v1.0.0
This commit is contained in:
82
assets/css/kgv-cookie.css
Normal file
82
assets/css/kgv-cookie.css
Normal file
@@ -0,0 +1,82 @@
|
||||
.kgv-cookie-banner {
|
||||
position: fixed;
|
||||
left: 1rem;
|
||||
right: 1rem;
|
||||
z-index: 99999;
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
background: #13315c;
|
||||
color: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 1rem 1.25rem;
|
||||
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
|
||||
opacity: 0;
|
||||
transform: translateY(12px);
|
||||
transition: opacity 0.25s ease, transform 0.25s ease;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner--bottom {
|
||||
bottom: 1rem;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner--top {
|
||||
top: 1rem;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner.is-visible {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
.kgv-cookie-banner__text p {
|
||||
margin: 0.25rem 0;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner__text a {
|
||||
color: #f7d488;
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner__actions {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.kgv-cookie-btn,
|
||||
.kgv-cookie-reopen {
|
||||
border: 1px solid rgba(255, 255, 255, 0.5);
|
||||
background: transparent;
|
||||
color: #fff;
|
||||
border-radius: 8px;
|
||||
padding: 0.55rem 0.85rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.kgv-cookie-btn--primary {
|
||||
border-color: #f7d488;
|
||||
background: #f7d488;
|
||||
color: #13315c;
|
||||
}
|
||||
|
||||
.kgv-cookie-btn:hover,
|
||||
.kgv-cookie-reopen:hover {
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
@media (max-width: 680px) {
|
||||
.kgv-cookie-banner {
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner__actions {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.kgv-cookie-btn {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
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