mirror of
https://github.com/jaandrle/deka-dom-el
synced 2024-11-23 00:59:38 +01:00
25 lines
9.4 KiB
JavaScript
25 lines
9.4 KiB
JavaScript
//deka-dom-el library is available via global namespace `dde`
|
|
(()=> {
|
|
var A={isObservable(t){return!1},processReactiveAttribute(t,e,n,c){return n}};function B(t,e=!0){return e?Object.assign(A,t):(Object.setPrototypeOf(t,A),t)}function R(t){return A.isPrototypeOf(t)&&t!==A?t:A}function x(t){return typeof t>"u"}function _(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function W(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(c,r){return Reflect.set(c,I(r),e(t,r)),c},{})}function I(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var a={setDeleteAttr:Z,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function Z(t,e,n){if(Reflect.set(t,e,n),!!x(n)){if(Reflect.deleteProperty(t,e),t instanceof a.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var y="__dde_lifecycleToEvents",g="__dde_disconnect_mode";var v=[{get scope(){return a.D.body},host:t=>t?t(a.D.body):a.D.body,custom_element:!1,prevent:!0}],E={get current(){return v[v.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...v]},push(t={}){return v.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return v.push(v[0])},pop(){if(v.length!==1)return v.pop()}};function j(...t){return this.appendOriginal(...t),this}function G(t){return t.append===j||(t.appendOriginal=t.append,t.append=j),t}var O;function M(t,e,...n){let c=R(this),r=0,o,i;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{r=1,E.push({scope:t,host:(...h)=>h.length?(r===1?n.unshift(...h):h.forEach(l=>l(i)),void 0):i}),o=t(e||void 0);let d=o instanceof a.F;if(o.nodeName==="#comment")break;let p=M.mark({type:"component",name:t.name,host:d?"this":"parentElement"});o.prepend(p),d&&(i=p);break}case t==="#text":o=w.call(this,a.D.createTextNode(""),e);break;case(t==="<>"||!t):o=w.call(this,a.D.createDocumentFragment(),e);break;case!!O:o=w.call(this,a.D.createElementNS(O,t),e);break;case!o:o=w.call(this,a.D.createElement(t),e)}return G(o),i||(i=o),n.forEach(d=>d(i)),r&&E.pop(),r=2,o}function ft(t,e=t,n=void 0){let c=Symbol.for("default"),r=Array.from(e.querySelectorAll("slot")).reduce((i,d)=>Reflect.set(i,d.name||c,d)&&i,{}),o=Reflect.has(r,c);if(t.append=new Proxy(t.append,{apply(i,d,p){if(!p.length)return t;let h=a.D.createDocumentFragment();for(let l of p){if(!l||!l.slot){o&&h.appendChild(l);continue}let D=l.slot,s=r[D];K(l,"remove","slot"),s&&(V(s,l,n),Reflect.deleteProperty(r,D))}return o&&(r[c].replaceWith(h),Reflect.deleteProperty(r,c)),t.append=i,t}}),t!==e){let i=Array.from(t.childNodes);i.forEach(d=>d.remove()),t.append(...i)}return e}function V(t,e,n){n&&n(t,e);try{t.replaceWith(w(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}M.mark=function(t,e=!1){t=Object.entries(t).map(([r,o])=>r+`="${o}"`).join(" ");let n=e?"":"/",c=a.D.createComment(`<dde:mark ${t}${a.ssr}${n}>`);return e&&(c.end=a.D.createComment("</dde:mark>")),c};function at(t){let e=this;return function(...c){O=t;let r=M.call(e,...c);return O=void 0,r}}var S=new WeakMap,{setDeleteAttr:q}=a;function w(t,...e){if(!e.length)return t;S.set(t,U(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))$.call(this,t,n,c);return S.delete(t),t}function $(t,e,n){let{setRemoveAttr:c,s:r}=U(t,this),o=this;n=r.processReactiveAttribute(t,e,n,(d,p)=>$.call(o,t,d,p));let[i]=e;if(i==="=")return c(e.slice(1),n);if(i===".")return F(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),c(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return c(e,n,"http://www.w3.org/1999/xlink");case"textContent":return q(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return L(r,n,F.bind(null,t[e]));case"ariaset":return L(r,n,(d,p)=>c("aria-"+d,p));case"classList":return J.call(o,t,n)}return Q(t,e)?q(t,e,n):c(e,n)}function U(t,e){if(S.has(t))return S.get(t);let c=(t instanceof a.S?Y:X).bind(null,t,"Attribute"),r=R(e);return{setRemoveAttr:c,s:r}}function J(t,e){let n=R(this);return L(n,e,(c,r)=>t.classList.toggle(c,r===-1?void 0:!!r)),t}function dt(t){return Array.from(t.children).forEach(e=>e.remove()),t}function K(t,e,n,c){return t instanceof a.H?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function Q(t,e){if(!Reflect.has(t,e))return!1;let n=H(t,e);return!x(n.set)}function H(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||H(t,e)}function L(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([r,o]){r&&(o=t.processReactiveAttribute(e,r,o,n),n(r,o))})}function z(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function X(t,e,n,c){return t[(x(c)?"remove":"set")+e](n,z(c))}function Y(t,e,n,c,r=null){return t[(x(c)?"remove":"set")+e+"NS"](r,n,z(c))}function F(t,e,n){if(Reflect.set(t,e,n),!!x(n))return Reflect.deleteProperty(t,e)}function gt(t,e,n=tt){E.push({scope:t,host:(...r)=>r.length?r.forEach(o=>o(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let c=e.call(t,n);return E.pop(),c}function vt(t){return T(t.prototype,"connectedCallback",function(e,n,c){e.apply(n,c),n.dispatchEvent(new Event("dde:connected"))}),t.prototype[g]||(t.prototype[g]="dde"),T(t.prototype,"disconnectedCallback",function(e,n,c){e.apply(n,c);let r=()=>n.dispatchEvent(new Event("dde:disconnected"));if(n[g]!=="dde")return r();(queueMicrotask||setTimeout)(()=>!n.isConnected&&r())}),T(t.prototype,"attributeChangedCallback",function(e,n,c){let[r,,o]=c;n.dispatchEvent(new CustomEvent("dde:attributeChanged",{detail:[r,o]})),e.apply(n,c)}),t.prototype[y]=!0,t}function T(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function tt(t){return W(t,(e,n)=>e.getAttribute(n))}function mt(t,e,n){return e||(e={}),function(r,...o){n&&(o.unshift(r),r=typeof n=="function"?n():n);let i=o.length?new CustomEvent(t,Object.assign({detail:o[0]},e)):new Event(t,e);return r.dispatchEvent(i)}}function C(t,e,n){return function(r){return r.addEventListener(t,e,n),r}}var N=a.M?nt():new Proxy({},{get(){return()=>{}}}),et=new WeakSet;C.connected=function(t,e){let{custom_element:n}=E.current,c="connected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(o){n&&(o=n);let i="dde:"+c;return o.addEventListener(i,t,e),o[y]?o:o.isConnected?(o.dispatchEvent(new Event(i)),o):(_(e.signal,()=>N.offConnected(o,t))&&N.onConnected(o,t),o)}};C.disconnected=function(t,e){let{custom_element:n}=E.current,c="disconnected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(o){n&&(o=n),o[g]||(o[g]="dde");let i="dde:"+c;return o.addEventListener(i,t,e),o[y]||_(e.signal,()=>N.offDisconnected(o,t))&&N.onDisconnected(o,t),o}};var k=new WeakMap;C.disconnectedAsAbort=function(t){if(k.has(t))return k.get(t);let e=new AbortController;return k.set(t,e),t(C.disconnected(()=>e.abort())),e};C.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(r){let o="dde:"+n;if(r.addEventListener(o,t,e),r[y]||et.has(r)||!a.M)return r;let i=new a.M(function(p){for(let{attributeName:h,target:l}of p)l.dispatchEvent(new CustomEvent(o,{detail:[h,l.getAttribute(h)]}))});return _(e.signal,()=>i.disconnect())&&i.observe(r,{attributes:!0}),r}};function nt(){let t=new Map,e=!1,n=new a.M(function(s){for(let u of s)if(u.type==="childList"){if(h(u.addedNodes,!0)){i();continue}l(u.removedNodes,!0)&&i()}});return{onConnected(s,u){o();let f=r(s);f.connected.has(u)||(f.connected.add(u),f.length_c+=1)},offConnected(s,u){if(!t.has(s))return;let f=t.get(s);f.connected.has(u)&&(f.connected.delete(u),f.length_c-=1,c(s,f))},onDisconnected(s,u){o();let f=r(s);f.disconnected.has(u)||(f.disconnected.add(u),f.length_d+=1)},offDisconnected(s,u){if(!t.has(s))return;let f=t.get(s);f.disconnected.has(u)&&(f.disconnected.delete(u),f.length_d-=1,c(s,f))}};function c(s,u){u.length_c||u.length_d||(t.delete(s),i())}function r(s){if(t.has(s))return t.get(s);let u={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(s,u),u}function o(){e||(e=!0,n.observe(a.D.body,{childList:!0,subtree:!0}))}function i(){!e||t.size||(e=!1,n.disconnect())}function d(){return new Promise(function(s){(requestIdleCallback||requestAnimationFrame)(s)})}async function p(s){t.size>30&&await d();let u=[];if(!(s instanceof Node))return u;for(let f of t.keys())f===s||!(f instanceof Node)||s.contains(f)&&u.push(f);return u}function h(s,u){let f=!1;for(let b of s){if(u&&p(b).then(h),!t.has(b))continue;let m=t.get(b);m.length_c&&(b.dispatchEvent(new Event("dde:connected")),m.connected=new WeakSet,m.length_c=0,m.length_d||t.delete(b),f=!0)}return f}function l(s,u){let f=!1;for(let b of s){if(u&&p(b).then(l),!t.has(b)||!t.get(b).length_d)continue;let P=D(b);b[g]==="dde"?(queueMicrotask||setTimeout)(P):P(),f=!0}return f}function D(s){return()=>{s.isConnected||(s.dispatchEvent(new Event("dde:disconnected")),t.delete(s))}}}
|
|
globalThis.dde= {assign: w,
|
|
assignAttribute: $,
|
|
chainableAppend: G,
|
|
classListDeclarative: J,
|
|
createElement: M,
|
|
createElementNS: at,
|
|
customElementRender: gt,
|
|
customElementWithDDE: vt,
|
|
dispatchEvent: mt,
|
|
el: M,
|
|
elNS: at,
|
|
elementAttribute: K,
|
|
empty: dt,
|
|
lifecycleToEvents: vt,
|
|
observedAttributes: tt,
|
|
on: C,
|
|
registerReactivity: B,
|
|
scope: E,
|
|
simulateSlots: ft
|
|
};
|
|
|
|
})(); |