-
Notifications
You must be signed in to change notification settings - Fork 1
/
consenty.min.js
1 lines (1 loc) · 3.42 KB
/
consenty.min.js
1
var consenty;(()=>{class e{elements={};loadProps={};allow(e){this.change(e,!0)}click(e){console.log(e),document.querySelectorAll(e).forEach((e=>{console.log(e),e.click()}))}getStorage(){let e,t=localStorage.getItem("consenty")||"{}";try{return e=JSON.parse(t),"object"!=typeof e?{}:e||{}}catch(e){return{}}}handleChange(e){const t=e.target;if("checkbox"===t.type&&t.hasAttribute("consenty-toggle")){const e=t.getAttribute("consenty-toggle");t.checked?this.allow(e):this.revoke(e)}}handleClick(e){let t;if(t=e.target.closest("[consenty-allow]")){const e=t.getAttribute("consenty-allow");this.allow(e)}else if(t=e.target.closest("[consenty-revoke]")){const e=t.getAttribute("consenty-revoke");this.revoke(e)}else if(t=e.target.closest("[consenty-show]")){const e=t.getAttribute("consenty-show");this.load(e)}if(t=e.target.closest("[consenty-click]")){const n=t.getAttribute("consenty-click");document.dispatchEvent(new CustomEvent("consenty:click",{detail:n})),e.preventDefault()}}hideElement(e){const t=this.elements[e];t.new&&(t.new.remove(),t.new=!1,this.elements[e]=t)}init(){document.dispatchEvent(new CustomEvent("consenty:beforeinit")),this.reload(),document.dispatchEvent(new CustomEvent("consenty:init"))}initCheckboxes(){document.querySelectorAll("[consenty-toggle]").forEach((e=>{e.checked=this.isTrue(e.getAttribute("consenty-toggle"))}))}isTrue(e){let t=this.getStorage();return!(!t||!(e in t))&&!!t[e]}reload(){this.toggleIFs(),this.initCheckboxes()}revoke(e){this.change(e,!1)}change(e,t){document.dispatchEvent(new CustomEvent("consenty:beforechange",{detail:{name:e,from:this.isTrue(e),to:!!t}}));let n=this.getStorage();const o=this.isTrue(e);t?(document.dispatchEvent(new CustomEvent("consenty:beforeallow",{detail:e})),n[e]=1,document.dispatchEvent(new CustomEvent("consenty:allow",{detail:e}))):(document.dispatchEvent(new CustomEvent("consenty:beforerevoke",{detail:e})),delete n[e],document.dispatchEvent(new CustomEvent("consenty:revoke",{detail:e}))),this.saveStorage(n),document.dispatchEvent(new CustomEvent("consenty:change",{detail:{name:e,from:o,to:this.isTrue(e)}})),this.reload()}saveStorage(e){localStorage.setItem("consenty",JSON.stringify(e))}toggleIFs(){document.querySelectorAll("[consenty-if]").forEach((e=>{const t=e.getAttribute("consenty-if"),n=t.startsWith("!")?"hideif":"showif";let o="showif"===n?t:t.substring(1);const s=this.loadProps.hasOwnProperty(o);let i;e.hasAttribute("data-consenty-id")?i=e.getAttribute("data-consenty-id"):(i=Object.keys(this.elements).length+1,this.elements[i]={old:e,new:!1,prop:o},e.setAttribute("data-consenty-id",i)),"showif"===n?s||this.isTrue(o)?this.loadElement(i):this.hideElement(i):s||this.isTrue(o)?this.hideElement(i):this.loadElement(i)}))}load(e){return new Promise((t=>{this.loadProps[e]=!0,this.reload(),t()}))}loadElement(e){const t=this.elements[e];if(!t.new){if("template"!==t.old.tagName.toLowerCase())return;document.dispatchEvent(new CustomEvent("consenty:beforeload",{detail:e}));let n=t.old,o=document.createElement("span"===n.getAttribute("consenty-tag")?"span":"div");n.parentNode.insertBefore(o,n.nextSibling),o.appendChild(document.importNode(n.content,!0)),t.new=o,this.elements[e]=t,document.dispatchEvent(new CustomEvent("consenty:load",{detail:e})),this.reload()}}}document.addEventListener("DOMContentLoaded",(function(){(consenty=new e).init(),document.addEventListener("click",consenty.handleClick.bind(consenty)),document.addEventListener("change",consenty.handleChange.bind(consenty))}))})();