UNPKG

toastification

Version:

Toast Notifications for TypeScript & Lestin

3 lines (2 loc) 9.1 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 o={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 i(t,e={}){let{children:s,...n}=e;Array.isArray(s)||(s=[s]);let i,a=s.length;for(let t=0;t<a;t++){const e=s[t];Boolean(e)&&(!Array.isArray(e)||e.length)||0===e||s.splice(t,1)}if(a=s.length,e.children=s,"function"==typeof t)return t(e);r.includes(t)&&(n.xmlns=o.svg),i=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}i.className=e}else if(t.startsWith("on")){const s=t.replace(/Capture$/,""),n=t!==s;let o=s.toLowerCase().substring(2);"doubleclick"===o&&(o="dblclick"),Array.isArray(e)||(e=[e]);const r=e.length;for(let t=0;t<r;t++)o&&e[t]&&i.addEventListener(o,e[t],n)}else"style"===t?"string"==typeof e?i.style.cssText=e:Object.assign(i.style,e):"dataset"===t?Object.assign(i.dataset,e):"htmlFor"==t||"for"==t?i.htmlFor=e:["innerHTML","innerText","textContent"].includes(t)?i[t]=e:"ref"===t?e.current=i:"assign"===t&&"function"==typeof e?e(i):"xmlns"===t?i.setAttributeNS(o.xmlns,t,e.toString()):(!0===e&&(e=t),i.setAttribute(t,e.toString()))}for(let t=0;t<a;t++)l(i,s[t]);return i}const a=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 c;exports.ToastType=void 0,(c=exports.ToastType||(exports.ToastType={})).Successful="Success",c.Error="Error",c.Info="Info";var u={Successful:{classname:"Success",text:"Operation successful",color:"#47B35F"},Error:{classname:"Error",text:"An error occurred",color:"#47B35F"},Info:{classname:"Info",text:"Processing...",color:"#47B35F"}},d={duration:5e3,hasLoader:!1},h=/*#__PURE__*/s("toastInterval"),f=/*#__PURE__*/s("toastTextElement"),p=/*#__PURE__*/s("toastProgressBarValueElement"),T=/*#__PURE__*/s("toastActionPinElement"),m=/*#__PURE__*/s("toastInitialOptions"),v=/*#__PURE__*/s("isPinned"),y=/*#__PURE__*/function(){function e(t,e,s){return void 0===t&&(t=exports.ToastType.Info),void 0===e&&(e=""),void 0===s&&(s={}),this.toastElement=void 0,this.currentPercent=null,Object.defineProperty(this,h,{writable:!0,value:null}),Object.defineProperty(this,f,{writable:!0,value:null}),Object.defineProperty(this,p,{writable:!0,value:null}),Object.defineProperty(this,T,{writable:!0,value:null}),Object.defineProperty(this,m,{writable:!0,value:null}),Object.defineProperty(this,v,{writable:!0,value:!1}),this.SetGoing=this.SetInterval,this.toastElement=i("div",{class:"Toast"}),this.BuildToast(t,e,s)}var s,o,r=e.prototype;return r.BuildToast=function(e,s,o){var r,l=this;void 0===e&&(e=exports.ToastType.Info),void 0===s&&(s=""),void 0===o&&(o={}),o=t({},d,o);var c=u.Info;e==exports.ToastType.Successful?c=u.Successful:e==exports.ToastType.Error&&(c=u.Error),null==s&&(s=c.text),n(this,v)[v]=o.isPinned||-1===o.duration;var h=!((o.noPin||o.isPinned)&&o.noDismiss),y=!(o.noPin||n(this,v)[v]),x=!o.noDismiss;n(this,m)[m]=o,n(this,f)[f]=i(a,{}),s&&(n(this,f)[f]=i("p",{class:"Toast-Text",style:t({},o.textAlign?{textAlign:o.textAlign}:{},o.textSize?{fontSize:o.textSize}:{},o.textWeight?{fontWeight:o.textWeight}:{}),children:s})),n(this,p)[p]=i("div",{class:"Toast-ProgressBar-Value"}),y&&(n(this,T)[T]=i("button",{class:"Toast-Action Pin",onClick:function(){return l.Pin()},children:i("i",{class:"fas fa-thumbtack"})})),this.toastElement.innerHTML="",this.SetToastType(e),h&&this.toastElement.append(i("div",{class:"Toast-ActionBox",children:[y&&n(this,T)[T],x&&i("button",{class:"Toast-Action Dismiss",onClick:function(){return l.Dismiss()},children:i("i",{class:"fas fa-times"})})]}));var g,w=(o.title||s||o.buttons)&&!o.onlyLoader,b=o.onlyLoader||o.hasLoader&&!w;return o.hasLoader&&(g=i("div",{class:"Toast-LoaderBox",children:i("div",{class:"Toast-Loader"})})),this.toastElement.append(i("div",{class:b?"Toast-Body Toast-LoaderOnly":"Toast-Body",children:[g,w&&i("div",{class:"Toast-Content",children:[o.title&&i("h5",{class:"Toast-Title",style:t({},o.titleAlign?{textAlign:o.titleAlign}:{},o.titleSize?{fontSize:o.titleSize}:{},o.titleWeight?{fontWeight:o.titleWeight}:{}),children:o.title}),n(this,f)[f],o.buttons&&i("div",{class:"Toast-ButtonBox",children:null==(r=o.buttons)?void 0:r.map(function(t){return i("button",{class:"Toast-Button "+t.style,onClick:t.onClick,children:t.text})})})]}),i("div",{class:"Toast-ProgressBar",children:n(this,p)[p]})]})),n(this,v)[v]||this.SetInterval(o.duration),this},r.BuildSuccessToast=function(t,e){return void 0===t&&(t=""),void 0===e&&(e={}),this.BuildToast(exports.ToastType.Successful,t,e)},r.BuildErrorToast=function(t,e){return void 0===t&&(t=""),void 0===e&&(e={}),this.BuildToast(exports.ToastType.Error,t,e)},r.BuildInfoToast=function(t,e){return void 0===t&&(t=""),void 0===e&&(e={}),this.BuildToast(exports.ToastType.Info,t,e)},r.BuildLoaderToast=function(e,s){return void 0===e&&(e=""),void 0===s&&(s={}),this.BuildToast(exports.ToastType.Info,e,t({hasLoader:!0,isPinned:!0,noDismiss:!0},s))},r.Pin=function(t){return void 0===t&&(t=0),n(this,v)[v]=!0,n(this,h)[h]&&clearInterval(n(this,h)[h]),n(this,T)[T]&&n(this,T)[T].remove(),n(this,p)[p].style.width=t+"%",this},r.SetText=function(t){return void 0===t&&(t=""),n(this,f)[f].textContent=t,this},r.Dismiss=function(t){var e=this;return void 0===t&&(t=0),n(this,h)[h]&&clearInterval(n(this,h)[h]),setTimeout(function(){requestAnimationFrame(function(){e.toastElement.style.height=e.toastElement.scrollHeight+"px",requestAnimationFrame(function(){e.toastElement.classList.add("Bye"),e.toastElement.style.height="0",e.toastElement.style.margin="0",setTimeout(function(){return e.toastElement.remove()},500)})})},t)},r.SetPercent=function(t){return void 0===t&&(t=0),this.Pin(t)},r.SetInterval=function(t,e){var s=this;return void 0===t&&(t=d.duration),void 0===e&&(e=0),n(this,v)[v]=!1,n(this,h)[h]&&clearInterval(n(this,h)[h]),this.currentPercent=e,n(this,h)[h]=setInterval(function(){s.currentPercent>=100?(clearInterval(n(s,h)[h]),s.Dismiss()):(s.currentPercent++,n(s,p)[p].style.width=s.currentPercent+"%")},(t-200)/100),n(this,m)[m].NoPauseOnHover||(this.toastElement.addEventListener("mouseenter",function(){return clearInterval(n(s,h)[h])}),this.toastElement.addEventListener("mouseleave",function(){n(s,v)[v]||s.SetInterval(t,s.currentPercent)})),this},r.SetToastType=function(t){var e=u.Info;return t==exports.ToastType.Successful?e=u.Successful:t==exports.ToastType.Error&&(e=u.Error),this.toastElement.classList.remove("Info","Success","Error"),this.toastElement.classList.add(e.classname),this},s=e,(o=[{key:"isPinned",get:function(){return n(this,v)[v]}},{key:"innerHTML",set:function(t){this.toastElement.innerHTML=t}}])&&function(t,e){for(var s=0;s<e.length;s++){var n=e[s];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,"symbol"==typeof(o=function(t,e){if("object"!=typeof t||null===t)return t;var s=t[Symbol.toPrimitive];if(void 0!==s){var n=s.call(t,"string");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(n.key))?o:String(o),n)}var o}(s.prototype,o),Object.defineProperty(s,"prototype",{writable:!1}),e}(),x=function(){return i("div",{id:"ToastBox"})};function g(t,e,s,n){void 0===t&&(t=exports.ToastType.Info),void 0===n&&(n=document.body);var o=new y(t,e,s),r=document.getElementById("ToastBox");return null===r&&(r=i(x,{}),n.prepend(r)),r.appendChild(o.toastElement),o}exports.ShowErrorToast=function(t,e,s){return g(exports.ToastType.Error,t,e,s)},exports.ShowInfoToast=function(t,e,s){return g(exports.ToastType.Info,t,e,s)},exports.ShowLoaderToast=function(e,s,n){return g(exports.ToastType.Info,e,t({hasLoader:!0,isPinned:!0,noDismiss:!0},s),n)},exports.ShowSuccessToast=function(t,e,s){return g(exports.ToastType.Successful,t,e,s)},exports.ShowToast=g,exports.Toast=y,exports.ToastBox=x,exports.ToastTypeData=u; //# sourceMappingURL=Toast.cjs.map