1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-23 00:59:38 +01:00
deka-dom-el/dist/esm-with-observables.js
2024-01-14 13:48:18 +01:00

5 lines
13 KiB
JavaScript

var D={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function $(t,e=!0){return e?Object.assign(D,t):(Object.setPrototypeOf(t,D),t)}function P(t){return D.isPrototypeOf(t)&&t!==D?t:D}function O(t){return typeof t>"u"}function G(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function L(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function T(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(r,o){return Reflect.set(r,ft(o),e(t,o)),r},{})}function ft(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var d={setDeleteAttr:ut,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function ut(t,e,n){if(Reflect.set(t,e,n),!!O(n)){if(Reflect.deleteProperty(t,e),t instanceof d.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var C="__dde_lifecycleToEvents",x="__dde_disconnect_mode";var A=[{get scope(){return d.D.body},host:t=>t?t(d.D.body):d.D.body,custom_element:!1,prevent:!0}],v={get current(){return A[A.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...A]},push(t={}){return A.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return A.push(A[0])},pop(){if(A.length!==1)return A.pop()}};function V(...t){return this.appendOriginal(...t),this}function at(t){return t.append===V||(t.appendOriginal=t.append,t.append=V),t}var j;function k(t,e,...n){let r=P(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,v.push({scope:t,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(b=>b(s)),void 0):s}),c=t(e||void 0);let a=c instanceof d.F;if(c.nodeName==="#comment")break;let p=k.mark({type:"component",name:t.name,host:a?"this":"parentElement"});c.prepend(p),a&&(s=p);break}case t==="#text":c=S.call(this,d.D.createTextNode(""),e);break;case(t==="<>"||!t):c=S.call(this,d.D.createDocumentFragment(),e);break;case!!j:c=S.call(this,d.D.createElementNS(j,t),e);break;case!c:c=S.call(this,d.D.createElement(t),e)}return at(c),s||(s=c),n.forEach(a=>a(s)),o&&v.pop(),o=2,c}function Nt(t,e=t,n=void 0){let r=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((s,a)=>Reflect.set(s,a.name||r,a)&&s,{}),c=Reflect.has(o,r);if(t.append=new Proxy(t.append,{apply(s,a,p){if(!p.length)return t;let l=d.D.createDocumentFragment();for(let b of p){if(!b||!b.slot){c&&l.appendChild(b);continue}let N=b.slot,i=o[N];lt(b,"remove","slot"),i&&(dt(i,b,n),Reflect.deleteProperty(o,N))}return c&&(o[r].replaceWith(l),Reflect.deleteProperty(o,r)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(a=>a.remove()),t.append(...s)}return e}function dt(t,e,n){n&&n(t,e);try{t.replaceWith(S(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=d.D.createComment(`<dde:mark ${t}${d.ssr}${n}>`);return e&&(r.end=d.D.createComment("</dde:mark>")),r};function Pt(t){let e=this;return function(...r){j=t;let o=k.call(e,...r);return j=void 0,o}}var M=new WeakMap,{setDeleteAttr:K}=d;function S(t,...e){if(!e.length)return t;M.set(t,Y(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))X.call(this,t,n,r);return M.delete(t),t}function X(t,e,n){let{setRemoveAttr:r,s:o}=Y(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(a,p)=>X.call(c,t,a,p));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Q(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 K(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return U(o,n,Q.bind(null,t[e]));case"ariaset":return U(o,n,(a,p)=>r("aria-"+a,p));case"classList":return pt.call(c,t,n)}return ht(t,e)?K(t,e,n):r(e,n)}function Y(t,e){if(M.has(t))return M.get(t);let r=(t instanceof d.S?vt:bt).bind(null,t,"Attribute"),o=P(e);return{setRemoveAttr:r,s:o}}function pt(t,e){let n=P(this);return U(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function Lt(t){return Array.from(t.children).forEach(e=>e.remove()),t}function lt(t,e,n,r){return t instanceof d.H?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function ht(t,e){if(!Reflect.has(t,e))return!1;let n=tt(t,e);return!O(n.set)}function tt(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||tt(t,e)}function U(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 et(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function bt(t,e,n,r){return t[(O(r)?"remove":"set")+e](n,et(r))}function vt(t,e,n,r,o=null){return t[(O(r)?"remove":"set")+e+"NS"](o,n,et(r))}function Q(t,e,n){if(Reflect.set(t,e,n),!!O(n))return Reflect.deleteProperty(t,e)}function qt(t,e,n=gt){v.push({scope:t,host:(...o)=>o.length?o.forEach(c=>c(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let r=e.call(t,n);return v.pop(),r}function Ft(t){return z(t.prototype,"connectedCallback",function(e,n,r){e.apply(n,r),n.dispatchEvent(new Event("dde:connected"))}),t.prototype[x]||(t.prototype[x]="dde"),z(t.prototype,"disconnectedCallback",function(e,n,r){e.apply(n,r);let o=()=>n.dispatchEvent(new Event("dde:disconnected"));if(n[x]!=="dde")return o();(queueMicrotask||setTimeout)(()=>!n.isConnected&&o())}),z(t.prototype,"attributeChangedCallback",function(e,n,r){let[o,,c]=r;n.dispatchEvent(new CustomEvent("dde:attributeChanged",{detail:[o,c]})),e.apply(n,r)}),t.prototype[C]=!0,t}function z(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function gt(t){return T(t,(e,n)=>e.getAttribute(n))}function Ht(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function y(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var W=d.M?mt():new Proxy({},{get(){return()=>{}}}),Et=new WeakSet;y.connected=function(t,e){let{custom_element:n}=v.current,r="connected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c[C]?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(L(e.signal,()=>W.offConnected(c,t))&&W.onConnected(c,t),c)}};y.disconnected=function(t,e){let{custom_element:n}=v.current,r="disconnected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(c){n&&(c=n),c[x]||(c[x]="dde");let s="dde:"+r;return c.addEventListener(s,t,e),c[C]||L(e.signal,()=>W.offDisconnected(c,t))&&W.onDisconnected(c,t),c}};var H=new WeakMap;y.disconnectedAsAbort=function(t){if(H.has(t))return H.get(t);let e=new AbortController;return H.set(t,e),t(y.disconnected(()=>e.abort())),e};y.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let c="dde:"+n;if(o.addEventListener(c,t,e),o[C]||Et.has(o)||!d.M)return o;let s=new d.M(function(p){for(let{attributeName:l,target:b}of p)b.dispatchEvent(new CustomEvent(c,{detail:[l,b.getAttribute(l)]}))});return L(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function mt(){let t=new Map,e=!1,n=new d.M(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 u=o(i);u.connected.has(f)||(u.connected.add(f),u.length_c+=1)},offConnected(i,f){if(!t.has(i))return;let u=t.get(i);u.connected.has(f)&&(u.connected.delete(f),u.length_c-=1,r(i,u))},onDisconnected(i,f){c();let u=o(i);u.disconnected.has(f)||(u.disconnected.add(f),u.length_d+=1)},offDisconnected(i,f){if(!t.has(i))return;let u=t.get(i);u.disconnected.has(f)&&(u.disconnected.delete(f),u.length_d-=1,r(i,u))}};function r(i,f){f.length_c||f.length_d||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function c(){e||(e=!0,n.observe(d.D.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){t.size>30&&await a();let f=[];if(!(i instanceof Node))return f;for(let u of t.keys())u===i||!(u instanceof Node)||i.contains(u)&&f.push(u);return f}function l(i,f){let u=!1;for(let m of i){if(f&&p(m).then(l),!t.has(m))continue;let R=t.get(m);R.length_c&&(m.dispatchEvent(new Event("dde:connected")),R.connected=new WeakSet,R.length_c=0,R.length_d||t.delete(m),u=!0)}return u}function b(i,f){let u=!1;for(let m of i){if(f&&p(m).then(b),!t.has(m)||!t.get(m).length_d)continue;let Z=N(m);m[x]==="dde"?(queueMicrotask||setTimeout)(Z):Z(),u=!0}return u}function N(i){return()=>{i.isConnected||(i.dispatchEvent(new Event("dde:disconnected")),t.delete(i))}}}var h=Symbol.for("observable");function q(t){try{return Reflect.has(t,h)}catch{return!1}}var F=[],g=new WeakMap;function E(t,e){if(typeof t!="function")return nt(!1,t,e);if(q(t))return t;let n=nt(!0),r=function(){let[o,...c]=g.get(r);if(g.set(r,new Set([o])),F.push(r),it(n,t()),F.pop(),!c.length)return;let s=g.get(r);for(let a of c)s.has(a)||w(a,r)};return g.set(n[h],r),g.set(r,new Set([n])),r(),n}E.action=function(t,e,...n){let r=t[h],{actions:o}=r;if(!o||!Reflect.has(o,e))throw new Error(`'${t}' has no action with name '${e}'!`);if(o[e].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};E.on=function t(e,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(e))return e.forEach(c=>t(c,n,r));J(e,n),o&&o.addEventListener("abort",()=>w(e,n))}};E.symbols={onclear:Symbol.for("Observable.onclear")};E.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[h];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,h)}function e(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 _="__dde_reactive";E.el=function(t,e){let n=k.mark({type:"reactive"},!0),r=n.end,o=d.D.createDocumentFragment();o.append(n,r);let{current:c}=v,s=a=>{if(!n.parentNode||!r.parentNode)return w(t,s);v.push(c);let p=e(a);v.pop(),Array.isArray(p)||(p=[p]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...p),n.isConnected&&yt(c.host())};return J(t,s),ct(t,s,n,e),s(t()),o};function yt(t){!t||!t[_]||(requestIdleCallback||setTimeout)(function(){t[_]=t[_].filter(([e,n])=>n.isConnected?!0:(w(...e),!1))})}var xt={_set(t){this.value=t}};function At(t){return function(e,n){let r=(...c)=>c.length?e.setAttribute(n,...c):e.getAttribute(n),o=st(r,r(),xt);return t[n]=o,o}}var B="__dde_attributes";E.observedAttributes=function(t){let e=t[B]={},n=T(t,At(e));return y.attributeChanged(function({detail:o}){/*! This maps attributes to observables (`O.observedAttributes`).
* Investigate `__dde_attributes` key of the element.*/let[c,s]=o,a=this[B][c];if(a)return E.action(a,"_set",s)})(t),y.disconnected(function(){/*! This removes all observables mapped to attributes (`O.observedAttributes`).
* Investigate `__dde_attributes` key of the element.*/E.clear(...Object.values(this[B]))})(t),n};var ot={isObservable:q,processReactiveAttribute(t,e,n,r){if(!q(n))return n;let o=c=>{if(!t.isConnected)return w(n,o);r(e,c)};return J(n,o),ct(n,o,t,e),n()}};function ct(t,e,...n){let{current:r}=v;r.prevent||r.host(function(o){o[_]||(o[_]=[],y.disconnected(()=>o[_].forEach(([[c,s]])=>w(c,s,c[h]?.host()===o)))(o)),o[_].push([[t,e],...n])})}function nt(t,e,n){let r=t?()=>rt(r):(...o)=>o.length?it(r,...o):rt(r);return st(r,e,n)}var Ot=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),I=class extends Error{constructor(){super();let[e,...n]=this.stack.split(`
`),r=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function st(t,e,n){let r=[];G(n)!=="[object Object]"&&(n={});let{onclear:o}=E.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=v;return Reflect.defineProperty(t,h,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new I},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[h],Ot),t}function _t(){return F[F.length-1]}function rt(t){if(!t[h])return;let{value:e,listeners:n}=t[h],r=_t();return r&&n.add(r),g.has(r)&&g.get(r).add(t),e}function it(t,e,n){if(!t[h])return;let r=t[h];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function J(t,e){if(t[h])return t[h].listeners.add(e)}function w(t,e,n){let r=t[h];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(E.clear(t),!g.has(r))return o;let c=g.get(r);if(!g.has(c))return o;g.get(c).forEach(s=>w(s,c,!0))}return o}$(ot);export{E as O,S as assign,X as assignAttribute,at as chainableAppend,pt as classListDeclarative,k as createElement,Pt as createElementNS,qt as customElementRender,Ft as customElementWithDDE,Ht as dispatchEvent,k as el,Pt as elNS,lt as elementAttribute,Lt as empty,q as isObservable,Ft as lifecycleToEvents,E as observable,gt as observedAttributes,y as on,$ as registerReactivity,v as scope,Nt as simulateSlots};