!(function () { var headers = new Headers(); headers.append("Content-Type", "application/json"); var raw = JSON.stringify({ "projectId": window.okidokSettings.projectId, "instanceId": window.okidokSettings.instanceId }); var requestOptions = { method: 'POST', headers: headers, body: raw, redirect: 'follow' }; fetch("https://okidok.net/api/v1/project", requestOptions) .then(response => response.json()) .then(result => { let okidokWall = new OkidokWallConsent(result.data); okidokWall.render(); // check if cookies token is present var cookieOkidok = (document.cookie.match(/^(?:.*;)?\s*okidok_consent\s*=\s*([^;]+)(?:.*)?$/)||[,null])[1]; var consentCookie = JSON.parse(cookieOkidok); if (cookieOkidok == null) { let okidok = new Okidok(result.data, [], result.data.debug); okidok.okidokDisplayCMP(); } else if (result.data.wall.lastUpdate > (consentCookie.createdAt/1000)) { let okidok = new Okidok(result.data, consentCookie.data, result.data.debug); okidok.okidokDisplayCMP(); } else { let okidok = new Okidok(result.data, consentCookie.data, result.data.debug); } }) .catch(error => console.log('error', error)); })(); class OkidokWallConsent { constructor(data) { this.data = data; } render() { let contentHead = ''; let contentHtml = ''; contentHead += this.renderFonts(); contentHead += this.renderCss(); contentHtml += `
`; contentHtml += this.renderSteps(); contentHtml += `
`; var child = document.createElement('okidok'); child.innerHTML = contentHtml; child.innerHTML += contentHead; document.body.appendChild(child); child.outerHTML = child.outerHTML.replace(/<\/?okidok>/, ''); } templateStepCookies() { let htmlTemplate = ` `; } renderCss() { let data = this.data; let cssProperties = ''; for (const cssProperty in data.template.style.cssProperties) { cssProperties += '--' + data.template.style.cssProperties[cssProperty].property + ': ' + data.template.style.cssProperties[cssProperty].value + '; '; } let cssTemplate = ` `; return cssTemplate; } renderSteps() { let data = this.data; let steps = this.data.wall.data; let template = this.data.template; let position = 1; let totalPosition = this.data.wall.data.length; // +1 because step 1 is added let htmlTemplate = ''; /* * First */ /*htmlTemplate += `
${template.title}
${template.content}
${template.legal}
`;*/ /* * Dynamic */ for (const i in steps) { let step = steps[i]; let items = step.items; if (step.items.length > 0) { var mainTitle = step.name; var mainSubtitle = step.subtitle; var mainDescription = step.description; var mainLegal = step.legal; } else { var mainTitle = step.name; var mainSubtitle = ''; var mainDescription = step.description; var mainLegal = step.legal; } if (position ==1) { var classActive = 'active'; } else { var classActive = ''; } htmlTemplate += `
`; if (position == 1) { htmlTemplate += ` `; } htmlTemplate += `
${mainTitle}
`; if (mainSubtitle != '') { htmlTemplate += `
${mainSubtitle}
`; } htmlTemplate += `
${mainDescription}
`; if (mainLegal != '') { htmlTemplate += `
${mainLegal}
`; } if (position != 1) { htmlTemplate += `
${template.btn.all_checked}
`; } htmlTemplate += `
`; for (const j in items) { let item = items[j]; htmlTemplate += ` `; } // end for items htmlTemplate += `
`; if (position == 1) { htmlTemplate += `
`; } else { htmlTemplate += `
${position}/${totalPosition}
`; } htmlTemplate += `
`; if (position == 1) { if (position < totalPosition) { htmlTemplate += ` `; } else { htmlTemplate += ` `; } } else { let previousPosition = position - 1; htmlTemplate += ` `; if (position < totalPosition) { let nextPosition = position + 1; htmlTemplate += ``; } else { htmlTemplate += ``; } } htmlTemplate += `
`; position++; } return htmlTemplate; } renderFonts() { let template = ``; template += ``; template += ``; return template; } } class Okidok { constructor(data, cookiesAccepted, okidokDebug) { if(okidokDebug==undefined){ this.okidokDebug=false; } else { this.okidokDebug=okidokDebug; } if(this.okidokDebug){console.log('------ Okidok.init ------');} this.okidokCookiesList = []; this.okidokCookiesListRaw = []; this.okidokCookiesAccepted = []; // Set CookiesList this.okidokSetCookiesListRaw(data); // Set CookiesAccepted this.okidokSetCookiesAcceptedRaw(cookiesAccepted); // Init Cookies list this.okidokInitCookiesList(); // GTM Datalayer - consented cookies to transfer at page loading this.okidokConsentsToGTM(this.okidokCookiesAccepted); // Init Checked Cookies this.okidokiInitCheckedCookies(); // Check if it's needed to change the Switch All Cookies this.okidokClicSwitchAllCheck(1); // Events Click - Display CMP const okidokBtnDisplayCMPs = document.getElementsByClassName('okidok-btn-displayCMP'); for (const item of okidokBtnDisplayCMPs) { item.addEventListener("click", () => {this.okidokDisplayCMP();}); } // Events Click - Steps Navigation const okidokBtnStepNavs = document.getElementsByClassName('okidok-btn-steps-nav'); for (const item of okidokBtnStepNavs) { item.addEventListener("click", (evt) => { this.okidokDisplayStep( evt.currentTarget.closest('.okidok-steps').dataset.stepactive, evt.currentTarget.dataset.steptarget, evt ); }); } // Events Click - Switch on a Cookie const okidokSwitchsCookie = document.querySelectorAll('.okidok-cookie-switch input'); for (const item of okidokSwitchsCookie) { item.addEventListener("click", (evt) => { this.okidokClicSwitchCookie( evt.currentTarget.closest('.okidok-cookie').dataset.cookie, evt.currentTarget, evt ); }); } // Events Click - Switch on all Cookies const okidokSwitchsAllCookies = document.querySelectorAll('.okidok-select-all input'); for (const item of okidokSwitchsAllCookies) { item.addEventListener("click", (evt) => { this.okidokClicSwitchAllCookies( evt.currentTarget.closest('.okidok-content').querySelector('.okidok-cookies-list'), evt.currentTarget, evt ); }); } // Events Click - Accept all cookies const okidokBtnsAcceptAll = document.querySelectorAll('.okidok-btn-accept-all'); for (const item of okidokBtnsAcceptAll) { item.addEventListener("click", (evt) => { this.okidokAcceptAllCookies(); }); } // Events Click - Accept selected cookies const okidokBtnsAcceptSelectedCookies = document.querySelectorAll('.okidok-btn-validate'); for (const item of okidokBtnsAcceptSelectedCookies) { item.addEventListener("click", (evt) => { this.okidokAcceptSelectedCookies(); }); } // Events Click - Accept no cookies const okidokBtnsAcceptNothing = document.querySelectorAll('.okidok-link-no-accept'); for (const item of okidokBtnsAcceptNothing) { item.addEventListener("click", (evt) => { this.okidokAcceptNoCookies(); }); } } okidokSetCookiesListRaw(data) { this.okidokCookiesListRaw.push([]); for (const i in data.wall.data) { let step = data.wall.data[i]; let arrItems = []; for (const j in step.items) { let item = step.items[j]; arrItems.push(item.reference); } if (arrItems.length > 0) { this.okidokCookiesListRaw.push(arrItems); } } } okidokSetCookiesAcceptedRaw(cookies) { for (const i in cookies) { this.okidokCookiesAccepted.push(cookies[i]); } } /* Create Cookies list */ okidokInitCookiesList() { if(this.okidokDebug){console.log('------ Okidok.okidokInitCookiesList ------');} if(this.okidokDebug){console.log('okidokCookiesAccepted =' + this.okidokCookiesAccepted);} this.okidokCookiesList = this.okidokFlatArray(this.okidokCookiesListRaw); if(this.okidokDebug){console.log('Okidok.okidokCookiesList= '+this.okidokCookiesList);} } okidokFlatArray(target) { if(this.okidokDebug){console.log('------ Okidok.okidokFlatArray ------');} //if(this.okidokDebug){console.log('target =' + target);} this.tmpFlatArray = []; target.forEach(function concatLev1(item) { if (item != '') { //if(this.okidokDebug){console.log('target Lv1= '+item);} item.forEach(function concatLev2(item2) { //if(this.okidokDebug){console.log('target Lv2= '+item2);} this.tmpFlatArray.push(item2); }, this); } }, this); if(this.okidokDebug){console.log('tmpFlatArray= '+this.tmpFlatArray);} return this.tmpFlatArray; } /* Display CMP */ okidokDisplayCMP() { if(this.okidokDebug){console.log('------ Okidok.okidokDisplayCMP------ ');} document.getElementById("okidok-cmp-15426").classList.add("active"); } /* Close CMP */ okidokCloseCMP() { if(this.okidokDebug){console.log('------ Okidok.okidokCloseCMP ------');} document.getElementById("okidok-cmp-15426").classList.remove("active"); } /* Display CMP - 'Home' */ okidokDisplayHome() { if(this.okidokDebug){console.log('------ Okidok.okidokDisplayHome ------');} const okidokAllSteps = document.querySelectorAll('.okidok-steps'); for (const item of okidokAllSteps) { item.classList.remove("active"); } document.getElementById('okidok-step-1').classList.add("active"); } /* Navig between Steps */ okidokDisplayStep(activeStep,targetStep,evt) { if(this.okidokDebug){ console.log('------ Okidok.okidokInitSteps ------'); console.log('activeStep='+activeStep); console.log('targetStep='+targetStep); } document.getElementById('okidok-step-'+activeStep).classList.toggle("active"); document.getElementById('okidok-step-'+targetStep).classList.toggle("active"); // Check if it's needed to change the Switch All Cookies this.okidokClicSwitchAllCheck(targetStep); } /* Switch on a Cookie */ okidokClicSwitchCookie(activeCookie,targetSwitch,evt) { if(this.okidokDebug){console.log('------ Okidok.okidokClicSwitchCookie ------');} if(targetSwitch.hasAttribute("checked")){ targetSwitch.removeAttribute("checked"); targetSwitch.checked = false; } else { targetSwitch.setAttribute("checked", ""); targetSwitch.checked = true; } // Check if it's needed to change the Switch All Cookies this.okidokClicSwitchAllCheck(targetSwitch.closest('.okidok-steps').dataset.stepactive); } /* Switch Select all cookies */ okidokClicSwitchAllCookies(targetCookiesList,targetSwitch,evt) { if(this.okidokDebug){console.log('------ Okidok.okidokClicSwitchAllCookies ------');} let targetCookiesListInputs = targetCookiesList.querySelectorAll('.okidok-cookie-switch input'); if(targetSwitch.hasAttribute("checked")){ // Uncheck all cookies in the targeted list targetSwitch.removeAttribute("checked"); targetSwitch.checked = false; for (const targetCookiesListInput of targetCookiesListInputs) { targetCookiesListInput.removeAttribute("checked"); targetCookiesListInput.checked = false; } } else { // Check all cookies in the targeted list targetSwitch.setAttribute("checked", ""); targetSwitch.checked = true; for (const targetCookiesListInput of targetCookiesListInputs) { targetCookiesListInput.setAttribute("checked", ""); targetCookiesListInput.checked = true; } } } /* Switch all cookies - Auto switch depending the numbers of child 'checked cookies' */ okidokClicSwitchAllCheck(activeStep) { if(this.okidokDebug){console.log('------ Okidok.okidokClicSwitchAllCookiesCheck ------');} if(this.okidokDebug){console.log('activeStep ='+activeStep);} if(this.okidokDebug){console.log("this.okidokCookiesListRaw[activeStep-1].length= "+this.okidokCookiesListRaw[activeStep-1].length);} if(this.okidokCookiesListRaw[activeStep-1].length >=1){ let i=0; let okidokActiveStep = document.getElementById("okidok-step-"+activeStep); let itemsCookies = okidokActiveStep.querySelectorAll('.okidok-cookie-switch input'); for (const itemsCookie of itemsCookies) { if(this.okidokDebug){console.log('>> Loop Cookies - '+ itemsCookie.closest('.okidok-cookie').dataset.cookie);} if(itemsCookie.hasAttribute("checked")){ i++ } } if(this.okidokDebug){console.log("i= "+i);} if(this.okidokDebug){console.log("this.okidokCookiesListRaw[activeStep-1].length= "+this.okidokCookiesListRaw[activeStep-1].length);} let itemSwitchAll = okidokActiveStep.querySelector('.okidok-select-all input'); if(itemSwitchAll!=undefined){ if(i == this.okidokCookiesListRaw[activeStep-1].length){ if(this.okidokDebug){console.log("All cookies checked in this step");} itemSwitchAll.setAttribute("checked", ""); itemSwitchAll.checked = true; } else { if(this.okidokDebug){console.log("Not all cookies checked in this step");} itemSwitchAll.removeAttribute("checked"); itemSwitchAll.checked = false; } } } } /* Accept all cookies */ okidokAcceptAllCookies() { if(this.okidokDebug){console.log('------ Okidok.okidokAcceptAllCookies ------');} // Push all cookies in a specific array and add checked on all of them const okidokAllSteps = document.querySelectorAll('.okidok-steps'); for (const item of okidokAllSteps) { if(this.okidokDebug){console.log('> Loop Step - '+item.dataset.stepactive);} const itemSwitchAll = item.querySelector('.okidok-select-all input'); if(itemSwitchAll!=undefined){ itemSwitchAll.setAttribute("checked", ""); itemSwitchAll.checked = true; } const itemsCookies = item.querySelectorAll('.okidok-cookie-switch input'); for (const itemsCookie of itemsCookies) { if(this.okidokDebug){console.log('>> Loop Cookies - '+ itemsCookie.closest('.okidok-cookie').dataset.cookie);} itemsCookie.setAttribute("checked", ""); itemsCookie.checked = true; //this.okidokCookiesAccepted.push(itemsCookie.closest('.okidok-cookie').dataset.cookie); } } this.okidokCookiesAccepted = this.okidokCookiesList; if(this.okidokDebug){console.log('Accepted Cookie List= ' + this.okidokCookiesAccepted);} // GTM Datalayer - datas to transfer this.okidokConsentsToGTM(this.okidokCookiesAccepted); this.okidokConsentsToAPI(this.okidokCookiesAccepted); this.okidokCloseCMP(); this.okidokDisplayHome(); } /* Accept selected cookies */ okidokAcceptSelectedCookies() { if(this.okidokDebug){console.log('------ Okidok.okidokAcceptSelectedCookies ------');} this.okidokCookiesAccepted = []; // reset the Accepted Cookies list if(this.okidokDebug){console.log('Reset Cookies List= ' + this.okidokCookiesAccepted);} // Push selected cookies in a specific array const okidokAllSteps = document.querySelectorAll('.okidok-steps'); for (const item of okidokAllSteps) { if(this.okidokDebug){console.log('> Loop Step - '+item.dataset.stepactive);} const itemsCookies = item.querySelectorAll('.okidok-cookie-switch input'); for (const itemsCookie of itemsCookies) { if(this.okidokDebug){console.log('>> Loop Cookies - '+ itemsCookie.closest('.okidok-cookie').dataset.cookie);} if(itemsCookie.hasAttribute("checked")){ if(this.okidokDebug){console.log('Add selected Cookie= ' + itemsCookie.closest('.okidok-cookie').dataset.cookie);} this.okidokCookiesAccepted.push(itemsCookie.closest('.okidok-cookie').dataset.cookie); } } } if(this.okidokDebug){console.log('All accepted Cookies List= ' + this.okidokCookiesAccepted);} // GTM Datalayer - datas to transfer this.okidokConsentsToGTM(this.okidokCookiesAccepted); this.okidokConsentsToAPI(this.okidokCookiesAccepted); this.okidokCloseCMP(); this.okidokDisplayHome(); } /* Init checked cookies */ okidokiInitCheckedCookies() { if(this.okidokDebug){console.log('------ Okidok.okidokiInitCheckedCookies ------');} const okidokAllSteps = document.querySelectorAll('.okidok-steps'); for (const item of okidokAllSteps) { if(this.okidokDebug){console.log('> Loop Step - '+item.dataset.stepactive);} const itemsCookies = item.querySelectorAll('.okidok-cookie-switch input'); for (const itemsCookie of itemsCookies) { if(this.okidokDebug){console.log('>> Loop Cookies - '+ itemsCookie.closest('.okidok-cookie').dataset.cookie);} // Cookie checked if(this.okidokCookiesAccepted.indexOf(itemsCookie.closest('.okidok-cookie').dataset.cookie) != -1){ if(this.okidokDebug){console.log('Add selected Cookie= ' + itemsCookie.closest('.okidok-cookie').dataset.cookie);} itemsCookie.setAttribute("checked", ""); itemsCookie.checked = true; } } } } /* Accept no cookies -> Only essentials cookies */ okidokAcceptNoCookies() { if(this.okidokDebug){console.log('------ Okidok.okidokAcceptNoCookies ------');} // Uncheck all cookies const okidokAllSteps = document.querySelectorAll('.okidok-steps'); for (const item of okidokAllSteps) { if(this.okidokDebug){console.log('> Loop Step - '+item.dataset.stepactive);} const itemSwitchAll = item.querySelector('.okidok-select-all input'); if(itemSwitchAll!=undefined){ itemSwitchAll.removeAttribute("checked"); itemSwitchAll.checked = false; } const itemsCookies = item.querySelectorAll('.okidok-cookie-switch input'); for (const itemsCookie of itemsCookies) { if(this.okidokDebug){console.log('>> Loop Cookies - '+ itemsCookie.closest('.okidok-cookie').dataset.cookie);} itemsCookie.removeAttribute("checked"); itemsCookie.checked = false; } } this.okidokCookiesAccepted = []; if(this.okidokDebug){console.log('Accepted Cookie List= ' + this.okidokCookiesAccepted);} // GTM Datalayer - datas to transfer this.okidokConsentsToGTM(this.okidokCookiesAccepted); this.okidokCloseCMP(); this.okidokDisplayHome(); this.okidokConsentsToAPI(this.okidokCookiesAccepted); } /* * GTM - Datalayer push * CMP - Push */ okidokConsentsToGTM(datas) { if(this.okidokDebug){console.log('------ Okidok.okidokConsentsToGTM------ ');} if(this.okidokDebug){console.log('Consents pushed to GTM= '+datas);} // Send full list of activated cookies window.dataLayer = window.dataLayer || []; window.dataLayer.push({ 'event': 'okidok_cookies_consent', 'okidok_cookies_enabled': datas.toString(), }); // Send separated (un)activated cookies this.okidokCookiesList.forEach(function (item) { if(datas.indexOf(item) === -1) { // Event - cookie unactivated if(this.okidokDebug){console.log('------ Event - cookie - Unactivated------ '+item);} window.dataLayer.push({ 'event': 'okidok_unactivate_'+item, ['okidok_'+item]: false, 'okidok_cookie_name': item, }); } else { // Event - cookie activated if(this.okidokDebug){console.log('------ Event - cookie - Activated------ '+item);} window.dataLayer.push({ 'event': 'okidok_activate_'+item, ['okidok_'+item]: true, 'okidok_cookie_name': item, }); } }, this); } okidokConsentsToAPI(datas) { let expirationDays = 365; let values = ''; values = { projectId: window.okidokSettings.projectId, instanceId: window.okidokSettings.instanceId, token: ""+this.okidokGenerateToken()+"", data: datas, createdAt: ""+ Date.now() +"" }; var headers = new Headers(); headers.append("Content-Type", "application/json"); var raw = JSON.stringify(values); var requestOptions = { method: 'POST', headers: headers, body: raw, redirect: 'follow' }; fetch("https://okidok.net/api/v1/consent", requestOptions) .then(response => response.json()) .then(result => { const d = new Date(); d.setTime(d.getTime() + (expirationDays*24*60*60*1000)); let expires = "expires="+ d.toUTCString(); document.cookie = "okidok_consent=" + raw + ";" + expires + ";path=/"; }) .catch(error => console.log('error', error)); } okidokGenerateToken() { return String(this.simpleHash(window.location.hostname) + this.uniqid()); } uniqid(prefix = "", random = false) { const sec = Date.now() * 1000 + Math.random() * 1000; const id = sec.toString(16).replace(/\./g, "").padEnd(14, "0"); return `${prefix}${id}${random ? `.${Math.trunc(Math.random() * 100000000)}`:""}`; }; simpleHash(str) { var hash = 0, i, chr; if (str.length === 0) return hash; for (i = 0; i < str.length; i++) { chr = str.charCodeAt(i); hash = ((hash << 5) - hash) + chr; hash |= 0; // Convert to 32bit integer } return hash; } setCookie(cname, cvalue, exdays) { const d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); let expires = "expires="+ d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; } }