toastification
Version:
Toast Notifications for TypeScript & Lestin
3 lines (2 loc) • 7.66 kB
JavaScript
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var s=arguments[e];for(var n in s)Object.prototype.hasOwnProperty.call(s,n)&&(t[n]=s[n])}return t},t.apply(this,arguments)}var e=0;function s(t){return"__private_"+e+++"_"+t}function n(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}const i={svg:"http://www.w3.org/2000/svg",html:"http://www.w3.org/1999/xhtml",xml:"http://www.w3.org/XML/1998/namespace",xlink:"http://www.w3.org/1999/xlink",xmlns:"http://www.w3.org/2000/xmlns/",mathMl:"http://www.w3.org/1998/Math/MathML"},r=["animate","animateMotion","animateTransform","circle","clipPath","defs","desc","discard","ellipse","filter","foreignObject","g","image","line","linearGradient","marker","mask","metadata","mpath","path","pattern","polygon","polyline","radialGradient","rect","set","stop","svg","switch","symbol","text","textPath","tspan","use","view"];function o(t,e={}){let{children:s,...n}=e;Array.isArray(s)||(s=[s]);let o,l=s.length;for(let t=0;t<l;t++){const e=s[t];Boolean(e)&&(!Array.isArray(e)||e.length)||0===e||s.splice(t,1)}if(l=s.length,e.children=s,"function"==typeof t)return t(e);r.includes(t)&&(n.xmlns=i.svg),o=n.xmlns?document.createElementNS(n.xmlns,t):document.createElement(t);for(const t in n){if(!Object.hasOwn(n,t))continue;let e=n[t];if(t&&(e||"number"==typeof e||""===e)&&"__source"!==t&&"__self"!==t&&"tsxTag"!==t)if("class"==t||"className"==t){let t="";if(Array.isArray(e)){e=e.flat(5);const s=e.length;for(let n=0;n<s;n++)e[n]&&(t+=(t?" ":"")+e[n].trim());e=t}o.className=e}else if(t.startsWith("on")){const s=t.replace(/Capture$/,""),n=t!==s;let i=s.toLowerCase().substring(2);"doubleclick"===i&&(i="dblclick"),Array.isArray(e)||(e=[e]);const r=e.length;for(let t=0;t<r;t++)i&&e[t]&&o.addEventListener(i,e[t],n)}else"style"===t?"string"==typeof e?o.style.cssText=e:Object.assign(o.style,e):"dataset"===t?Object.assign(o.dataset,e):"htmlFor"==t||"for"==t?o.htmlFor=e:["innerHTML","innerText","textContent"].includes(t)?o[t]=e:"ref"===t?e.current=o:"assign"===t&&"function"==typeof e?e(o):"xmlns"===t?o.setAttributeNS(i.xmlns,t,e.toString()):(!0===e&&(e=t),o.setAttribute(t,e.toString()))}for(let t=0;t<l;t++)a(o,s[t]);return o}const l=t=>t.children;function a(t,e){if(Array.isArray(e)){const s=e.length;for(let n=0;n<s;n++)a(t,e[n])}else{const s=e instanceof Element;t.appendChild(s?e:document.createTextNode(e))}}var c;!function(t){t.Successful="Success",t.Error="Error",t.Info="Info"}(c||(c={}));const h={Successful:{classname:"Success",text:"Operation successful",color:"#47B35F"},Error:{classname:"Error",text:"An error occurred",color:"#47B35F"},Info:{classname:"Info",text:"Processing...",color:"#47B35F"}},u={duration:5e3,hasLoader:!1};var d=/*#__PURE__*/s("toastInterval"),m=/*#__PURE__*/s("toastTextElement"),f=/*#__PURE__*/s("toastProgressBarValueElement"),p=/*#__PURE__*/s("toastActionPinElement"),g=/*#__PURE__*/s("toastInitialOptions"),v=/*#__PURE__*/s("isPinned");class y{get isPinned(){return n(this,v)[v]}constructor(t=c.Info,e="",s={}){return this.toastElement=void 0,this.currentPercent=null,Object.defineProperty(this,d,{writable:!0,value:null}),Object.defineProperty(this,m,{writable:!0,value:null}),Object.defineProperty(this,f,{writable:!0,value:null}),Object.defineProperty(this,p,{writable:!0,value:null}),Object.defineProperty(this,g,{writable:!0,value:null}),Object.defineProperty(this,v,{writable:!0,value:!1}),this.SetGoing=this.SetInterval,this.toastElement=o("div",{class:"Toast"}),this.BuildToast(t,e,s)}BuildToast(e=c.Info,s="",i={}){var r;i=t({},u,i);let a=h.Info;e==c.Successful?a=h.Successful:e==c.Error&&(a=h.Error),null==s&&(s=a.text),n(this,v)[v]=i.isPinned||-1===i.duration;const d=!((i.noPin||i.isPinned)&&i.noDismiss),y=!(i.noPin||n(this,v)[v]),T=!i.noDismiss;n(this,g)[g]=i,n(this,m)[m]=o(l,{}),s&&(n(this,m)[m]=o("p",{class:"Toast-Text",style:t({},i.textAlign?{textAlign:i.textAlign}:{},i.textSize?{fontSize:i.textSize}:{},i.textWeight?{fontWeight:i.textWeight}:{}),children:s})),n(this,f)[f]=o("div",{class:"Toast-ProgressBar-Value"}),y&&(n(this,p)[p]=o("button",{class:"Toast-Action Pin",onClick:()=>this.Pin(),children:o("i",{class:"fas fa-thumbtack"})})),this.toastElement.innerHTML="",this.SetToastType(e),d&&this.toastElement.append(o("div",{class:"Toast-ActionBox",children:[y&&n(this,p)[p],T&&o("button",{class:"Toast-Action Dismiss",onClick:()=>this.Dismiss(),children:o("i",{class:"fas fa-times"})})]}));const x=(i.title||s||i.buttons)&&!i.onlyLoader,w=i.onlyLoader||i.hasLoader&&!x;let E;return i.hasLoader&&(E=o("div",{class:"Toast-LoaderBox",children:o("div",{class:"Toast-Loader"})})),this.toastElement.append(o("div",{class:w?"Toast-Body Toast-LoaderOnly":"Toast-Body",children:[E,x&&o("div",{class:"Toast-Content",children:[i.title&&o("h5",{class:"Toast-Title",style:t({},i.titleAlign?{textAlign:i.titleAlign}:{},i.titleSize?{fontSize:i.titleSize}:{},i.titleWeight?{fontWeight:i.titleWeight}:{}),children:i.title}),n(this,m)[m],i.buttons&&o("div",{class:"Toast-ButtonBox",children:null==(r=i.buttons)?void 0:r.map(t=>o("button",{class:"Toast-Button "+t.style,onClick:t.onClick,children:t.text}))})]}),o("div",{class:"Toast-ProgressBar",children:n(this,f)[f]})]})),n(this,v)[v]||this.SetInterval(i.duration),this}BuildSuccessToast(t="",e={}){return this.BuildToast(c.Successful,t,e)}BuildErrorToast(t="",e={}){return this.BuildToast(c.Error,t,e)}BuildInfoToast(t="",e={}){return this.BuildToast(c.Info,t,e)}BuildLoaderToast(e="",s={}){return this.BuildToast(c.Info,e,t({hasLoader:!0,isPinned:!0,noDismiss:!0},s))}Pin(t=0){return n(this,v)[v]=!0,n(this,d)[d]&&clearInterval(n(this,d)[d]),n(this,p)[p]&&n(this,p)[p].remove(),n(this,f)[f].style.width=t+"%",this}SetText(t=""){return n(this,m)[m].textContent=t,this}Dismiss(t=0){return n(this,d)[d]&&clearInterval(n(this,d)[d]),setTimeout(()=>{requestAnimationFrame(()=>{this.toastElement.style.height=this.toastElement.scrollHeight+"px",requestAnimationFrame(()=>{this.toastElement.classList.add("Bye"),this.toastElement.style.height="0",this.toastElement.style.margin="0",setTimeout(()=>this.toastElement.remove(),500)})})},t)}SetPercent(t=0){return this.Pin(t)}SetInterval(t=u.duration,e=0){return n(this,v)[v]=!1,n(this,d)[d]&&clearInterval(n(this,d)[d]),this.currentPercent=e,n(this,d)[d]=setInterval(()=>{this.currentPercent>=100?(clearInterval(n(this,d)[d]),this.Dismiss()):(this.currentPercent++,n(this,f)[f].style.width=this.currentPercent+"%")},(t-200)/100),n(this,g)[g].NoPauseOnHover||(this.toastElement.addEventListener("mouseenter",()=>clearInterval(n(this,d)[d])),this.toastElement.addEventListener("mouseleave",()=>{n(this,v)[v]||this.SetInterval(t,this.currentPercent)})),this}SetToastType(t){let e=h.Info;return t==c.Successful?e=h.Successful:t==c.Error&&(e=h.Error),this.toastElement.classList.remove("Info","Success","Error"),this.toastElement.classList.add(e.classname),this}set innerHTML(t){this.toastElement.innerHTML=t}}const T=()=>o("div",{id:"ToastBox"});function x(t=c.Info,e,s,n=document.body){const i=new y(t,e,s);let r=document.getElementById("ToastBox");return null===r&&(r=o(T,{}),n.prepend(r)),r.appendChild(i.toastElement),i}function w(t,e,s){return x(c.Successful,t,e,s)}function E(t,e,s){return x(c.Error,t,e,s)}function b(t,e,s){return x(c.Info,t,e,s)}function P(e,s,n){return x(c.Info,e,t({hasLoader:!0,isPinned:!0,noDismiss:!0},s),n)}export{E as ShowErrorToast,b as ShowInfoToast,P as ShowLoaderToast,w as ShowSuccessToast,x as ShowToast,y as Toast,T as ToastBox,c as ToastType,h as ToastTypeData};
//# sourceMappingURL=Toast.modern.mjs.map