undraw-js
Version:
Js library for Undraw illustration
1 lines • 1.78 kB
JavaScript
(()=>{"use strict";const t=(t,e)=>`https://unpkg.com/undraw-js@${e}/illus/${t}.svg`,e=async(t,r,o,s,i,n=0)=>{const a=()=>{n<i.maxRetry?setTimeout((()=>{e(t,r,o,s,i,n+1)}),i.retryDelay):o()};try{const e=await fetch(t);if(200===e.status){const t=await e.text();try{r(t)}catch(t){console.log(t)}}else a()}catch(t){a()}},r=(t,e)=>{const r=((t,e)=>e?t.replace(/#6c63ff/g,e):t)(t,e);return`data:image/svg+xml;utf8,${encodeURIComponent(r)}`},o=(o,s)=>{for(let i=0;i<o.length;i+=1){const n=o[i],a=n.getAttribute(s.nameAttr),l=n.getAttribute(s.colorAttr)||s.defaultColor,c=n.getAttribute(s.fallbackAttr);if(a){const o=t(a,s.version);e(o,(t=>{n.src=r(t,l),n.setAttribute(s.doneAttr,"true")}),(()=>{c?n.src=c:n.getAttribute("alt")||n.setAttribute("alt",a),console.error(`[undraw] failed to load illustration ${a}`)}),a,s)}else console.error("[undraw] Illustration name not provided")}};window.UndrawJS=new class{_observed=!1;initialConfigs={nameAttr:"data-ujs-name",colorAttr:"data-ujs-color",doneAttr:"data-ujs-done",fallbackAttr:"data-ujs-fall-img",version:"1.0.6"};maxRetry=2;retryDelay=2e3;_initObserver=()=>{if(!this._observed){const t={childList:!0,subtree:!0};new MutationObserver(this._observerCallback).observe(document.body,t),this._observed=!0}};_getQuerySelector=()=>`img[${this.options.nameAttr}]:not([${this.options.doneAttr}])`;_observerCallback=t=>{for(const e of t)for(const t of e.addedNodes)t.matches&&t.matches(this._getQuerySelector())?o([t],this.options):t.querySelectorAll&&t.querySelectorAll(this._getQuerySelector()).length&&o(t,this.options)};init=(t={})=>{this.options={...this.initialConfigs,...t},this._initObserver(),this.refresh()};refresh=()=>{const t=document.querySelectorAll(this._getQuerySelector());o(t,this.options)}},window.UndrawJs=window.UndrawJS})();