@dlightjs/dlight
Advanced tools
Comparing version 1.0.0-alpha.42 to 1.0.0-alpha.43
@@ -1,2 +0,2 @@ | ||
var f={Comp:0,For:1,Cond:2,Env:3,Exp:4,Subview:5},d=class{_$dlNodeType;constructor(t){this._$dlNodeType=t}get _$el(){return d.toEls(this._$nodes)}_$parentEl;_$nodes;static toEls(t){let e=[];return this.loopShallowEls(t,s=>{e.push(s)}),e}static loopDLNodes(t,e){t.forEach(s=>{e(s),s._$nodes&&d.loopDLNodes(s._$nodes,e)})}static loopDLNodesInsideOut(t,e){t.forEach(s=>{s._$nodes&&d.loopDLNodesInsideOut(s._$nodes,e),e(s)})}static loopShallowEls(t,e){t.forEach(s=>{if(!("_$dlNodeType"in s))return e(s);s._$nodes&&d.loopShallowEls(s._$nodes,e)})}static loopShallowDLNodes(t,e){t.forEach(s=>{"_$dlNodeType"in s&&(e(s),s._$nodes&&d.loopShallowDLNodes(s._$nodes,e))})}static didMountInsideOut(t){t.forEach(e=>{"_$dlNodeType"in e&&e._$nodes&&d.didMountInsideOut(e._$nodes),e.didMount?.()})}static addParentEl(t,e){this.loopShallowDLNodes(t,s=>{s._$parentEl=e}),this.didMountInsideOut(t)}static getFlowIndexFromNodes(t,e){let s=0,n=[...t];for(;n.length>0;){let r=n.shift();if(r===e)break;"_$dlNodeType"in r?r._$nodes&&n.unshift(...r._$nodes):s++}return s}static appendNodesWithSibling(t,e,s){return s?this.insertNodesBefore(t,e,s):this.appendNodes(t,e)}static appendNodesWithIndex(t,e,s,n){return n=n??e.childNodes.length,n!==s?this.insertNodesBefore(t,e,e.childNodes[s]):this.appendNodes(t,e)}static insertNodesBefore(t,e,s){let n=0;return this.loopShallowEls(t,r=>{e.insertBefore(r,s),n++}),n}static appendNodes(t,e){let s=0;return this.loopShallowEls(t,n=>{e.appendChild(n),s++}),s}static lifecycleFunc(t,e){t(),e?.()}static addWillUnmount(t,e){t.willUnmount=this.lifecycleFunc.bind(this,e,t.willUnmount?.bind(t))}static addDidMount(t,e){t.didMount=this.lifecycleFunc.bind(this,e,t.didMount?.bind(t))}};import{Store as E}from"@dlightjs/store";"global"in E||(typeof window<"u"?E.global=window:typeof global<"u"?E.global=global:E.global={});"document"in E||typeof document<"u"&&(E.document=document);var h=E;function C(o){h.global=o}function H(o){h.document=o}function K(o,t){Object.assign(o.style,t)}function j(o,t){Object.assign(o.dataset,t)}function y(o,t,e){let s=`$${t}`;s in o&&o[s]===e||(o[t]=e,o[s]=e)}function B(o,t){Object.entries(t).forEach(([e,s])=>{y(o,e,s)})}function F(o,t,e){let s=`$${t}`;s in o&&o[s]===e||(o.setAttribute(t,e),o[s]=e)}function V(o,t){Object.entries(t).forEach(([e,s])=>{F(o,e,s)})}function q(o,t,e){let s=o[`$on${t}`];s&&o.removeEventListener(t,s),o.addEventListener(t,e),o[`$on${t}`]=e}function Y(o){let t=h.document.createElement("template");t.innerHTML=o;let e=t.content.firstChild;return()=>e.cloneNode(!0)}function Z(o){return h.document.createElement(o)}function P(o,t,e){o._$nodes||(o._$nodes=Array.from(o.childNodes)),o._$nodes.splice(e,0,t);let s=d.getFlowIndexFromNodes(o._$nodes,t);d.appendNodesWithIndex([t],o,s),d.addParentEl([t],o)}function I(o,t,e){if(t==="style"){K(o,e);return}if(t==="dataset"){j(o,e);return}if(t!=="element"){if(t==="prop"){B(o,e);return}if(t==="attr"){V(o,e);return}if(t==="innerHTML"){y(o,"innerHTML",e);return}if(t!=="forwardProp"){if(t.startsWith("on")){q(o,t.slice(2).toLowerCase(),e);return}F(o,t,e)}}}var D=class extends d{constructor(){super(f.Comp)}_$init(t,e,s,n){this._$notInitd=!0,n&&n._$addForwardProps(this),e!==null&&this._$setContent(e),t&&Object.entries(t).forEach(([r,$])=>{this._$setProp(r,$)}),s&&(this._$children=s),h.global.DLEnvStore&&Object.entries(h.global.DLEnvStore.envs).forEach(([r,[$,a]])=>{a.addNode(this),this._$initEnv(r,$,a)}),this._$callUpdatesBeforeInit(),this.willMount?.(),this._$nodes=this.View?.()??[]}_$callUpdatesBeforeInit(){let t=Object.getOwnPropertyNames(Object.getPrototypeOf(this)),e=Object.getOwnPropertyNames(this);[...t,...e].forEach(n=>{if(n.startsWith("$w$"))return this[n.slice(3)]();n.startsWith("$f$")&&(this[`$${n.slice(3)}`]=this[n])}),delete this._$notInitd}_$setForwardProp(t,e){if(t in this){this[t]=e;return}this._$forwardPropsId.push(t);let s=`$${t}`;this[s]=e,Object.defineProperty(this,t,{get(){return this[s]},set(n){this[s]!==n&&(this[s]=n,this._$forwardPropsSet?.forEach(r=>{r._$dlNodeType===f.Comp&&r._$setProp(t,n),r instanceof HTMLElement&&I(r,t,n)}))}})}_$addForwardProps(t){this._$forwardPropsSet.add(t),this._$forwardPropsId.forEach(e=>{let s=this[e];this._$forwardPropsSet?.forEach(n=>{n._$dlNodeType===f.Comp&&("_$forwardProps"in n&&n._$forwardPropsId.push(e),n._$setProp(e,s)),n instanceof HTMLElement&&I(n,e,s)})}),d.addWillUnmount(t,this._$forwardPropsSet.delete.bind(this._$forwardPropsSet,t))}_$setProp(t,e){"_$forwardProps"in this&&this._$setForwardProp(t,e),`$p$${t}`in this&&(this[t]=e)}_$initEnv(t,e,s){`$e$${t}`in this&&(this[t]=e,this[`$en$${t}`]=s)}_$updateEnv(t,e,s){`$e$${t}`in this&&s===this[`$en$${t}`]&&this[t]!==e&&(this[t]=e)}_$setContent(t){let e=this._$contentKey;e&&this[e]!==t&&(this[e]=t)}_$updateProp(t,e){let s=`$${t}`;this[s]!==e&&(this[s]=e,this._$updateDerived(t),this._$updateView(t))}_$updateDerived(t){"_$notInitd"in this||this[`$s$${t}`]?.forEach(e=>{`$w$${e}`in this?this[e]():this[`$${e}`]=this[`$f$${e}`]})}_$updateView(t){let e=this[`$$${t}`];e&&this._$update?.(e)}},ot=D;function nt(o,t){o._$updateDerived(t),o._$updateView(t)}var S=class{constructor(){this.envs={},this.currentEnvNodes=[]}addEnvNode(t){this.currentEnvNodes.push(t),this.mergeEnvs()}replaceEnvNodes(t){this.currentEnvNodes=t,this.mergeEnvs()}removeEnvNode(){this.currentEnvNodes.pop(),this.mergeEnvs()}mergeEnvs(){this.envs={},this.currentEnvNodes.forEach(t=>{Object.entries(t.envs).forEach(([e,s])=>{this.envs[e]=[s,t]})})}},T=class extends d{constructor(t){super(f.Env),h.global.DLEnvStore||(h.global.DLEnvStore=new S),this.envs=t,this.updateNodes=new Set,h.global.DLEnvStore.addEnvNode(this)}updateEnv(t,e){this.envs[t]=e,h.global.DLEnvStore.currentEnvNodes.includes(this)&&h.global.DLEnvStore.mergeEnvs(),this.updateNodes.forEach(s=>{s._$updateEnv(t,e,this)})}addNode(t){this.updateNodes.add(t),d.addWillUnmount(t,this.updateNodes.delete.bind(this.updateNodes,t))}initNodes(t){this._$nodes=t,h.global.DLEnvStore.removeEnvNode()}};function at(o){return h.document.createTextNode(o)}function lt(o,t){o.textContent!==t&&(o.textContent=t)}var M=class{propViewFunc;dlUpdateFunc=new Set;constructor(t){this.propViewFunc=t}build(){let t,e=n=>{n.initd=!0,t=n,this.dlUpdateFunc.add(n)},s=this.propViewFunc(e);return s.length===0?[]:(t&&d.addWillUnmount(s[0],this.dlUpdateFunc.delete.bind(this.dlUpdateFunc,t)),s)}update(t){this.dlUpdateFunc.forEach(e=>{if(e.initd){delete e.initd;return}e(t)})}};var O=class extends d{constructor(){super(f.Subview)}};var p=class extends d{constructor(t){super(t),h.global.DLEnvStore&&h.global.DLEnvStore.currentEnvNodes.length>0&&(this.savedEnvNodes=[...h.global.DLEnvStore.currentEnvNodes])}initNewNodes(t){d.addParentEl(t,this._$parentEl)}geneNewNodesInEnv(t){if(!this.savedEnvNodes){let n=t();return this.initNewNodes(n),n}let e=h.global.DLEnvStore.currentEnvNodes;h.global.DLEnvStore.replaceEnvNodes(this.savedEnvNodes);let s=t();return h.global.DLEnvStore.replaceEnvNodes(e),this.initNewNodes(s),s}removeNodes(t){d.loopDLNodes(t,e=>e.willUnmount?.()),d.loopShallowEls(t,e=>{this._$parentEl?.removeChild(e)}),d.loopDLNodesInsideOut(t,e=>e.didUnmount?.())}};var g=class extends p{array;keys;nodeFunc;nodess;depNum;updateArr=[];constructor(t,e,s){super(f.For),this.array=[...t],this.keys=s,this.depNum=e}addNodeFunc(t){this.nodeFunc=t,this.nodess=this.array.map((e,s)=>t(e,this.updateArr,s)),this._$nodes=this.nodess.flat(1)}update(t){if(!(t&this.depNum))for(let e=0;e<this.array.length;e++)console.log(this.updateArr[e]),this.updateItem(e,t)}updateItem(t,e){this.updateArr[t]?.(e??this.depNum,this.array[t])}updateArray(t,e){if(e){this.updateWithKey(t,e);return}this.updateWithOutKey(t)}getNewNodes(t,e){return this.geneNewNodesInEnv(()=>this.nodeFunc(this.array[t],e??this.updateArr,t))}updateWithOutKey(t){let e=this.array.length,s=t.length;if(this.array=[...t],this.updateArr=this.updateArr.slice(0,s),e===s){for(let r=0;r<this.array.length;r++)this.updateItem(r);return}let n=this._$parentEl;if(e<s){let r=p.getFlowIndexFromNodes(n._$nodes,this),$=n.childNodes.length;for(let a=0;a<s;a++){if(a<e){r+=p.getFlowIndexFromNodes(this.nodess[a]),this.updateItem(a);continue}let l=this.getNewNodes(a);p.appendNodesWithIndex(l,n,r,$),this.nodess.push(l)}this._$nodes=this.nodess.flat(1);return}for(let r=0;r<s;r++)this.updateItem(r);for(let r=s;r<e;r++)this.removeNodes(this.nodess[r]);this.nodess=this.nodess.slice(0,s),this._$nodes=this.nodess.flat(1)}updateWithKey(t,e){let s=this.keys;if(this.array=[...t],this.keys=e,g.arrayEqual(s,this.keys)){for(let i=0;i<this.array.length;i++)this.updateItem(i);return}let n=this._$parentEl,r=this.nodess;if(this.keys.length===0){let i=n._$nodes??[];if(i.length===1&&i[0]===this)n.innerHTML="";else for(let c=0;c<s.length;c++)this.removeNodes(r[c]);this.nodess=[],this._$nodes=[],this.updateArr=[];return}let $=p.getFlowIndexFromNodes(n._$nodes,this);if(s.length===0){let i=n.childNodes[$];for(let c=0;c<this.keys.length;c++){let u=this.getNewNodes(c);p.appendNodesWithSibling(u,n,i),this.nodess.push(u)}this._$nodes=this.nodess.flat(1);return}let a=[],l=[],m=[];for(let i=0;i<s.length;i++){let c=s[i];if(this.keys.includes(c)){a.push(c),l.push(r[i]),m.push(this.updateArr[i]);continue}this.removeNodes(r[i])}let _=n.childNodes.length,N=$;for(let i=0;i<this.keys.length;i++){let c=this.keys[i],u=a.indexOf(c);if(u!==-1){N+=p.getFlowIndexFromNodes(l[u]),this.updateArr[u]?.(this.depNum,this.array[i]);continue}m.splice(i,0,null);let x=this.getNewNodes(i,m),b=p.appendNodesWithIndex(x,n,N,_);N+=b,_+=b,l.splice(i,0,x),a.splice(i,0,c)}if(g.arrayEqual(this.keys,a)){this.nodess=l,this._$nodes=this.nodess.flat(1),this.updateArr=m;return}N=$;let L=[];for(let i=0;i<this.keys.length;i++){let c=this.keys[i],u=a.indexOf(c),x=L[i];if(x){let w=p.appendNodesWithIndex(x,n,N+p.getFlowIndexFromNodes(x),_);N+=w,_+=w,L[i]=void 0}else if(u===i){N+=p.getFlowIndexFromNodes(l[i]);continue}else{L[this.keys.indexOf(a[i])]=l[i];let w=p.appendNodesWithIndex(l[u],n,N,_);N+=w,_+=w}let b=l[i];l[i]=l[u],l[u]=b;let A=a[i];a[i]=a[u],a[u]=A;let U=m[i];m[i]=m[u],m[u]=U}this.nodess=l,this._$nodes=this.nodess.flat(1),this.updateArr=m}static arrayEqual(t,e){return t.length!==e.length?!1:t.every((s,n)=>s===e[n])}};var v=class extends p{nodesFunc;constructor(t){super(f.Exp),this.nodesFunc=t,this._$nodes=v.formatNodes(t())}update(){let t=this.geneNewNodesInEnv(()=>v.formatNodes(this.nodesFunc()));if(this.removeNodes(this._$nodes),t.length===0){this._$nodes=[];return}let e=this._$parentEl,s=p.getFlowIndexFromNodes(e._$nodes,this),n=e.childNodes[s];p.appendNodesWithSibling(t,e,n),this._$nodes=t}static formatNodes(t){return Array.isArray(t)||(t=[t]),t.flat(1).filter(e=>e!=null&&typeof e!="boolean").map(e=>typeof e=="string"||typeof e=="number"||typeof e=="bigint"?h.document.createTextNode(`${e}`):"propViewFunc"in e?e.build():e).flat(1)}};var W=class extends p{condFunc;cond;depNum;constructor(t){super(f.Cond),this.depNum=t}addCondFunc(t){this.cond=-1,this.condFunc=t,this._$nodes=this.condFunc(this)}updateCond(){let t=this.geneNewNodesInEnv(()=>this.condFunc(this));if(this.didntChange)return this.didntChange=!1,this.updateFunc?.(this.depNum),this._$nodes;if(this._$nodes&&this._$nodes.length>0&&this.removeNodes(this._$nodes),t.length===0)return this._$nodes=[],this._$nodes;let e=this._$parentEl,s=p.getFlowIndexFromNodes(e._$nodes,this),n=e.childNodes[s];return p.appendNodesWithSibling(t,e,n),this._$nodes=t,this._$nodes}update(t){t&this.depNum||this.updateFunc?.(t)}};function Tt(o,t){let e=o;if(typeof o=="string"){let n=h.document.getElementById(o);if(n)e=n;else throw new Error(`DLight: Element with id ${o} not found`)}e.innerHTML="";let s=new t;s._$init(),P(e,s,0)}function Mt(o,t){return o()}function G(o){return o}var Ot=G;export{Ot as $,D as CompNode,W as CondNode,T as EnvNode,v as ExpNode,g as ForNode,M as PropView,O as SubViewNode,ot as View,Z as createElement,Y as createTemplate,at as createTextNode,G as escape,I as forwardHTMLProp,P as insertNode,Mt as manual,Tt as render,j as setDataset,H as setDocument,q as setEvent,C as setGlobal,F as setHTMLAttr,V as setHTMLAttrs,y as setHTMLProp,B as setHTMLProps,K as setStyle,nt as update,lt as updateText}; | ||
var f={Comp:0,For:1,Cond:2,Env:3,Exp:4,Subview:5},d=class{_$dlNodeType;constructor(t){this._$dlNodeType=t}get _$el(){return d.toEls(this._$nodes)}_$parentEl;_$nodes;static toEls(t){let e=[];return this.loopShallowEls(t,s=>{e.push(s)}),e}static loopDLNodes(t,e){t.forEach(s=>{e(s),s._$nodes&&d.loopDLNodes(s._$nodes,e)})}static loopDLNodesInsideOut(t,e){t.forEach(s=>{s._$nodes&&d.loopDLNodesInsideOut(s._$nodes,e),e(s)})}static loopShallowEls(t,e){t.forEach(s=>{if(!("_$dlNodeType"in s))return e(s);s._$nodes&&d.loopShallowEls(s._$nodes,e)})}static loopShallowDLNodes(t,e){t.forEach(s=>{"_$dlNodeType"in s&&(e(s),s._$nodes&&d.loopShallowDLNodes(s._$nodes,e))})}static didMountInsideOut(t){t.forEach(e=>{"_$dlNodeType"in e&&e._$nodes&&d.didMountInsideOut(e._$nodes),e.didMount?.()})}static addParentEl(t,e){this.loopShallowDLNodes(t,s=>{s._$parentEl=e}),this.didMountInsideOut(t)}static getFlowIndexFromNodes(t,e){let s=0,n=[...t];for(;n.length>0;){let r=n.shift();if(r===e)break;"_$dlNodeType"in r?r._$nodes&&n.unshift(...r._$nodes):s++}return s}static appendNodesWithSibling(t,e,s){return s?this.insertNodesBefore(t,e,s):this.appendNodes(t,e)}static appendNodesWithIndex(t,e,s,n){return n=n??e.childNodes.length,n!==s?this.insertNodesBefore(t,e,e.childNodes[s]):this.appendNodes(t,e)}static insertNodesBefore(t,e,s){let n=0;return this.loopShallowEls(t,r=>{e.insertBefore(r,s),n++}),n}static appendNodes(t,e){let s=0;return this.loopShallowEls(t,n=>{e.appendChild(n),s++}),s}static lifecycleFunc(t,e){t(),e?.()}static addWillUnmount(t,e){t.willUnmount=this.lifecycleFunc.bind(this,e,t.willUnmount?.bind(t))}static addDidMount(t,e){t.didMount=this.lifecycleFunc.bind(this,e,t.didMount?.bind(t))}};import{Store as E}from"@dlightjs/store";"global"in E||(typeof window<"u"?E.global=window:typeof global<"u"?E.global=global:E.global={});"document"in E||typeof document<"u"&&(E.document=document);var h=E;function C(o){h.global=o}function H(o){h.document=o}function K(o,t){Object.assign(o.style,t)}function j(o,t){Object.assign(o.dataset,t)}function y(o,t,e){let s=`$${t}`;s in o&&o[s]===e||(o[t]=e,o[s]=e)}function B(o,t){Object.entries(t).forEach(([e,s])=>{y(o,e,s)})}function F(o,t,e){let s=`$${t}`;s in o&&o[s]===e||(o.setAttribute(t,e),o[s]=e)}function V(o,t){Object.entries(t).forEach(([e,s])=>{F(o,e,s)})}function q(o,t,e){let s=o[`$on${t}`];s&&o.removeEventListener(t,s),o.addEventListener(t,e),o[`$on${t}`]=e}function Y(o){let t=h.document.createElement("template");t.innerHTML=o;let e=t.content.firstChild;return()=>e.cloneNode(!0)}function Z(o){return h.document.createElement(o)}function P(o,t,e){o._$nodes||(o._$nodes=Array.from(o.childNodes)),o._$nodes.splice(e,0,t);let s=d.getFlowIndexFromNodes(o._$nodes,t);d.appendNodesWithIndex([t],o,s),d.addParentEl([t],o)}function I(o,t,e){if(t==="style"){K(o,e);return}if(t==="dataset"){j(o,e);return}if(t!=="element"){if(t==="prop"){B(o,e);return}if(t==="attr"){V(o,e);return}if(t==="innerHTML"){y(o,"innerHTML",e);return}if(t!=="forwardProp"){if(t.startsWith("on")){q(o,t.slice(2).toLowerCase(),e);return}F(o,t,e)}}}var D=class extends d{constructor(){super(f.Comp)}_$init(t,e,s,n){this._$notInitd=!0,n&&n._$addForwardProps(this),e!==null&&this._$setContent(e),t&&Object.entries(t).forEach(([r,$])=>{this._$setProp(r,$)}),s&&(this._$children=s),h.global.DLEnvStore&&Object.entries(h.global.DLEnvStore.envs).forEach(([r,[$,a]])=>{a.addNode(this),this._$initEnv(r,$,a)}),this._$callUpdatesBeforeInit(),this.willMount?.(),this._$nodes=this.View?.()??[]}_$callUpdatesBeforeInit(){let t=Object.getOwnPropertyNames(Object.getPrototypeOf(this)),e=Object.getOwnPropertyNames(this);[...t,...e].forEach(n=>{if(n.startsWith("$w$"))return this[n.slice(3)]();n.startsWith("$f$")&&(this[`$${n.slice(3)}`]=this[n])}),delete this._$notInitd}_$setForwardProp(t,e){if(t in this){this[t]=e;return}this._$forwardPropsId.push(t);let s=`$${t}`;this[s]=e,Object.defineProperty(this,t,{get(){return this[s]},set(n){this[s]!==n&&(this[s]=n,this._$forwardPropsSet?.forEach(r=>{r._$dlNodeType===f.Comp&&r._$setProp(t,n),r instanceof HTMLElement&&I(r,t,n)}))}})}_$addForwardProps(t){this._$forwardPropsSet.add(t),this._$forwardPropsId.forEach(e=>{let s=this[e];this._$forwardPropsSet?.forEach(n=>{n._$dlNodeType===f.Comp&&("_$forwardProps"in n&&n._$forwardPropsId.push(e),n._$setProp(e,s)),n instanceof HTMLElement&&I(n,e,s)})}),d.addWillUnmount(t,this._$forwardPropsSet.delete.bind(this._$forwardPropsSet,t))}_$setProp(t,e){"_$forwardProps"in this&&this._$setForwardProp(t,e),`$p$${t}`in this&&(this[t]=e)}_$initEnv(t,e,s){`$e$${t}`in this&&(this[t]=e,this[`$en$${t}`]=s)}_$updateEnv(t,e,s){`$e$${t}`in this&&s===this[`$en$${t}`]&&this[t]!==e&&(this[t]=e)}_$setContent(t){let e=this._$contentKey;e&&this[e]!==t&&(this[e]=t)}_$updateProp(t,e){let s=`$${t}`;this[s]!==e&&(this[s]=e,this._$updateDerived(t),this._$updateView(t))}_$updateDerived(t){"_$notInitd"in this||this[`$s$${t}`]?.forEach(e=>{`$w$${e}`in this?this[e]():this[`$${e}`]=this[`$f$${e}`]})}_$updateView(t){let e=this[`$$${t}`];e&&this._$update?.(e)}},ot=D;function nt(o,t){o._$updateDerived(t),o._$updateView(t)}var S=class{constructor(){this.envs={},this.currentEnvNodes=[]}addEnvNode(t){this.currentEnvNodes.push(t),this.mergeEnvs()}replaceEnvNodes(t){this.currentEnvNodes=t,this.mergeEnvs()}removeEnvNode(){this.currentEnvNodes.pop(),this.mergeEnvs()}mergeEnvs(){this.envs={},this.currentEnvNodes.forEach(t=>{Object.entries(t.envs).forEach(([e,s])=>{this.envs[e]=[s,t]})})}},T=class extends d{constructor(t){super(f.Env),h.global.DLEnvStore||(h.global.DLEnvStore=new S),this.envs=t,this.updateNodes=new Set,h.global.DLEnvStore.addEnvNode(this)}updateEnv(t,e){this.envs[t]=e,h.global.DLEnvStore.currentEnvNodes.includes(this)&&h.global.DLEnvStore.mergeEnvs(),this.updateNodes.forEach(s=>{s._$updateEnv(t,e,this)})}addNode(t){this.updateNodes.add(t),d.addWillUnmount(t,this.updateNodes.delete.bind(this.updateNodes,t))}initNodes(t){this._$nodes=t,h.global.DLEnvStore.removeEnvNode()}};function at(o){return h.document.createTextNode(o)}function lt(o,t){o.textContent!==t&&(o.textContent=t)}var M=class{propViewFunc;dlUpdateFunc=new Set;constructor(t){this.propViewFunc=t}build(){let t,e=n=>{n.initd=!0,t=n,this.dlUpdateFunc.add(n)},s=this.propViewFunc(e);return s.length===0?[]:(t&&d.addWillUnmount(s[0],this.dlUpdateFunc.delete.bind(this.dlUpdateFunc,t)),s)}update(t){this.dlUpdateFunc.forEach(e=>{if(e.initd){delete e.initd;return}e(t)})}};var O=class extends d{constructor(){super(f.Subview)}};var p=class extends d{constructor(t){super(t),h.global.DLEnvStore&&h.global.DLEnvStore.currentEnvNodes.length>0&&(this.savedEnvNodes=[...h.global.DLEnvStore.currentEnvNodes])}initNewNodes(t){d.addParentEl(t,this._$parentEl)}geneNewNodesInEnv(t){if(!this.savedEnvNodes){let n=t();return this.initNewNodes(n),n}let e=h.global.DLEnvStore.currentEnvNodes;h.global.DLEnvStore.replaceEnvNodes(this.savedEnvNodes);let s=t();return h.global.DLEnvStore.replaceEnvNodes(e),this.initNewNodes(s),s}removeNodes(t){d.loopDLNodes(t,e=>e.willUnmount?.()),d.loopShallowEls(t,e=>{this._$parentEl?.removeChild(e)}),d.loopDLNodesInsideOut(t,e=>e.didUnmount?.())}};var g=class extends p{array;keys;nodeFunc;nodess;depNum;updateArr=[];constructor(t,e,s){super(f.For),this.array=[...t],this.keys=s,this.depNum=e}addNodeFunc(t){this.nodeFunc=t,this.nodess=this.array.map((e,s)=>t(e,this.updateArr,s)),this._$nodes=this.nodess.flat(1)}update(t){if(!(t&this.depNum))for(let e=0;e<this.array.length;e++)this.updateItem(e,t)}updateItem(t,e){this.updateArr[t]?.(e??this.depNum,this.array[t])}updateArray(t,e){if(e){this.updateWithKey(t,e);return}this.updateWithOutKey(t)}getNewNodes(t,e){return this.geneNewNodesInEnv(()=>this.nodeFunc(this.array[t],e??this.updateArr,t))}updateWithOutKey(t){let e=this.array.length,s=t.length;if(this.array=[...t],this.updateArr=this.updateArr.slice(0,s),e===s){for(let r=0;r<this.array.length;r++)this.updateItem(r);return}let n=this._$parentEl;if(e<s){let r=p.getFlowIndexFromNodes(n._$nodes,this),$=n.childNodes.length;for(let a=0;a<s;a++){if(a<e){r+=p.getFlowIndexFromNodes(this.nodess[a]),this.updateItem(a);continue}let l=this.getNewNodes(a);p.appendNodesWithIndex(l,n,r,$),this.nodess.push(l)}this._$nodes=this.nodess.flat(1);return}for(let r=0;r<s;r++)this.updateItem(r);for(let r=s;r<e;r++)this.removeNodes(this.nodess[r]);this.nodess=this.nodess.slice(0,s),this._$nodes=this.nodess.flat(1)}updateWithKey(t,e){let s=this.keys;if(this.array=[...t],this.keys=e,g.arrayEqual(s,this.keys)){for(let i=0;i<this.array.length;i++)this.updateItem(i);return}let n=this._$parentEl,r=this.nodess;if(this.keys.length===0){let i=n._$nodes??[];if(i.length===1&&i[0]===this)n.innerHTML="";else for(let c=0;c<s.length;c++)this.removeNodes(r[c]);this.nodess=[],this._$nodes=[],this.updateArr=[];return}let $=p.getFlowIndexFromNodes(n._$nodes,this);if(s.length===0){let i=n.childNodes[$];for(let c=0;c<this.keys.length;c++){let u=this.getNewNodes(c);p.appendNodesWithSibling(u,n,i),this.nodess.push(u)}this._$nodes=this.nodess.flat(1);return}let a=[],l=[],m=[];for(let i=0;i<s.length;i++){let c=s[i];if(this.keys.includes(c)){a.push(c),l.push(r[i]),m.push(this.updateArr[i]);continue}this.removeNodes(r[i])}let _=n.childNodes.length,N=$;for(let i=0;i<this.keys.length;i++){let c=this.keys[i],u=a.indexOf(c);if(u!==-1){N+=p.getFlowIndexFromNodes(l[u]),this.updateArr[u]?.(this.depNum,this.array[i]);continue}m.splice(i,0,null);let x=this.getNewNodes(i,m),b=p.appendNodesWithIndex(x,n,N,_);N+=b,_+=b,l.splice(i,0,x),a.splice(i,0,c)}if(g.arrayEqual(this.keys,a)){this.nodess=l,this._$nodes=this.nodess.flat(1),this.updateArr=m;return}N=$;let L=[];for(let i=0;i<this.keys.length;i++){let c=this.keys[i],u=a.indexOf(c),x=L[i];if(x){let w=p.appendNodesWithIndex(x,n,N+p.getFlowIndexFromNodes(x),_);N+=w,_+=w,L[i]=void 0}else if(u===i){N+=p.getFlowIndexFromNodes(l[i]);continue}else{L[this.keys.indexOf(a[i])]=l[i];let w=p.appendNodesWithIndex(l[u],n,N,_);N+=w,_+=w}let b=l[i];l[i]=l[u],l[u]=b;let A=a[i];a[i]=a[u],a[u]=A;let U=m[i];m[i]=m[u],m[u]=U}this.nodess=l,this._$nodes=this.nodess.flat(1),this.updateArr=m}static arrayEqual(t,e){return t.length!==e.length?!1:t.every((s,n)=>s===e[n])}};var v=class extends p{nodesFunc;constructor(t){super(f.Exp),this.nodesFunc=t,this._$nodes=v.formatNodes(t())}update(){let t=this.geneNewNodesInEnv(()=>v.formatNodes(this.nodesFunc()));if(this.removeNodes(this._$nodes),t.length===0){this._$nodes=[];return}let e=this._$parentEl,s=p.getFlowIndexFromNodes(e._$nodes,this),n=e.childNodes[s];p.appendNodesWithSibling(t,e,n),this._$nodes=t}static formatNodes(t){return Array.isArray(t)||(t=[t]),t.flat(1).filter(e=>e!=null&&typeof e!="boolean").map(e=>typeof e=="string"||typeof e=="number"||typeof e=="bigint"?h.document.createTextNode(`${e}`):"propViewFunc"in e?e.build():e).flat(1)}};var W=class extends p{condFunc;cond;depNum;constructor(t){super(f.Cond),this.depNum=t}addCondFunc(t){this.cond=-1,this.condFunc=t,this._$nodes=this.condFunc(this)}updateCond(){let t=this.geneNewNodesInEnv(()=>this.condFunc(this));if(this.didntChange)return this.didntChange=!1,this.updateFunc?.(this.depNum),this._$nodes;if(this._$nodes&&this._$nodes.length>0&&this.removeNodes(this._$nodes),t.length===0)return this._$nodes=[],this._$nodes;let e=this._$parentEl,s=p.getFlowIndexFromNodes(e._$nodes,this),n=e.childNodes[s];return p.appendNodesWithSibling(t,e,n),this._$nodes=t,this._$nodes}update(t){t&this.depNum||this.updateFunc?.(t)}};function Tt(o,t){let e=o;if(typeof o=="string"){let n=h.document.getElementById(o);if(n)e=n;else throw new Error(`DLight: Element with id ${o} not found`)}e.innerHTML="";let s=new t;s._$init(),P(e,s,0)}function Mt(o,t){return o()}function G(o){return o}var Ot=G;export{Ot as $,D as CompNode,W as CondNode,T as EnvNode,v as ExpNode,g as ForNode,M as PropView,O as SubViewNode,ot as View,Z as createElement,Y as createTemplate,at as createTextNode,G as escape,I as forwardHTMLProp,P as insertNode,Mt as manual,Tt as render,j as setDataset,H as setDocument,q as setEvent,C as setGlobal,F as setHTMLAttr,V as setHTMLAttrs,y as setHTMLProp,B as setHTMLProps,K as setStyle,nt as update,lt as updateText}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@dlightjs/dlight", | ||
"version": "1.0.0-alpha.42", | ||
"version": "1.0.0-alpha.43", | ||
"description": "DX-first UI rendering library", | ||
@@ -5,0 +5,0 @@ "author": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
140874