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 • 3.63 kB
JavaScript
(()=>{"use strict";var t={d:(e,i)=>{for(var n in i)t.o(i,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:i[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{TinyToastNotify:()=>i});const i=class{#t;#e;#i;#n;#a;#r;constructor(t="top",e="right",i=3e3,n=50,a=300,r=".notify-container"){this.#o(t),this.#s(e),this.#l(i,"baseDuration"),this.#l(n,"extraPerChar"),this.#l(a,"fadeOutDuration"),this.#t=t,this.#e=e,this.#i=i,this.#n=n,this.#a=a;const o=document.querySelector(`${r}.${t}.${e}`);o instanceof HTMLElement?this.#r=o:(this.#r=document.createElement("div"),this.#r.className=`notify-container ${t} ${e}`,document.body.appendChild(this.#r))}getContainer(){if(!(this.#r instanceof HTMLElement))throw new Error("Container is not a valid HTMLElement.");return this.#r}#o(t){if(!["top","bottom"].includes(t))throw new Error(`Invalid vertical direction "${t}". Expected "top" or "bottom".`)}#s(t){if(!["left","right","center"].includes(t))throw new Error(`Invalid horizontal position "${t}". Expected "left", "right" or "center".`)}#l(t,e){if("number"!=typeof t||t<0||!Number.isFinite(t))throw new Error(`Invalid value for "${e}": ${t}. Must be a non-negative finite number.`)}getY(){return this.#t}setY(t){this.#o(t);const e=this.getContainer();e.classList.remove(this.#t),e.classList.add(t),this.#t=t}getX(){return this.#e}setX(t){this.#s(t);const e=this.getContainer();e.classList.remove(this.#e),e.classList.add(t),this.#e=t}getBaseDuration(){return this.#i}setBaseDuration(t){this.#l(t,"baseDuration"),this.#i=t}getExtraPerChar(){return this.#n}setExtraPerChar(t){this.#l(t,"extraPerChar"),this.#n=t}getFadeOutDuration(){return this.#a}setFadeOutDuration(t){this.#l(t,"fadeOutDuration"),this.#a=t}show(t){let e="",i="",n=null,a=!1,r=null;const o=document.createElement("div");if(o.className="notify enter","string"==typeof t)e=t;else{if("object"!=typeof t||null===t||"string"!=typeof t.message)throw new Error("Invalid argument for show(): expected string or { message: string, title?: string, onClick?: function, html?: boolean, avatar?: string }");if(e=t.message,i="string"==typeof t.title?t.title:"",a=!0===t.html,r="string"==typeof t.avatar?t.avatar:null,void 0!==t.onClick){if("function"!=typeof t.onClick)throw new Error("onClick must be a function if defined");n=t.onClick,o.classList.add("clickable")}}const s=document.createElement("button");if(s.innerHTML="×",s.className="close",s.setAttribute("aria-label","Close"),s.addEventListener("mouseenter",()=>{s.style.color="var(--notif-close-color-hover)"}),s.addEventListener("mouseleave",()=>{s.style.color="var(--notif-close-color)"}),r){const t=document.createElement("img");t.src=r,t.alt="avatar",t.className="avatar",o.appendChild(t)}if(i){const t=document.createElement("strong");t.textContent=i,t.style.display="block",o.appendChild(t)}if(a){const t=document.createElement("div");t.innerHTML=e,o.appendChild(t)}else o.appendChild(document.createTextNode(e));o.appendChild(s),this.getContainer().appendChild(o);const l=this.#i+e.length*this.#n+this.#a;let c=!1;const d=()=>{c||(c=!0,o.classList.remove("enter","show"),o.classList.add("exit"),setTimeout(()=>o.remove(),this.#a))};"function"==typeof n&&o.addEventListener("click",t=>{t.target!==s&&n(t,d)}),s.addEventListener("click",t=>{t.stopPropagation(),d()}),setTimeout(()=>{o.classList.remove("enter"),o.classList.add("show")},1),setTimeout(()=>d(),l)}destroy(){this.#r instanceof HTMLElement&&(this.#r.querySelectorAll(".notify").forEach(t=>t.remove()),this.#r.parentNode&&this.#r.parentNode.removeChild(this.#r),this.#r=null)}};window.TinyToastNotify=e.TinyToastNotify})();