41 lines
7.7 KiB
JavaScript
41 lines
7.7 KiB
JavaScript
"use strict";function BootstrapCookieConsentSettings(n){function v(){const n=t.content,r='<a href="'+t.props.privacyPolicyUrl+'">'+n.privacyPolicy+"<\/a>",u='<a href="'+t.props.legalNoticeUrl+'">'+n.legalNotice+"<\/a>";t.content[t.lang]===undefined&&(t.lang=t.props.defaultLang);t.content.body=t.content.body.replace(/--privacy-policy--/,r);let i="";for(const n of t.props.categories){const r=t.content.categories[n];r||console.error("no content for category",n,"found in language file",t.lang);let u="";for(const n of r.description)u+=`<li>${n}</li>`;i+=`<div class="bccs-option" data-name="${n}">
|
|
<div class="form-check mb-1">
|
|
<input type="checkbox" class="form-check-input" id="bccs-checkbox-${n}">
|
|
<label class="form-check-label" for="bccs-checkbox-${n}"><b>${r.title}</b></label>
|
|
</div>
|
|
<ul>
|
|
${u}
|
|
</ul>
|
|
</div>`}t.modalContent=`<!-- cookie banner => https://github.com/shaack/bootstrap-cookie-consent-settings -->
|
|
<div class="modal-dialog modal-lg shadow" role="document">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h4 class="modal-title">${t.content.title}</h4>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="bccs-body-text" style="font-size: 80%">
|
|
<p>${t.content.body}</p>
|
|
</div>
|
|
<p class="d-flex justify-content-between mb-0">
|
|
${u}
|
|
<a href="#bccs-options" data-bs-toggle="collapse">${t.content.mySettings}</a>
|
|
</p>
|
|
<div id="bccs-options" class="collapse">
|
|
<div class="mt-4">
|
|
${i}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button id="bccs-buttonDoNotAgree" type="button"
|
|
class="${t.props.buttonDontAgreeClass}">
|
|
${t.content.buttonNotAgree}
|
|
</button>
|
|
<button id="bccs-buttonAgree" type="button" class="${t.props.buttonAgreeClass}">${t.content.buttonAgree}</button>
|
|
<button id="bccs-buttonSave" type="button" class="${t.props.buttonSaveClass}">
|
|
${t.content.buttonSaveSelection}
|
|
</button>
|
|
<button id="bccs-buttonAgreeAll" type="button" class="${t.props.buttonAgreeClass}">${t.content.buttonAgreeAll}</button>
|
|
</div>
|
|
</div>
|
|
</div>`;!e(t.props.cookieName)&&t.props.autoShowModal&&s()}function s(){b(function(){t.modalElement=document.getElementById(t.props.modalId);t.modalElement?t.modal.show():(t.modalElement=document.createElement("div"),t.modalElement.id=t.props.modalId,t.modalElement.setAttribute("class","modal fade"),t.modalElement.setAttribute("tabindex","-1"),t.modalElement.setAttribute("role","dialog"),t.modalElement.setAttribute("aria-labelledby",t.props.modalId),t.modalElement.innerHTML=t.modalContent,document.body.append(t.modalElement),t.props.postSelectionCallback&&t.modalElement.addEventListener("hidden.bs.modal",function(){t.props.postSelectionCallback()}),t.modal=new bootstrap.Modal(t.modalElement,{backdrop:"static",keyboard:!1}),t.modal.show(),t.buttonDoNotAgree=t.modalElement.querySelector("#bccs-buttonDoNotAgree"),t.buttonAgree=t.modalElement.querySelector("#bccs-buttonAgree"),t.buttonSave=t.modalElement.querySelector("#bccs-buttonSave"),t.buttonAgreeAll=t.modalElement.querySelector("#bccs-buttonAgreeAll"),u(),y(),t.modalElement.querySelector("#bccs-options").addEventListener("hide.bs.collapse",function(){r=!1;u()}),t.modalElement.querySelector("#bccs-options").addEventListener("show.bs.collapse",function(){r=!0;u()}),t.buttonDoNotAgree.addEventListener("click",function(){p()}),t.buttonAgree.addEventListener("click",function(){h()}),t.buttonSave.addEventListener("click",function(){w()}),t.buttonAgreeAll.addEventListener("click",function(){h()}))}.bind(this))}function y(){const n=t.getSettings();if(n)for(let i in n){const r=t.modalElement.querySelector("#bccs-checkbox-"+i);r.checked=n[i]==="true"}const i=t.modalElement.querySelector("#bccs-checkbox-necessary");i.checked=!0;i.disabled=!0}function u(){r?(t.buttonDoNotAgree.style.display="none",t.buttonAgree.style.display="none",t.buttonSave.style.removeProperty("display"),t.buttonAgreeAll.style.removeProperty("display")):(t.buttonDoNotAgree.style.removeProperty("display"),t.buttonAgree.style.removeProperty("display"),t.buttonSave.style.display="none",t.buttonAgreeAll.style.display="none")}function f(n=undefined){const i={};for(const r of t.props.categories)if(n===undefined){const n=t.modalElement.querySelector("#bccs-checkbox-"+r);n||console.error("checkbox not found for category",r);i[r]=n.checked}else i[r]=n;return i.necessary=!0,i}function h(){i(t.props.cookieName,f(!0),t.props.cookieStorageDays);t.modal.hide()}function p(){i(t.props.cookieName,f(!1),t.props.cookieStorageDays);t.modal.hide()}function w(){i(t.props.cookieName,f(),t.props.cookieStorageDays);t.modal.hide()}function c(n,i){const r=new XMLHttpRequest;r.overrideMimeType("application/json");const u=t.props.contentURL+"/"+n+".json";r.open("GET",u,!0);r.onreadystatechange=function(){r.readyState===4&&r.status===200&&(r.status===200?i(r.responseText):console.error(u,r.status))};r.onloadend=function(){r.status===404&&n!==t.props.defaultLang&&(console.warn("language "+n+" not found trying defaultLang "+t.props.defaultLang),c(t.props.defaultLang,i))};r.send(null)}function i(n,t,i){let r="";if(i){const n=new Date;n.setTime(n.getTime()+i*864e5);r="; expires="+n.toUTCString()}const u=new URLSearchParams(t).toString();document.cookie=n+"="+(u||"")+r+"; Path=/; SameSite=Strict;";localStorage.setItem(n,u)}function e(n){const r=n+"=",u=document.cookie.split(";");for(let n=0;n<u.length;n++){let t=u[n];while(t.charAt(0)===" ")t=t.substring(1,t.length);if(t.indexOf(r)===0){const i=new URLSearchParams(t.substring(r.length,t.length)),n={};for(const[t,r]of i)n[t]=r;return n}}const f=localStorage.getItem(n);if(f){const u=new URLSearchParams(f),r={};for(const[n,t]of u)r[n]=t;return i(n,r,t.props.cookieStorageDays),r}return null}function l(n){document.cookie=n+"=; Path=/; SameSite=Strict; Expires=Thu, 01 Jan 1970 00:00:01 GMT;"}function b(n){document.readyState!=="loading"?n():document.addEventListener("DOMContentLoaded",n)}const t=this;let r=!1;this.props={privacyPolicyUrl:undefined,legalNoticeUrl:undefined,contentURL:"/cookie-consent-content",buttonAgreeClass:"btn btn-primary",buttonDontAgreeClass:"btn btn-link text-decoration-none",buttonSaveClass:"btn btn-secondary",autoShowModal:!0,alsoUseLocalStorage:!0,postSelectionCallback:undefined,lang:navigator.language,defaultLang:"en",categories:["necessary","statistics","marketing","personalization"],cookieName:"cookie-consent-settings",cookieStorageDays:365,modalId:"bootstrapCookieConsentSettingsModal"};n.privacyPolicyUrl||console.error("please set `privacyPolicyUrl` in the props of BootstrapCookieConsentSettings");n.legalNoticeUrl||console.error("please set `legalNoticeUrl` in the props of BootstrapCookieConsentSettings");for(const t in n)this.props[t]=n[t];this.lang=this.props.lang;this.lang.indexOf("-")!==-1&&(this.lang=this.lang.split("-")[0]);const o=e(this.props.cookieName);if(o)try{for(const n of this.props.categories)if(o[n]===undefined){console.log("cookie settings changed, removing settings cookie");l(this.props.cookieName);break}}catch(a){console.warn("cookie settings changed, removing settings cookie",a);l(this.props.cookieName)}c(t.lang,n=>{t.content=JSON.parse(n),v()});this.showDialog=function(){s()};this.getSettings=function(n){const i=e(t.props.cookieName);return i?n===undefined?i:i?i[n]:!1:undefined};this.setSetting=function(n,r){let u=t.getSettings()||{};for(const n of this.props.categories)u[n]===undefined&&(u[n]=!0);u[n]=r;i(t.props.cookieName,u,t.props.cookieStorageDays)}} |