@dlightjs/dlight
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -57,6 +57,8 @@ declare enum DLNodeType { | ||
declare class EnvNode extends DLNode { | ||
addPropFuncs: ((node: CustomNode) => any)[]; | ||
constructor(); | ||
_$addNodes(nodes: DLNode[]): void; | ||
_$addProp(key: string, propOrFunc: any | (() => any), dlScope?: CustomNode, listenDeps?: string[]): void; | ||
addNodesProp(nodes: DLNode[], key: string, propOrFunc: any | (() => any), dlScope?: CustomNode, listenDeps?: string[]): void; | ||
addProps(node: CustomNode): void; | ||
addPropsToNodes(nodes: DLNode[]): void; | ||
_$init(): void; | ||
@@ -147,3 +149,3 @@ render(parentEl: HTMLElement): void; | ||
Afterset(): void; | ||
AfterConstruct(): void; | ||
AfterBody(): void; | ||
_$init(): void; | ||
@@ -150,0 +152,0 @@ _$addProp(key: string, propFunc: any | (() => any), dlScope?: CustomNode, listenDeps?: string[], isTwoWayConnected?: boolean): void; |
@@ -1,1 +0,1 @@ | ||
function T(n){for(let e of n){if(Array.isArray(e)){T(e);continue}e._$init()}}function w(n,e){for(let t of n){if(Array.isArray(t)){w(t,e);continue}t._$parentNode=e}}function _(n,e){for(let t of n)e(t)&&_(t._$nodes,e)}function c(n,e,t=!0){for(let o of n)[0,1].includes(o._$nodeType)?(e(o._$el,o),t&&c(o._$nodes,e)):c(o._$nodes,e,t)}function F(n){let e=[];return c(n,(t,o)=>{o._$nodeType===0&&e.push(t)},!1),e}var f=(i=>(i[i.HTML=0]="HTML",i[i.Text=1]="Text",i[i.Custom=2]="Custom",i[i.For=3]="For",i[i.If=4]="If",i[i.Env=5]="Env",i[i.Expression=6]="Expression",i))(f||{}),h=class{_$nodeType;__$el;get _$el(){return this.__$el??F(this._$nodes)}set _$el(e){this.__$el=e}_$parentNode;_$nodes=[];_$depObjectIds=[];_$bindNodes(){w(this._$nodes,this),T(this._$nodes)}constructor(e){this._$nodeType=e}_$init(){}render(e){}};function b(n,e,t,o,s,d,r){if(t in n){if(!d){n[t]=o;return}if(n[`_$$${t}`]===`_$${e}`){W(s,n,t,o,d);return}if(r&&s[`_$$${d[0]}`]!==void 0){V(s,n,t,o,d);return}k(s,n,t,o,d)}}function W(n,e,t,o,s){let d={};e._$depObjectIds.push(d),e[t]=o(),n._$addDeps(s,d,()=>{e[t]=o(),e._$runDeps(t)})}function V(n,e,t,o,s){let d={};e._$depObjectIds.push(d);for(let r of s){let i=()=>n[r]=e[t];e._$addDeps([t],d,i),e[t]=o(),n._$addDeps(s,d,()=>{e._$deleteDep(t,d),e[t]=o(),e._$addDeps([t],d,i)})}}function k(n,e,t,o,s){let d={};e._$depObjectIds.push(d),e[`_$${t}`]=o(),n._$addDeps(s,d,()=>{e[`_$${t}`]=o(),e._$runDeps(t)})}var E=class extends h{_$deps={};_$envNodes;_$derivedPairs;_$children;_$tag="";Body;constructor(){super(2)}_$addAfterset(e){let t=this.Preset;this.Preset=()=>{t(),e()}}_$runDeps(e){if(this._$deps[e]===void 0){console.warn(`${e} is not a dependency in ${this.constructor.name}`);return}for(let t of this._$deps[e].values())t.call(this)}_$addChilds(e){this._$children=e}_$initDecorators(){if(this._$derivedPairs)for(let[e,t]of Object.entries(this._$derivedPairs)){let o=this[e];if(typeof o!="function")return;this[e]=this[e]();let s=this[e];this._$addDeps(t,{},()=>{let d=o();d!==s&&(this[e]=d,s=d,this._$runDeps(e))})}}_$addDeps(e,t,o){for(let s of e)this._$deps[s].set(t,o)}_$deleteDep(e,t){this._$deps[e].delete(t)}_$deleteDeps(e){for(let t in this._$deps)this._$deleteDep(t,e)}Preset(){}Afterset(){}AfterConstruct(){}_$init(){this._$initDecorators(),this.Preset(),this._$nodes=this.Body(),this._$bindNodes(),this.Afterset()}_$addProp(e,t,o,s,d){b(this,"prop",e,t,o,s,d)}render(e){this.willMount(this);for(let t of this._$nodes)t.render(e);this.didMount(this)}willMount(e){}didMount(e){}willUnmount(e){}didUnmount(e){}_$addLifeCycle(e,t){let o=this[t];this[t]=function(s){e.call(this,this),o.call(this,this)}}};var M=class extends h{_$envNodes=[];constructor(e){super(0),this._$el=document.createElement(e)}_$init(){this._$bindNodes();for(let e of this._$nodes)e.render(this._$el)}_$addNodes(e){this._$nodes=e}_$addProp(e,t,o,s){let d;if(e[0]==="_"?d=a=>this._$el.style[e.slice(1)]=a:e==="innerText"?d=a=>this._$el.innerText=a:d=a=>this._$el[e]=a,!s){d(t);return}let r=t();d(r);let i=()=>{let a=t();r!==a&&(d(a),r=a)},l={};this._$depObjectIds.push(l),o._$addDeps(s,l,i)}willAppear(e){}didAppear(e){}willDisappear(e){}didDisappear(e){}_$addLifeCycle(e,t){let o=this[t];this[t]=function(s){return o.call(this,s),e.call(this,s)}}render(e){this.willAppear(this._$el),e.appendChild(this._$el),this.didAppear(this._$el)}};function y(n){z(n),c(n,(e,t)=>{t._$nodeType===0&&t.willDisappear(e),e.remove(),t._$nodeType===0&&t.didDisappear(e)}),G(n)}function L(n,e){_(n,t=>{for(let o of t._$depObjectIds)e._$deleteDeps(o);return!0})}function D(n,e,t,o){let s=o??t.childNodes.length;return B(n),c(n,(d,r)=>{let i=t.childNodes[e];[0].includes(r._$nodeType)&&r.willAppear(d),e===s?t.appendChild(d):t.insertBefore(d,i),[0].includes(r._$nodeType)&&r.didAppear(d),e++,s++},!1),q(n),[e,s]}function g(n,e){return U(n._$nodes,e)}function C(n){return U(n,void 0)}function U(n,e){let t=0,o=!1;return _(n,s=>o?!1:s===e?(o=!0,!1):[1,0].includes(s._$nodeType)?(t++,!1):!0),t}function I(n,e){_(n,t=>([2,6].includes(t._$nodeType)&&t[e](t),!0))}function B(n){I(n,"willMount")}function q(n){I(n,"didMount")}function z(n){I(n,"willUnmount")}function G(n){I(n,"didUnmount")}var x=class extends h{afterUpdateNewNodes(e){}addAfterUpdateNewNodesFunc(e){let t=this.afterUpdateNewNodes;this.afterUpdateNewNodes=function(o){e.call(this,o),t.call(this,o)}}onUpdateNodes(e,t){}addOnUpdateNodesFunc(e){let t=this.onUpdateNodes;this.onUpdateNodes=function(o,s){e.call(this,o,s),t.call(this,o,s)}}_$bindNewNodes(e){this.afterUpdateNewNodes(e),w(e,this),T(e)}};var A=class extends x{keys=[];array=[];_$nodess=[];nodeFunc;keyFunc;arrayFunc;dlScope;listenDeps;_$envNodes=[];constructor(){super(3)}duplicatedOrNoKey=!1;_$getItem(e,t){let o=this.duplicatedOrNoKey?t:this.keys.indexOf(e);return this.array[o]}_$addNodeFunc(e){this.nodeFunc=e}_$addKeyFunc(e){this.keyFunc=e}_$addArrayFunc(e,t,o){this.dlScope=e,this.arrayFunc=t,this.listenDeps=o}_$addNodess(e){this._$nodess=e,this._$nodes=this._$nodess.flat(1)}setArray(){this.array=[...this.arrayFunc()]}setKeys(){if(!this.keyFunc){this.duplicatedOrNoKey=!0;return}let e=[...this.keyFunc()];if(e.length===[...new Set(e)].length){this.keys=e;return}this.keys=[...Array(this.array.length).keys()],console.warn("\u91CD\u590Dkey\u4E86"),this.duplicatedOrNoKey=!0}_$init(){if(!this.listenDeps){this._$bindNodes();return}let e=this._$parentNode;for(;e&&e._$nodeType!==0;)e=e._$parentNode;if(!e)return;let t=this.keyFunc?()=>this.updateWithKey(e):()=>this.updateWithOutKey(e),o={};if(this._$depObjectIds.push(o),this.dlScope._$addDeps(this.listenDeps,o,t),this.setArray(),this.setKeys(),this.duplicatedOrNoKey)for(let s of this.array.keys())this._$nodess.push(this.nodeFunc(null,s,this));else for(let[s,d]of this.keys.entries())this._$nodess.push(this.nodeFunc(d,s,this));this._$nodes=this._$nodess.flat(1),this._$bindNodes()}render(e){for(let t of this._$nodes)t.render(e)}getNewNodes(e,t){let o=this.nodeFunc(e,t,this);return this._$bindNewNodes(o),o}updateWithOutKey(e){let t=e._$el,o=this.array.length;this.setArray();let s=this.array.length;if(o!==s){if(o<s){let d=g(e,this),r=t.childNodes.length;for(let i=0;i<s;i++){if(i<o){d+=C(this._$nodess[i]);continue}let l=this.getNewNodes(null,i);[d,r]=D(l,d,t,r),this._$nodess.push(l)}this._$nodes=this._$nodess.flat(1);return}for(let d=s;d<o;d++)L(this._$nodess[d],this.dlScope),y(this._$nodess[d]);this._$nodess=this._$nodess.slice(0,s),this._$nodes=this._$nodess.flat(1)}}async updateWithKey(e){let t=e._$el,o=g(e,this),s=this.keys,d=[...this.array],r=[...this._$nodess],i=[...this._$nodes];this.setArray(),this.setKeys(),this.duplicatedOrNoKey&&(s=[...Array(d.length).keys()]);let l=[],a=[],m=[];for(let[p,N]of s.entries()){if(this.keys.includes(N)){l.push(N),a.push(r[p]);continue}L(r[p],this.dlScope),y(r[p]),m.push(p)}s=l;let u=o,H=t.childNodes.length;for(let[p,N]of this.keys.entries()){if(s.includes(N)){u+=C(a[s.indexOf(N)]);continue}let $=this.getNewNodes(N,p);[u,H]=D($,u,t,H),a.splice(p,0,$),s.splice(p,0,N)}u=o;for(let[p,N]of this.keys.entries()){let $=s.indexOf(N);if($===p){u+=C(a[p]);continue}let P=a[$],S=s[$];[u,H]=D(P,u,t,H),a.splice($,1),s.splice($,1),a.splice(p+1,0,P),s.splice(p+1,0,S)}this._$nodess=a,this._$nodes=this._$nodess.flat(1),this.onUpdateNodes(i,this._$nodes)}_$listen(e,t,o,s){let d={};e._$depObjectIds.push(d),e._$addDeps(o,d,()=>{let r=t();if(r===void 0){e._$deleteDeps(d);return}s(r)})}};var O=class extends x{conditionPairs=[];condition;listenDeps=[];dlScope;_$envNodes=[];constructor(){super(4)}_$addCond(e,t,o,s){this.conditionPairs.push({condition:e,node:t}),s&&(this.dlScope||(this.dlScope=o),this.listenDeps.push(...s))}_$init(){let e=this._$parentNode;for(;e&&e._$nodeType!==0;)e=e._$parentNode;if(e){let o={};this._$depObjectIds.push(o),this.dlScope?._$addDeps(this.listenDeps,o,()=>this.update(e))}let t=[];for(let o of this.conditionPairs)if(o.condition()){this.condition=o.condition.toString(),t=o.node();break}this._$nodes=t,this._$bindNodes()}update(e){let t=this._$nodes,o=this.condition;this._$nodes=[];for(let r of this.conditionPairs)if(r.condition()){this.condition!==r.condition.toString()?(L(t,this.dlScope),y(t),this.condition=r.condition.toString(),this._$nodes=r.node(),this._$bindNewNodes(this._$nodes)):this._$nodes=t;break}if(t.length!==0&&this._$nodes.length===0&&(this.condition="[none]",L(t,this.dlScope),y(t)),o===this.condition)return;let s=g(e,this),d=e._$el;D(this._$nodes,s,d,d.childNodes.length),this.onUpdateNodes(t,this._$nodes)}render(e){for(let t of this._$nodes)t.render(e)}};var v=class extends h{constructor(e,t,o){if(super(1),!o){this._$el=document.createTextNode(e);return}e=e;let s=e();this._$el=document.createTextNode(s);let d=()=>{let i=e();s!==i&&(this._$el.nodeValue=i,s=i)},r={};this._$depObjectIds.push(r),t._$addDeps(o,r,d)}render(e){e.appendChild(this._$el)}};var j=class extends h{constructor(){super(5)}_$addNodes(e){this._$nodes=e}_$addProp(e,t,o,s){this.addNodesProp(this._$nodes,e,t,o,s)}addNodesProp(e,t,o,s,d){_(e,r=>(r._$nodeType===2&&b(r,"env",t,o,s,d),!0))}_$init(){this._$bindNodes()}render(e){for(let t of this._$nodes)t.render(e)}};var K=class extends x{nodeOrFunc;listenDeps;dlScope;propFuncs=[];propScope=()=>!0;deepLoopEl=!1;constructor(e,t,o){if(super(6),!o){this._$nodes=this.formatNodes(e);return}this.nodeOrFunc=e,this.listenDeps=o,this.dlScope=t,this._$nodes=this.formatNodes(this.nodeOrFunc())}_$onUpdateNodes(e){_(this._$nodes,t=>([4,3,6].includes(t._$nodeType)&&t.addOnUpdateNodesFunc(e),!0))}_$addProp(e,t,o,s){let d=this.propScope,r=this.deepLoopEl,i=l=>{let a=l._$el;!d(a,l)||e[0]==="_"&&(l._$el.style[e.slice(1)]??"").trim()!==""||e[0]!=="_"&&l._$el[e]!==void 0||l._$addProp(e,t,o,s)};this.propFuncs.push(()=>{for(let l of this._$nodes)switch(l._$nodeType){case 0:i(l),r&&c(l._$nodes,(a,m)=>{m._$nodeType===0&&i(m)},!0);break;case 3:case 4:case 6:l.addAfterUpdateNewNodesFunc(a=>{c(a,(m,u)=>{u._$nodeType===0&&i(u)},r)});default:c(l._$nodes,(a,m)=>{m._$nodeType===0&&i(m)},r)}})}formatNodes(e){return Array.isArray(e)||(e=[e]),e=e.flat(1),e=e.filter(t=>t!=null).map(t=>t._$nodeType!==void 0?t:new v(t)),e}_$init(){if(this.listenDeps===void 0){this._$bindNodes();for(let o of this.propFuncs)o();return}let e=this._$parentNode;for(;e&&e._$nodeType!==0;)e=e._$parentNode;if(!e)return;let t={};this._$depObjectIds.push(t),this.dlScope._$addDeps(this.listenDeps,t,()=>this.update(e)),this._$bindNodes();for(let o of this.propFuncs){o();let s={};this._$depObjectIds.push(s),this.dlScope._$addDeps(this.listenDeps,s,o)}}render(e){this.willMount(this);for(let t of this._$nodes)t.render(e);this.didMount(this)}update(e){let t=this._$nodes;L(this._$nodes,this.dlScope),y(this._$nodes),this._$nodes=this.formatNodes(this.nodeOrFunc()),this._$bindNewNodes(this._$nodes);let o=e._$el,s=g(e,this);D(this._$nodes,s,o,o.childNodes.length),this.onUpdateNodes(t,this._$nodes)}willMount(e){}didMount(e){}willUnmount(e){}didUnmount(e){}_$addLifeCycle(e,t){let o=this[t];this[t]=function(s){e.call(this,this),o.call(this,this)}}};var Ge=E,Je=void 0;function Qe(n,e){let t=new M("div");t._$addNodes([e]),t._$addProp("id",n),t._$init(),document.getElementById(n).replaceWith(t._$el)}export{E as CustomNode,h as DLNode,f as DLNodeType,j as EnvNode,K as ExpressionNode,A as ForNode,M as HtmlNode,O as IfNode,v as TextNode,Ge as View,w as bindParentNode,T as initNodes,c as loopEls,_ as loopNodes,Qe as render,Je as required,F as toEls}; | ||
function T(n){for(let e of n){if(Array.isArray(e)){T(e);continue}e._$init()}}function w(n,e){for(let t of n){if(Array.isArray(t)){w(t,e);continue}t._$parentNode=e}}function _(n,e){for(let t of n)e(t)&&_(t._$nodes,e)}function c(n,e,t=!0){for(let o of n)[0,1].includes(o._$nodeType)?(e(o._$el,o),t&&c(o._$nodes,e)):c(o._$nodes,e,t)}function F(n){let e=[];return c(n,(t,o)=>{o._$nodeType===0&&e.push(t)},!1),e}var f=(i=>(i[i.HTML=0]="HTML",i[i.Text=1]="Text",i[i.Custom=2]="Custom",i[i.For=3]="For",i[i.If=4]="If",i[i.Env=5]="Env",i[i.Expression=6]="Expression",i))(f||{}),h=class{_$nodeType;__$el;get _$el(){return this.__$el??F(this._$nodes)}set _$el(e){this.__$el=e}_$parentNode;_$nodes=[];_$depObjectIds=[];_$bindNodes(){w(this._$nodes,this),T(this._$nodes)}constructor(e){this._$nodeType=e}_$init(){}render(e){}};function b(n,e,t,o,s,d,r){if(t in n){if(!d){n[t]=o;return}if(console.log(),n[`_$$${t}`]===`_$${e}`){W(s,n,t,o,d);return}if(r&&s[`_$$${d[0]}`]!==void 0){V(s,n,t,o,d);return}k(s,n,t,o,d)}}function W(n,e,t,o,s){let d={};e._$depObjectIds.push(d),e[t]=o(),n._$addDeps(s,d,()=>{e[t]=o(),e._$runDeps(t)})}function V(n,e,t,o,s){let d={};e._$depObjectIds.push(d);for(let r of s){let i=()=>n[r]=e[t];e._$addDeps([t],d,i),e[t]=o(),n._$addDeps(s,d,()=>{e._$deleteDep(t,d),e[t]=o(),e._$addDeps([t],d,i)})}}function k(n,e,t,o,s){let d={};e._$depObjectIds.push(d),e[`_$${t}`]=o(),n._$addDeps(s,d,()=>{e[`_$${t}`]=o(),e._$runDeps(t)})}var E=class extends h{_$deps={};_$envNodes;_$derivedPairs;_$children;_$tag="";Body;constructor(){super(2)}_$addAfterset(e){let t=this.Preset;this.Preset=()=>{t(),e()}}_$runDeps(e){if(this._$deps[e]===void 0){console.warn(`${e} is not a dependency in ${this.constructor.name}`);return}for(let t of this._$deps[e].values())t.call(this)}_$addChilds(e){this._$children=e}_$initDecorators(){if(this._$derivedPairs)for(let[e,t]of Object.entries(this._$derivedPairs)){let o=this[e];if(typeof o!="function")return;this[e]=this[e]();let s=this[e];this._$addDeps(t,{},()=>{let d=o();d!==s&&(this[e]=d,s=d,this._$runDeps(e))})}}_$addDeps(e,t,o){for(let s of e)this._$deps[s].set(t,o)}_$deleteDep(e,t){this._$deps[e].delete(t)}_$deleteDeps(e){for(let t in this._$deps)this._$deleteDep(t,e)}Preset(){}Afterset(){}AfterBody(){}_$init(){this._$initDecorators(),this.Preset(),this._$nodes=this.Body(),this.AfterBody(),this._$bindNodes(),this.Afterset()}_$addProp(e,t,o,s,d){b(this,"prop",e,t,o,s,d)}render(e){this.willMount(this);for(let t of this._$nodes)t.render(e);this.didMount(this)}willMount(e){}didMount(e){}willUnmount(e){}didUnmount(e){}_$addLifeCycle(e,t){let o=this[t];this[t]=function(s){e.call(this,this),o.call(this,this)}}};var M=class extends h{_$envNodes=[];constructor(e){super(0),this._$el=document.createElement(e)}_$init(){this._$bindNodes();for(let e of this._$nodes)e.render(this._$el)}_$addNodes(e){this._$nodes=e}_$addProp(e,t,o,s){let d;if(e[0]==="_"?d=a=>this._$el.style[e.slice(1)]=a:e==="innerText"?d=a=>this._$el.innerText=a:d=a=>this._$el[e]=a,!s){d(t);return}let r=t();d(r);let i=()=>{let a=t();r!==a&&(d(a),r=a)},p={};this._$depObjectIds.push(p),o._$addDeps(s,p,i)}willAppear(e){}didAppear(e){}willDisappear(e){}didDisappear(e){}_$addLifeCycle(e,t){let o=this[t];this[t]=function(s){return o.call(this,s),e.call(this,s)}}render(e){this.willAppear(this._$el),e.appendChild(this._$el),this.didAppear(this._$el)}};function y(n){z(n),c(n,(e,t)=>{t._$nodeType===0&&t.willDisappear(e),e.remove(),t._$nodeType===0&&t.didDisappear(e)}),G(n)}function L(n,e){_(n,t=>{for(let o of t._$depObjectIds)e._$deleteDeps(o);return!0})}function D(n,e,t,o){let s=o??t.childNodes.length;return B(n),c(n,(d,r)=>{let i=t.childNodes[e];[0].includes(r._$nodeType)&&r.willAppear(d),e===s?t.appendChild(d):t.insertBefore(d,i),[0].includes(r._$nodeType)&&r.didAppear(d),e++,s++},!1),q(n),[e,s]}function g(n,e){return A(n._$nodes,e)}function C(n){return A(n,void 0)}function A(n,e){let t=0,o=!1;return _(n,s=>o?!1:s===e?(o=!0,!1):[1,0].includes(s._$nodeType)?(t++,!1):!0),t}function I(n,e){_(n,t=>([2,6].includes(t._$nodeType)&&t[e](t),!0))}function B(n){I(n,"willMount")}function q(n){I(n,"didMount")}function z(n){I(n,"willUnmount")}function G(n){I(n,"didUnmount")}var x=class extends h{afterUpdateNewNodes(e){}addAfterUpdateNewNodesFunc(e){let t=this.afterUpdateNewNodes;this.afterUpdateNewNodes=function(o){e.call(this,o),t.call(this,o)}}onUpdateNodes(e,t){}addOnUpdateNodesFunc(e){let t=this.onUpdateNodes;this.onUpdateNodes=function(o,s){e.call(this,o,s),t.call(this,o,s)}}_$bindNewNodes(e){this.afterUpdateNewNodes(e),w(e,this),T(e)}};var U=class extends x{keys=[];array=[];_$nodess=[];nodeFunc;keyFunc;arrayFunc;dlScope;listenDeps;_$envNodes=[];constructor(){super(3)}duplicatedOrNoKey=!1;_$getItem(e,t){let o=this.duplicatedOrNoKey?t:this.keys.indexOf(e);return this.array[o]}_$addNodeFunc(e){this.nodeFunc=e}_$addKeyFunc(e){this.keyFunc=e}_$addArrayFunc(e,t,o){this.dlScope=e,this.arrayFunc=t,this.listenDeps=o}_$addNodess(e){this._$nodess=e,this._$nodes=this._$nodess.flat(1)}setArray(){this.array=[...this.arrayFunc()]}setKeys(){if(!this.keyFunc){this.duplicatedOrNoKey=!0;return}let e=[...this.keyFunc()];if(e.length===[...new Set(e)].length){this.keys=e;return}this.keys=[...Array(this.array.length).keys()],console.warn("\u91CD\u590Dkey\u4E86"),this.duplicatedOrNoKey=!0}_$init(){if(!this.listenDeps){this._$bindNodes();return}let e=this._$parentNode;for(;e&&e._$nodeType!==0;)e=e._$parentNode;if(!e)return;let t=this.keyFunc?()=>this.updateWithKey(e):()=>this.updateWithOutKey(e),o={};if(this._$depObjectIds.push(o),this.dlScope._$addDeps(this.listenDeps,o,t),this.setArray(),this.setKeys(),this.duplicatedOrNoKey)for(let s of this.array.keys())this._$nodess.push(this.nodeFunc(null,s,this));else for(let[s,d]of this.keys.entries())this._$nodess.push(this.nodeFunc(d,s,this));this._$nodes=this._$nodess.flat(1),this._$bindNodes()}render(e){for(let t of this._$nodes)t.render(e)}getNewNodes(e,t){let o=this.nodeFunc(e,t,this);return this._$bindNewNodes(o),o}updateWithOutKey(e){let t=e._$el,o=this.array.length;this.setArray();let s=this.array.length;if(o!==s){if(o<s){let d=g(e,this),r=t.childNodes.length;for(let i=0;i<s;i++){if(i<o){d+=C(this._$nodess[i]);continue}let p=this.getNewNodes(null,i);[d,r]=D(p,d,t,r),this._$nodess.push(p)}this._$nodes=this._$nodess.flat(1);return}for(let d=s;d<o;d++)L(this._$nodess[d],this.dlScope),y(this._$nodess[d]);this._$nodess=this._$nodess.slice(0,s),this._$nodes=this._$nodess.flat(1)}}async updateWithKey(e){let t=e._$el,o=g(e,this),s=this.keys,d=[...this.array],r=[...this._$nodess],i=[...this._$nodes];this.setArray(),this.setKeys(),this.duplicatedOrNoKey&&(s=[...Array(d.length).keys()]);let p=[],a=[],m=[];for(let[l,N]of s.entries()){if(this.keys.includes(N)){p.push(N),a.push(r[l]);continue}L(r[l],this.dlScope),y(r[l]),m.push(l)}s=p;let u=o,H=t.childNodes.length;for(let[l,N]of this.keys.entries()){if(s.includes(N)){u+=C(a[s.indexOf(N)]);continue}let $=this.getNewNodes(N,l);[u,H]=D($,u,t,H),a.splice(l,0,$),s.splice(l,0,N)}u=o;for(let[l,N]of this.keys.entries()){let $=s.indexOf(N);if($===l){u+=C(a[l]);continue}let v=a[$],S=s[$];[u,H]=D(v,u,t,H),a.splice($,1),s.splice($,1),a.splice(l+1,0,v),s.splice(l+1,0,S)}this._$nodess=a,this._$nodes=this._$nodess.flat(1),this.onUpdateNodes(i,this._$nodes)}_$listen(e,t,o,s){let d={};e._$depObjectIds.push(d),e._$addDeps(o,d,()=>{let r=t();if(r===void 0){e._$deleteDeps(d);return}s(r)})}};var O=class extends x{conditionPairs=[];condition;listenDeps=[];dlScope;_$envNodes=[];constructor(){super(4)}_$addCond(e,t,o,s){this.conditionPairs.push({condition:e,node:t}),s&&(this.dlScope||(this.dlScope=o),this.listenDeps.push(...s))}_$init(){let e=this._$parentNode;for(;e&&e._$nodeType!==0;)e=e._$parentNode;if(e){let o={};this._$depObjectIds.push(o),this.dlScope?._$addDeps(this.listenDeps,o,()=>this.update(e))}let t=[];for(let o of this.conditionPairs)if(o.condition()){this.condition=o.condition.toString(),t=o.node();break}this._$nodes=t,this._$bindNodes()}update(e){let t=this._$nodes,o=this.condition;this._$nodes=[];for(let r of this.conditionPairs)if(r.condition()){this.condition!==r.condition.toString()?(L(t,this.dlScope),y(t),this.condition=r.condition.toString(),this._$nodes=r.node(),this._$bindNewNodes(this._$nodes)):this._$nodes=t;break}if(t.length!==0&&this._$nodes.length===0&&(this.condition="[none]",L(t,this.dlScope),y(t)),o===this.condition)return;let s=g(e,this),d=e._$el;D(this._$nodes,s,d,d.childNodes.length),this.onUpdateNodes(t,this._$nodes)}render(e){for(let t of this._$nodes)t.render(e)}};var P=class extends h{constructor(e,t,o){if(super(1),!o){this._$el=document.createTextNode(e);return}e=e;let s=e();this._$el=document.createTextNode(s);let d=()=>{let i=e();s!==i&&(this._$el.nodeValue=i,s=i)},r={};this._$depObjectIds.push(r),t._$addDeps(o,r,d)}render(e){e.appendChild(this._$el)}};var j=class extends h{addPropFuncs=[];constructor(){super(5)}_$addNodes(e){this._$nodes=e}_$addProp(e,t,o,s){this.addPropFuncs.push(d=>b(d,"env",e,t,o,s))}addProps(e){for(let t of this.addPropFuncs)t(e)}addPropsToNodes(e){_(e,t=>(t._$nodeType===2&&(this.addProps(t),t.AfterBody=()=>{this.addPropsToNodes(t._$nodes)}),!0))}_$init(){this.addPropsToNodes(this._$nodes),this._$bindNodes()}render(e){for(let t of this._$nodes)t.render(e)}};var K=class extends x{nodeOrFunc;listenDeps;dlScope;propFuncs=[];propScope=()=>!0;deepLoopEl=!1;constructor(e,t,o){if(super(6),!o){this._$nodes=this.formatNodes(e);return}this.nodeOrFunc=e,this.listenDeps=o,this.dlScope=t,this._$nodes=this.formatNodes(this.nodeOrFunc())}_$onUpdateNodes(e){_(this._$nodes,t=>([4,3,6].includes(t._$nodeType)&&t.addOnUpdateNodesFunc(e),!0))}_$addProp(e,t,o,s){let d=this.propScope,r=this.deepLoopEl,i=p=>{let a=p._$el;!d(a,p)||e[0]==="_"&&(p._$el.style[e.slice(1)]??"").trim()!==""||e[0]!=="_"&&p._$el[e]!==void 0||p._$addProp(e,t,o,s)};this.propFuncs.push(()=>{for(let p of this._$nodes)switch(p._$nodeType){case 0:i(p),r&&c(p._$nodes,(a,m)=>{m._$nodeType===0&&i(m)},!0);break;case 3:case 4:case 6:p.addAfterUpdateNewNodesFunc(a=>{c(a,(m,u)=>{u._$nodeType===0&&i(u)},r)});default:c(p._$nodes,(a,m)=>{m._$nodeType===0&&i(m)},r)}})}formatNodes(e){return Array.isArray(e)||(e=[e]),e=e.flat(1),e=e.filter(t=>t!=null).map(t=>t._$nodeType!==void 0?t:new P(t)),e}_$init(){if(this.listenDeps===void 0){this._$bindNodes();for(let o of this.propFuncs)o();return}let e=this._$parentNode;for(;e&&e._$nodeType!==0;)e=e._$parentNode;if(!e)return;let t={};this._$depObjectIds.push(t),this.dlScope._$addDeps(this.listenDeps,t,()=>this.update(e)),this._$bindNodes();for(let o of this.propFuncs){o();let s={};this._$depObjectIds.push(s),this.dlScope._$addDeps(this.listenDeps,s,o)}}render(e){this.willMount(this);for(let t of this._$nodes)t.render(e);this.didMount(this)}update(e){let t=this._$nodes;L(this._$nodes,this.dlScope),y(this._$nodes),this._$nodes=this.formatNodes(this.nodeOrFunc()),this._$bindNewNodes(this._$nodes);let o=e._$el,s=g(e,this);D(this._$nodes,s,o,o.childNodes.length),this.onUpdateNodes(t,this._$nodes)}willMount(e){}didMount(e){}willUnmount(e){}didUnmount(e){}_$addLifeCycle(e,t){let o=this[t];this[t]=function(s){e.call(this,this),o.call(this,this)}}};var Ge=E,Je=void 0;function Qe(n,e){let t=new M("div");t._$addNodes([e]),t._$addProp("id",n),t._$init(),document.getElementById(n).replaceWith(t._$el)}export{E as CustomNode,h as DLNode,f as DLNodeType,j as EnvNode,K as ExpressionNode,U as ForNode,M as HtmlNode,O as IfNode,P as TextNode,Ge as View,w as bindParentNode,T as initNodes,c as loopEls,_ as loopNodes,Qe as render,Je as required,F as toEls}; |
{ | ||
"name": "@dlightjs/dlight", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"files": [ | ||
@@ -10,3 +10,3 @@ "dist" | ||
"main": "dist/index.cjs", | ||
"module": "src/index.ts", | ||
"module": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
@@ -13,0 +13,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
35827
356