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

30 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`
(()=> {
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
* 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);
2023-11-24 21:07:48 +01:00
globalThis.dde= {O: m,
assign: A,
2023-11-24 21:07:48 +01:00
assignAttribute: Z,
chainableAppend: ne,
classListDeclarative: oe,
createElement: R,
createElementNS: _e,
customElementRender: Se,
customElementWithDDE: Ce,
dispatchEvent: Pe,
el: R,
elNS: _e,
elementAttribute: _,
empty: Oe,
2023-11-24 21:07:48 +01:00
isObservable: W,
lifecycleToEvents: Ce,
2023-11-24 21:07:48 +01:00
observable: m,
on: E,
registerReactivity: M,
scope: p,
simulateSlots: xe
2023-11-24 20:41:04 +01:00
};
})();