tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
1 lines • 4.93 kB
JavaScript
(()=>{"use strict";var e={d:(t,n)=>{for(var o in n)e.o(n,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:n[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{TinyCookieConsent:()=>n});const n=class{#e;#t={message:"We use cookies to improve your experience.",acceptText:"Accept All",rejectText:"Reject All",settingsText:"Manage Settings",categories:[],storageKey:"cookie-consent-preferences",onSave:e=>console.log("Preferences saved:",e),animationDuration:400,renderBar:null,renderModal:null};get preferences(){const e=localStorage.getItem(this.#t.storageKey);return e?JSON.parse(e):{}}get config(){return{...this.#t,categories:[...this.#t.categories]}}set config(e){this.validateConfig(e),this.#t=Object.assign(this.#t,e)}constructor(e){if(!e||"object"!=typeof e)throw new TypeError("Config must be an object.");this.config=e;const t=this.loadPreferences();this.#e=t??{},t||this.showConsentBar()}validateConfig(e){if(void 0!==e.message&&"string"!=typeof e.message)throw new TypeError("Config.message must be a string.");if(["acceptText","rejectText","settingsText","storageKey"].forEach(t=>{if(void 0!==e[t]&&"string"!=typeof e[t])throw new TypeError(`Config.${t} must be a string.`)}),void 0!==e.categories){if(!Array.isArray(e.categories))throw new TypeError("Config.categories must be an array of Category objects.");e.categories.forEach((e,t)=>this.validateCategory(e,t))}if(void 0!==e.onSave&&"function"!=typeof e.onSave)throw new TypeError("Config.onSave must be a function.");if(void 0!==e.animationDuration&&("number"!=typeof e.animationDuration||e.animationDuration<0))throw new TypeError("Config.animationDuration must be a positive number.");if(void 0!==e.renderBar&&null!==e.renderBar&&"function"!=typeof e.renderBar)throw new TypeError("Config.renderBar must be a function or null.");if(void 0!==e.renderModal&&null!==e.renderModal&&"function"!=typeof e.renderModal)throw new TypeError("Config.renderModal must be a function or null.")}validateCategory(e,t){if(!e||"object"!=typeof e)throw new TypeError(`Category at index ${t} must be an object.`);if("string"!=typeof e.label)throw new TypeError(`Category.label at index ${t} must be a string.`);if("boolean"!=typeof e.required)throw new TypeError(`Category.required at index ${t} must be a boolean.`);if("boolean"!=typeof e.default)throw new TypeError(`Category.default at index ${t} must be a boolean.`)}loadPreferences(){const e=localStorage.getItem(this.#t.storageKey);return e?JSON.parse(e):null}removeWithAnimation(e){if(!(e instanceof HTMLElement))throw new TypeError("removeWithAnimation expects an HTMLElement.");e.classList.add("closing"),setTimeout(()=>e.remove(),this.#t.animationDuration)}savePreferences(e){if(!e||"object"!=typeof e)throw new TypeError("Preferences must be an object.");localStorage.setItem(this.#t.storageKey,JSON.stringify(e)),this.#t.onSave(e)}showConsentBar(){const e=document.createElement("div");e.className="cookie-consent-bar",e.innerHTML=this.#t.renderBar?this.#t.renderBar(this.#t):`\n <div><p>${this.#t.message}</p></div>\n <div><button class="accept">${this.#t.acceptText}</button></div>\n <div><button class="reject">${this.#t.rejectText}</button></div>\n <div><button class="settings">${this.#t.settingsText}</button></div>\n `,document.body.appendChild(e);const t=e.querySelector(".accept");t instanceof HTMLElement&&(t.onclick=()=>{const t={};this.#t.categories.forEach(e=>t[e.label]=!0),this.savePreferences(t),this.removeWithAnimation(e)});const n=e.querySelector(".reject");n instanceof HTMLElement&&(n.onclick=()=>{const t={};this.#t.categories.forEach(e=>t[e.label]=e.required),this.savePreferences(t),this.removeWithAnimation(e)});const o=e.querySelector(".settings");o instanceof HTMLElement&&(o.onclick=()=>this.showSettingsModal(e))}showSettingsModal(e){const t=document.createElement("div");t.className="cookie-consent-modal",t.innerHTML=this.#t.renderModal?this.#t.renderModal(this.#t):`\n <div class="modal-content">\n <h2>Cookie Settings</h2>\n <form class="settings-form">\n ${this.#t.categories.map(e=>`\n <label>\n <input type="checkbox" name="${e.label}" \n ${e.default?"checked":""} \n ${e.required?"disabled":""}>\n ${e.label}\n </label>\n `).join("")}\n </form>\n <button class="save">Save Preferences</button>\n </div>\n `,document.body.appendChild(t);const n=t.querySelector(".save");n instanceof HTMLElement&&(n.onclick=()=>{const n={};t.querySelectorAll("input[type=checkbox]").forEach(e=>{e instanceof HTMLInputElement&&(n[e.name]=e.checked)}),this.savePreferences(n),this.removeWithAnimation(t),e&&this.removeWithAnimation(e)})}isAllowed(e){if("string"!=typeof e)throw new TypeError("isAllowed expects a string as category.");return!!this.#e&&!!this.#e[e]}};window.TinyCookieConsent=t.TinyCookieConsent})();