/
dist.min.js
1 lines (1 loc) 路 2.99 KB
/
dist.min.js
1
const e="readystatechange";let t;const n=e=>`color: ${e}; font-weight: bold;`,o="#f012be";function s({component:e,e:t,module:s,color:r}){if("boolean"==typeof __DEV__&&!__DEV__)return;const c=t.type;console.groupCollapsed(`${s}> %c${c}%c on %c${e}`,n(r||o),"",n("#1a80cc")),console.log(t),t.target&&console.log(t.target),console.groupEnd()}const r={};function c(e,t){if(!e)throw new Error(t)}function u(n){c("string"==typeof n&&!!n,"Component name must be a non-empty string"),c(!r[n],`The component of the given name is already registered: ${n}`);const o=`${n}-馃拪`,u=[({el:e})=>{e.classList.add(n),e.classList.add(o),e.addEventListener(`__ummount__:${n}`,(()=>{e.classList.remove(o)}),{once:!0})}],m=[],d=e=>{if(!e.classList.contains(o)){const t=l(new CustomEvent("__mount__",{bubbles:!1}),e);u.map((e=>{e(t)})),m.map((e=>{e(t)}))}};d.sel=`.${n}:not(.${o})`,r[n]=d,(t=t||new Promise((t=>{const n=document,o=()=>{"complete"===n.readyState&&(t(),n.removeEventListener(e,o))};n.addEventListener(e,o),o()}))).then((()=>{a(n)}));const _=new Proxy((()=>{}),{set:(e,t,o)=>i(n,u,m,t,o),get:(e,t)=>"outside"===t?new Proxy({},{set:(e,t,o)=>(c("function"==typeof o,`Event handler must be a function, ${typeof o} (${o}) is given`),u.push((({el:e})=>{const r=t=>{e===t.target||e.contains(t.target)||(s({module:"outside",color:"#39cccc",e:t,component:n}),o(l(t,e)))};document.addEventListener(t,r),e.addEventListener(`__unmount__:${n}`,(()=>{document.removeEventListener(t,r)}),{once:!0})})),!0)}):null,apply(e,t,o){const s=o[0];return c("string"==typeof s,"Delegation selector must be a string. ${typeof selector} is given."),new Proxy({},{set:(e,t,o)=>i(n,u,m,t,o,s)})}}),p=e=>{u.push((({el:t})=>{t.classList.add(e)}))};return{on:_,is:p,sub:e=>p(`sub:${e}`),innerHTML:e=>{u.push((({el:t})=>{t.innerHTML=e}))}}}function l(e,t){return{e:e,el:t,query:e=>t.querySelector(e),queryAll:e=>t.querySelectorAll(e),pub:(e,t)=>{document.querySelectorAll(`.sub\\:${e}`).forEach((n=>{n.dispatchEvent(new CustomEvent(e,{bubbles:!1,detail:t}))}))}}}function i(e,t,n,o,r,u){return c("function"==typeof r,`Event handler must be a function, ${typeof r} (${r}) is given`),"__mount__"===o?(n.push(r),!0):"__unmount__"===o?(t.push((({el:t})=>{t.addEventListener(`__unmount__:${e}`,(()=>{r(l(new CustomEvent("__unmount__"),t))}),{once:!0})})),!0):(t.push((({el:t})=>{const n=n=>{u&&![].some.call(t.querySelectorAll(u),(e=>e===n.target||e.contains(n.target)))||(s({module:"馃拪",color:"#e0407b",e:n,component:e}),r(l(n,t)))};t.addEventListener(`__unmount__:${e}`,(()=>{t.removeEventListener(o,n)}),{once:!0}),t.addEventListener(o,n)})),!0)}function a(e,t){let n;e?(!function(e){c("string"==typeof e,"The name should be a string"),c(!!r[e],`The component of the given name is not registered: ${e}`)}(e),n=[e]):n=Object.keys(r),n.map((e=>{[].map.call((t||document).querySelectorAll(r[e].sel),r[e])}))}function m(e,t){c(!!r[e],`The component of the given name is not registered: ${e}`),t.dispatchEvent(new CustomEvent(`__unmount__:${e}`))}export{u as component};export{a as mount};export{m as unmount};