1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-24 17:39:36 +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-29 18:25:21 +01:00
var A={isObservable(e){return!1},processReactiveAttribute(e,t,n,r){return n}};function T(e,t=!0){return t?Object.assign(A,e):(Object.setPrototypeOf(e,A),e)}function C(e){return A.isPrototypeOf(e)&&e!==A?e:A}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 N(e,t){if(!e||!(e instanceof AbortSignal))return!0;if(!e.aborted)return e.addEventListener("abort",t),function(){e.removeEventListener("abort",t)}}var $={setDeleteAttr:ee,ssr:""};function ee(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 E=[{scope:document.body,host:e=>e?e(document.body):document.body,custom_element:!1,prevent:!0}],b={get current(){return E[E.length-1]},get host(){return this.current.host},preventDefault(){let{current:e}=this;return e.prevent=!0,e},get state(){return[...E]},push(e={}){return E.push(Object.assign({},this.current,{prevent:!1},e))},pushRoot(){return E.push(E[0])},pop(){if(E.length!==1)return E.pop()}};function H(...e){return this.appendOriginal(...e),this}function te(e){return e.append===H||(e.appendOriginal=e.append,e.append=H),e}var j;function y(e,t,...n){let r=C(this),o=0,c,s;switch((Object(t)!==t||r.isObservable(t))&&(t={textContent:t}),!0){case typeof e=="function":{o=1,b.push({scope:e,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(v=>v(s)),void 0):s}),c=e(t||void 0);let d=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let u=y.mark({type:"component",name:e.name,host:d?"this":"parentElement"});c.prepend(u),d&&(s=u);break}case e==="#text":c=P.call(this,document.createTextNode(""),t);break;case(e==="<>"||!e):c=P.call(this,document.createDocumentFragment(),t);break;case!!j:c=P.call(this,document.createElementNS(j,e),t);break;case!c:c=P.call(this,document.createElement(e),t)}return te(c),s||(s=c),n.forEach(d=>d(s)),o&&b.pop(),o=2,c}function _e(e){let t=Symbol.for("default"),n=Array.from(e.querySelectorAll("slot")).reduce((o,c)=>Reflect.set(o,c.name||t,c)&&o,{}),r=Reflect.has(n,t);return e.append=new Proxy(e.append,{apply(o,c,s){if(!s.length)return e;let d=document.createDocumentFragment();for(let u of s){if(!u||!u.slot){r&&d.appendChild(u);continue}let l=u.slot,v=n[l];O(u,"remove","slot"),v&&(v.replaceWith(u),Reflect.deleteProperty(n,l))}return r&&(n[t].replaceWith(d),Reflect.deleteProperty(n,t)),Object.values(n).forEach(u=>u.replaceWith(y().append(...Array.from(u.childNodes)))),e}}),e}y.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 Ee(e){let t=this;return function(...r){j=e;let o=y.call(t,...r);return j=void 0,o}}var{setDeleteAttr:I}=$,L=new WeakMap;function P(e,...t){if(!t.length)return e;L.set(e,G(e,this));for(let[n,r]of Object.entries(Object.assign({},...t)))Z.call(this,e,n,r);return L.delete(e),e}function Z(e,t,n){let{setRemoveAttr:r,s:o}=G(e,this),c=this;n=o.processReactiveAttribute(e,t,n,(d,u)=>Z.call(c,e,d,u));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,(d,u)=>r("aria-"+d,u));case"classList":return ne.call(c,e,n)}return re(e,t)?I(e,t,n):r(t,n)}function G(e,t){if(L.has(e))return L.get(e);let r=(e instanceof SVGElement?ce:oe).bind(null,e,"Attribute"),o=C(t);return{setRemoveAttr:r,s:o}}function ne(e,t){let n=C(this);return F(n,t,(r,o)=>e.classList.toggle(r,o===-1?void 0:!!o)),e}function xe(e){return Array.from(e.children).forEach(t=>t.remove()),e}function O(e,t,n,r){return e instanceof HTMLElement?e[t+"At
2023-11-24 21:07:48 +01:00
* Investigate `__dde_attributes` key of the element.*/let[d,u]=s,l=r[R][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.*/m.clear(...Object.values(r[R]))})(r)}),new Proxy(n,{apply(o,c,s){if(!s.length)return o();let d=s[0];return O(r,"set",e,d)}})};var X={isObservable:W,processReactiveAttribute(e,t,n,r){if(!W(n))return n;let o=c=>r(t,c);return U(n,o),Y(n,o,e,t),n()}};function Y(e,t,...n){let{current:r}=b;r.prevent||r.host(function(o){o[k]||(o[k]=[],_.disconnected(()=>o[k].forEach(([[c,s]])=>S(c,s,c[p]?.host()===o)))(o)),o[k].push([[e,t],...n])})}function Q(e,t){let n=(...r)=>r.length?le(n,...r):de(n);return ue(n,e,t)}var fe=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 ue(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}=b;return Reflect.defineProperty(e,p,{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[p],fe),e}function ae(){return M[M.length-1]}function de(e){if(!e[p])return;let{value:t,listeners:n}=e[p],r=ae();return r&&n.add(r),h.has(r)&&h.get(r).add(e),t}function le(e,t,n){if(!e[p])return;let r=e[p];if(!(!n&&r.value===t))return r.value=t,r.listeners.forEach(o=>o(t)),t}function U(e,t){if(e[p])return e[p].listeners.add(t)}function S(e,t,n){let r=e[p];if(!r)return;let o=r.listeners.delete(t);if(n&&!r.listeners.size){if(e.clear(e),!h.has(r))return o;let c=h.get(r);if(!h.has(c))return o;h.get(c).forEach(s=>S(s,c,!0))}return o}T(X);
globalThis.dde= {O: m,
assign: P,
assignAttribute: Z,
chainableAppend: te,
classListDeclarative: ne,
2023-11-24 20:41:04 +01:00
createElement: y,
2023-11-24 21:07:48 +01:00
createElementNS: Ee,
dispatchEvent: we,
2023-11-24 20:41:04 +01:00
el: y,
2023-11-24 21:07:48 +01:00
elNS: Ee,
elementAttribute: O,
empty: xe,
isObservable: W,
observable: m,
2023-11-24 20:51:39 +01:00
on: _,
2023-11-24 21:07:48 +01:00
registerReactivity: T,
2023-11-24 20:51:39 +01:00
scope: b,
2023-11-24 21:07:48 +01:00
simulateSlots: _e
2023-11-24 20:41:04 +01:00
};
})();