1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-25 01:39:37 +01:00
deka-dom-el/dist/dde.js
2024-02-03 09:06:04 +01:00

25 lines
9.2 KiB
JavaScript

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