toastification
Version:
Toast Notifications for TypeScript & Lestin
3 lines (2 loc) • 8.8 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/"};function r(t,e={}){let{children:n,...s}=e;Array.isArray(n)||(n=[n]);let i,r=n.length;for(let t=0;t<r;t++){const e=n[t];Boolean(e)&&(!Array.isArray(e)||e.length)||0===e||n.splice(t,1)}if(r=n.length,e.children=n,"function"==typeof t)return t(e);"svg"!==t&&"path"!==t&&"circle"!==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("http://www.w3.org/2000/xmlns/",t,e.toString()):(!0===e&&(e=t),i.setAttribute(t,e.toString()))}for(let t=0;t<r;t++)l(i,n[t]);return i}const a=t=>t.children;function l(t,e){if(Array.isArray(e)){const n=e.length;for(let s=0;s<n;s++)l(t,e[s])}else{const n=e instanceof Element;t.appendChild(n?e:document.createTextNode(e))}}var c;t.ToastType=void 0,(c=t.ToastType||(t.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"),T=/*#__PURE__*/s("toastProgressBarValueElement"),v=/*#__PURE__*/s("toastActionPinElement"),p=/*#__PURE__*/s("toastInitialOptions"),m=/*#__PURE__*/s("isPinned"),y=/*#__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,h,{writable:!0,value:null}),Object.defineProperty(this,f,{writable:!0,value:null}),Object.defineProperty(this,T,{writable:!0,value:null}),Object.defineProperty(this,v,{writable:!0,value:null}),Object.defineProperty(this,p,{writable:!0,value:null}),Object.defineProperty(this,m,{writable:!0,value:!1}),this.SetGoing=this.SetInterval,this.toastElement=r("div",{class:"Toast"}),this.BuildToast(e,n,s)}var s,o,l=n.prototype;return l.BuildToast=function(n,s,o){var l,c=this;void 0===n&&(n=t.ToastType.Info),void 0===s&&(s=""),void 0===o&&(o={}),o=e({},d,o);var h=u.Info;n==t.ToastType.Successful?h=u.Successful:n==t.ToastType.Error&&(h=u.Error),null==s&&(s=h.text),i(this,m)[m]=o.isPinned||-1===o.duration;var y=!((o.noPin||o.isPinned)&&o.noDismiss),g=!(o.noPin||i(this,m)[m]),b=!o.noDismiss;i(this,p)[p]=o,i(this,f)[f]=r(a,{}),s&&(i(this,f)[f]=r("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]=r("div",{class:"Toast-ProgressBar-Value"}),g&&(i(this,v)[v]=r("button",{class:"Toast-Action Pin",onClick:function(){return c.Pin()},children:r("i",{class:"fas fa-thumbtack"})})),this.toastElement.innerHTML="",this.SetToastType(n),y&&this.toastElement.append(r("div",{class:"Toast-ActionBox",children:[g&&i(this,v)[v],b&&r("button",{class:"Toast-Action Dismiss",onClick:function(){return c.Dismiss()},children:r("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=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:[o.title&&r("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,f)[f],o.buttons&&r("div",{class:"Toast-ButtonBox",children:null==(l=o.buttons)?void 0:l.map(function(t){return r("button",{class:"Toast-Button "+t.style,onClick:t.onClick,children:t.text})})})]}),r("div",{class:"Toast-ProgressBar",children:i(this,T)[T]})]})),i(this,m)[m]||this.SetInterval(o.duration),this},l.BuildSuccessToast=function(e,n){return void 0===e&&(e=""),void 0===n&&(n={}),this.BuildToast(t.ToastType.Successful,e,n)},l.BuildErrorToast=function(e,n){return void 0===e&&(e=""),void 0===n&&(n={}),this.BuildToast(t.ToastType.Error,e,n)},l.BuildInfoToast=function(e,n){return void 0===e&&(e=""),void 0===n&&(n={}),this.BuildToast(t.ToastType.Info,e,n)},l.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))},l.Pin=function(t){return void 0===t&&(t=0),i(this,m)[m]=!0,i(this,h)[h]&&clearInterval(i(this,h)[h]),i(this,v)[v]&&i(this,v)[v].remove(),i(this,T)[T].style.width=t+"%",this},l.SetText=function(t){return void 0===t&&(t=""),i(this,f)[f].textContent=t,this},l.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)},l.SetPercent=function(t){return void 0===t&&(t=0),this.Pin(t)},l.SetInterval=function(t,e){var n=this;return void 0===t&&(t=d.duration),void 0===e&&(e=0),i(this,m)[m]=!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,T)[T].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,m)[m]||n.SetInterval(t,n.currentPercent)})),this},l.SetToastType=function(e){var n=u.Info;return e==t.ToastType.Successful?n=u.Successful:e==t.ToastType.Error&&(n=u.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,m)[m]}},{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}(),g=function(){return r("div",{id:"ToastBox"})};function b(e,n,s,i){void 0===e&&(e=t.ToastType.Info),void 0===i&&(i=document.body);var o=new y(e,n,s),a=document.getElementById("ToastBox");return null===a&&(a=r(g,{}),i.prepend(a)),a.appendChild(o.toastElement),o}t.ShowErrorToast=function(e,n,s){return b(t.ToastType.Error,e,n,s)},t.ShowInfoToast=function(e,n,s){return b(t.ToastType.Info,e,n,s)},t.ShowLoaderToast=function(n,s,i){return b(t.ToastType.Info,n,e({hasLoader:!0,isPinned:!0,noDismiss:!0},s),i)},t.ShowSuccessToast=function(e,n,s){return b(t.ToastType.Successful,e,n,s)},t.ShowToast=b,t.Toast=y,t.ToastBox=g,t.ToastTypeData=u});
//# sourceMappingURL=Toast.umd.js.map