praiadeseselle/site/modules/PrivacyWire/js/PrivacyWire.js

1 line
9.8 KiB
JavaScript
Raw Permalink Normal View History

2024-04-04 15:12:59 +02:00
!function(){"use strict";String.prototype.formatUnicorn=String.prototype.formatUnicorn||function(){var e=this.toString();if(arguments.length){var t,s=typeof arguments[0],n="string"===s||"number"===s?Array.prototype.slice.call(arguments):arguments[0];for(t in n)e=e.replace(new RegExp("\\{"+t+"\\}","gi"),n[t])}return e};class PrivacyWire{constructor(e){this.name="privacywire",this.toggleToStatus=!0,this.cookieGroups=Object.freeze(["necessary","functional","statistics","marketing","external_media"]),this.settings=this.sanitizeSettings(e),this.userConsent=this.sanitizeStoredConsent(),this.elements=this.initiateElements(),this.syncConsentToCheckboxes(),this.checkForValidConsent()||this.showBanner(),this.checkElementsWithRequiredConsent(),this.handleButtons()}sanitizeSettings(e){let t={};t.version=parseInt(e.version),t.dnt=Boolean(parseInt(e.dnt)),t.bots=Boolean(parseInt(e.bots)),t.customFunction=`${e.customFunction}`,t.messageTimeout=parseInt(e.messageTimeout),t.consentByClass=Boolean(parseInt(e.consentByClass)),t.cookieGroups={};for(const s of this.cookieGroups)t.cookieGroups[s]=`${e.cookieGroups[s]}`;return t}sanitizeStoredConsent(){if(!window.localStorage.getItem(this.name))return this.getDefaultConsent();const e=JSON.parse(window.localStorage.getItem(this.name));if(parseInt(e.version)!==this.settings.version)return this.getDefaultConsent();if(void 0===e.cookieGroups)return this.removeStoredConsent(),this.getDefaultConsent();let t={};t.version=parseInt(e.version),t.cookieGroups={};for(const s of this.cookieGroups)t.cookieGroups[s]=Boolean(e.cookieGroups[s]);return t}getDefaultConsent(){let e={version:0,cookieGroups:{}};for(const t of this.cookieGroups)e.cookieGroups[t]="necessary"===t;return e}initiateElements(){let e={banner:{}};e.banner.wrapper=document.getElementById("privacywire-wrapper"),e.banner.intro=e.banner.wrapper.getElementsByClassName("privacywire-banner"),e.banner.options=e.banner.wrapper.getElementsByClassName("privacywire-options"),e.banner.message=e.banner.wrapper.getElementsByClassName("privacywire-message"),e.buttons={},e.buttons.acceptAll=e.banner.wrapper.getElementsByClassName("allow-all"),e.buttons.acceptNecessary=e.banner.wrapper.getElementsByClassName("allow-necessary"),e.buttons.choose=e.banner.wrapper.getElementsByClassName("choose"),e.buttons.toggle=e.banner.wrapper.getElementsByClassName("toggle"),e.buttons.save=e.banner.wrapper.getElementsByClassName("save"),e.buttons.askForConsent=document.getElementsByClassName("privacywire-consent-button"),e.buttons.externalTrigger=document.getElementsByClassName("privacywire-show-options"),e.checkboxes={};for(const t of this.cookieGroups)"necessary"!==t&&(e.checkboxes[t]=document.getElementById(t));return e.blueprint=document.getElementById("privacywire-ask-consent-blueprint"),e.elementsWithRequiredConsent=!0===this.settings.consentByClass?document.getElementsByClassName("require-consent"):document.querySelectorAll("[data-category]"),e.consentWindows=document.getElementsByClassName("privacywire-ask-consent"),e}handleButtons(){this.handleButtonHelper(this.elements.buttons.acceptAll,"handleButtonAcceptAll"),this.handleButtonHelper(this.elements.buttons.acceptNecessary,"handleButtonAcceptNecessary"),this.handleButtonHelper(this.elements.buttons.choose,"handleButtonChoose"),this.handleButtonHelper(this.elements.buttons.toggle,"handleButtonToggle"),this.handleButtonHelper(this.elements.buttons.save,"handleButtonSave"),this.handleButtonHelper(this.elements.buttons.askForConsent,"handleButtonAskForConsent"),this.handleButtonHelper(this.elements.buttons.externalTrigger,"handleButtonExternalTrigger")}handleButtonHelper(e,t){if(e){const s=this;Array.from(e).forEach((e=>{s[t](e)}))}}reHandleExternalButtons(){this.elements.buttons.externalTrigger=document.getElementsByClassName("privacywire-show-options"),this.handleButtonHelper(this.elements.buttons.externalTrigger,"handleButtonExternalTrigger")}handleButtonAcceptAll(e){e.addEventListener("click",(()=>{for(const e of this.cookieGroups)this.userConsent.cookieGroups[e]=!0;this.syncConsentToCheckboxes(),this.saveConsent(