1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-22 15:59:37 +01:00
deka-dom-el/dist/esm-with-observables.js
Jan Andrle e88a495525
💥 customElement (#11)
* 🎉

* Update customElement.js

* 💥 `observedAttributes`
2024-01-05 16:49:05 +01:00

5 lines
13 KiB
JavaScript

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