j-templates 7.0.31 → 7.0.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,7 @@
1
1
  export declare function CreateNodeValueAssignment(target: HTMLElement): (value: string) => void;
2
- export declare function CreatePropertyAssignment(target: any): (next: {
2
+ export declare function CreatePropertyAssignment(target: any): ((next: {
3
+ nodeValue: string;
4
+ }) => void) | ((next: {
3
5
  [prop: string]: any;
4
- }) => void;
6
+ }) => void);
5
7
  export declare function AssignProperties(target: any, next: any): void;
@@ -52,6 +52,11 @@ function GetAssignmentFunction(path) {
52
52
  }
53
53
  }
54
54
  function CreatePropertyAssignment(target) {
55
+ if (target.nodeType === Node.TEXT_NODE) {
56
+ return function (next) {
57
+ AssignNodeValue(target, next.nodeValue);
58
+ };
59
+ }
55
60
  const last = [
56
61
  ["", null, null],
57
62
  ];
package/Node/vNode.js CHANGED
@@ -11,8 +11,9 @@ var vNode;
11
11
  function Create(definition) {
12
12
  return {
13
13
  definition,
14
+ type: definition.type,
14
15
  injector: injector_1.Injector.Current() ?? new injector_1.Injector(),
15
- node: null,
16
+ node: definition.node ?? null,
16
17
  children: null,
17
18
  destroyed: false,
18
19
  component: null,
@@ -69,7 +70,7 @@ var vNode;
69
70
  })(vNode || (exports.vNode = vNode = {}));
70
71
  function InitNode(vnode) {
71
72
  const { type, namespace, props, attrs, on, data, componentConstructor, children, childrenArray, } = vnode.definition;
72
- const node = (vnode.node = nodeConfig_1.NodeConfig.createNode(type, namespace));
73
+ const node = (vnode.node = vnode.definition.node ?? nodeConfig_1.NodeConfig.createNode(type, namespace));
73
74
  vnode.definition = null;
74
75
  if (props) {
75
76
  const assignProperties = nodeConfig_1.NodeConfig.createPropertyAssignment(node);
@@ -133,48 +134,86 @@ function InitNode(vnode) {
133
134
  vNode.InitAll(vnode.children);
134
135
  }
135
136
  else if (children) {
136
- if (data) {
137
- DynamicChildren(vnode, children, ToArray(data));
138
- }
139
- else
140
- StaticChildren(vnode, children);
137
+ Children(vnode, children, data);
141
138
  }
142
139
  UpdateChildren(vnode, true, !!childrenArray);
143
140
  }
144
- function StaticChildren(vnode, children) {
145
- const childrenScope = Store_1.ObservableScope.Create(WrapStaticChildren(vnode.injector, children));
141
+ function Children(vnode, children, data) {
142
+ const childrenScope = CreateChildrenScope(vnode, children, data);
146
143
  vnode.scopes.push(childrenScope);
147
- const child = Store_1.ObservableScope.Peek(childrenScope);
148
- switch (typeof child) {
149
- case 'string': {
150
- const node = injector_1.Injector.Scope(vnode.injector, vNode.Create, {
151
- type: "text",
152
- namespace: null,
153
- props() {
154
- return { nodeValue: Store_1.ObservableScope.Value(childrenScope) };
155
- },
156
- });
157
- vnode.children = [node];
158
- break;
159
- }
160
- default: {
161
- Store_1.ObservableScope.Touch(childrenScope);
162
- Store_1.ObservableScope.Watch(childrenScope, CreateScheduledCallback(function () {
163
- if (vnode.destroyed)
164
- return;
165
- vNode.DestroyAll(vnode.children);
166
- const nodes = Store_1.ObservableScope.Peek(childrenScope);
167
- vnode.children = Array.isArray(nodes) ? nodes : [nodes];
168
- UpdateChildren(vnode);
169
- }));
170
- vnode.children = (Array.isArray(child) ? child : [child]);
171
- break;
172
- }
173
- }
144
+ Store_1.ObservableScope.Watch(childrenScope, CreateScheduledCallback(function (scope) {
145
+ if (vnode.destroyed)
146
+ return;
147
+ AssignChildren(vnode, scope);
148
+ UpdateChildren(vnode);
149
+ }));
150
+ AssignChildren(vnode, childrenScope);
174
151
  }
175
- function WrapStaticChildren(injector, children) {
152
+ function CreateChildrenScope(vnode, children, data) {
153
+ if (data === undefined)
154
+ return Store_1.ObservableScope.Create(WrapStaticChildren(vnode, children));
155
+ const nodeList = list_1.List.Create();
156
+ const scope = Store_1.ObservableScope.Create(WrapChildren(vnode.injector, children, ToArray(data), nodeList));
157
+ Store_1.ObservableScope.OnDestroyed(scope, function () {
158
+ DestroyNodeList(nodeList);
159
+ });
160
+ return scope;
161
+ }
162
+ function WrapStaticChildren(vnode, children) {
163
+ return function () {
164
+ vnode.children && vNode.DestroyAll(vnode.children);
165
+ return injector_1.Injector.Scope(vnode.injector, children, undefined);
166
+ };
167
+ }
168
+ function WrapChildren(injector, children, data, nodeList) {
176
169
  return function () {
177
- return injector_1.Injector.Scope(injector, children, undefined);
170
+ const nextData = data();
171
+ switch (nextData.length) {
172
+ case 0:
173
+ DestroyNodeList(nodeList);
174
+ return [];
175
+ default: {
176
+ const nodeListMap = list_1.List.ToListMap(nodeList, GetData);
177
+ const nextNodeList = list_1.List.Create();
178
+ const nextNodeArray = [];
179
+ for (let x = 0; x < nextData.length; x++) {
180
+ const data = nextData[x];
181
+ const existingNodeList = nodeListMap.get(data);
182
+ const existingNode = existingNodeList && list_1.List.PopNode(existingNodeList);
183
+ if (existingNode) {
184
+ list_1.List.AddNode(nextNodeList, existingNode);
185
+ if (existingNode.data.scope.dirty) {
186
+ vNode.DestroyAll(existingNode.data.nodes);
187
+ existingNode.data.nodes = Store_1.ObservableScope.Value(existingNode.data.scope);
188
+ }
189
+ }
190
+ else {
191
+ nodeListMap.delete(data);
192
+ const childrenScope = Store_1.ObservableScope.Create(function () {
193
+ const childNodes = injector_1.Injector.Scope(injector, children, data);
194
+ const nodes = typeof childNodes === 'string' ? [vNode.Create({
195
+ type: 'text',
196
+ namespace: null,
197
+ props: {
198
+ nodeValue: childNodes
199
+ }
200
+ })] : Array.isArray(childNodes) ? childNodes : [childNodes];
201
+ return nodes;
202
+ });
203
+ list_1.List.Add(nextNodeList, {
204
+ data,
205
+ nodes: Store_1.ObservableScope.Value(childrenScope),
206
+ scope: childrenScope
207
+ });
208
+ }
209
+ nextNodeArray.push(...nextNodeList.tail.data.nodes);
210
+ }
211
+ for (let value of nodeListMap.values())
212
+ DestroyNodeList(value);
213
+ list_1.List.Append(nodeList, nextNodeList);
214
+ return nextNodeArray;
215
+ }
216
+ }
178
217
  };
179
218
  }
180
219
  function DestroyNodeList(nodeList) {
@@ -182,76 +221,30 @@ function DestroyNodeList(nodeList) {
182
221
  vNode.DestroyAll(node.data.nodes);
183
222
  Store_1.ObservableScope.Destroy(node.data.scope);
184
223
  }
185
- }
186
- function DynamicChildren(vnode, children, data) {
187
- const dataScope = Store_1.ObservableScope.Create(data);
188
- vnode.scopes.push(dataScope);
189
- const nodeList = list_1.List.Create();
190
- const childrenScope = Store_1.ObservableScope.Create(WrapDynamicChildren(dataScope, nodeList, vnode.injector, children));
191
- vnode.scopes.push(childrenScope);
192
- Store_1.ObservableScope.OnDestroyed(dataScope, function () {
193
- DestroyNodeList(nodeList);
194
- });
195
- Store_1.ObservableScope.Watch(childrenScope, CreateScheduledCallback(function () {
196
- if (vnode.destroyed)
197
- return;
198
- vnode.children = Store_1.ObservableScope.Peek(childrenScope);
199
- UpdateChildren(vnode);
200
- }));
201
- vnode.children = Store_1.ObservableScope.Value(childrenScope);
224
+ list_1.List.Clear(nodeList);
202
225
  }
203
226
  function GetData(data) {
204
227
  return data.data;
205
228
  }
206
- function WrapDynamicChildren(dataScope, nodeList, injector, children) {
207
- return function () {
208
- const nextData = Store_1.ObservableScope.Value(dataScope);
209
- const nodeMap = list_1.List.ToNodeMap(nodeList, GetData);
210
- const nextNodeList = list_1.List.Create();
211
- const nextNodeArray = [];
212
- for (let x = 0; x < nextData.length; x++) {
213
- const data = nextData[x];
214
- const existingNodeArray = nodeMap.get(data);
215
- let existingNode = null;
216
- for (let x = 0; existingNodeArray &&
217
- x < existingNodeArray.length &&
218
- existingNode === null; x++) {
219
- existingNode = existingNodeArray[x];
220
- existingNodeArray[x] = null;
221
- }
222
- if (existingNode !== null) {
223
- list_1.List.RemoveNode(nodeList, existingNode);
224
- list_1.List.AddNode(nextNodeList, existingNode);
225
- if (existingNode.data.scope.dirty) {
226
- vNode.DestroyAll(existingNode.data.nodes);
227
- existingNode.data.nodes = Store_1.ObservableScope.Value(existingNode.data.scope);
228
- }
229
- }
230
- else {
231
- const childrenScope = Store_1.ObservableScope.Create(function () {
232
- const childNodes = injector_1.Injector.Scope(injector, children, data);
233
- const nodes = typeof childNodes === 'string' ? [vNode.Create({
234
- type: 'text',
235
- namespace: null,
236
- props: {
237
- nodeValue: childNodes
238
- }
239
- })] : Array.isArray(childNodes) ? childNodes : [childNodes];
240
- return nodes;
241
- });
242
- list_1.List.Add(nextNodeList, {
243
- data,
244
- nodes: Store_1.ObservableScope.Value(childrenScope),
245
- scope: childrenScope,
246
- });
247
- }
248
- nextNodeArray.push(...nextNodeList.tail.data.nodes);
229
+ function AssignChildren(vnode, childrenScope) {
230
+ const children = Store_1.ObservableScope.Peek(childrenScope);
231
+ switch (typeof children) {
232
+ case 'string': {
233
+ vnode.children = [vNode.Create({
234
+ type: 'text',
235
+ namespace: null,
236
+ node: vnode.children?.[0]?.type === 'text' && vnode.children[0].node || undefined,
237
+ props: {
238
+ nodeValue: children
239
+ }
240
+ })];
241
+ break;
249
242
  }
250
- DestroyNodeList(nodeList);
251
- list_1.List.Clear(nodeList);
252
- list_1.List.Append(nodeList, nextNodeList);
253
- return nextNodeArray;
254
- };
243
+ default: {
244
+ vnode.children = Array.isArray(children) ? children : [children];
245
+ break;
246
+ }
247
+ }
255
248
  }
256
249
  function UpdateChildren(vnode, init = false, skipInit = false) {
257
250
  if (!vnode.children)
@@ -261,7 +254,7 @@ function UpdateChildren(vnode, init = false, skipInit = false) {
261
254
  if (vnode.destroyed || children !== vnode.children)
262
255
  return;
263
256
  for (let x = 0; !skipInit && x < children.length; x++)
264
- if (children[x].node === null) {
257
+ if (children[x].definition) {
265
258
  const childNode = children[x];
266
259
  (0, thread_1.Schedule)(function () {
267
260
  if (vnode.destroyed || children !== vnode.children)
@@ -310,13 +303,13 @@ function ScheduledAssignment(assign) {
310
303
  }
311
304
  function CreateScheduledCallback(callback) {
312
305
  let scheduled = false;
313
- return function () {
306
+ return function (...args) {
314
307
  if (scheduled)
315
308
  return;
316
309
  scheduled = true;
317
310
  nodeConfig_1.NodeConfig.scheduleUpdate(function () {
318
311
  scheduled = false;
319
- callback();
312
+ callback(...args);
320
313
  });
321
314
  };
322
315
  }
@@ -14,6 +14,7 @@ export type vNodeEvents<E extends {
14
14
  };
15
15
  export type vNodeChildrenFunction<T> = ((data: T) => vNode | vNode[]) | ((data: T) => string);
16
16
  export type vNode = {
17
+ type: string;
17
18
  definition: vNodeDefinition<any, any, any>;
18
19
  injector: Injector;
19
20
  node: Node | null;
@@ -24,6 +25,7 @@ export type vNode = {
24
25
  };
25
26
  export type vNodeDefinition<P = HTMLElement, E = HTMLElementEventMap, T = never> = {
26
27
  type: string;
28
+ node?: Node;
27
29
  namespace: string | null;
28
30
  props?: FunctionOr<RecursivePartial<P>>;
29
31
  attrs?: FunctionOr<{
@@ -232,7 +232,6 @@ function UpdateEmitters(scope, right) {
232
232
  function DestroyScope(scope) {
233
233
  if (!scope)
234
234
  return;
235
- const emitters = scope.emitters;
236
235
  scope.emitters = null;
237
236
  scope.emitter = null;
238
237
  scope.getFunction = null;
package/Utils/list.d.ts CHANGED
@@ -26,4 +26,5 @@ export declare namespace List {
26
26
  }): void;
27
27
  function Append<T>(appendTo: IList<T>, append: IList<T>): void;
28
28
  function ToNodeMap<T>(list: IList<T>, keyCallback: (data: T) => unknown): Map<any, INode<T>[]>;
29
+ function ToListMap<T>(list: IList<T>, keyCallback: (data: T) => unknown): Map<any, IList<T>>;
29
30
  }
package/Utils/list.js CHANGED
@@ -209,4 +209,18 @@ var List;
209
209
  return map;
210
210
  }
211
211
  List.ToNodeMap = ToNodeMap;
212
+ function ToListMap(list, keyCallback) {
213
+ const map = new Map();
214
+ let node = list.head;
215
+ while (node !== null) {
216
+ const key = keyCallback(node.data);
217
+ const mapList = map.get(key) ?? List.Create();
218
+ List.RemoveNode(list, node);
219
+ List.AddNode(mapList, node);
220
+ map.set(key, mapList);
221
+ node = list.head;
222
+ }
223
+ return map;
224
+ }
225
+ List.ToListMap = ToListMap;
212
226
  })(List || (exports.List = List = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "j-templates",
3
- "version": "7.0.31",
3
+ "version": "7.0.33",
4
4
  "description": "j-templates",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/TypesInCode/jTemplates",