UNPKG

toastification

Version:

Toast Notifications for TypeScript & Lestin

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