Files
myAi/web/wwwroot/js/modules/cookie-consent.js
T
claude 98979b58f8 refactor: Extract shared JavaScript utilities (Step 1 of 6)
Create reusable utility modules to eliminate duplication across main.js,
cv-matcher.js, and legal.js:

- js/utils/form-helpers.js: showFieldError, clearFieldErrors, isValidEmail,
  extractApiError — shared form validation and error handling
- js/utils/i18n.js: currentLang, t, applyLanguage, updateLegalLinks,
  browserLang — shared translation and language switching
- js/utils/api.js: checkApiLive, getRecaptchaWebKey, getGoogleTagManagerId,
  loadGoogleTagManager — shared API configuration loading
- js/modules/cookie-consent.js: getConsent, setConsent, initConsent,
  setupConsentHandlers — cookie banner and consent management

All utilities exposed on window.MyAi namespace for use by existing pages.
Full JSDoc headers and inline comments for maintainability.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-05-29 09:05:51 +03:00

132 lines
3.0 KiB
JavaScript

/**
* Cookie Consent Management
*
* Handles cookie banner display, user consent preferences, and localStorage persistence.
* Integrates with analytics loading via applyConsent() callback.
*/
var CONSENT_KEY = "myai_cookie_consent";
/**
* Parse consent object from localStorage.
* Expected format: { necessary: true, analytics: bool, ts: iso-timestamp }
*
* @returns {object|null} - Parsed consent object or null if not found or invalid JSON
*/
function getConsent() {
try {
return JSON.parse(localStorage.getItem(CONSENT_KEY));
} catch (e) {
return null;
}
}
/**
* Persist consent object to localStorage as JSON.
* Stores { necessary, analytics, ts } properties.
*
* @param {object} consent - Consent object with necessary, analytics, ts properties
*/
function setConsent(consent) {
localStorage.setItem(CONSENT_KEY, JSON.stringify(consent));
}
/**
* Apply stored consent preferences.
* Currently loads Google Tag Manager if analytics consent is true.
* Hook for extending to other tracking/analytics systems.
*
* @param {object} consent - Consent object from localStorage
*/
function applyConsent(consent) {
if (window.MyAi && window.MyAi.applyConsent) {
window.MyAi.applyConsent(consent);
}
}
/**
* Show cookie consent banner with fade-in animation.
*/
function showBanner() {
$('#cookieBanner').fadeIn(200);
}
/**
* Hide cookie consent banner with fade-out animation.
*/
function hideBanner() {
$('#cookieBanner').fadeOut(200);
}
/**
* Show "manage cookies" button.
* Appears after user has made a consent choice.
*/
function showManage() {
$('#cookieManage').show();
}
/**
* Initialize cookie consent flow on page load.
* If user has already made a choice, apply preferences and show manage button.
* If no choice yet, show consent banner.
*/
function initConsent() {
var consent = getConsent();
if (!consent) {
showBanner();
} else {
applyConsent(consent);
showManage();
}
}
/**
* Bind cookie consent button handlers.
* Reject or Necessary-only button → analytics: false
* Accept button → analytics: true (and load GTM)
* Manage button → show banner again
*/
function setupConsentHandlers() {
$('#cookieReject, #cookieNecessary').on('click', function () {
setConsent({
necessary: true,
analytics: false,
ts: new Date().toISOString()
});
hideBanner();
showManage();
});
$('#cookieAccept').on('click', function () {
var consent = {
necessary: true,
analytics: true,
ts: new Date().toISOString()
};
setConsent(consent);
applyConsent(consent);
hideBanner();
showManage();
});
$('#cookieManage').on('click', function (e) {
e.preventDefault();
showBanner();
});
}
// Expose consent utilities on window.MyAi
window.MyAi = window.MyAi || {};
window.MyAi.getConsent = getConsent;
window.MyAi.setConsent = setConsent;
window.MyAi.initConsent = initConsent;
window.MyAi.showBanner = showBanner;
window.MyAi.hideBanner = hideBanner;
// Initialize on DOM ready
$(function() {
setupConsentHandlers();
initConsent();
});