var consentManager = {}; consentManager.ajax = (function () { var csrfToken = "htxGjx7DY00Ddee197BiH2BG4ZrStLoeG0qm7GGDyQqaQZZKHbynzzInkGA6ZXUc"; var serializeData = function (data) { var serialized = ""; if (!data) { return serialized; } for (var key in data) { if (!data.hasOwnProperty(key)) { continue; } if (serialized !== "") { serialized += "&"; } serialized += key + "=" + encodeURIComponent(data[key]) } return serialized; }; var prepareUrl = function (url, data) { url += (url.indexOf('?') === -1) ? '?' : '&'; return attachVersion(url + serializeData(data)); }; var makeRequest = function (type, options, callback) { if (!options.url) { throw new Error("Missing request url"); } var method = type.toUpperCase() || "GET"; var data = options.data || {}; var url = method === 'POST' ? options.url : prepareUrl(options.url, options.data); var request = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); request.open(method, url, true); request.setRequestHeader("X-CSRFToken", csrfToken); request.onload = function () { if (callback && typeof(callback) === "function") { var data = request.responseText; try { data = JSON.parse(request.responseText); } catch (e) { // } callback(data, request.status, request); } }; var postData = undefined; if (method === 'POST') { request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); postData = serializeData(data); } request.send(postData); }; var get = function (options, callback) { return makeRequest('GET', options, callback); }; var post = function (options, callback) { return makeRequest('POST', options, callback); }; var attachVersion = function (urlString, paramName) { urlString += (urlString.indexOf('?') === -1) ? '?' : '&'; urlString += (paramName | 'v') + '=' + getRandomString(); return urlString; }; var getRandomString = function () { var randLetters = Math.random().toString(36).substring(2) + Math.random().toString(36).substring(2); return Date.now() + '.' + randLetters + '.'; }; return { get: get, post: post, getRandomString: getRandomString }; })(); consentManager.cookie = (function () { var readCookie = function (cookieName) { var value = "; " + document.cookie; var parts = value.split("; " + cookieName + "="); if (parts.length === 2) { return parts.pop().split(";").shift(); } return null; }; var setCookie = function (cookieName, value, exdays) { var expires = ""; if (exdays) { var exdate = new Date(); var exhours = 24 * exdays; exdate.setHours(exdate.getHours() + exhours); expires = "; expires=" + exdate.toUTCString(); } document.cookie = cookieName + "=" + encodeURIComponent(value) + expires + "; path=/"; }; var exists = function (cookieName) { return readCookie(cookieName) !== null; }; return { read: readCookie, set: setCookie, exists: exists }; })(); consentManager.consent = (function () { var gdprModalId = 'gdpr_modal'; var gdprModal = null; var url = { consentList: "/gdpr/ajax/consents", authUserConsent: "/gdpr/ajax/userconsent/", consentContent: "/gdpr/ajax/userconsent/" }; var timers = { closeTime: 7, postpone: 183, accept: 604800 }; var isUserAuthenticated = false; var collectForAnonymous = true; var consentList = null; var validatedConsents = []; var handling = null; var init = function () { gdprModal = document.getElementById(gdprModalId); gdprModal.addEventListener('handled', function (e) { handleConsents(); }); registerConsentActions(); fetchConsentList(handleConsents); }; var fetchConsentList = function (callback) { consentManager.ajax.get({ url: url.consentList }, function (data, status) { if (!data) { throw new Error("Unable to fetch consents list"); } consentList = data; if (callback && typeof callback === "function") { callback(data); } }); }; var handleConsents = function () { for (var i in consentList) { if (!consentList.hasOwnProperty(i) || validatedConsents.indexOf(consentList[i].code) !== -1) { continue; } if (consentManager.cookie.exists(getCookieName(consentList[i]))) { validatedConsents.push(consentList[i].code); continue; } handleConsent(consentList[i]); return; } }; var handleConsent = function (consent) { handling = consent; if (isUserAuthenticated) { consentManager.ajax.get({ url: url.authUserConsent + consent.code }, function (data, status) { if (status !== 200) { raiseModal(consent); } else { handleConsents(); } }); } else { if (collectForAnonymous) { raiseModal(consent); } } }; var sendConsentResponse = function (consent, value, cb) { consentManager.ajax.post({ url: url.authUserConsent + consent.code, data: { response: value } }, function (data, status) { if (status === 200) { validatedConsents.push(consent.code); // need to store it manually cos cookie write out is to slow } else { console.error("An error occurred while trying to send consent response"); } if (cb && typeof cb === "function") { cb({ consent: consent, }); } }); }; var raiseModal = function (consent) { document.getElementById('js-consent-short-description').innerHTML = consent.short_description; $.magnificPopup.open({ items: { src: "#" + gdprModalId, type: 'inline', mainClass: 'mfp--gdpr' }, modal: true }); }; var registerConsentActions = function () { var consentButtons = document.getElementsByClassName("js-consent-action"); for (var i in consentButtons) { consentButtons[i].onclick = handleConsentAction; } }; var getConsentByPropValue = function (propVal, prop) { prop = prop || 'code'; for (var i in consentList) { if (!consentList.hasOwnProperty(i)) { continue; } if (consentList[i][prop] === propVal) { return consentList[i]; } } throw new Error("Missing consent with '" + prop + "': '" + consentCode + "'"); }; var handleConsentAction = function () { var action = this.getAttribute('data-action'); var consent = handling; switch (action) { case 'close': consentManager.cookie.set(getCookieName(consent), 0, getExpiryTimeByValue(0)); closeModal({ consent: consent }); break; case 'understand': consentManager.cookie.set(getCookieName(consent), 1, getExpiryTimeByValue(1)); closeModal({ consent: consent }); break; case 'postpone': sendConsentResponse(consent, 0, closeModal); break; case 'accept': sendConsentResponse(consent, 1, closeModal); break; default: throw new Error('Undefined consent action'); } }; var closeModal = function (options) { if (!options || !options.consent) { throw new Error("Missing consent"); } var consent = options.consent; validatedConsents.push(consent.code); var event = new Event('handled'); $.magnificPopup.instance.close(); gdprModal.dispatchEvent(event); }; var getCookieName = function (consent) { if (consent.has_anon_cookie === false) { return consent.cookie_name; } return isUserAuthenticated ? consent.cookie_name : consent.cookie_name_anon; }; var getExpiryTimeByValue = function (value) { if (value !== undefined && parseInt(value) === 1) { return isUserAuthenticated ? secondsToDays(timers.accept) : timers.postpone; } return timers.closeTime; }; var secondsToDays = function (seconds) { return Math.floor(seconds / 86400); }; return { init: init }; })(); document.addEventListener("DOMContentLoaded", function (event) { consentManager.consent.init(); });