1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-22 07:49:38 +01:00
deka-dom-el/dist/dde-with-observables.js

27 lines
12 KiB
JavaScript
Raw Normal View History

2023-11-24 20:41:04 +01:00
//deka-dom-el library is available via global namespace `dde`
(()=> {
2023-11-24 20:51:39 +01:00
var R={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function $(t,e=!0){return e?Object.assign(R,t):(Object.setPrototypeOf(t,R),t)}function N(t){return R.isPrototypeOf(t)&&t!==R?t:R}function x(t){return typeof t>"u"}function H(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function P(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}var F={setDeleteAttr:et,ssr:""};function et(t,e,n){if(Reflect.set(t,e,n),!!x(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 v=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],b={get current(){return v[v.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...v]},push(t={}){return v.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return v.push(v[0])},pop(){if(v.length!==1)return v.pop()}};function I(...t){return this.appendOriginal(...t),this}function nt(t){return t.append===I||(t.appendOriginal=t.append,t.append=I),t}var L;function y(t,e,...n){let r=N(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,b.push({scope:t,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(m=>m(s)),void 0):s}),c=t(e||void 0);let d=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let u=y.mark({type:"component",name:t.name,host:d?"this":"parentElement"});c.prepend(u),d&&(s=u);break}case t==="#text":c=j.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):c=j.call(this,document.createDocumentFragment(),e);break;case!!L:c=j.call(this,document.createElementNS(L,t),e);break;case!c:c=j.call(this,document.createElement(t),e)}return nt(c),s||(s=c),n.forEach(d=>d(s)),o&&b.pop(),o=2,c}function Et(t){let e=Symbol.for("default"),n=Array.from(t.querySelectorAll("slot")).reduce((o,c)=>Reflect.set(o,c.name||e,c)&&o,{}),r=Reflect.has(n,e);return t.append=new Proxy(t.append,{apply(o,c,s){if(!s.length)return t;let d=document.createDocumentFragment();for(let u of s){if(!u||!u.slot){r&&d.appendChild(u);continue}let l=u.slot,m=n[l];w(u,"remove","slot"),m&&(m.replaceWith(u),Reflect.deleteProperty(n,l))}return r&&(n[e].replaceWith(d),Reflect.deleteProperty(n,e)),Object.values(n).forEach(u=>u.replaceWith(y().append(...Array.from(u.childNodes)))),t}}),t}y.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=document.createComment(`<dde:mark ${t}${F.ssr}${n}>`);return e||(r.end=document.createComment("</dde:mark>")),r};function xt(t){let e=this;return function(...r){L=t;let o=y.call(e,...r);return L=void 0,o}}var{setDeleteAttr:J}=F,D=new WeakMap;function j(t,...e){if(!e.length)return t;D.set(t,V(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))G.call(this,t,n,r);return D.delete(t),t}function G(t,e,n){let{setRemoveAttr:r,s:o}=V(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(d,u)=>G.call(c,t,d,u));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Z(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 J(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return z(o,n,Z.bind(null,t[e]));case"ariaset":return z(o,n,(d,u)=>r("aria-"+d,u));case"classList":return rt.call(c,t,n)}return ot(t,e)?J(t,e,n):r(e,n)}function V(t,e){if(D.has(t))return D.get(t);let r=(t instanceof SVGElement?st:ct).bind(null,t,"Attribute"),o=N(e);return{setRemoveAttr:r,s:o}}function rt(t,e){let n=N(this);return z(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function Ot(t){return Array.from(t.children).forEach(e=>e.remove()),t}function w(t,e,n,r){return t instanceof HTMLElement?t[e+"At
* Investigate `__dde_attributes` key of the element.*/let[d,u]=s,l=r[S][d];if(l)return l(u)})(r),_.disconnected(function(){/*! This removes all observables mapped to attributes (`S.attribute`).
* Investigate `__dde_attributes` key of the element.*/O.clear(...Object.values(r[S]))})(r)}),new Proxy(n,{apply(o,c,s){if(!s.length)return o();let d=s[0];return w(r,"set",t,d)}})};var Y={isObservable:M,processReactiveAttribute(t,e,n,r){if(!M(n))return n;let o=c=>r(e,c);return B(n,o),tt(n,o,t,e),n()}};function tt(t,e,...n){let{current:r}=b;r.prevent||r.host(function(o){o[W]||(o[W]=[],_.disconnected(()=>o[W].forEach(([[c,s]])=>C(c,s,c[p]?.host()===o)))(o)),o[W].push([[t,e],...n])})}function X(t,e){let n=(...r)=>r.length?pt(n,...r):lt(n);return at(n,t,e)}var ut=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),U=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 at(t,e,n){let r=[];H(n)!=="[object Object]"&&(n={});let{onclear:o}=O.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=b;return Reflect.defineProperty(t,p,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new U},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[p],ut),t}function dt(){return T[T.length-1]}function lt(t){if(!t[p])return;let{value:e,listeners:n}=t[p],r=dt();return r&&n.add(r),h.has(r)&&h.get(r).add(t),e}function pt(t,e,n){if(!t[p])return;let r=t[p];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function B(t,e){if(t[p])return t[p].listeners.add(e)}function C(t,e,n){let r=t[p];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(t.clear(t),!h.has(r))return o;let c=h.get(r);if(!h.has(c))return o;h.get(c).forEach(s=>C(s,c,!0))}return o}$(Y);
globalThis.dde= {O: E,
assign: j,
assignAttribute: G,
chainableAppend: nt,
classListDeclarative: rt,
2023-11-24 20:41:04 +01:00
createElement: y,
2023-11-24 20:51:39 +01:00
createElementNS: xt,
dispatchEvent: At,
2023-11-24 20:41:04 +01:00
el: y,
2023-11-24 20:51:39 +01:00
elNS: xt,
elementAttribute: w,
empty: Ot,
isObservable: M,
observable: E,
on: _,
registerReactivity: $,
scope: b,
simulateSlots: Et
2023-11-24 20:41:04 +01:00
};
})();