1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-21 23:39:37 +01:00
deka-dom-el/dist/dde.js
Jan Andrle d2ddaaec6f
Dev bugs@v0.7.6 (#12)
* Update observables-lib.js

* `collectChildren`

* Update todosComponent.js

* 🚀 filter for `collectChildren`
2024-01-07 12:33:56 +01:00

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