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