@knighttower/toast
Version:
A dead simple toast library
1 lines • 4.24 kB
JavaScript
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=(t,...n)=>n.reduce((t,n)=>(e(n)?Object.entries(n).forEach(([n,i])=>{t[n]=e(i)&&e(t[n])?o(t[n],i):i}):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,...n){return n.forEach(n=>{(function(t,n){return t.classList.contains(n)})(t,n)||t.classList.add(n)}),t}function u(t,n,e,o=!1){return t.addEventListener(n,e,o),t}function f(t,n,e,o=!1){return t.removeEventListener(n,e,o),t}let l=null;function d(){if(l){return l}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){l=n[e];break}}return l}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=>{f(n.target,n.type,r,!1),s(),"function"==typeof i&&i(t)};setTimeout(()=>{const e=d();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};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 l=this.classmap;let p=this.settings;"number"==typeof s?p.duration=s:p=o(this.settings,s||{}),!1===l.hasOwnProperty(p.transition)&&m(this,p.transition);const g=l[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 b=c(r("div"),e,p.classname,g.toasts[t],g.animate.init);return b.textContent=i,w||n.body.append(a(y)),function(t,n,e,o,i,s,r){const a=n=>{f(n.target,n.type,a,!1),"function"==typeof r&&r(t)};let l=e.childNodes;l=l[!0===s?0:l.length],e.insertBefore(n,l),setTimeout(()=>{const e=d();void 0!==e?u(n,e,a,!1):"function"==typeof r&&r(t),c(n,o.show)},100)}(t,b,y,g.animate,p.duration,p.insertBefore,p.onShow),p.autoClose?h(t,b,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,b,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,b,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)}}}(window,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();