@dlightjs/dlight
Advanced tools
Comparing version 0.4.4 to 0.4.5
@@ -51,4 +51,4 @@ declare enum DLNodeType { | ||
_$detach(): void; | ||
_$beforeInitSubNodes(): void; | ||
_$addBeforeInitSubNodes(func: () => any): void; | ||
_$beforeInitSubNodes(_nodes: DLNode[]): void; | ||
_$addBeforeInitSubNodes(func: (_nodes: DLNode[]) => any): void; | ||
_$bindNodes(): void; | ||
@@ -65,3 +65,3 @@ constructor(nodeType: DLNodeType); | ||
addProps(node: CustomNode): void; | ||
addPropsToNodes(node: DLNode): void; | ||
addPropsToNodes(nodes: DLNode[]): void; | ||
_$init(): void; | ||
@@ -68,0 +68,0 @@ } |
@@ -1,1 +0,1 @@ | ||
function x(n){for(let e of n){if(Array.isArray(e)){x(e);continue}e._$init()}}function T(n,e){for(let t of n){if(Array.isArray(t)){T(t,e);continue}t._$parentNode=e}}function l(n,e){for(let t of n)e(t)&&l(t._$nodes,e)}function f(n,e,t=!0){for(let s of n)[0,1].includes(s._$nodeType)?(e(s._$el,s),t&&f(s._$nodes,e)):f(s._$nodes,e,t)}function j(n){let e=[];return f(n,(t,s)=>{s._$nodeType===0&&e.push(t)},!1),e}var c=(r=>(r[r.HTML=0]="HTML",r[r.Text=1]="Text",r[r.Custom=2]="Custom",r[r.For=3]="For",r[r.If=4]="If",r[r.Env=5]="Env",r[r.Expression=6]="Expression",r))(c||{}),u=class{_$nodeType;__$el;get _$el(){return this.__$el??j(this._$nodes)}set _$el(e){this.__$el=e}_$parentNode;_$nodes=[];_$depObjectIds=[];_$detach(){this._$parentNode=void 0,this._$nodes=[],this._$depObjectIds=[],[1,0].includes(this._$nodeType)||(this.__$el=void 0),this._$beforeInitSubNodes=function(){}}_$beforeInitSubNodes(){}_$addBeforeInitSubNodes(e){let t=this._$beforeInitSubNodes;this._$beforeInitSubNodes=function(){t.call(this),e.call(this)}}_$bindNodes(){T(this._$nodes,this),this._$beforeInitSubNodes(),x(this._$nodes)}constructor(e){this._$nodeType=e}_$init(){}};function k(n,e,t,s,o){n[`_$$${e}`]=o?t():t,Object.defineProperty(n,e,{get(){return this[`_$$${e}`]},set(i){this[`_$$${e}`]!==i&&(this[`_$$${e}`]=i,this._$runDeps(e))}}),n._$deps[e]=new Map,o&&O(s,n,e,t,o)}function H(n,e,t,s,o,i,d){if(n?._$forwardProps){k(n,t,s,o,i);return}if(t in n){if(!i){n[t]=s;return}if(!(n[`_$$${t}`]!==`_$${e}`&&!(`_$$${t}`in n))){if(n[`_$$${t}`]===`_$${e}`){q(o,n,t,s,i);return}if(d&&`_$$${i[0]}`in o){O(o,n,t,s,i);return}z(o,n,t,s,i)}}}function q(n,e,t,s,o){let i={};e._$depObjectIds.push(i),e[t]=s(),n._$addDeps(o,i,()=>{e[t]=s(),e._$runDeps(t)})}function O(n,e,t,s,o){let i={};e._$depObjectIds.push(i);for(let d of o){let r=()=>n[d]=e[t];e._$addDeps([t],i,r),e[t]=s(),n._$addDeps(o,i,()=>{e._$deleteDep(t,i),e[t]=s(),e._$addDeps([t],i,r)})}}function z(n,e,t,s,o){let i={};e._$depObjectIds.push(i),e[`_$${t}`]=s(),n._$addDeps(o,i,()=>{e[`_$${t}`]=s(),e._$runDeps(t)})}function F(n,e){l(e,t=>{switch(t._$nodeType){case 1:case 0:n._$el.appendChild(t._$el);break;default:F(n,t._$nodes);break}return!1})}function y(n){Q(n),f(n,(e,t)=>{document.body.contains(e)&&(t._$nodeType===0&&t.willDisappear(e,t),e.remove(),t._$nodeType===0&&t.didDisappear(e,t))}),R(n)}function $(n){for(let e of n)e._$detach()}function N(n,e){l(n,t=>{for(let s of t._$depObjectIds)e._$deleteDeps(s);return t._$nodeType===2&&N(t._$children,e),!0})}function L(n,e,t,s){let o=s??t.childNodes.length;return G(n),f(n,(i,d)=>{let r=document.body.contains(i);[0].includes(d._$nodeType)&&!r&&d.willAppear(i,d),e===o?t.appendChild(i):t.insertBefore(i,t.childNodes[e]),[0].includes(d._$nodeType)&&!r&&d.didAppear(i,d),e++,o++},!1),J(n),[e,o]}function g(n,e){return U(n._$nodes,e)}function C(n){return U(n,void 0)}function U(n,e){let t=0,s=!1;return l(n,o=>s?!1:o===e?(s=!0,!1):[1,0].includes(o._$nodeType)?(t++,!1):!0),t}function M(n,e){l(n,t=>([2].includes(t._$nodeType)&&t[e](t),!0))}function G(n){M(n,"willMount")}function J(n){M(n,"didMount")}function Q(n){M(n,"willUnmount")}function R(n){M(n,"didUnmount")}var I=class extends u{_$deps={};_$envNodes;_$derivedPairs;_$tag="";constructor(){super(2)}_$addAfterset(e){let t=this.Afterset.bind(this);this.Afterset=function(){t(),e()}.bind(this)}_$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)}_$children=[];_$addChildren(e){this._$children=e}_$resetChildren(){for(let e of this._$children)e._$nodes=[]}_$initDecorators(){if(this._$derivedPairs)for(let[e,t]of Object.entries(this._$derivedPairs)){let s=this[e];if(typeof s!="function")return;this[e]=this[e]();let o=this[e];this._$addDeps(t,{},()=>{let i=s();i!==o&&(this[e]=i,o=i,this._$runDeps(e))})}}_$addDeps(e,t,s){for(let o of e)this._$deps[o].set(t,s)}_$deleteDep(e,t){this._$deps[e].delete(t)}_$deleteDeps(e){for(let t in this._$deps)this._$deleteDep(t,e)}AfterConstruct(){}Preset(){}Afterset(){}_$init(){this.AfterConstruct(),this._$initDecorators(),this.Preset(),this._$nodes=(this.Body.bind(this)??(()=>[]))(),this._$bindNodes(),this.Afterset()}_$addProp(e,t,s,o,i){H(this,"prop",e,t,s,o,i)}willMount(e,t){}didMount(e,t){}willUnmount(e,t){}didUnmount(e,t){}_$addLifeCycle(e,t){let s=this[t];this[t]=function(o,i){e.call(this,this._$el,this),s.call(this,this._$el,this)}}render(e){let t=new P("div");t._$addNodes([this]),t._$addProp("id",typeof e=="string"?e:e.id),t._$init(),this.willMount(this._$el,this),l(this._$nodes,s=>{switch(s._$nodeType){case 0:s.willAppear(s._$el,s);break;case 2:s.willMount(s._$el,s);break}return!0}),typeof e=="string"&&(e=document.getElementById(e)),e.replaceWith(t._$el),l(this._$nodes,s=>{switch(s._$nodeType){case 0:s.didAppear(s._$el,s);break;case 2:s.didMount(s._$el,s);break}return!0}),this.didMount(this._$el,this)}_$detach(){super._$detach(),$(this._$children)}_$forwardProps=!1;forwardProps(e){let t=[...new Set(Object.getOwnPropertyNames(this).filter(s=>s.startsWith("_$$")).map(s=>s.replace(/_\$\$/g,"")))];for(let s of t)e._$addProp(s,()=>this[s],this,[s],!0);e._$nodeType===2?e._$children=this._$children:e._$nodes=this._$children}};var P=class extends u{_$envNodes=[];constructor(e){super(0),this._$el=document.createElement(e)}_$init(){this._$bindNodes(),F(this,this._$nodes)}_$addNodes(e){this._$nodes=e}_$addProp(e,t,s,o){let i;if(e[0]==="_")i=a=>this._$el.style[e.slice(1)]=a;else if(e==="className"){let a=this._$el.className;i=v=>this._$el.className=`${a} ${v}`.trim()}else i=a=>this._$el[e]=a;if(!o){i(t);return}let d=t();i(d);let r=()=>{let a=t();d!==a&&(i(a),d=a)},h={};this._$depObjectIds.push(h),s._$addDeps(o,h,r)}willAppear(e,t){}didAppear(e,t){}willDisappear(e,t){}didDisappear(e,t){}_$addLifeCycle(e,t){let s=this[t];this[t]=function(o,i,...d){return s.call(this,o,i),e.call(this,o,i)}}};var D=class extends u{afterUpdateNewNodes(e){}addAfterUpdateNewNodesFunc(e){let t=this.afterUpdateNewNodes;this.afterUpdateNewNodes=function(s){e.call(this,s),t.call(this,s)}}onUpdateNodes(e,t){}addOnUpdateNodesFunc(e){let t=this.onUpdateNodes;this.onUpdateNodes=function(s,o){e.call(this,s,o),t.call(this,s,o)}}_$bindNewNodes(e){T(e,this),this._$beforeInitSubNodes(),x(e),this.afterUpdateNewNodes(e)}_$detach(){super._$detach()}};var S=class extends D{keys=[];array=[];_$nodess=[];nodeFunc;keyFunc;arrayFunc;dlScope;listenDeps;nodesFunc;constructor(){super(3)}duplicatedOrNoKey=!1;_$getItem(e,t){let s=this.duplicatedOrNoKey?t:this.keys.indexOf(e);return this.array[s]}_$addNodeFunc(e){this.nodeFunc=e}_$addKeyFunc(e){this.keyFunc=e}_$addArrayFunc(e,t,s){this.dlScope=e,this.arrayFunc=t,this.listenDeps=s}_$addNodess(e){this.nodesFunc=e}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._$nodess=this.nodesFunc(),this._$nodes=this._$nodess.flat(1),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),s={};if(this._$depObjectIds.push(s),this.dlScope._$addDeps(this.listenDeps,s,t),this.setArray(),this.setKeys(),this.duplicatedOrNoKey)for(let o of this.array.keys())this._$nodess.push(this.nodeFunc(null,o,this));else for(let[o,i]of this.keys.entries())this._$nodess.push(this.nodeFunc(i,o,this));this._$nodes=this._$nodess.flat(1),this._$bindNodes()}getNewNodes(e,t){let s=this.nodeFunc(e,t,this);return this._$bindNewNodes(s),s}updateWithOutKey(e){let t=e._$el,s=this.array.length;this.setArray();let o=this.array.length;if(s!==o){if(s<o){let i=g(e,this),d=t.childNodes.length;for(let r=0;r<o;r++){if(r<s){i+=C(this._$nodess[r]);continue}let h=this.getNewNodes(null,r);[i,d]=L(h,i,t,d),this._$nodess.push(h)}this._$nodes=this._$nodess.flat(1);return}for(let i=o;i<s;i++)N(this._$nodess[i],this.dlScope),y(this._$nodess[i]),$(this._$nodess[i]);this._$nodess=this._$nodess.slice(0,o),this._$nodes=this._$nodess.flat(1)}}updateWithKey(e){let t=e._$el,s=g(e,this),o=this.keys,i=[...this.array],d=[...this._$nodess],r=[...this._$nodes];this.setArray(),this.setKeys(),this.duplicatedOrNoKey&&(o=[...Array(i.length).keys()]);let h=[],a=[],v=[];for(let[p,_]of o.entries()){if(this.keys.includes(_)){h.push(_),a.push(d[p]);continue}N(d[p],this.dlScope),y(d[p]),$(d[p]),v.push(p)}o=h;let b=s,w=t.childNodes.length;for(let[p,_]of this.keys.entries()){if(o.includes(_)){b+=C(a[o.indexOf(_)]);continue}let m=this.getNewNodes(_,p);[b,w]=L(m,b,t,w),a.splice(p,0,m),o.splice(p,0,_)}b=s;for(let[p,_]of this.keys.entries()){let m=o.indexOf(_);if(m===p){b+=C(a[p]);continue}let A=a[m],B=o[m];[b,w]=L(A,b,t,w),a.splice(m,1),o.splice(m,1),a.splice(p+1,0,A),o.splice(p+1,0,B)}this._$nodess=a,this._$nodes=this._$nodess.flat(1),this.onUpdateNodes(r,this._$nodes)}_$listen(e,t,s,o){let i={};e._$depObjectIds.push(i),e._$addDeps(s,i,()=>{let d=t();if(d===void 0){e._$deleteDeps(i);return}o(d)})}_$detach(){super._$detach(),this._$nodess=[]}};var K=class extends D{conditionPairs=[];condition;listenDeps=[];dlScope;_$envNodes=[];constructor(){super(4)}_$addCond(e,t,s,o){this.conditionPairs.push({condition:e,node:t}),o&&(this.dlScope||(this.dlScope=s),this.listenDeps.push(...o))}_$init(){let e=[];for(let s of this.conditionPairs)if(s.condition()){this.condition=s.condition.toString(),e=s.node();break}this._$nodes=e;let t=this._$parentNode;for(;t&&t._$nodeType!==0;)t=t._$parentNode;if(t){let s={};this._$depObjectIds.push(s),this.dlScope?._$addDeps(this.listenDeps,s,()=>this.update(t))}this._$bindNodes()}update(e){let t=this._$nodes,s=this.condition;this._$nodes=[];for(let d of this.conditionPairs)if(d.condition()){this.condition!==d.condition.toString()?(N(t,this.dlScope),y(t),this.condition=d.condition.toString(),this._$nodes=d.node(),this._$bindNewNodes(this._$nodes)):this._$nodes=t;break}if(t.length!==0&&this._$nodes.length===0&&(this.condition="[none]",N(t,this.dlScope),y(t)),s===this.condition)return;let o=g(e,this),i=e._$el;L(this._$nodes,o,i,i.childNodes.length),$(t),this.onUpdateNodes(t,this._$nodes)}};var E=class extends u{constructor(e,t,s){if(super(1),!s){this._$el=document.createTextNode(e);return}e=e;let o=e();this._$el=document.createTextNode(o);let i=()=>{let r=e();o!==r&&(this._$el.nodeValue=r,o=r)},d={};this._$depObjectIds.push(d),t._$addDeps(s,d,i)}};var W=class extends u{addPropFuncs=[];constructor(){super(5)}_$addNodes(e){this._$nodes=e}_$addProp(e,t,s,o){this.addPropFuncs.push(i=>H(i,"env",e,t,s,o))}addProps(e){for(let t of this.addPropFuncs)t(e)}addPropsToNodes(e){l(e._$nodes,t=>(t._$addBeforeInitSubNodes(()=>{this.addPropsToNodes(t)}),t._$nodeType===2&&this.addProps(t),!1))}_$init(){this.addPropsToNodes(this),this._$bindNodes()}};var V=class extends D{nodeOrFunc;listenDeps;dlScope;propFuncs=[];constructor(e,t,s){if(super(6),!s){this._$nodes=this.formatNodes(e);return}this.nodeOrFunc=e,this.listenDeps=s,this.dlScope=t,this._$nodes=this.formatNodes(this.nodeOrFunc())}_$onUpdateNodes(e){l(this._$nodes,t=>([4,3,6].includes(t._$nodeType)&&t.addOnUpdateNodesFunc(e),!0))}_$addProp(e,t,s,o){let i=d=>{if(["willAppear","didAppear","willDisappear","didDisappear"].includes(e)){d._$addLifeCycle(t,e);return}e[0]==="_"&&(d._$el.style[e.slice(1)]??"").trim()!==""||!["className"].includes(e)&&e[0]!=="_"&&d._$el[e]!==void 0||d._$addProp(e,t,s,o)};this.propFuncs.push(()=>{for(let d of this._$nodes)switch(d._$nodeType){case 0:i(d);break;case 3:case 4:case 6:d.addAfterUpdateNewNodesFunc(r=>{f(r,(h,a)=>{a._$nodeType===0&&i(a)},!1)});default:f(d._$nodes,(r,h)=>{h._$nodeType===0&&i(h)},!1)}})}formatNodes(e){return Array.isArray(e)||(e=[e]),e=e.flat(1),e=e.filter(t=>t!=null&&typeof t!="boolean").map(t=>t._$nodeType!==void 0?t:new E(t)),e}_$init(){if(this.listenDeps===void 0){this._$bindNodes();for(let s of this.propFuncs)s();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 s of this.propFuncs){s();let o={};this._$depObjectIds.push(o),this.dlScope._$addDeps(this.listenDeps,o,s)}}update(e){let t=this._$nodes;N(this._$nodes,this.dlScope),y(this._$nodes),this._$nodes=this.formatNodes(this.nodeOrFunc()),this._$bindNewNodes(this._$nodes);let s=e._$el,o=g(e,this);L(this._$nodes,o,s,s.childNodes.length),$(t),this.onUpdateNodes(t,this._$nodes)}};var Ze=I;function et(n,e){new e().render(n)}export{I as CustomNode,u as DLNode,c as DLNodeType,W as EnvNode,V as ExpressionNode,S as ForNode,P as HtmlNode,K as IfNode,E as TextNode,Ze as View,T as bindParentNode,x as initNodes,f as loopEls,l as loopNodes,et as render,j as toEls}; | ||
function x(n){for(let e of n){if(Array.isArray(e)){x(e);continue}e._$init()}}function T(n,e){for(let t of n){if(Array.isArray(t)){T(t,e);continue}t._$parentNode=e}}function l(n,e){for(let t of n)e(t)&&l(t._$nodes,e)}function f(n,e,t=!0){for(let s of n)[0,1].includes(s._$nodeType)?(e(s._$el,s),t&&f(s._$nodes,e)):f(s._$nodes,e,t)}function j(n){let e=[];return f(n,(t,s)=>{s._$nodeType===0&&e.push(t)},!1),e}var c=(r=>(r[r.HTML=0]="HTML",r[r.Text=1]="Text",r[r.Custom=2]="Custom",r[r.For=3]="For",r[r.If=4]="If",r[r.Env=5]="Env",r[r.Expression=6]="Expression",r))(c||{}),u=class{_$nodeType;__$el;get _$el(){return this.__$el??j(this._$nodes)}set _$el(e){this.__$el=e}_$parentNode;_$nodes=[];_$depObjectIds=[];_$detach(){this._$parentNode=void 0,this._$nodes=[],this._$depObjectIds=[],[1,0].includes(this._$nodeType)||(this.__$el=void 0),this._$beforeInitSubNodes=function(){}}_$beforeInitSubNodes(e){}_$addBeforeInitSubNodes(e){let t=this._$beforeInitSubNodes;this._$beforeInitSubNodes=function(s){t.call(this,s),e.call(this,s)}}_$bindNodes(){T(this._$nodes,this),this._$beforeInitSubNodes(this._$nodes),x(this._$nodes)}constructor(e){this._$nodeType=e}_$init(){}};function k(n,e,t,s,o){n[`_$$${e}`]=o?t():t,Object.defineProperty(n,e,{get(){return this[`_$$${e}`]},set(i){this[`_$$${e}`]!==i&&(this[`_$$${e}`]=i,this._$runDeps(e))}}),n._$deps[e]=new Map,o&&O(s,n,e,t,o)}function H(n,e,t,s,o,i,d){if(n?._$forwardProps){k(n,t,s,o,i);return}if(t in n){if(!i){n[t]=s;return}if(!(n[`_$$${t}`]!==`_$${e}`&&!(`_$$${t}`in n))){if(n[`_$$${t}`]===`_$${e}`){q(o,n,t,s,i);return}if(d&&`_$$${i[0]}`in o){O(o,n,t,s,i);return}z(o,n,t,s,i)}}}function q(n,e,t,s,o){let i={};e._$depObjectIds.push(i),e[t]=s(),n._$addDeps(o,i,()=>{e[t]=s(),e._$runDeps(t)})}function O(n,e,t,s,o){let i={};e._$depObjectIds.push(i);for(let d of o){let r=()=>n[d]=e[t];e._$addDeps([t],i,r),e[t]=s(),n._$addDeps(o,i,()=>{e._$deleteDep(t,i),e[t]=s(),e._$addDeps([t],i,r)})}}function z(n,e,t,s,o){let i={};e._$depObjectIds.push(i),e[`_$${t}`]=s(),n._$addDeps(o,i,()=>{e[`_$${t}`]=s(),e._$runDeps(t)})}function F(n,e){l(e,t=>{switch(t._$nodeType){case 1:case 0:n._$el.appendChild(t._$el);break;default:F(n,t._$nodes);break}return!1})}function y(n){Q(n),f(n,(e,t)=>{document.body.contains(e)&&(t._$nodeType===0&&t.willDisappear(e,t),e.remove(),t._$nodeType===0&&t.didDisappear(e,t))}),R(n)}function $(n){for(let e of n)e._$detach()}function N(n,e){l(n,t=>{for(let s of t._$depObjectIds)e._$deleteDeps(s);return t._$nodeType===2&&N(t._$children,e),!0})}function L(n,e,t,s){let o=s??t.childNodes.length;return G(n),f(n,(i,d)=>{let r=document.body.contains(i);[0].includes(d._$nodeType)&&!r&&d.willAppear(i,d),e===o?t.appendChild(i):t.insertBefore(i,t.childNodes[e]),[0].includes(d._$nodeType)&&!r&&d.didAppear(i,d),e++,o++},!1),J(n),[e,o]}function g(n,e){return U(n._$nodes,e)}function C(n){return U(n,void 0)}function U(n,e){let t=0,s=!1;return l(n,o=>s?!1:o===e?(s=!0,!1):[1,0].includes(o._$nodeType)?(t++,!1):!0),t}function M(n,e){l(n,t=>([2].includes(t._$nodeType)&&t[e](t),!0))}function G(n){M(n,"willMount")}function J(n){M(n,"didMount")}function Q(n){M(n,"willUnmount")}function R(n){M(n,"didUnmount")}var I=class extends u{_$deps={};_$envNodes;_$derivedPairs;_$tag="";constructor(){super(2)}_$addAfterset(e){let t=this.Afterset.bind(this);this.Afterset=function(){t(),e()}.bind(this)}_$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)}_$children=[];_$addChildren(e){this._$children=e}_$resetChildren(){for(let e of this._$children)e._$nodes=[]}_$initDecorators(){if(this._$derivedPairs)for(let[e,t]of Object.entries(this._$derivedPairs)){let s=this[e];if(typeof s!="function")return;this[e]=this[e]();let o=this[e];this._$addDeps(t,{},()=>{let i=s();i!==o&&(this[e]=i,o=i,this._$runDeps(e))})}}_$addDeps(e,t,s){for(let o of e)this._$deps[o].set(t,s)}_$deleteDep(e,t){this._$deps[e].delete(t)}_$deleteDeps(e){for(let t in this._$deps)this._$deleteDep(t,e)}AfterConstruct(){}Preset(){}Afterset(){}_$init(){this.AfterConstruct(),this._$initDecorators(),this.Preset(),this._$nodes=(this.Body.bind(this)??(()=>[]))(),this._$bindNodes(),this.Afterset()}_$addProp(e,t,s,o,i){H(this,"prop",e,t,s,o,i)}willMount(e,t){}didMount(e,t){}willUnmount(e,t){}didUnmount(e,t){}_$addLifeCycle(e,t){let s=this[t];this[t]=function(o,i){e.call(this,this._$el,this),s.call(this,this._$el,this)}}render(e){let t=new P("div");t._$addNodes([this]),t._$addProp("id",typeof e=="string"?e:e.id),t._$init(),this.willMount(this._$el,this),l(this._$nodes,s=>{switch(s._$nodeType){case 0:s.willAppear(s._$el,s);break;case 2:s.willMount(s._$el,s);break}return!0}),typeof e=="string"&&(e=document.getElementById(e)),e.replaceWith(t._$el),l(this._$nodes,s=>{switch(s._$nodeType){case 0:s.didAppear(s._$el,s);break;case 2:s.didMount(s._$el,s);break}return!0}),this.didMount(this._$el,this)}_$detach(){super._$detach(),$(this._$children)}_$forwardProps=!1;forwardProps(e){let t=[...new Set(Object.getOwnPropertyNames(this).filter(s=>s.startsWith("_$$")).map(s=>s.replace(/_\$\$/g,"")))];for(let s of t)e._$addProp(s,()=>this[s],this,[s],!0);e._$nodeType===2?e._$children=this._$children:e._$nodes=this._$children}};var P=class extends u{_$envNodes=[];constructor(e){super(0),this._$el=document.createElement(e)}_$init(){this._$bindNodes(),F(this,this._$nodes)}_$addNodes(e){this._$nodes=e}_$addProp(e,t,s,o){let i;if(e[0]==="_")i=a=>this._$el.style[e.slice(1)]=a;else if(e==="className"){let a=this._$el.className;i=v=>this._$el.className=`${a} ${v}`.trim()}else i=a=>this._$el[e]=a;if(!o){i(t);return}let d=t();i(d);let r=()=>{let a=t();d!==a&&(i(a),d=a)},h={};this._$depObjectIds.push(h),s._$addDeps(o,h,r)}willAppear(e,t){}didAppear(e,t){}willDisappear(e,t){}didDisappear(e,t){}_$addLifeCycle(e,t){let s=this[t];this[t]=function(o,i,...d){return s.call(this,o,i),e.call(this,o,i)}}};var D=class extends u{afterUpdateNewNodes(e){}addAfterUpdateNewNodesFunc(e){let t=this.afterUpdateNewNodes;this.afterUpdateNewNodes=function(s){e.call(this,s),t.call(this,s)}}onUpdateNodes(e,t){}addOnUpdateNodesFunc(e){let t=this.onUpdateNodes;this.onUpdateNodes=function(s,o){e.call(this,s,o),t.call(this,s,o)}}_$bindNewNodes(e){T(e,this),this._$beforeInitSubNodes(e),x(e),this.afterUpdateNewNodes(e)}_$detach(){super._$detach()}};var S=class extends D{keys=[];array=[];_$nodess=[];nodeFunc;keyFunc;arrayFunc;dlScope;listenDeps;nodesFunc;constructor(){super(3)}duplicatedOrNoKey=!1;_$getItem(e,t){let s=this.duplicatedOrNoKey?t:this.keys.indexOf(e);return this.array[s]}_$addNodeFunc(e){this.nodeFunc=e}_$addKeyFunc(e){this.keyFunc=e}_$addArrayFunc(e,t,s){this.dlScope=e,this.arrayFunc=t,this.listenDeps=s}_$addNodess(e){this.nodesFunc=e}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._$nodess=this.nodesFunc(),this._$nodes=this._$nodess.flat(1),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),s={};if(this._$depObjectIds.push(s),this.dlScope._$addDeps(this.listenDeps,s,t),this.setArray(),this.setKeys(),this.duplicatedOrNoKey)for(let o of this.array.keys())this._$nodess.push(this.nodeFunc(null,o,this));else for(let[o,i]of this.keys.entries())this._$nodess.push(this.nodeFunc(i,o,this));this._$nodes=this._$nodess.flat(1),this._$bindNodes()}getNewNodes(e,t){let s=this.nodeFunc(e,t,this);return this._$bindNewNodes(s),s}updateWithOutKey(e){let t=e._$el,s=this.array.length;this.setArray();let o=this.array.length;if(s!==o){if(s<o){let i=g(e,this),d=t.childNodes.length;for(let r=0;r<o;r++){if(r<s){i+=C(this._$nodess[r]);continue}let h=this.getNewNodes(null,r);[i,d]=L(h,i,t,d),this._$nodess.push(h)}this._$nodes=this._$nodess.flat(1);return}for(let i=o;i<s;i++)N(this._$nodess[i],this.dlScope),y(this._$nodess[i]),$(this._$nodess[i]);this._$nodess=this._$nodess.slice(0,o),this._$nodes=this._$nodess.flat(1)}}updateWithKey(e){let t=e._$el,s=g(e,this),o=this.keys,i=[...this.array],d=[...this._$nodess],r=[...this._$nodes];this.setArray(),this.setKeys(),this.duplicatedOrNoKey&&(o=[...Array(i.length).keys()]);let h=[],a=[],v=[];for(let[p,_]of o.entries()){if(this.keys.includes(_)){h.push(_),a.push(d[p]);continue}N(d[p],this.dlScope),y(d[p]),$(d[p]),v.push(p)}o=h;let b=s,w=t.childNodes.length;for(let[p,_]of this.keys.entries()){if(o.includes(_)){b+=C(a[o.indexOf(_)]);continue}let m=this.getNewNodes(_,p);[b,w]=L(m,b,t,w),a.splice(p,0,m),o.splice(p,0,_)}b=s;for(let[p,_]of this.keys.entries()){let m=o.indexOf(_);if(m===p){b+=C(a[p]);continue}let A=a[m],B=o[m];[b,w]=L(A,b,t,w),a.splice(m,1),o.splice(m,1),a.splice(p+1,0,A),o.splice(p+1,0,B)}this._$nodess=a,this._$nodes=this._$nodess.flat(1),this.onUpdateNodes(r,this._$nodes)}_$listen(e,t,s,o){let i={};e._$depObjectIds.push(i),e._$addDeps(s,i,()=>{let d=t();if(d===void 0){e._$deleteDeps(i);return}o(d)})}_$detach(){super._$detach(),this._$nodess=[]}};var K=class extends D{conditionPairs=[];condition;listenDeps=[];dlScope;_$envNodes=[];constructor(){super(4)}_$addCond(e,t,s,o){this.conditionPairs.push({condition:e,node:t}),o&&(this.dlScope||(this.dlScope=s),this.listenDeps.push(...o))}_$init(){let e=[];for(let s of this.conditionPairs)if(s.condition()){this.condition=s.condition.toString(),e=s.node();break}this._$nodes=e;let t=this._$parentNode;for(;t&&t._$nodeType!==0;)t=t._$parentNode;if(t){let s={};this._$depObjectIds.push(s),this.dlScope?._$addDeps(this.listenDeps,s,()=>this.update(t))}this._$bindNodes()}update(e){let t=this._$nodes,s=this.condition;this._$nodes=[];for(let d of this.conditionPairs)if(d.condition()){this.condition!==d.condition.toString()?(N(t,this.dlScope),y(t),this.condition=d.condition.toString(),this._$nodes=d.node(),this._$bindNewNodes(this._$nodes)):this._$nodes=t;break}if(t.length!==0&&this._$nodes.length===0&&(this.condition="[none]",N(t,this.dlScope),y(t)),s===this.condition)return;let o=g(e,this),i=e._$el;L(this._$nodes,o,i,i.childNodes.length),$(t),this.onUpdateNodes(t,this._$nodes)}};var E=class extends u{constructor(e,t,s){if(super(1),!s){this._$el=document.createTextNode(e);return}e=e;let o=e();this._$el=document.createTextNode(o);let i=()=>{let r=e();o!==r&&(this._$el.nodeValue=r,o=r)},d={};this._$depObjectIds.push(d),t._$addDeps(s,d,i)}};var W=class extends u{addPropFuncs=[];constructor(){super(5)}_$addNodes(e){this._$nodes=e}_$addProp(e,t,s,o){this.addPropFuncs.push(i=>H(i,"env",e,t,s,o))}addProps(e){for(let t of this.addPropFuncs)t(e)}addPropsToNodes(e){l(e,t=>(console.log("called",t),t._$addBeforeInitSubNodes(s=>{this.addPropsToNodes(s)}),t._$nodeType===2&&this.addProps(t),!1))}_$init(){this.addPropsToNodes(this._$nodes),this._$bindNodes()}};var V=class extends D{nodeOrFunc;listenDeps;dlScope;propFuncs=[];constructor(e,t,s){if(super(6),!s){this._$nodes=this.formatNodes(e);return}this.nodeOrFunc=e,this.listenDeps=s,this.dlScope=t,this._$nodes=this.formatNodes(this.nodeOrFunc())}_$onUpdateNodes(e){l(this._$nodes,t=>([4,3,6].includes(t._$nodeType)&&t.addOnUpdateNodesFunc(e),!0))}_$addProp(e,t,s,o){let i=d=>{if(["willAppear","didAppear","willDisappear","didDisappear"].includes(e)){d._$addLifeCycle(t,e);return}e[0]==="_"&&(d._$el.style[e.slice(1)]??"").trim()!==""||!["className"].includes(e)&&e[0]!=="_"&&d._$el[e]!==void 0||d._$addProp(e,t,s,o)};this.propFuncs.push(()=>{for(let d of this._$nodes)switch(d._$nodeType){case 0:i(d);break;case 3:case 4:case 6:d.addAfterUpdateNewNodesFunc(r=>{f(r,(h,a)=>{a._$nodeType===0&&i(a)},!1)});default:f(d._$nodes,(r,h)=>{h._$nodeType===0&&i(h)},!1)}})}formatNodes(e){return Array.isArray(e)||(e=[e]),e=e.flat(1),e=e.filter(t=>t!=null&&typeof t!="boolean").map(t=>t._$nodeType!==void 0?t:new E(t)),e}_$init(){if(this.listenDeps===void 0){this._$bindNodes();for(let s of this.propFuncs)s();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 s of this.propFuncs){s();let o={};this._$depObjectIds.push(o),this.dlScope._$addDeps(this.listenDeps,o,s)}}update(e){let t=this._$nodes;N(this._$nodes,this.dlScope),y(this._$nodes),this._$nodes=this.formatNodes(this.nodeOrFunc()),this._$bindNewNodes(this._$nodes);let s=e._$el,o=g(e,this);L(this._$nodes,o,s,s.childNodes.length),$(t),this.onUpdateNodes(t,this._$nodes)}};var Ze=I;function et(n,e){new e().render(n)}export{I as CustomNode,u as DLNode,c as DLNodeType,W as EnvNode,V as ExpressionNode,S as ForNode,P as HtmlNode,K as IfNode,E as TextNode,Ze as View,T as bindParentNode,x as initNodes,f as loopEls,l as loopNodes,et as render,j as toEls}; |
{ | ||
"name": "@dlightjs/dlight", | ||
"version": "0.4.4", | ||
"version": "0.4.5", | ||
"files": [ | ||
@@ -5,0 +5,0 @@ "dist" |
Sorry, the diff of this file is not supported yet
59008