UNPKG

toastification

Version:

Toast Notifications for TypeScript & Lestin

3 lines (2 loc) 8.81 kB
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i])}return t},t.apply(this,arguments)}var e=0;function n(t){return"__private_"+e+++"_"+t}function i(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}const r={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"},s=["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:n,...i}=e;Array.isArray(n)||(n=[n]);let o,a=n.length;for(let t=0;t<a;t++){const e=n[t];Boolean(e)&&(!Array.isArray(e)||e.length)||0===e||n.splice(t,1)}if(a=n.length,e.children=n,"function"==typeof t)return t(e);s.includes(t)&&(i.xmlns=r.svg),o=i.xmlns?document.createElementNS(i.xmlns,t):document.createElement(t);for(const t in i){if(!Object.hasOwn(i,t))continue;let e=i[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 n=e.length;for(let i=0;i<n;i++)e[i]&&(t+=(t?" ":"")+e[i].trim());e=t}o.className=e}else if(t.startsWith("on")){const n=t.replace(/Capture$/,""),i=t!==n;let r=n.toLowerCase().substring(2);"doubleclick"===r&&(r="dblclick"),Array.isArray(e)||(e=[e]);const s=e.length;for(let t=0;t<s;t++)r&&e[t]&&o.addEventListener(r,e[t],i)}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(r.xmlns,t,e.toString()):(!0===e&&(e=t),o.setAttribute(t,e.toString()))}for(let t=0;t<a;t++)l(o,n[t]);return o}const a=t=>t.children;function l(t,e){if(Array.isArray(e)){const n=e.length;for(let i=0;i<n;i++)l(t,e[i])}else{const n=e instanceof Element;t.appendChild(n?e:document.createTextNode(e))}}var c;!function(t){t.Successful="Success",t.Error="Error",t.Info="Info"}(c||(c={}));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__*/n("toastInterval"),f=/*#__PURE__*/n("toastTextElement"),m=/*#__PURE__*/n("toastProgressBarValueElement"),v=/*#__PURE__*/n("toastActionPinElement"),p=/*#__PURE__*/n("toastInitialOptions"),y=/*#__PURE__*/n("isPinned"),g=/*#__PURE__*/function(){function e(t,e,n){return void 0===t&&(t=c.Info),void 0===e&&(e=""),void 0===n&&(n={}),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,m,{writable:!0,value:null}),Object.defineProperty(this,v,{writable:!0,value:null}),Object.defineProperty(this,p,{writable:!0,value:null}),Object.defineProperty(this,y,{writable:!0,value:!1}),this.SetGoing=this.SetInterval,this.toastElement=o("div",{class:"Toast"}),this.BuildToast(t,e,n)}var n,r,s=e.prototype;return s.BuildToast=function(e,n,r){var s,l=this;void 0===e&&(e=c.Info),void 0===n&&(n=""),void 0===r&&(r={}),r=t({},d,r);var h=u.Info;e==c.Successful?h=u.Successful:e==c.Error&&(h=u.Error),null==n&&(n=h.text),i(this,y)[y]=r.isPinned||-1===r.duration;var g=!((r.noPin||r.isPinned)&&r.noDismiss),T=!(r.noPin||i(this,y)[y]),b=!r.noDismiss;i(this,p)[p]=r,i(this,f)[f]=o(a,{}),n&&(i(this,f)[f]=o("p",{class:"Toast-Text",style:t({},r.textAlign?{textAlign:r.textAlign}:{},r.textSize?{fontSize:r.textSize}:{},r.textWeight?{fontWeight:r.textWeight}:{}),children:n})),i(this,m)[m]=o("div",{class:"Toast-ProgressBar-Value"}),T&&(i(this,v)[v]=o("button",{class:"Toast-Action Pin",onClick:function(){return l.Pin()},children:o("i",{class:"fas fa-thumbtack"})})),this.toastElement.innerHTML="",this.SetToastType(e),g&&this.toastElement.append(o("div",{class:"Toast-ActionBox",children:[T&&i(this,v)[v],b&&o("button",{class:"Toast-Action Dismiss",onClick:function(){return l.Dismiss()},children:o("i",{class:"fas fa-times"})})]}));var w,x=(r.title||n||r.buttons)&&!r.onlyLoader,E=r.onlyLoader||r.hasLoader&&!x;return r.hasLoader&&(w=o("div",{class:"Toast-LoaderBox",children:o("div",{class:"Toast-Loader"})})),this.toastElement.append(o("div",{class:E?"Toast-Body Toast-LoaderOnly":"Toast-Body",children:[w,x&&o("div",{class:"Toast-Content",children:[r.title&&o("h5",{class:"Toast-Title",style:t({},r.titleAlign?{textAlign:r.titleAlign}:{},r.titleSize?{fontSize:r.titleSize}:{},r.titleWeight?{fontWeight:r.titleWeight}:{}),children:r.title}),i(this,f)[f],r.buttons&&o("div",{class:"Toast-ButtonBox",children:null==(s=r.buttons)?void 0:s.map(function(t){return o("button",{class:"Toast-Button "+t.style,onClick:t.onClick,children:t.text})})})]}),o("div",{class:"Toast-ProgressBar",children:i(this,m)[m]})]})),i(this,y)[y]||this.SetInterval(r.duration),this},s.BuildSuccessToast=function(t,e){return void 0===t&&(t=""),void 0===e&&(e={}),this.BuildToast(c.Successful,t,e)},s.BuildErrorToast=function(t,e){return void 0===t&&(t=""),void 0===e&&(e={}),this.BuildToast(c.Error,t,e)},s.BuildInfoToast=function(t,e){return void 0===t&&(t=""),void 0===e&&(e={}),this.BuildToast(c.Info,t,e)},s.BuildLoaderToast=function(e,n){return void 0===e&&(e=""),void 0===n&&(n={}),this.BuildToast(c.Info,e,t({hasLoader:!0,isPinned:!0,noDismiss:!0},n))},s.Pin=function(t){return void 0===t&&(t=0),i(this,y)[y]=!0,i(this,h)[h]&&clearInterval(i(this,h)[h]),i(this,v)[v]&&i(this,v)[v].remove(),i(this,m)[m].style.width=t+"%",this},s.SetText=function(t){return void 0===t&&(t=""),i(this,f)[f].textContent=t,this},s.Dismiss=function(t){var e=this;return void 0===t&&(t=0),i(this,h)[h]&&clearInterval(i(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)},s.SetPercent=function(t){return void 0===t&&(t=0),this.Pin(t)},s.SetInterval=function(t,e){var n=this;return void 0===t&&(t=d.duration),void 0===e&&(e=0),i(this,y)[y]=!1,i(this,h)[h]&&clearInterval(i(this,h)[h]),this.currentPercent=e,i(this,h)[h]=setInterval(function(){n.currentPercent>=100?(clearInterval(i(n,h)[h]),n.Dismiss()):(n.currentPercent++,i(n,m)[m].style.width=n.currentPercent+"%")},(t-200)/100),i(this,p)[p].NoPauseOnHover||(this.toastElement.addEventListener("mouseenter",function(){return clearInterval(i(n,h)[h])}),this.toastElement.addEventListener("mouseleave",function(){i(n,y)[y]||n.SetInterval(t,n.currentPercent)})),this},s.SetToastType=function(t){var e=u.Info;return t==c.Successful?e=u.Successful:t==c.Error&&(e=u.Error),this.toastElement.classList.remove("Info","Success","Error"),this.toastElement.classList.add(e.classname),this},n=e,(r=[{key:"isPinned",get:function(){return i(this,y)[y]}},{key:"innerHTML",set:function(t){this.toastElement.innerHTML=t}}])&&function(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,"symbol"==typeof(r=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var i=n.call(t,"string");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(i.key))?r:String(r),i)}var r}(n.prototype,r),Object.defineProperty(n,"prototype",{writable:!1}),e}(),T=function(){return o("div",{id:"ToastBox"})};function b(t,e,n,i){void 0===t&&(t=c.Info),void 0===i&&(i=document.body);var r=new g(t,e,n),s=document.getElementById("ToastBox");return null===s&&(s=o(T,{}),i.prepend(s)),s.appendChild(r.toastElement),r}function w(t,e,n){return b(c.Successful,t,e,n)}function x(t,e,n){return b(c.Error,t,e,n)}function E(t,e,n){return b(c.Info,t,e,n)}function P(e,n,i){return b(c.Info,e,t({hasLoader:!0,isPinned:!0,noDismiss:!0},n),i)}export{x as ShowErrorToast,E as ShowInfoToast,P as ShowLoaderToast,w as ShowSuccessToast,b as ShowToast,g as Toast,T as ToastBox,c as ToastType,u as ToastTypeData}; //# sourceMappingURL=Toast.mjs.map