131 lines
3.6 KiB
JavaScript
Executable File
131 lines
3.6 KiB
JavaScript
Executable File
window.cookieConsent = {
|
|
storageKey: 'gprompt_cookie_consent',
|
|
bannerId: 'cookieConsentBanner',
|
|
preferencesModalId: 'cookieConsentPreferences',
|
|
|
|
init() {
|
|
if (!this.hasConsented()) {
|
|
this.showBanner();
|
|
}
|
|
},
|
|
|
|
hasConsented() {
|
|
try {
|
|
return localStorage.getItem(this.storageKey) !== null;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
},
|
|
|
|
getConsents() {
|
|
try {
|
|
const stored = localStorage.getItem(this.storageKey);
|
|
return stored ? JSON.parse(stored) : null;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
},
|
|
|
|
showBanner() {
|
|
const banner = document.getElementById(this.bannerId);
|
|
if (banner) {
|
|
banner.style.display = 'block';
|
|
document.body.style.paddingBottom = banner.offsetHeight + 'px';
|
|
}
|
|
},
|
|
|
|
hideBanner() {
|
|
const banner = document.getElementById(this.bannerId);
|
|
if (banner) {
|
|
banner.style.display = 'none';
|
|
document.body.style.paddingBottom = '';
|
|
}
|
|
},
|
|
|
|
showPreferences() {
|
|
const modalEl = document.getElementById(this.preferencesModalId);
|
|
if (modalEl) {
|
|
const modal = new bootstrap.Modal(modalEl);
|
|
modal.show();
|
|
|
|
const consents = this.getConsents();
|
|
if (consents) {
|
|
document.getElementById('cookieStatistics').checked = !!consents.statistics;
|
|
document.getElementById('cookieMarketing').checked = !!consents.marketing;
|
|
document.getElementById('cookiePreferences').checked = !!consents.preferences;
|
|
}
|
|
}
|
|
},
|
|
|
|
acceptAll() {
|
|
const consents = {
|
|
necessary: true,
|
|
statistics: true,
|
|
marketing: true,
|
|
preferences: true,
|
|
};
|
|
|
|
this.save(consents);
|
|
this.hideBanner();
|
|
},
|
|
|
|
reject() {
|
|
const consents = {
|
|
necessary: true,
|
|
statistics: false,
|
|
marketing: false,
|
|
preferences: false,
|
|
};
|
|
|
|
this.save(consents);
|
|
this.hideBanner();
|
|
},
|
|
|
|
savePreferences() {
|
|
const consents = {
|
|
necessary: true,
|
|
statistics: document.getElementById('cookieStatistics').checked,
|
|
marketing: document.getElementById('cookieMarketing').checked,
|
|
preferences: document.getElementById('cookiePreferences').checked,
|
|
};
|
|
|
|
this.save(consents);
|
|
this.hideBanner();
|
|
|
|
const modalEl = document.getElementById(this.preferencesModalId);
|
|
if (modalEl) {
|
|
const modal = bootstrap.Modal.getInstance(modalEl);
|
|
if (modal) {
|
|
modal.hide();
|
|
}
|
|
}
|
|
},
|
|
|
|
save(consents) {
|
|
try {
|
|
localStorage.setItem(this.storageKey, JSON.stringify(consents));
|
|
} catch (e) {
|
|
console.warn('localStorage is not available');
|
|
}
|
|
|
|
fetch('/cookie-consent', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
|
|
},
|
|
body: JSON.stringify({ consents }),
|
|
}).catch(() => {
|
|
// Silently fail - consent saved locally
|
|
});
|
|
},
|
|
};
|
|
|
|
if (document.readyState === 'interactive' || document.readyState === 'complete') {
|
|
window.cookieConsent.init();
|
|
} else {
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
window.cookieConsent.init();
|
|
});
|
|
}
|