UNPKG

toastification

Version:

Toast Notifications for TypeScript & Lestin

3 lines (2 loc) 7.32 kB
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/"};function r(t,e={}){let{children:s,...n}=e;Array.isArray(s)||(s=[s]);let r,o=s.length;for(let t=0;t<o;t++){const e=s[t];Boolean(e)&&(!Array.isArray(e)||e.length)||0===e||s.splice(t,1)}if(o=s.length,e.children=s,"function"==typeof t)return t(e);"svg"!==t&&"path"!==t&&"circle"!==t||(n.xmlns=i.svg),r=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}r.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 o=e.length;for(let t=0;t<o;t++)i&&e[t]&&r.addEventListener(i,e[t],n)}else"style"===t?"string"==typeof e?r.style.cssText=e:Object.assign(r.style,e):"dataset"===t?Object.assign(r.dataset,e):"htmlFor"==t||"for"==t?r.htmlFor=e:["innerHTML","innerText","textContent"].includes(t)?r[t]=e:"ref"===t?e.current=r:"assign"===t&&"function"==typeof e?e(r):"xmlns"===t?r.setAttributeNS("http://www.w3.org/2000/xmlns/",t,e.toString()):(!0===e&&(e=t),r.setAttribute(t,e.toString()))}for(let t=0;t<o;t++)l(r,s[t]);return r}const o=t=>t.children;function l(t,e){if(Array.isArray(e)){const s=e.length;for(let n=0;n<s;n++)l(t,e[n])}else{const s=e instanceof Element;t.appendChild(s?e:document.createTextNode(e))}}var a;!function(t){t.Successful="Success",t.Error="Error",t.Info="Info"}(a||(a={}));const c={Successful:{classname:"Success",text:"Operation successful",color:"#47B35F"},Error:{classname:"Error",text:"An error occurred",color:"#47B35F"},Info:{classname:"Info",text:"Processing...",color:"#47B35F"}},h={duration:5e3,hasLoader:!1};var u=/*#__PURE__*/s("toastInterval"),d=/*#__PURE__*/s("toastTextElement"),f=/*#__PURE__*/s("toastProgressBarValueElement"),m=/*#__PURE__*/s("toastActionPinElement"),p=/*#__PURE__*/s("toastInitialOptions"),g=/*#__PURE__*/s("isPinned");class v{get isPinned(){return n(this,g)[g]}constructor(t=a.Info,e="",s={}){return this.toastElement=void 0,this.currentPercent=null,Object.defineProperty(this,u,{writable:!0,value:null}),Object.defineProperty(this,d,{writable:!0,value:null}),Object.defineProperty(this,f,{writable:!0,value:null}),Object.defineProperty(this,m,{writable:!0,value:null}),Object.defineProperty(this,p,{writable:!0,value:null}),Object.defineProperty(this,g,{writable:!0,value:!1}),this.SetGoing=this.SetInterval,this.toastElement=r("div",{class:"Toast"}),this.BuildToast(t,e,s)}BuildToast(e=a.Info,s="",i={}){var l;i=t({},h,i);let u=c.Info;e==a.Successful?u=c.Successful:e==a.Error&&(u=c.Error),null==s&&(s=u.text),n(this,g)[g]=i.isPinned||-1===i.duration;const v=!((i.noPin||i.isPinned)&&i.noDismiss),y=!(i.noPin||n(this,g)[g]),T=!i.noDismiss;n(this,p)[p]=i,n(this,d)[d]=r(o,{}),s&&(n(this,d)[d]=r("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]=r("div",{class:"Toast-ProgressBar-Value"}),y&&(n(this,m)[m]=r("button",{class:"Toast-Action Pin",onClick:()=>this.Pin(),children:r("i",{class:"fas fa-thumbtack"})})),this.toastElement.innerHTML="",this.SetToastType(e),v&&this.toastElement.append(r("div",{class:"Toast-ActionBox",children:[y&&n(this,m)[m],T&&r("button",{class:"Toast-Action Dismiss",onClick:()=>this.Dismiss(),children:r("i",{class:"fas fa-times"})})]}));const x=(i.title||s||i.buttons)&&!i.onlyLoader,E=i.onlyLoader||i.hasLoader&&!x;let w;return i.hasLoader&&(w=r("div",{class:"Toast-LoaderBox",children:r("div",{class:"Toast-Loader"})})),this.toastElement.append(r("div",{class:E?"Toast-Body Toast-LoaderOnly":"Toast-Body",children:[w,x&&r("div",{class:"Toast-Content",children:[i.title&&r("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,d)[d],i.buttons&&r("div",{class:"Toast-ButtonBox",children:null==(l=i.buttons)?void 0:l.map(t=>r("button",{class:"Toast-Button "+t.style,onClick:t.onClick,children:t.text}))})]}),r("div",{class:"Toast-ProgressBar",children:n(this,f)[f]})]})),n(this,g)[g]||this.SetInterval(i.duration),this}BuildSuccessToast(t="",e={}){return this.BuildToast(a.Successful,t,e)}BuildErrorToast(t="",e={}){return this.BuildToast(a.Error,t,e)}BuildInfoToast(t="",e={}){return this.BuildToast(a.Info,t,e)}BuildLoaderToast(e="",s={}){return this.BuildToast(a.Info,e,t({hasLoader:!0,isPinned:!0,noDismiss:!0},s))}Pin(t=0){return n(this,g)[g]=!0,n(this,u)[u]&&clearInterval(n(this,u)[u]),n(this,m)[m]&&n(this,m)[m].remove(),n(this,f)[f].style.width=t+"%",this}SetText(t=""){return n(this,d)[d].textContent=t,this}Dismiss(t=0){return n(this,u)[u]&&clearInterval(n(this,u)[u]),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=h.duration,e=0){return n(this,g)[g]=!1,n(this,u)[u]&&clearInterval(n(this,u)[u]),this.currentPercent=e,n(this,u)[u]=setInterval(()=>{this.currentPercent>=100?(clearInterval(n(this,u)[u]),this.Dismiss()):(this.currentPercent++,n(this,f)[f].style.width=this.currentPercent+"%")},(t-200)/100),n(this,p)[p].NoPauseOnHover||(this.toastElement.addEventListener("mouseenter",()=>clearInterval(n(this,u)[u])),this.toastElement.addEventListener("mouseleave",()=>{n(this,g)[g]||this.SetInterval(t,this.currentPercent)})),this}SetToastType(t){let e=c.Info;return t==a.Successful?e=c.Successful:t==a.Error&&(e=c.Error),this.toastElement.classList.remove("Info","Success","Error"),this.toastElement.classList.add(e.classname),this}set innerHTML(t){this.toastElement.innerHTML=t}}const y=()=>r("div",{id:"ToastBox"});function T(t=a.Info,e,s,n=document.body){const i=new v(t,e,s);let o=document.getElementById("ToastBox");return null===o&&(o=r(y,{}),n.prepend(o)),o.appendChild(i.toastElement),i}function x(t,e,s){return T(a.Successful,t,e,s)}function E(t,e,s){return T(a.Error,t,e,s)}function w(t,e,s){return T(a.Info,t,e,s)}function b(e,s,n){return T(a.Info,e,t({hasLoader:!0,isPinned:!0,noDismiss:!0},s),n)}export{E as ShowErrorToast,w as ShowInfoToast,b as ShowLoaderToast,x as ShowSuccessToast,T as ShowToast,v as Toast,y as ToastBox,a as ToastType,c as ToastTypeData}; //# sourceMappingURL=Toast.modern.mjs.map