-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmalert.min.js
1 lines (1 loc) · 7.65 KB
/
gmalert.min.js
1
var Gmal=function(){"use strict";function e(e,t){e.addEventListener("animationend",(e=>{t(e.animationName)}))}function t(e,t){e.style.animationName=t}function n(e){return`gmal-${e}`}function o(e){return`var(--${n(e)})`}function s(e,...t){const n=document.createElement(e);return n.classList.add(...t),n}function i(...e){return s("div",...e)}const c=()=>{let e=document.querySelector(".gmal");return e||(e=i("gmal"),document.body.append(e)),e};function r(e,t){const n=e.style.cssText.split(";");n.pop(),e.style.cssText=`${n.concat(t).join(";")};`}function l(e=!0){const t=document.body;e?(t.style.paddingRight=window.innerWidth-document.documentElement.clientWidth+"px",t.style.overflow="hidden"):(t.style.overflow="",t.style.paddingRight="")}const a="currentColor",d=`style="fill:${a}"`,u="stroke-linecap:round;stroke-linejoin:round;stroke-width:2px",$=`style="fill:none;stroke:${a};${u}"`,p=(e,t=12,n=12)=>`<circle cx="12" cy="${t}" r="${n}" style="fill:${e}"/>`,m=(e,t="",n=24)=>{return`<svg class="${t}" ${o=n,`viewBox="0 0 ${o} ${o}"`} style="width:1em;height:1em;vertical-align:baseline">${e}</svg>`;var o},v=(e,t)=>`<path d="${e}" ${t}/>`,f=(e=!1)=>{const t=`${$} ${e?'transform="translate(-4 -4)"':""}`;return`${v("M16,8,8,16",t)}${v("M8,8l8,8",t)}`},h=m(f(!0),"",16),g=e=>m(`${v("M1,12A11,11,0,0,0,12,23h0A11,11,0,0,0,12,1",`style="fill:none;stroke:${e};${u}"`)}${v("M18.5,12A6.5,6.5,0,1,0,12,18.5h0",`style="fill:none;stroke:${e};${u}"`)}`,n("spin")),y=g(o("loading")),C=[e=>m(`${p(e)}${v("M12,17.5a1,1,0,0,1-1-1v-5h-.5a1,1,0,0,1,0-2H12a1,1,0,0,1,1,1v6A1,1,0,0,1,12,17.5Z",d)}${v("M14,18.5H10a1,1,0,0,1,0-2h4a1,1,0,0,1,0,2Z",d)}${p(a,6,1.5)}`),e=>m(`${p(e)}${v("M12,19.5A1.5,1.5,0,1,0,10.5,18,1.5,1.5,0,0,0,12,19.5Z",d)}${v("M12,14a1.5,1.5,0,0,1-1.5-1.5v-7a1.5,1.5,0,0,1,3,0v7A1.5,1.5,0,0,1,12,14Z",d)}`),e=>m(`${p(e)}${f()}`),e=>m(`${v("M12,1l3,2.1H18.8L20,6.5l3,2.1L21.83,12,23,15.4l-3,2.1L18.8,20.9H15L12,23,9,20.9H5.2L4,17.5,1,15.4,2.17,12,1,8.6,4,6.5,5.2,3.1H9Z",`style="fill:${e};stroke:${e};${u}"`)}${v("M7.5,12l3,3,6-6",$)}`),g],w=["info","warn","error","success","loading"];function x(e,t,o){return`class="${n("aniicon")} ${n(t)} ${e?n("dense"):""} ${o}"`}const k=`<div class="${n("ani-wrap")}"></div>`;function M(e,t=!1,o=""){switch(e){case"success":return function(e=!1,t=""){return`<div ${x(e,"ok-icon",t)}>${k}<div class="${n("ok-line")} ${n("line-tip")}"></div><div class="${n("ok-line")} ${n("line-long")}"></div><div class="${n("ok-ring")}"></div></div>`}(t,o);case"error":return function(e=!1,t=""){return`<div ${x(e,"err-icon",t)}>${k}<div class="${n("err-r")}"><div class="${n("err-ll")}"></div><div class="${n("err-lr")}"></div></div></div>`}(t,o);case"warn":return function(e=!1,t=""){return`<div ${x(e,"warn-icon",t)}>${k}<div class="${n("warn-content")}">!</div></div>`}(t,o);case"info":return function(e=!1,t=""){return`<div ${x(e,"info-icon",t)}>${k}<div class="${n("info-content")}">i</div></div>`}(t,o);case"loading":return function(e=!1,t=""){return`<div ${x(e,"load-icon",t)}>${y}</div>`}(t,o);default:return""}}class b{timeout=2500;maxCount=8;activeInsts=new Map;constructor(e,t){this.type=t,this.core=e,1===t&&(this.timeout=0)}config(e){this.timeout=e.timeout||this.timeout,this.maxCount=e.maxCount||this.maxCount}fire(e){const t=this.mkMsg(e);return"loading"!==e.type&&this.sT(t,e?.timeout||this.timeout),t}sT(e,t){if(!t)return;const{$el:n}=e,o=this.mkP(e,t);o.resume(),n.onmouseenter=o.pause,n.onmouseleave=o.resume}mkP(e,t){e.progress?.remove();const{$el:o}=e,s=i(n("progress")),c=i(n("progress-bar"));s.append(c),o.append(s),c.ontransitionend=()=>{e.close(-1)};const l=()=>c.clientWidth/s.clientWidth;return e.progress={pause:()=>{r(c,["transition:none",`width:${100*l()}%`])},resume:()=>{r(c,["width:0",`transition:width ${t*l()}ms linear`])},remove:()=>{s.remove()}}}mkMsg(e){const o=`${e.content}${e.type}`;if(!this.type&&this.activeInsts.has(o)){const e=this.activeInsts.get(o);return e.count+=1,function(e,o){const s=n("count");let i=e.querySelector(`.${s}`);i||(i=document.createElement("span"),i.classList.add(s),e.append(i)),i.innerHTML=`${o>99?"99":o}`,t(i,""),setTimeout((()=>{t(i,n("shake"))}))}(e.$el,e.count),e}const s={...e,onClosed:t=>{e?.onClosed&&e.onClosed(t)},onClose:()=>{this.activeInsts.delete(o)}},i=this.core(s);if(1===this.type||this.activeInsts.size>=this.maxCount){const e=this.activeInsts.values().next().value;e&&(e.progress?.pause(),e.close(-2))}const c={...i,id:o,count:1};return this.activeInsts.set(o,c),i.open(),c}}function L(e,t){const n=new b(e,t),o=(...e)=>n.fire(function(e){const t={content:"success",type:"success"};let n=!1;const o=e=>{switch(typeof e){case"string":n?t.type=e:(t.content=e,n=!0);break;case"number":t.timeout=e;break;case"object":Object.assign(t,e)}};for(let t=0;t<4;t++){const n=e[t];n&&o(n)}return t}(e));return o.config=n.config.bind(n),o}function H(){const e=s("i",n("close"));return e.innerHTML=h,e}function T(e,t){const o=document.createElement("button");return o.textContent=e,o.onclick=t,o.classList.add(n("alert-btn")),o}function q(o){const s=i(n("alert-box")),r=i(n("alert")),a=M(o.type,!1,n("alert-icon"));if(s.append(r),r.innerHTML=`${a}<div class="${n("alert-title")}">${o.content}</div>`,o.text||o.html){const e=i(n("alert-content"));o.html?"string"==typeof o.html?e.innerHTML=o.html:e.append(o.html):e.textContent=o.text||"hello",r.append(e)}const d=c(),u=i=>(o.onClose(),t(r,n("alert-out")),new Promise((t=>{e(r,(e=>{e===n("alert-out")&&(s.remove(),document.querySelector(`.${n("alert")}`)||l(!1),o.onClosed(i),t())}))})));if(s.onclick=e=>{o.hideClose||e.target===s&&(o.onClose(),u(0))},o.showCancel||o.showConfirm){const e=i(n("alert-btn-group"));o.showCancel&&e.append(T("取消",(()=>{u(0)}))),o.showConfirm&&e.append(T("确定",(()=>{u(1)}))),r.append(e)}if(!o.hideClose){const e=H();e.onclick=()=>{u(0)},r.append(e)}return{close:u,open:()=>{l(),d.append(s)},$el:r}}const I=L(q,1);function S(s){const r=function(e,t=""){const n=w.indexOf(e);return`<i class="${t}">${(C[n]??"")(o(e))}</i>`}(s.type,n("icon")),l=i(n("msg")),a=i(n("msg-main"));l.append(a),a.innerHTML=`${r}<div class=${n("msg-content")}>${s.content}</div>`;return{open:()=>{(()=>{let e=document.querySelector(`.${n("msg-box")}`);return e||(e=i(n("msg-box")),c().append(e),e)})().append(l),t(l,n("alert-in"))},close:o=>(s.onClose(),a.style.maxHeight=`${a.offsetHeight}px`,t(l,n("msg-out")),t(a,n("msg-close")),new Promise((t=>{e(l,(e=>{e===n("msg-out")&&(l.remove(),s.onClosed(o),t())}))}))),$el:l}}const A=L(S,0);function P(o){const s=M(o.type,!0,n("notice-icon")),l=i(n("notice"));l.innerHTML=`<div class="${n("notice-main")}">${s}<div class="${n("notice-content")}">${o.content}</div></div>`;return{open:()=>{(()=>{let e=document.querySelector(`.${n("notice-box")}`);return e||(e=i(n("notice-box")),c().append(e),e)})().prepend(l),r(l,[`--mh:${l.offsetHeight+10}px`]),t(l,n("open")),setTimeout((()=>{const e=l.querySelector(`.${n("notice-icon")}`);e&&(e.style.opacity="1")}),300)},close:s=>(o.onClose(),t(l,n("close")),new Promise((t=>{e(l,(e=>{e===n("close")&&(l.remove(),o.onClosed(s),t())}))}))),$el:l}}const E=L(P,0);function Z(o){const s=`var(--gmal-${o.type})`,r=i(n("info"));r.innerHTML=`<div class="${n("info-header")}"><div class="${n("info-title")} ${"loading"===o.type?n("load"):""}" style="background:${s}">${o.title||""}</div><span style="margin-right:auto">${o.headerLeft||""}</span><span style="opacity:.7">${o.headerRight||""}</span></div><div class="${n("info-content")}">${o.content}</div>`;const l=s=>(o.onClose(),new Promise((i=>{t(r,n("info-out")),e(r,(e=>{e===n("info-out")&&(r.remove(),o.onClosed(s),i())}))})));if(!o.hideClose){const e=r.querySelector(`.${n("info-header")}`),t=H();t.onclick=()=>{l(0)},e.append(t)}return{open:()=>{c().append(r)},close:l,$el:r}}return{alert:I,information:L(Z,1),notice:E,message:A}}();