UNPKG

@knighttower/toast

Version:
1 lines 4.62 kB
var t,n;t=this,n=()=>(()=>{"use strict";var t={d:(n,e)=>{for(var o in e){t.o(e,o)&&!t.o(n,o)&&Object.defineProperty(n,o,{enumerable:!0,get:e[o]})}},o:(t,n)=>Object.prototype.hasOwnProperty.call(t,n),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},n={};t.r(n),t.d(n,{default:()=>i});const e=t=>t&&"object"==typeof t&&t.constructor===Object,o=function(t){for(var n=arguments.length,i=new Array(n>1?n-1:0),s=1;s<n;s++){i[s-1]=arguments[s]}return i.reduce(((t,n)=>(e(n)?Object.entries(n).forEach((n=>{let[i,s]=n;t[i]=e(s)&&e(t[i])?o(t[i],s):s})):Object.assign(t,n),t)),{...t})},i=function(t,n){const e="x-toast",i={classname:"x",transition:"slide-left-right",insertBefore:!0,duration:3e3,autoClose:!0,progressBar:!0,onShow:function(t){},onHide:function(t){},position:"top-right"},s={container:"{:class-name}__container",mainwrap:"{:class-name}__wrapper",toasts:{info:"{:class-name}--info",success:"{:class-name}--success",warning:"{:class-name}--warning",error:"{:class-name}--error"},animate:{init:"{:transition}--init",show:"{:transition}--show",hide:"{:transition}--hide"},progress:"{:class-name}__progress",position:"--{:position}"};function r(t){return n.createElement(t||"div")}function a(t){return t.parentElement||t.parentNode}function c(t){for(var n=arguments.length,e=new Array(n>1?n-1:0),o=1;o<n;o++){e[o-1]=arguments[o]}return e.forEach((n=>{(function(t,n){return t.classList.contains(n)})(t,n)||t.classList.add(n)})),t}function u(t,n,e){let o=arguments.length>3&&void 0!==arguments[3]&&arguments[3];return t.addEventListener(n,e,o),t}function d(t,n,e){let o=arguments.length>3&&void 0!==arguments[3]&&arguments[3];return t.removeEventListener(n,e,o),t}let f=null;function l(){if(f){return f}const t=document.createElement("div"),n={WebkitTransition:"webkitTransitionEnd",MozTransition:"transitionend",OTransition:"oTransitionEnd otransitionend",transition:"transitionend"};for(let e in n){if(e in t.style){f=n[e];break}}return f}function p(t,n){for(const e in t){if(!0===t.hasOwnProperty(e)){switch(typeof t[e]){case"object":p(t[e],n);break;case"string":for(const o in n){!0===n.hasOwnProperty(o)&&(t[e]=t[e].replace(o,n[o]))}}}}return t}const h=function(t,n,e,o,i){const s=()=>{const t=a(n);n.remove(),t.childNodes.length<1&&a(t).remove()},r=n=>{d(n.target,n.type,r,!1),s(),"function"==typeof i&&i(t)};setTimeout((()=>{const e=l();void 0!==e?u(n,e,r,!1):(s(),"function"==typeof i&&i(t)),c(n,o.hide)}),e)},m=function(t,n){return"string"==typeof n&&(t.classmap[n]=p({...s},{"{:class-name}":e,"{:transition}":n,"{:position}":t.settings.position})),n};return t.$toast=t.$toast||new class{constructor(t){this.settings={},this.classmap={},this.config(t||i),this.transition(this.settings.transition)}config(t){return this.settings=o(i,this.settings,t||{}),this}transition(t){return this.settings.transition=m(this,t),this}toast(t,i,s){const f=this.classmap;let p=this.settings;"number"==typeof s?p.duration=s:p=o(this.settings,s||{}),!1===f.hasOwnProperty(p.transition)&&m(this,p.transition);const g=f[p.transition];let y=n.querySelector("."+g.container);const w=!!y;if(w){y=y.querySelector("."+g.mainwrap)}else{y=c(r("div"),g.container,g.container,g.position);const t=c(r("div"),g.mainwrap);y.appendChild(y=t)}const v=c(r("div"),e,p.classname,g.toasts[t],g.animate.init);return v.textContent=i,w||n.body.append(a(y)),function(t,n,e,o,i,s,r){const a=n=>{d(n.target,n.type,a,!1),"function"==typeof r&&r(t)};let f=e.childNodes;f=f[!0===s?0:f.length],e.insertBefore(n,f),setTimeout((()=>{const e=l();void 0!==e?u(n,e,a,!1):"function"==typeof r&&r(t),c(n,o.show)}),100)}(t,v,y,g.animate,p.duration,p.insertBefore,p.onShow),p.autoClose?h(t,v,p.duration,g.animate,p.onHide):function(t,n,e,o,i){c(n,i),u(n,"click",(function(i){i.stopPropagation(),h(t,n,0,e,o)}))}(t,v,g.animate,p.onHide,"close-on-click"),p.progressBar&&p.autoClose&&function(t,n,e,o){setTimeout((()=>{const i=c(r("div"),o.progress,o.progress+"--"+t);n.appendChild(i);let s=0,a=0;const u=setInterval((()=>{s++,a=Math.round(1500*s/e),a>100?clearInterval(u):i.style.width=a+"%"}),10)}),100)}(t,v,p.duration,g),this}info(t,n){this.toast("info",t,n)}success(t,n){this.toast("success",t,n)}warning(t,n){this.toast("warning",t,n)}error(t,n){this.toast("error",t,n)}}}("undefined"!=typeof window?window:{},"undefined"!=typeof document?document:{});return n})(),"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define("toast",[],n):"object"==typeof exports?exports.toast=n():t.toast=n();