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