UNPKG

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.16 kB
(()=>{"use strict";var e={d:(t,s)=>{for(var n in s)e.o(s,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:s[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{TinyLoadingScreen:()=>s});const s=class{#e=null;get overlay(){return this.#e}#t=null;get messageElement(){return this.#t}#s;get container(){return this.#s}#n={fadeIn:null,fadeOut:null,zIndex:9999};get options(){return{...this.#n}}set options(e){if("object"!=typeof e||null===e)throw new TypeError("options must be an object");if(void 0!==e.fadeIn&&null!==e.fadeIn&&("number"!=typeof e.fadeIn||e.fadeIn<0))throw new TypeError("fadeIn must be a non-negative number or null");if(void 0!==e.fadeOut&&null!==e.fadeOut&&("number"!=typeof e.fadeOut||e.fadeOut<0))throw new TypeError("fadeOut must be a non-negative number or null");if(void 0!==e.zIndex&&("number"!=typeof e.zIndex||!Number.isInteger(e.zIndex)))throw new TypeError("zIndex must be an integer number");this.#n={fadeIn:e.fadeIn??null,fadeOut:e.fadeOut??null,zIndex:e.zIndex??9999}}#a="none";get status(){return this.#a}#i="";get defaultMessage(){return this.#i}set defaultMessage(e){if("string"!=typeof e&&!(e instanceof HTMLElement))throw new TypeError("defaultMessage must be a string or an HTMLElement");this.#i=e}#o=null;get message(){return this.#o}#l=!1;get allowHtmlText(){return this.#l}set allowHtmlText(e){if("boolean"!=typeof e)throw new TypeError("allowHtmlText must be a boolean");this.#l=e}#r=null;get fadeInTimeout(){return null!==this.#r}#u=null;get fadeOutTimeout(){return null!==this.#u}get visible(){return!!this.#e}#d=null;get onChange(){return this.#d}set onChange(e){if(null!==e&&"function"!=typeof e)throw new TypeError("onChange must be a function or null");this.#d=e}_emitChange(){"function"==typeof this.#d&&this.#d(this.#a)}constructor(e=document.body){if(!(e instanceof HTMLElement))throw new TypeError("container must be an HTMLElement");this.#s=e}_updateMessage(e=this.#i){if(!this.#t)throw new Error("messageElement is not initialized");if("string"!=typeof e&&!(e instanceof HTMLElement))throw new TypeError("message must be a string or an HTMLElement");if(this.#o=e,"string"==typeof e)this.#l?this.#t.innerHTML=e:this.#t.textContent=e;else{if(!this.#l)throw new Error("HTMLElement messages require allowHtmlText = true");this.#t.textContent="",this.#t.appendChild(e)}}_removeOldClasses(){this.#e?.classList.remove("active"),this.#e?.classList.remove("fadeIn"),this.#e?.classList.remove("fadeOut")}start(e=this.#i){if("string"!=typeof e&&!(e instanceof HTMLElement))throw new TypeError("message must be a string or an HTMLElement");if(!this.#e){this.#e=document.createElement("div"),this.#e.classList.add("loading-overlay"),this.#e.style.zIndex=String(this.#n.zIndex);const t=document.createElement("div");t.classList.add("loading-container");const s=document.createElement("div");s.classList.add("loading-content");const n=document.createElement("div");n.classList.add("loading-spinner"),this.#t=document.createElement("div"),this.#t.classList.add("loading-message"),s.appendChild(n),s.appendChild(this.#t),t.appendChild(s),this.#e.appendChild(t),this.#s.appendChild(this.#e),this._removeOldClasses(),this.#a="fadeIn",this.#e.classList.add("fadeIn"),this._emitChange();const a=()=>{this._removeOldClasses(),this.#r=null,this.#a="active",this.#e?.classList.add("active"),this._emitChange()};return"number"==typeof this.#n.fadeIn?(this.#r&&clearTimeout(this.#r),this.#r=setTimeout(a,this.#n.fadeIn)):a(),this._updateMessage(e),!0}return this.#t&&this._updateMessage(e),!1}update(e=this.#i){if("string"!=typeof e&&!(e instanceof HTMLElement))throw new TypeError("message must be a string or an HTMLElement");return!!this.#t&&(this._updateMessage(e),!0)}stop(){if(this.#e){this._removeOldClasses(),this.#a="fadeOut",this.#e.classList.add("fadeOut"),this._emitChange();const e=()=>{this._removeOldClasses(),this.#u=null,this.#a="none",this.#e?.remove(),this.#e=null,this.#t=null,this.#o=null,this._emitChange()};return"number"==typeof this.#n.fadeOut?(this.#u&&clearTimeout(this.#u),this.#u=setTimeout(e,this.#n.fadeOut)):e(),!0}return!1}};window.TinyLoadingScreen=t.TinyLoadingScreen})();