UNPKG

toastification

Version:

Toast Notifications for TypeScript & Lestin

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