j-templates 6.0.25 → 6.0.27

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.
@@ -0,0 +1,3 @@
1
+ export declare function CreateAssignment(target: any, createAssignment: {
2
+ (target: any, key: string): (next: any) => void;
3
+ }): (next: any) => void;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CreateAssignment = CreateAssignment;
4
+ function CreateAssignment(target, createAssignment) {
5
+ let last;
6
+ let writeTo;
7
+ return function AssignNext(next) {
8
+ if (next === last)
9
+ return;
10
+ last = next;
11
+ writeTo ??= {};
12
+ const nextKeys = next && Object.keys(next);
13
+ for (let x = 0; nextKeys && x < nextKeys.length; x++) {
14
+ const key = nextKeys[x];
15
+ writeTo[key] ??= createAssignment(target, key);
16
+ }
17
+ const writeKeys = Object.keys(writeTo);
18
+ for (let x = 0; x < writeKeys.length; x++)
19
+ writeTo[writeKeys[x]](next);
20
+ };
21
+ }
@@ -1,3 +1 @@
1
- export declare function CreateEventAssignment(target: HTMLElement): (next: null | {
2
- [event: string]: (event: Event) => void;
3
- }) => void;
1
+ export declare function CreateEventAssignment(target: HTMLElement, event: string): (next: any) => void;
@@ -1,29 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CreateEventAssignment = CreateEventAssignment;
4
- function AssignEvent(target, event) {
5
- let lastValue;
6
- return function Assign(value) {
7
- if (value !== lastValue) {
8
- lastValue && target.removeEventListener(event, lastValue);
9
- value && target.addEventListener(event, value);
10
- lastValue = value;
11
- }
12
- };
13
- }
14
- function DefaultAssignment(target, writeTo, next, event) {
15
- const value = next;
16
- writeTo[event] ??= AssignEvent(target, event);
17
- writeTo[event](value);
18
- }
19
- function CreateEventAssignment(target) {
20
- let priorKeys;
21
- const writeTo = {};
22
- return function AssignNext(next) {
23
- const nextKeys = next !== null ? Object.keys(next) : [];
24
- const keys = priorKeys === undefined ? nextKeys : nextKeys.concat(priorKeys);
25
- priorKeys = nextKeys;
26
- for (let x = 0; x < keys.length; x++)
27
- DefaultAssignment(target, writeTo, next && next[keys[x]], keys[x]);
4
+ function CreateEventAssignment(target, event) {
5
+ let lastEvent;
6
+ return function (next) {
7
+ const nextEvent = next && next[event];
8
+ if (nextEvent === lastEvent)
9
+ return;
10
+ lastEvent && target.removeEventListener(event, lastEvent);
11
+ nextEvent && target.addEventListener(event, nextEvent);
12
+ lastEvent = nextEvent;
28
13
  };
29
14
  }
@@ -1,4 +1,4 @@
1
- export declare function CreateNodeValueAssignment(target: HTMLElement): (value: {
2
- nodeValue: string;
1
+ export declare function CreateNodeValueAssignment(target: HTMLElement): (value: string) => void;
2
+ export declare function CreatePropertyAssignment(target: any): (next: {
3
+ [prop: string]: any;
3
4
  }) => void;
4
- export declare function CreatePropertyAssignment(target: any, root?: boolean): (next: any) => void;
@@ -2,86 +2,75 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CreateNodeValueAssignment = CreateNodeValueAssignment;
4
4
  exports.CreatePropertyAssignment = CreatePropertyAssignment;
5
- const jsonType_1 = require("../Utils/jsonType");
6
- function AssignProp(target, prop) {
7
- let lastValue = target[prop];
8
- return function Assign(value) {
9
- if (value !== lastValue) {
10
- target[prop] = value;
11
- lastValue = value;
12
- }
13
- };
14
- }
5
+ const json_1 = require("../Utils/json");
15
6
  function CreateNodeValueAssignment(target) {
16
7
  let lastValue = target.nodeValue;
17
8
  return function AssignNodeValue(value) {
18
- if (value.nodeValue !== lastValue) {
19
- target.nodeValue = value.nodeValue;
20
- lastValue = value.nodeValue;
21
- }
22
- };
23
- }
24
- function CreateValueAssignment(target) {
25
- let lastValue = target.value;
26
- target.addEventListener('blur', function () {
27
- target.value = lastValue;
28
- });
29
- return function AssignValue(value) {
30
9
  if (value !== lastValue) {
31
- const start = target.selectionStart;
32
- const end = target.selectionEnd;
33
- target.value = value;
34
- if (target.ownerDocument.activeElement === target)
35
- target.setSelectionRange(start, end);
10
+ target.nodeValue = value;
36
11
  lastValue = value;
37
12
  }
38
13
  };
39
14
  }
40
- function ValueAssignment(target, writeTo, next) {
41
- writeTo.value ??= target.nodeName === 'INPUT' ? CreateValueAssignment(target) : AssignProp(target, 'value');
42
- writeTo.value(next.value);
43
- }
44
- function DefaultAssignment(target, writeTo, next, prop) {
45
- const value = next && next[prop];
46
- writeTo[prop] ??= (0, jsonType_1.JsonType)(value) === 'value' ?
47
- AssignProp(target, prop) :
48
- CreatePropertyAssignment(target[prop], false);
49
- writeTo[prop](value);
50
- }
51
- function CreateAssignProp(target, prop) {
52
- let lastValue;
53
- let childPropertyAssignment;
54
- return function (next) {
55
- const nextValue = next && next[prop];
56
- if (nextValue === lastValue)
57
- return;
58
- const nextValueType = (0, jsonType_1.JsonType)(nextValue);
59
- switch (nextValueType) {
60
- case "value":
61
- target[prop] = nextValue;
15
+ function WalkValue(next, callback, index = 0, parent = "") {
16
+ const keys = Object.keys(next);
17
+ for (let x = 0; x < keys.length; x++) {
18
+ const value = next[keys[x]];
19
+ const type = (0, json_1.JsonType)(value);
20
+ switch (type) {
21
+ case "object":
22
+ index = WalkValue(value, callback, index, `${parent}${keys[x]}.`);
23
+ break;
62
24
  default:
63
- childPropertyAssignment ??= CreatePropertyAssignment(target[prop]);
64
- childPropertyAssignment(target[prop], nextValue);
25
+ callback(`${parent}${keys[x]}`, value, index);
26
+ index++;
27
+ break;
65
28
  }
66
- };
29
+ }
30
+ return index;
31
+ }
32
+ function AssignNodeValue(target, value) {
33
+ target.nodeValue = value;
34
+ }
35
+ function AssignValue(target, value) {
36
+ target.value = value;
67
37
  }
68
- function CreatePropertyAssignment(target, root = true) {
69
- let last;
70
- let writeTo;
71
- return function AssignNext(next) {
72
- if (next === last)
38
+ function AssignClassName(target, value) {
39
+ target.className = value;
40
+ }
41
+ function GetAssignmentFunction(path) {
42
+ switch (path) {
43
+ case "nodeValue":
44
+ return AssignNodeValue;
45
+ case "value":
46
+ return AssignValue;
47
+ case "className":
48
+ return AssignClassName;
49
+ default:
50
+ return new Function("t", "v", `t.${path} = v;`);
51
+ }
52
+ }
53
+ function CreatePropertyAssignment(target) {
54
+ const last = [["", null, null]];
55
+ function WalkCallback(path, value, index) {
56
+ if (index >= last.length || last[index][0] !== path) {
57
+ last[index] = [path, value, GetAssignmentFunction(path)];
58
+ last[index][2](target, value);
59
+ }
60
+ else if (last[index][1] !== value) {
61
+ last[index][2](target, value);
62
+ }
63
+ }
64
+ return function AssignProperty(next) {
65
+ if (next === null) {
66
+ for (let x = 0; x < last.length; x++)
67
+ last[x][2](target, null);
68
+ if (last.length > 0)
69
+ last.splice(0);
73
70
  return;
74
- last = next;
75
- writeTo ??= {};
76
- const nextKeys = next && Object.keys(next);
77
- for (let x = 0; nextKeys && x < nextKeys.length; x++) {
78
- const key = nextKeys[x];
79
- if (writeTo[key] === undefined) {
80
- writeTo[key] = CreateAssignProp(target, key);
81
- }
82
71
  }
83
- const writeKeys = Object.keys(writeTo);
84
- for (let x = 0; x < writeKeys.length; x++)
85
- writeTo[writeKeys[x]](next);
72
+ const endIndex = WalkValue(next, WalkCallback);
73
+ if (endIndex < last.length)
74
+ last.splice(endIndex);
86
75
  };
87
76
  }
@@ -5,19 +5,18 @@ const window_1 = require("./window");
5
5
  const list_1 = require("../Utils/list");
6
6
  const createPropertyAssignment_1 = require("./createPropertyAssignment");
7
7
  const createEventAssignment_1 = require("./createEventAssignment");
8
+ const createAssignment_1 = require("./createAssignment");
9
+ const createPropertyAssignment_2 = require("./createPropertyAssignment");
8
10
  let pendingUpdates = list_1.List.Create();
9
11
  let updateScheduled = false;
10
12
  const updateMs = 16;
11
13
  function processUpdates() {
12
14
  list_1.List.Add(pendingUpdates, null);
13
- const start = Date.now();
14
15
  let callback;
15
16
  while ((callback = list_1.List.Pop(pendingUpdates)))
16
17
  callback();
17
18
  if (pendingUpdates.size === 0)
18
19
  updateScheduled = false;
19
- else if (Date.now() - start < updateMs)
20
- processUpdates();
21
20
  else
22
21
  window_1.wndw.requestAnimationFrame(processUpdates);
23
22
  }
@@ -70,8 +69,11 @@ exports.DOMNodeConfig = {
70
69
  remove(target) {
71
70
  target && target.parentNode && target.parentNode.removeChild(target);
72
71
  },
72
+ createTextAssignment(target) {
73
+ return (0, createPropertyAssignment_1.CreateNodeValueAssignment)(target);
74
+ },
73
75
  setText(target, text) {
74
- target.textContent = text;
76
+ target.nodeValue = text;
75
77
  },
76
78
  getAttribute(target, attribute) {
77
79
  return target.getAttribute(attribute);
@@ -80,12 +82,10 @@ exports.DOMNodeConfig = {
80
82
  target.setAttribute(attribute, value);
81
83
  },
82
84
  createPropertyAssignment(target) {
83
- if (target.nodeType === Node.TEXT_NODE)
84
- return (0, createPropertyAssignment_1.CreateNodeValueAssignment)(target);
85
- return (0, createPropertyAssignment_1.CreatePropertyAssignment)(target);
85
+ return (0, createPropertyAssignment_2.CreatePropertyAssignment)(target);
86
86
  },
87
87
  createEventAssignment(target) {
88
- return (0, createEventAssignment_1.CreateEventAssignment)(target);
88
+ return (0, createAssignment_1.CreateAssignment)(target, createEventAssignment_1.CreateEventAssignment);
89
89
  },
90
90
  fireEvent(target, event, data) {
91
91
  var cEvent = new CustomEvent(event, data);
package/Node/boundNode.js CHANGED
@@ -5,36 +5,97 @@ const nodeConfig_1 = require("./nodeConfig");
5
5
  const observableScope_1 = require("../Store/Tree/observableScope");
6
6
  var BoundNode;
7
7
  (function (BoundNode) {
8
+ function WrapEventObject(node, events) {
9
+ const keys = Object.keys(events);
10
+ const ret = {};
11
+ for (let x = 0; x < keys.length; x++) {
12
+ const event = keys[x];
13
+ const eventFunc = events[event];
14
+ ret[event] = function (...args) {
15
+ if (node.destroyed)
16
+ return;
17
+ return eventFunc(...args);
18
+ };
19
+ }
20
+ return ret;
21
+ }
8
22
  function Init(boundNode) {
9
23
  const nodeDef = boundNode.nodeDef;
10
24
  if (nodeDef.props) {
11
- const scope = observableScope_1.ObservableScope.Create(nodeDef.props);
12
- boundNode.scopes ??= [];
13
- boundNode.scopes.push(scope);
14
25
  boundNode.assignProperties = nodeConfig_1.NodeConfig.createPropertyAssignment(boundNode.node);
15
- observableScope_1.ObservableScope.Watch(scope, function (scope) { ScheduleSetProperties(boundNode, scope); });
16
- const next = observableScope_1.ObservableScope.Value(scope);
17
- boundNode.assignProperties(next);
26
+ if (typeof nodeDef.props === 'function') {
27
+ const scope = observableScope_1.ObservableScope.Create(nodeDef.props);
28
+ boundNode.scopes ??= [];
29
+ boundNode.scopes.push(scope);
30
+ observableScope_1.ObservableScope.Watch(scope, function (scope) { ScheduleSetProperties(boundNode, scope); });
31
+ const next = observableScope_1.ObservableScope.Value(scope);
32
+ boundNode.assignProperties(next);
33
+ }
34
+ else {
35
+ boundNode.assignProperties(nodeDef.props);
36
+ boundNode.assignProperties = null;
37
+ }
18
38
  }
19
39
  if (nodeDef.attrs) {
20
- const scope = observableScope_1.ObservableScope.Create(nodeDef.attrs);
21
- boundNode.scopes ??= [];
22
- boundNode.scopes.push(scope);
23
- observableScope_1.ObservableScope.Watch(scope, function (scope) { ScheduleSetAttributes(boundNode, scope); });
24
- SetAttributes(boundNode, observableScope_1.ObservableScope.Value(scope));
40
+ if (typeof nodeDef.attrs === 'function') {
41
+ const scope = observableScope_1.ObservableScope.Create(nodeDef.attrs);
42
+ boundNode.scopes ??= [];
43
+ boundNode.scopes.push(scope);
44
+ observableScope_1.ObservableScope.Watch(scope, function (scope) { ScheduleSetAttributes(boundNode, scope); });
45
+ SetAttributes(boundNode, observableScope_1.ObservableScope.Value(scope));
46
+ }
47
+ else
48
+ SetAttributes(boundNode, nodeDef.attrs);
25
49
  }
26
50
  if (nodeDef.on) {
27
- const scope = observableScope_1.ObservableScope.Create(nodeDef.on);
28
- boundNode.scopes ??= [];
29
- boundNode.scopes.push(scope);
30
51
  boundNode.assignEvents = nodeConfig_1.NodeConfig.createEventAssignment(boundNode.node);
31
- observableScope_1.ObservableScope.Watch(scope, function (scope) { ScheduleSetEvents(boundNode, scope); });
32
- const next = observableScope_1.ObservableScope.Value(scope);
33
- boundNode.assignEvents(next);
52
+ if (typeof nodeDef.on === 'function') {
53
+ const scope = observableScope_1.ObservableScope.Create(nodeDef.on);
54
+ const eventScope = observableScope_1.ObservableScope.Create(function () {
55
+ const events = observableScope_1.ObservableScope.Value(scope);
56
+ return WrapEventObject(boundNode, events);
57
+ });
58
+ boundNode.scopes ??= [];
59
+ boundNode.scopes.push(scope, eventScope);
60
+ observableScope_1.ObservableScope.Watch(eventScope, function (scope) { ScheduleSetEvents(boundNode, scope); });
61
+ const next = observableScope_1.ObservableScope.Value(eventScope);
62
+ boundNode.assignEvents(next);
63
+ }
64
+ else {
65
+ boundNode.assignEvents(WrapEventObject(boundNode, nodeDef.on));
66
+ boundNode.assignEvents = null;
67
+ }
68
+ }
69
+ if (nodeDef.text) {
70
+ boundNode.assignText = nodeConfig_1.NodeConfig.createTextAssignment(boundNode.node);
71
+ if (typeof nodeDef.text === 'function') {
72
+ const scope = observableScope_1.ObservableScope.Create(nodeDef.text);
73
+ boundNode.scopes ??= [];
74
+ boundNode.scopes.push(scope);
75
+ observableScope_1.ObservableScope.Watch(scope, function (scope) { ScheduleSetText(boundNode, scope); });
76
+ const next = observableScope_1.ObservableScope.Value(scope);
77
+ boundNode.assignText(next);
78
+ }
79
+ else {
80
+ boundNode.assignText(nodeDef.text);
81
+ boundNode.assignText = null;
82
+ }
34
83
  }
35
84
  }
36
85
  BoundNode.Init = Init;
37
86
  })(BoundNode || (exports.BoundNode = BoundNode = {}));
87
+ function ScheduleSetText(node, scope) {
88
+ if (node.setText)
89
+ return;
90
+ node.setText = true;
91
+ nodeConfig_1.NodeConfig.scheduleUpdate(function () {
92
+ node.setText = false;
93
+ if (node.destroyed)
94
+ return;
95
+ const next = observableScope_1.ObservableScope.Value(scope);
96
+ node.assignText(next);
97
+ });
98
+ }
38
99
  function ScheduleSetProperties(node, scope) {
39
100
  if (node.setProperties)
40
101
  return;
@@ -44,7 +105,7 @@ function ScheduleSetProperties(node, scope) {
44
105
  if (node.destroyed)
45
106
  return;
46
107
  const next = observableScope_1.ObservableScope.Value(scope);
47
- node.assignProperties(next);
108
+ node.assignProperties(next || null);
48
109
  });
49
110
  }
50
111
  function ScheduleSetAttributes(node, scope) {
@@ -13,21 +13,23 @@ export interface NodeDefinition<T = any, E = any> {
13
13
  type: any;
14
14
  namespace: string;
15
15
  props?: FunctionOr<{
16
- [name: string]: any;
16
+ [name: string]: unknown;
17
17
  }>;
18
18
  attrs?: FunctionOr<{
19
19
  [name: string]: string;
20
20
  }>;
21
21
  on?: FunctionOr<NodeRefEvents>;
22
+ text?: FunctionOr<string>;
22
23
  }
23
24
  export interface BoundNodeFunctionParam {
24
25
  props?: FunctionOr<{
25
- [name: string]: any;
26
+ [name: string]: unknown;
26
27
  }>;
27
28
  attrs?: FunctionOr<{
28
29
  [name: string]: string;
29
30
  }>;
30
31
  on?: FunctionOr<NodeRefEvents>;
32
+ text?: FunctionOr<string>;
31
33
  }
32
34
  export interface IBoundNodeBase extends INodeRefBase {
33
35
  nodeDef: BoundNodeFunctionParam;
@@ -41,8 +43,12 @@ export interface IBoundNodeBase extends INodeRefBase {
41
43
  [event: string]: (event: Event) => void;
42
44
  }): void;
43
45
  };
46
+ assignText: {
47
+ (next: string): void;
48
+ };
44
49
  setAttributes: boolean;
45
50
  setEvents: boolean;
51
+ setText: boolean;
46
52
  }
47
53
  export interface IBoundNode extends IBoundNodeBase {
48
54
  type: NodeRefType.BoundNode;
@@ -12,9 +12,7 @@ export declare class Component<D = void, T = void, E = void> {
12
12
  protected get Data(): D;
13
13
  protected get NodeRef(): INodeRefBase;
14
14
  protected get Templates(): T;
15
- constructor(data: D | {
16
- (): D | Promise<D>;
17
- }, templates: T, nodeRef: INodeRefBase, componentEvents: ComponentNodeEvents<E>);
15
+ constructor(data: D | (() => (D | Promise<D>)), templates: T, nodeRef: INodeRefBase, componentEvents: ComponentNodeEvents<E>);
18
16
  Template(): NodeRefTypes | NodeRefTypes[];
19
17
  Bound(): void;
20
18
  Fire<P extends keyof E>(event: P, data?: E[P]): void;
package/Node/component.js CHANGED
@@ -27,7 +27,10 @@ class Component {
27
27
  constructor(data, templates, nodeRef, componentEvents) {
28
28
  this.nodeRef = nodeRef;
29
29
  this.componentEvents = componentEvents;
30
- this.scope = new observableScope_1.ObservableScope(data);
30
+ if (typeof data === 'function')
31
+ this.scope = new observableScope_1.ObservableScope(data);
32
+ else
33
+ this.scope = new observableScope_1.ObservableScope(() => data);
31
34
  this.templates = templates || {};
32
35
  }
33
36
  Template() {
@@ -102,13 +102,16 @@ function AddTemplate(node, init) {
102
102
  init: true,
103
103
  nodes
104
104
  });
105
- if (init)
105
+ if (init) {
106
106
  nodeRef_1.NodeRef.ReconcileChildren(node, list);
107
+ list_1.List.Clear(list);
108
+ }
107
109
  else
108
110
  nodeConfig_1.NodeConfig.scheduleUpdate(function () {
109
111
  if (node.destroyed)
110
112
  return;
111
113
  nodeRef_1.NodeRef.ReconcileChildren(node, list);
114
+ list_1.List.Clear(list);
112
115
  });
113
116
  });
114
117
  if (node.component.Bound !== component_1.Component.prototype.Bound)
@@ -72,6 +72,7 @@ function SetDefaultData(node) {
72
72
  nodes
73
73
  });
74
74
  nodeRef_1.NodeRef.ReconcileChildren(node, defaultNodeList);
75
+ list_1.List.Clear(defaultNodeList);
75
76
  });
76
77
  }
77
78
  });
@@ -155,8 +156,8 @@ function CreateNodeArray(childrenFunc, value) {
155
156
  if (typeof newNodes === "string" || !newNodes) {
156
157
  const textNode = nodeRef_1.NodeRef.Create("text", null, nodeRef_1.NodeRefType.BoundNode);
157
158
  textNode.nodeDef = {
158
- props: function () {
159
- return { nodeValue: childrenFunc(value) };
159
+ text: function () {
160
+ return childrenFunc(value);
160
161
  }
161
162
  };
162
163
  return [textNode];
@@ -18,6 +18,9 @@ export interface INodeConfig {
18
18
  removeChild(root: any, child: any): void;
19
19
  remove(target: any): void;
20
20
  fireEvent(target: any, event: string, data: any): void;
21
+ createTextAssignment(target: any): {
22
+ (next: string): void;
23
+ };
21
24
  createPropertyAssignment(target: any): {
22
25
  (next: any): void;
23
26
  };
package/Node/nodeRef.js CHANGED
@@ -51,8 +51,10 @@ var NodeRef;
51
51
  setProperties: false,
52
52
  assignProperties: null,
53
53
  assignEvents: null,
54
+ assignText: null,
54
55
  setAttributes: false,
55
56
  setEvents: false,
57
+ setText: false,
56
58
  scopes: null
57
59
  };
58
60
  case NodeRefType.ElementNode:
@@ -70,11 +72,13 @@ var NodeRef;
70
72
  setProperties: false,
71
73
  assignProperties: null,
72
74
  assignEvents: null,
75
+ assignText: null,
73
76
  setAttributes: false,
74
77
  setEvents: false,
75
78
  childrenFunc: null,
76
79
  nodeList: null,
77
80
  setData: false,
81
+ setText: false,
78
82
  scopes: null
79
83
  };
80
84
  case NodeRefType.ComponentNode:
@@ -201,9 +205,9 @@ var NodeRef;
201
205
  switch (node.type) {
202
206
  case NodeRefType.ComponentNode:
203
207
  node.component?.Destroy();
204
- case NodeRefType.ElementNode:
205
- node.assignEvents?.(null);
206
208
  case NodeRefType.BoundNode:
209
+ node.assignEvents?.(null);
210
+ case NodeRefType.ElementNode:
207
211
  for (let x = 0; node.scopes && x < node.scopes.length; x++)
208
212
  Store_1.ObservableScope.Destroy(node.scopes[x]);
209
213
  }
@@ -5,7 +5,6 @@ export declare const GET_TO_JSON = "toJSON";
5
5
  export declare namespace ObservableNode {
6
6
  function Create<T>(value: T): T;
7
7
  function Touch(value: unknown): void;
8
- function EnableDiff(value: boolean): void;
9
8
  function ApplyDiff(rootNode: any, diffResult: JsonDiffResult): void;
10
9
  function CreateFactory(alias?: (value: any) => any | undefined): <T>(value: T) => T;
11
10
  }