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-with-observables.js

30 lines
12 KiB
JavaScript

//deka-dom-el library is available via global namespace `dde`
(()=> {
var w={isObservable(e){return!1},processReactiveAttribute(e,t,n,r){return n}};function M(e,t=!0){return t?Object.assign(w,e):(Object.setPrototypeOf(e,w),e)}function N(e){return w.isPrototypeOf(e)&&e!==w?e:w}function x(e){return typeof e>"u"}function B(e){let t=typeof e;return t!=="object"?t:e===null?"null":Object.prototype.toString.call(e)}function P(e,t){if(!e||!(e instanceof AbortSignal))return!0;if(!e.aborted)return e.addEventListener("abort",t),function(){e.removeEventListener("abort",t)}}var $={setDeleteAttr:te,ssr:""};function te(e,t,n){if(Reflect.set(e,t,n),!!x(n)){if(Reflect.deleteProperty(e,t),e instanceof HTMLElement&&e.getAttribute(t)==="undefined")return e.removeAttribute(t);if(Reflect.get(e,t)==="undefined")return Reflect.set(e,t,"")}}var y=[{scope:document.body,host:e=>e?e(document.body):document.body,custom_element:!1,prevent:!0}],p={get current(){return y[y.length-1]},get host(){return this.current.host},preventDefault(){let{current:e}=this;return e.prevent=!0,e},get state(){return[...y]},push(e={}){return y.push(Object.assign({},this.current,{prevent:!1},e))},pushRoot(){return y.push(y[0])},pop(){if(y.length!==1)return y.pop()}};function H(...e){return this.appendOriginal(...e),this}function ne(e){return e.append===H||(e.appendOriginal=e.append,e.append=H),e}var L;function R(e,t,...n){let r=N(this),o=0,c,s;switch((Object(t)!==t||r.isObservable(t))&&(t={textContent:t}),!0){case typeof e=="function":{o=1,p.push({scope:e,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(b=>b(s)),void 0):s}),c=e(t||void 0);let u=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let d=R.mark({type:"component",name:e.name,host:u?"this":"parentElement"});c.prepend(d),u&&(s=d);break}case e==="#text":c=A.call(this,document.createTextNode(""),t);break;case(e==="<>"||!e):c=A.call(this,document.createDocumentFragment(),t);break;case!!L:c=A.call(this,document.createElementNS(L,e),t);break;case!c:c=A.call(this,document.createElement(e),t)}return ne(c),s||(s=c),n.forEach(u=>u(s)),o&&p.pop(),o=2,c}function xe(e,t=e,n=void 0){let r=Symbol.for("default"),o=Array.from(t.querySelectorAll("slot")).reduce((s,u)=>Reflect.set(s,u.name||r,u)&&s,{}),c=Reflect.has(o,r);if(e.append=new Proxy(e.append,{apply(s,u,d){if(!d.length)return e;let l=document.createDocumentFragment();for(let b of d){if(!b||!b.slot){c&&l.appendChild(b);continue}let i=b.slot,f=o[i];_(b,"remove","slot"),f&&(re(f,b,n),Reflect.deleteProperty(o,i))}return c&&(o[r].replaceWith(l),Reflect.deleteProperty(o,r)),e.append=s,e}}),e!==t){let s=Array.from(e.childNodes);s.forEach(u=>u.remove()),e.append(...s)}return t}function re(e,t,n){n&&n(e,t);try{e.replaceWith(A(t,{className:[t.className,e.className],dataset:{...e.dataset}}))}catch{e.replaceWith(t)}}R.mark=function(e,t=!1){e=Object.entries(e).map(([o,c])=>o+`="${c}"`).join(" ");let n=t?"":"/",r=document.createComment(`<dde:mark ${e}${$.ssr}${n}>`);return t||(r.end=document.createComment("</dde:mark>")),r};function _e(e){let t=this;return function(...r){L=e;let o=R.call(t,...r);return L=void 0,o}}var{setDeleteAttr:I}=$,j=new WeakMap;function A(e,...t){if(!t.length)return e;j.set(e,G(e,this));for(let[n,r]of Object.entries(Object.assign({},...t)))Z.call(this,e,n,r);return j.delete(e),e}function Z(e,t,n){let{setRemoveAttr:r,s:o}=G(e,this),c=this;n=o.processReactiveAttribute(e,t,n,(u,d)=>Z.call(c,e,u,d));let[s]=t;if(s==="=")return r(t.slice(1),n);if(s===".")return J(e,t.slice(1),n);if(/(aria|data)([A-Z])/.test(t))return t=t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(t,n);switch(t==="className"&&(t="class"),t){case"xlink:href":return r(t,n,"http://www.w3.org/1999/xlink");case"textContent":return I(e,t,n);case"style":if(typeof n!="object")break;case"dataset":return F(o,n,J.bind(null,e[t]));case"ariaset":return F(o,n,(u,d)=>r("aria-"+u,d));case"classList":return oe.call(c,e,n)}return ce(e,t)?I(e,t,n):r(t,n)}function G(e,t){if(j.has(e))return j.get(e);let r=(e instanceof SVGElement?ie:se).bind(null,e,"Attribute"),o=N(t);return{setRemoveAttr:r,s:o}}function oe(e,t){let n=N(this);return F(n,t,(r,o)=>e.classList.toggle(r,o===-1?void 0:!!o)),e}function Oe(e){return Array.from(e.children).forEach(t=>t.remove()),e}function _(e,t,n,r){return e instanceof HTMLElement?e[t+"Attribute"](n,r):e[t+"AttributeNS"](null,n,r)}function ce(e,t){if(!Reflect.has(e,t))return!1;let n=V(e,t);return!x(n.set)}function V(e,t){if(e=Object.getPrototypeOf(e),!e)return{};let n=Object.getOwnPropertyDescriptor(e,t);return n||V(e,t)}function F(e,t,n){if(!(typeof t!="object"||t===null))return Object.entries(t).forEach(function([o,c]){o&&(c=e.processReactiveAttribute(t,o,c,n),n(o,c))})}function K(e){return Array.isArray(e)?e.filter(Boolean).join(" "):e}function se(e,t,n,r){return e[(x(r)?"remove":"set")+t](n,K(r))}function ie(e,t,n,r,o=null){return e[(x(r)?"remove":"set")+t+"NS"](o,n,K(r))}function J(e,t,n){if(Reflect.set(e,t,n),!!x(n))return Reflect.deleteProperty(e,t)}function Se(e,t,n=e){p.push({scope:e,host:(...o)=>o.length?o.forEach(c=>c(e)):e,custom_element:e});let r=t.call(e,n);return p.pop(),r}function Ce(e){for(let n of["connected","disconnected"])Q(e.prototype,n+"Callback",function(r,o,c){r.apply(o,c),o.dispatchEvent(new Event("dde:"+n))});let t="attributeChanged";return Q(e.prototype,t+"Callback",function(n,r,o){let[c,,s]=o;r.dispatchEvent(new CustomEvent("dde:"+t,{detail:[c,s]})),n.apply(r,o)}),e.prototype.__dde_lifecycleToEvents=!0,e}function Q(e,t,n){e[t]=new Proxy(e[t]||(()=>{}),{apply:n})}function Pe(e,t,n){return t||(t={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(e,Object.assign({detail:c[0]},t)):new Event(e,t);return o.dispatchEvent(s)}}function E(e,t,n){return function(o){return o.addEventListener(e,t,n),o}}var D=ue(),fe=new WeakSet;E.connected=function(e,t){let{custom_element:n}=p.current,r="connected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(P(t.signal,()=>D.offConnected(c,e))&&D.onConnected(c,e),c)}};E.disconnected=function(e,t){let{custom_element:n}=p.current,r="disconnected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents||P(t.signal,()=>D.offDisconnected(c,e))&&D.onDisconnected(c,e),c}};var z=new WeakMap;E.disconnectedAsAbort=function(e){if(z.has(e))return z.get(e);let t=new AbortController;return z.set(e,t),e(E.disconnected(()=>t.abort())),t};E.attributeChanged=function(e,t){let n="attributeChanged";return typeof t!="object"&&(t={}),function(o){let c="dde:"+n;if(o.addEventListener(c,e,t),o.__dde_lifecycleToEvents||fe.has(o))return o;let s=new MutationObserver(function(d){for(let{attributeName:l,target:b}of d)b.dispatchEvent(new CustomEvent(c,{detail:[l,b.getAttribute(l)]}))});return P(t.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function ue(){let e=new Map,t=!1,n=new MutationObserver(function(i){for(let f of i)if(f.type==="childList"){if(l(f.addedNodes,!0)){s();continue}b(f.removedNodes,!0)&&s()}});return{onConnected(i,f){c();let a=o(i);a.connected.has(f)||(a.connected.add(f),a.length_c+=1)},offConnected(i,f){if(!e.has(i))return;let a=e.get(i);a.connected.has(f)&&(a.connected.delete(f),a.length_c-=1,r(i,a))},onDisconnected(i,f){c();let a=o(i);a.disconnected.has(f)||(a.disconnected.add(f),a.length_d+=1)},offDisconnected(i,f){if(!e.has(i))return;let a=e.get(i);a.disconnected.has(f)&&(a.disconnected.delete(f),a.length_d-=1,r(i,a))}};function r(i,f){f.length_c||f.length_d||(e.delete(i),s())}function o(i){if(e.has(i))return e.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return e.set(i,f),f}function c(){t||(t=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!t||e.size||(t=!1,n.disconnect())}function u(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function d(i){e.size>30&&await u();let f=[];if(!(i instanceof Node))return f;for(let a of e.keys())a===i||!(a instanceof Node)||i.contains(a)&&f.push(a);return f}function l(i,f){let a=!1;for(let v of i){if(f&&d(v).then(l),!e.has(v))continue;let O=e.get(v);O.length_c&&(v.dispatchEvent(new Event("dde:connected")),O.connected=new WeakSet,O.length_c=0,O.length_d||e.delete(v),a=!0)}return a}function b(i,f){let a=!1;for(let v of i)f&&d(v).then(b),!(!e.has(v)||!e.get(v).length_d)&&(v.dispatchEvent(new Event("dde:disconnected")),e.delete(v),a=!0);return a}}var h=Symbol.for("observable");function W(e){try{return Reflect.has(e,h)}catch{return!1}}var T=[],g=new WeakMap;function m(e,t){if(typeof e!="function")return X(e,t);if(W(e))return e;let n=X(),r=function(){let[o,...c]=g.get(r);if(g.set(r,new Set([o])),T.push(r),n(e()),T.pop(),!c.length)return;let s=g.get(r);for(let u of c)s.has(u)||C(u,r)};return g.set(n[h],r),g.set(r,new Set([n])),r(),n}m.action=function(e,t,...n){let r=e[h],{actions:o}=r;if(!o||!Reflect.has(o,t))throw new Error(`'${e}' has no action with name '${t}'!`);if(o[t].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};m.on=function e(t,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(t))return t.forEach(c=>e(c,n,r));U(t,n),o&&o.addEventListener("abort",()=>C(t,n))}};m.symbols={onclear:Symbol.for("Observable.onclear")};m.clear=function(...e){for(let n of e){Reflect.deleteProperty(n,"toJSON");let r=n[h];r.onclear.forEach(o=>o.call(r)),t(n,r),Reflect.deleteProperty(n,h)}function t(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!g.has(o))return;let c=g.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),g.delete(o))})}};var k="__dde_reactive";m.el=function(e,t){let n=R.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=p,s=u=>{if(!n.parentNode||!r.parentNode)return C(e,s);p.push(c);let d=t(u);p.pop(),Array.isArray(d)||(d=[d]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...d)};return U(e,s),ee(e,s,n,t),s(e()),o};var S="__dde_attributes";m.attribute=function(e,t=null){let n=m(t),r;return p.host(o=>{if(r=o,_(r,"has",e)?n(_(r,"get",e)):t!==null&&_(r,"set",e,t),o[S]){o[S][e]=n;return}r[S]={[e]:n},E.attributeChanged(function({detail:s}){/*! This maps attributes to observables (`S.attribute`).
* Investigate `__dde_attributes` key of the element.*/let[u,d]=s,l=r[S][u];if(l)return l(d)})(r),E.disconnected(function(){/*! This removes all observables mapped to attributes (`S.attribute`).
* Investigate `__dde_attributes` key of the element.*/m.clear(...Object.values(r[S]))})(r)}),new Proxy(n,{apply(o,c,s){if(!s.length)return o();let u=s[0];return _(r,"set",e,u)}})};var Y={isObservable:W,processReactiveAttribute(e,t,n,r){if(!W(n))return n;let o=c=>r(t,c);return U(n,o),ee(n,o,e,t),n()}};function ee(e,t,...n){let{current:r}=p;r.prevent||r.host(function(o){o[k]||(o[k]=[],E.disconnected(()=>o[k].forEach(([[c,s]])=>C(c,s,c[h]?.host()===o)))(o)),o[k].push([[e,t],...n])})}function X(e,t){let n=(...r)=>r.length?he(n,...r):pe(n);return de(n,e,t)}var ae=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[t,...n]=this.stack.split(`
`),r=t.slice(t.indexOf("@"),t.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function de(e,t,n){let r=[];B(n)!=="[object Object]"&&(n={});let{onclear:o}=m.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=p;return Reflect.defineProperty(e,h,{value:{value:t,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),e.toJSON=()=>e(),Object.setPrototypeOf(e[h],ae),e}function le(){return T[T.length-1]}function pe(e){if(!e[h])return;let{value:t,listeners:n}=e[h],r=le();return r&&n.add(r),g.has(r)&&g.get(r).add(e),t}function he(e,t,n){if(!e[h])return;let r=e[h];if(!(!n&&r.value===t))return r.value=t,r.listeners.forEach(o=>o(t)),t}function U(e,t){if(e[h])return e[h].listeners.add(t)}function C(e,t,n){let r=e[h];if(!r)return;let o=r.listeners.delete(t);if(n&&!r.listeners.size){if(e.clear(e),!g.has(r))return o;let c=g.get(r);if(!g.has(c))return o;g.get(c).forEach(s=>C(s,c,!0))}return o}M(Y);
globalThis.dde= {O: m,
assign: A,
assignAttribute: Z,
chainableAppend: ne,
classListDeclarative: oe,
createElement: R,
createElementNS: _e,
customElementRender: Se,
customElementWithDDE: Ce,
dispatchEvent: Pe,
el: R,
elNS: _e,
elementAttribute: _,
empty: Oe,
isObservable: W,
lifecycleToEvents: Ce,
observable: m,
on: E,
registerReactivity: M,
scope: p,
simulateSlots: xe
};
})();