cx 26.0.14 → 26.1.1

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.
Files changed (263) hide show
  1. package/build/ui/VDOM.d.ts +1 -20
  2. package/build/ui/VDOM.js +1 -3
  3. package/build/ui/Widget.d.ts +1 -1
  4. package/build/ui/Widget.js +0 -5
  5. package/build/ui/app/startAppLoop.js +2 -10
  6. package/build/util/Component.js +5 -0
  7. package/build/util/test/createTestRenderer.d.ts +3 -1
  8. package/build/util/test/createTestRenderer.js +8 -2
  9. package/build/widgets/form/Checkbox.d.ts +0 -1
  10. package/build/widgets/form/Checkbox.js +1 -0
  11. package/build/widgets/form/ColorField.d.ts +0 -1
  12. package/build/widgets/form/ColorField.js +2 -2
  13. package/build/widgets/form/DateTimeField.d.ts +0 -2
  14. package/build/widgets/form/DateTimeField.js +1 -0
  15. package/build/widgets/form/DateTimePicker.d.ts +0 -2
  16. package/build/widgets/form/DateTimePicker.js +1 -0
  17. package/build/widgets/form/Radio.d.ts +0 -1
  18. package/build/widgets/form/Slider.js +9 -1
  19. package/build/widgets/form/Switch.d.ts +0 -1
  20. package/build/widgets/form/Switch.js +1 -1
  21. package/build/widgets/form/Wheel.d.ts +0 -1
  22. package/build/widgets/grid/Grid.d.ts +0 -2
  23. package/build/widgets/grid/GridCellEditor.js +3 -1
  24. package/build/widgets/icons/calendar.js +4 -3
  25. package/build/widgets/icons/check.js +2 -2
  26. package/build/widgets/icons/clear.js +2 -2
  27. package/build/widgets/icons/close.js +2 -2
  28. package/build/widgets/icons/cx.js +2 -2
  29. package/build/widgets/icons/drop-down.js +2 -2
  30. package/build/widgets/icons/file.js +2 -2
  31. package/build/widgets/icons/folder-open.js +2 -2
  32. package/build/widgets/icons/folder.js +2 -2
  33. package/build/widgets/icons/forward.js +2 -2
  34. package/build/widgets/icons/loading.js +2 -2
  35. package/build/widgets/icons/menu.js +2 -2
  36. package/build/widgets/icons/pixel-picker.js +2 -2
  37. package/build/widgets/icons/search.js +2 -2
  38. package/build/widgets/icons/sort-asc.js +2 -2
  39. package/build/widgets/icons/square.js +2 -2
  40. package/build/widgets/overlay/Overlay.d.ts +3 -0
  41. package/build/widgets/overlay/Overlay.js +3 -2
  42. package/dist/manifest.js +867 -867
  43. package/dist/ui.js +4 -18
  44. package/dist/util.js +4 -0
  45. package/dist/widgets.js +395 -319
  46. package/package.json +11 -8
  47. package/src/charts/Chart.ts +108 -108
  48. package/src/core.d.ts +182 -182
  49. package/src/data/Expression.spec.ts +229 -229
  50. package/src/data/Expression.ts +233 -233
  51. package/src/data/Grouper.ts +158 -158
  52. package/src/data/Selector.ts +10 -10
  53. package/src/data/StringTemplate.spec.ts +132 -132
  54. package/src/data/StructuredSelector.ts +146 -146
  55. package/src/data/ZoomIntoPropertyView.spec.ts +64 -64
  56. package/src/data/comparer.ts +78 -78
  57. package/src/data/computable.spec.ts +87 -87
  58. package/src/data/createStructuredSelector.ts +62 -62
  59. package/src/data/getAccessor.spec.ts +11 -11
  60. package/src/data/getAccessor.ts +74 -74
  61. package/src/data/getSelector.spec.ts +43 -43
  62. package/src/data/getSelector.ts +66 -66
  63. package/src/data/ops/filter.spec.ts +35 -35
  64. package/src/data/ops/filter.ts +9 -9
  65. package/src/data/ops/merge.ts +13 -13
  66. package/src/data/ops/removeTreeNodes.spec.ts +37 -37
  67. package/src/data/ops/updateArray.spec.ts +69 -69
  68. package/src/data/ops/updateArray.ts +31 -31
  69. package/src/data/test-types.ts +7 -7
  70. package/src/hooks/invokeCallback.spec.tsx +4 -4
  71. package/src/hooks/resolveCallback.spec.tsx +4 -4
  72. package/src/hooks/store.spec.tsx +15 -15
  73. package/src/hooks/useTrigger.spec.tsx +16 -10
  74. package/src/hooks/useTrigger.ts +26 -26
  75. package/src/index.scss +6 -6
  76. package/src/jsx-runtime.ts +79 -79
  77. package/src/svg/BoundedObject.ts +101 -101
  78. package/src/svg/util/Rect.ts +105 -105
  79. package/src/ui/CSS.ts +87 -87
  80. package/src/ui/CSSHelper.ts +17 -17
  81. package/src/ui/ContentResolver.spec.tsx +31 -29
  82. package/src/ui/Controller.spec.tsx +47 -39
  83. package/src/ui/Culture.ts +159 -159
  84. package/src/ui/Cx.spec.tsx +10 -8
  85. package/src/ui/DataProxy.spec.tsx +18 -18
  86. package/src/ui/Instance.ts +866 -866
  87. package/src/ui/IsolatedScope.spec.tsx +16 -9
  88. package/src/ui/Prop.ts +140 -140
  89. package/src/ui/PureContainer.spec.tsx +20 -18
  90. package/src/ui/RenderingContext.ts +99 -99
  91. package/src/ui/Repeater.spec.tsx +8 -6
  92. package/src/ui/Rescope.spec.tsx +13 -13
  93. package/src/ui/Restate.spec.tsx +31 -27
  94. package/src/ui/StructuredInstanceDataAccessor.ts +32 -32
  95. package/src/ui/VDOM.ts +1 -34
  96. package/src/ui/Widget.tsx +0 -7
  97. package/src/ui/adapter/TreeAdapter.spec.ts +76 -76
  98. package/src/ui/adapter/TreeAdapter.ts +185 -185
  99. package/src/ui/app/History.ts +133 -133
  100. package/src/ui/app/Url.spec.ts +50 -50
  101. package/src/ui/app/startAppLoop.tsx +5 -9
  102. package/src/ui/app/startHotAppLoop.ts +41 -41
  103. package/src/ui/createFunctionalComponent.spec.tsx +20 -18
  104. package/src/ui/layout/ContentPlaceholder.spec.tsx +46 -34
  105. package/src/ui/layout/FirstVisibleChildLayout.spec.tsx +31 -19
  106. package/src/ui/layout/FirstVisibleChildLayout.ts +60 -60
  107. package/src/ui/selection/PropertySelection.ts +87 -87
  108. package/src/util/Component.spec.ts +30 -0
  109. package/src/util/Component.ts +301 -296
  110. package/src/util/Console.ts +13 -13
  111. package/src/util/DOM.ts +88 -88
  112. package/src/util/hasKey.ts +18 -18
  113. package/src/util/index.ts +55 -55
  114. package/src/util/isArray.ts +3 -3
  115. package/src/util/isDefined.ts +3 -3
  116. package/src/util/isString.ts +3 -3
  117. package/src/util/test/createTestRenderer.tsx +9 -2
  118. package/src/widgets/AccessorBindings.spec.tsx +4 -4
  119. package/src/widgets/DocumentTitle.ts +95 -95
  120. package/src/widgets/HtmlElement.spec.tsx +6 -6
  121. package/src/widgets/ReactElementWrapper.spec.tsx +37 -37
  122. package/src/widgets/autoFocus.ts +9 -9
  123. package/src/widgets/cx.ts +63 -63
  124. package/src/widgets/form/Checkbox.tsx +0 -1
  125. package/src/widgets/form/ColorField.tsx +15 -12
  126. package/src/widgets/form/DateTimeField.tsx +0 -2
  127. package/src/widgets/form/DateTimePicker.tsx +0 -2
  128. package/src/widgets/form/Radio.tsx +0 -1
  129. package/src/widgets/form/Slider.tsx +12 -4
  130. package/src/widgets/form/Switch.tsx +2 -3
  131. package/src/widgets/form/ValidationGroup.spec.tsx +12 -12
  132. package/src/widgets/form/Wheel.tsx +0 -1
  133. package/src/widgets/grid/Grid.tsx +0 -1
  134. package/src/widgets/grid/GridCellEditor.tsx +7 -1
  135. package/src/widgets/icons/calendar.tsx +20 -15
  136. package/src/widgets/icons/check.tsx +2 -1
  137. package/src/widgets/icons/clear.tsx +2 -1
  138. package/src/widgets/icons/close.tsx +2 -2
  139. package/src/widgets/icons/cx.tsx +2 -1
  140. package/src/widgets/icons/drop-down.tsx +2 -1
  141. package/src/widgets/icons/file.tsx +2 -1
  142. package/src/widgets/icons/folder-open.tsx +2 -1
  143. package/src/widgets/icons/folder.tsx +2 -1
  144. package/src/widgets/icons/forward.tsx +2 -1
  145. package/src/widgets/icons/loading.tsx +2 -1
  146. package/src/widgets/icons/menu.tsx +2 -1
  147. package/src/widgets/icons/pixel-picker.tsx +2 -2
  148. package/src/widgets/icons/search.tsx +2 -1
  149. package/src/widgets/icons/sort-asc.tsx +2 -1
  150. package/src/widgets/icons/square.tsx +2 -1
  151. package/src/widgets/nav/Route.spec.tsx +2 -2
  152. package/src/widgets/overlay/Overlay.tsx +5 -1
  153. package/src/widgets/overlay/captureMouse.ts +195 -195
  154. package/src/widgets/overlay/createHotPromiseWindowFactory.ts +71 -71
  155. package/src/widgets/overlay/index.d.ts +11 -11
  156. package/src/widgets/overlay/tooltip-ops.ts +173 -173
  157. package/build/data/ArrayElementView.spec.d.ts +0 -1
  158. package/build/data/ArrayElementView.spec.js +0 -81
  159. package/build/data/Binding.spec.d.ts +0 -1
  160. package/build/data/Binding.spec.js +0 -61
  161. package/build/data/Expression.spec.d.ts +0 -1
  162. package/build/data/Expression.spec.js +0 -196
  163. package/build/data/Grouper.spec.d.ts +0 -1
  164. package/build/data/Grouper.spec.js +0 -48
  165. package/build/data/Ref.spec.d.ts +0 -1
  166. package/build/data/Ref.spec.js +0 -72
  167. package/build/data/Store.spec.d.ts +0 -1
  168. package/build/data/Store.spec.js +0 -19
  169. package/build/data/StoreRef.spec.d.ts +0 -1
  170. package/build/data/StoreRef.spec.js +0 -22
  171. package/build/data/StringTemplate.spec.d.ts +0 -1
  172. package/build/data/StringTemplate.spec.js +0 -112
  173. package/build/data/StructuredSelector.spec.d.ts +0 -1
  174. package/build/data/StructuredSelector.spec.js +0 -102
  175. package/build/data/View.spec.d.ts +0 -1
  176. package/build/data/View.spec.js +0 -44
  177. package/build/data/ZoomIntoPropertyView.spec.d.ts +0 -1
  178. package/build/data/ZoomIntoPropertyView.spec.js +0 -54
  179. package/build/data/comparer.spec.d.ts +0 -1
  180. package/build/data/comparer.spec.js +0 -50
  181. package/build/data/computable.spec.d.ts +0 -1
  182. package/build/data/computable.spec.js +0 -56
  183. package/build/data/createAccessorModelProxy.spec.d.ts +0 -1
  184. package/build/data/createAccessorModelProxy.spec.js +0 -30
  185. package/build/data/createStructuredSelector.spec.d.ts +0 -1
  186. package/build/data/createStructuredSelector.spec.js +0 -42
  187. package/build/data/diff/diffs.spec.d.ts +0 -1
  188. package/build/data/diff/diffs.spec.js +0 -45
  189. package/build/data/getAccessor.spec.d.ts +0 -1
  190. package/build/data/getAccessor.spec.js +0 -10
  191. package/build/data/getSelector.spec.d.ts +0 -1
  192. package/build/data/getSelector.spec.js +0 -36
  193. package/build/data/ops/append.spec.d.ts +0 -1
  194. package/build/data/ops/append.spec.js +0 -24
  195. package/build/data/ops/filter.spec.d.ts +0 -1
  196. package/build/data/ops/filter.spec.js +0 -25
  197. package/build/data/ops/findTreeNode.spec.d.ts +0 -1
  198. package/build/data/ops/findTreeNode.spec.js +0 -20
  199. package/build/data/ops/merge.spec.d.ts +0 -1
  200. package/build/data/ops/merge.spec.js +0 -23
  201. package/build/data/ops/removeTreeNodes.spec.d.ts +0 -1
  202. package/build/data/ops/removeTreeNodes.spec.js +0 -35
  203. package/build/data/ops/updateArray.spec.d.ts +0 -1
  204. package/build/data/ops/updateArray.spec.js +0 -33
  205. package/build/data/ops/updateTree.spec.d.ts +0 -1
  206. package/build/data/ops/updateTree.spec.js +0 -44
  207. package/build/hooks/invokeCallback.spec.d.ts +0 -1
  208. package/build/hooks/invokeCallback.spec.js +0 -44
  209. package/build/hooks/resolveCallback.spec.d.ts +0 -1
  210. package/build/hooks/resolveCallback.spec.js +0 -35
  211. package/build/hooks/store.spec.d.ts +0 -1
  212. package/build/hooks/store.spec.js +0 -48
  213. package/build/hooks/useTrigger.spec.d.ts +0 -1
  214. package/build/hooks/useTrigger.spec.js +0 -59
  215. package/build/ui/Controller.spec.d.ts +0 -1
  216. package/build/ui/Controller.spec.js +0 -247
  217. package/build/ui/Cx.spec.d.ts +0 -1
  218. package/build/ui/Cx.spec.js +0 -153
  219. package/build/ui/DataProxy.spec.d.ts +0 -1
  220. package/build/ui/DataProxy.spec.js +0 -208
  221. package/build/ui/IsolatedScope.spec.d.ts +0 -1
  222. package/build/ui/IsolatedScope.spec.js +0 -42
  223. package/build/ui/PureContainer.spec.d.ts +0 -1
  224. package/build/ui/PureContainer.spec.js +0 -149
  225. package/build/ui/Repeater.spec.d.ts +0 -1
  226. package/build/ui/Repeater.spec.js +0 -109
  227. package/build/ui/Rescope.spec.d.ts +0 -1
  228. package/build/ui/Rescope.spec.js +0 -134
  229. package/build/ui/Restate.spec.d.ts +0 -1
  230. package/build/ui/Restate.spec.js +0 -257
  231. package/build/ui/adapter/ArrayAdapter.spec.d.ts +0 -1
  232. package/build/ui/adapter/ArrayAdapter.spec.js +0 -44
  233. package/build/ui/adapter/TreeAdapter.spec.d.ts +0 -1
  234. package/build/ui/adapter/TreeAdapter.spec.js +0 -71
  235. package/build/ui/app/Url.spec.d.ts +0 -1
  236. package/build/ui/app/Url.spec.js +0 -43
  237. package/build/ui/createFunctionalComponent.spec.d.ts +0 -1
  238. package/build/ui/createFunctionalComponent.spec.js +0 -272
  239. package/build/ui/layout/ContentPlaceholder.spec.d.ts +0 -1
  240. package/build/ui/layout/ContentPlaceholder.spec.js +0 -333
  241. package/build/ui/layout/FirstVisibleChildLayout.spec.d.ts +0 -1
  242. package/build/ui/layout/FirstVisibleChildLayout.spec.js +0 -101
  243. package/build/util/Format.spec.d.ts +0 -1
  244. package/build/util/Format.spec.js +0 -58
  245. package/build/util/TraversalStack.spec.d.ts +0 -1
  246. package/build/util/TraversalStack.spec.js +0 -43
  247. package/build/util/date/upperBoundCheck.spec.d.ts +0 -1
  248. package/build/util/date/upperBoundCheck.spec.js +0 -22
  249. package/build/util/getSearchQueryPredicate.spec.d.ts +0 -1
  250. package/build/util/getSearchQueryPredicate.spec.js +0 -33
  251. package/build/util/isValidIdentifierName.spec.d.ts +0 -1
  252. package/build/util/isValidIdentifierName.spec.js +0 -28
  253. package/build/util/routeAppend.spec.d.ts +0 -1
  254. package/build/util/routeAppend.spec.js +0 -14
  255. package/build/widgets/AccessorBindings.spec.d.ts +0 -1
  256. package/build/widgets/AccessorBindings.spec.js +0 -40
  257. package/build/widgets/HtmlElement.spec.d.ts +0 -1
  258. package/build/widgets/HtmlElement.spec.js +0 -38
  259. package/build/widgets/form/ValidationGroup.spec.d.ts +0 -1
  260. package/build/widgets/form/ValidationGroup.spec.js +0 -62
  261. package/build/widgets/nav/Route.spec.d.ts +0 -1
  262. package/build/widgets/nav/Route.spec.js +0 -15
  263. package/dist/manifest.d.ts +0 -1443
@@ -1,233 +1,233 @@
1
- import { computable } from "./computable";
2
- import { Format } from "../util/Format";
3
- import { Binding } from "./Binding";
4
-
5
- import { quoteStr } from "../util/quote";
6
- import { isFunction } from "../util/isFunction";
7
- import { isValidIdentifierName } from "../util/isValidIdentifierName";
8
- import { MemoSelector, Selector } from "./Selector";
9
-
10
- /*
11
- Helper usage example
12
-
13
- Expression.registerHelper('_', _);
14
- let e = Expression.compile('_.min({data})');
15
- */
16
-
17
- let helpers: Record<string, any> = {},
18
- helperNames: string[] = [],
19
- helperValues: any[] = [],
20
- expFatArrows: null | ((body: string) => string) = null;
21
-
22
- function getExpr(expr: Selector): MemoSelector {
23
- if (expr.memoize) return expr as MemoSelector;
24
-
25
- function memoize(): Selector {
26
- let lastValue: any,
27
- lastRunBindings: Record<string, Selector> = {},
28
- lastRunResults: Record<string, any> = {},
29
- getters: Record<string, Selector> = {},
30
- currentData: any,
31
- len = -1;
32
-
33
- let get = function (bindingWithFormat: string) {
34
- let getter = getters[bindingWithFormat];
35
- if (!getter) {
36
- let binding = bindingWithFormat,
37
- format;
38
- let colonIndex = bindingWithFormat.indexOf(":");
39
- if (colonIndex != -1) {
40
- format = Format.parse(bindingWithFormat.substring(colonIndex + 1));
41
- binding = bindingWithFormat.substring(0, colonIndex);
42
- } else {
43
- let nullSeparatorIndex = bindingWithFormat.indexOf(":");
44
- if (nullSeparatorIndex != -1) {
45
- format = Format.parse(bindingWithFormat.substring(nullSeparatorIndex));
46
- binding = bindingWithFormat.substring(0, nullSeparatorIndex - 1);
47
- }
48
- }
49
- let b = Binding.get(binding);
50
- getter = (data) => {
51
- let value = b.value(data);
52
- lastRunBindings[len] = b.value;
53
- lastRunResults[len] = value;
54
- len++;
55
- return value;
56
- };
57
-
58
- if (format) {
59
- let valueGetter = getter;
60
- getter = (data) => format(valueGetter(data));
61
- }
62
-
63
- getters[bindingWithFormat] = getter;
64
- }
65
- return getter(currentData);
66
- };
67
-
68
- return function (data) {
69
- let i = 0;
70
- for (; i < len; i++) if (lastRunBindings[i](data) !== lastRunResults[i]) break;
71
- if (i !== len) {
72
- len = 0;
73
- currentData = data;
74
- lastValue = expr(get);
75
- }
76
- return lastValue;
77
- };
78
- }
79
-
80
- let result: Selector = memoize();
81
- result.memoize = memoize;
82
- return result as MemoSelector;
83
- }
84
-
85
- export function expression(str: string | Selector): MemoSelector {
86
- if (isFunction(str)) return getExpr(str);
87
-
88
- let cache = getExpressionCache();
89
- let r = cache[str];
90
- if (r) return r;
91
-
92
- let quote: string | false = false;
93
-
94
- let termStart = -1,
95
- curlyBrackets = 0,
96
- percentExpression;
97
-
98
- let fb = ["return ("];
99
-
100
- let args: Record<string, string | Selector> = {};
101
- let formats = [];
102
- let subExprCount = 0;
103
- let invalidNameCount = 0;
104
-
105
- for (let i = 0; i < str.length; i++) {
106
- let c = str[i];
107
- switch (c) {
108
- case "{":
109
- if (curlyBrackets > 0 && !quote) curlyBrackets++;
110
- else if (!quote && termStart < 0 && (str[i + 1] != "{" || str[i - 1] == "%")) {
111
- termStart = i + 1;
112
- curlyBrackets = 1;
113
- percentExpression = str[i - 1] == "%";
114
- if (percentExpression) fb.pop(); //%
115
- } else if (termStart < 0 && (quote || str[i - 1] != "{")) fb.push(c);
116
- break;
117
-
118
- case "}":
119
- if (termStart >= 0) {
120
- if (quote) continue;
121
- if (--curlyBrackets == 0) {
122
- let term = str.substring(termStart, i);
123
- let formatStart = 0;
124
- if (term[0] == "[") formatStart = term.indexOf("]");
125
- let colon = term.indexOf(":", formatStart > 0 ? formatStart : 0);
126
- let binding = colon == -1 ? term : term.substring(0, colon);
127
- let format = colon == -1 ? null : term.substring(colon + 1);
128
- if (colon == -1) {
129
- let nullSepIndex = binding.indexOf("|", formatStart);
130
- if (nullSepIndex != -1) {
131
- format = binding.substring(nullSepIndex);
132
- binding = binding.substring(0, nullSepIndex);
133
- }
134
- }
135
- let argName = binding.replace(/\./g, "_");
136
- if (!isValidIdentifierName(argName)) argName = "inv" + ++invalidNameCount;
137
- if (percentExpression || (binding[0] == "[" && binding[binding.length - 1] == "]")) {
138
- argName = `expr${++subExprCount}`;
139
- args[argName] = expression(percentExpression ? binding : binding.substring(1, binding.length - 1));
140
- } else args[argName] = binding;
141
- if (format) {
142
- let formatter = "fmt" + formats.length;
143
- fb.push(formatter, "(", argName, ", ", quoteStr(format), ")");
144
- formats.push(Format.parse(format));
145
- } else fb.push(argName);
146
- termStart = -1;
147
- }
148
- } else fb.push(c);
149
-
150
- break;
151
-
152
- case '"':
153
- case "'":
154
- if (!quote) quote = c;
155
- else if (quote == c) {
156
- let at = i - 1;
157
- let slashCount = 0;
158
- while (at >= 0 && str[at] === "\\") {
159
- slashCount++;
160
- at--;
161
- }
162
- if (slashCount % 2 == 0) quote = false;
163
- }
164
-
165
- if (curlyBrackets == 0) fb.push(c);
166
-
167
- break;
168
-
169
- default:
170
- if (termStart < 0) fb.push(c);
171
- break;
172
- }
173
- }
174
-
175
- fb.push(")");
176
-
177
- let body = fb.join("");
178
-
179
- if (expFatArrows) body = expFatArrows(body);
180
-
181
- //console.log(body);
182
- let keys = Object.keys(args);
183
-
184
- try {
185
- let compute = new Function("fmt", ...formats.map((f, i) => "fmt" + i), ...helperNames, ...keys, body).bind(
186
- Format,
187
- Format.value,
188
- ...formats,
189
- ...helperValues,
190
- );
191
-
192
- let selector = computable(...keys.map((k) => args[k]), compute);
193
- cache[str] = selector;
194
- return selector;
195
- } catch (err) {
196
- throw new Error(`Failed to parse expression: '${str}'. ${err}`);
197
- }
198
- }
199
-
200
- export type GetFunction = (bindingPath: string) => any;
201
- export type SelectorFunction = (get: GetFunction) => any;
202
-
203
- export const Expression = {
204
- get: function (str: string | SelectorFunction): MemoSelector {
205
- return expression(str);
206
- },
207
-
208
- compile: function (str: string | SelectorFunction): Selector {
209
- return this.get(str).memoize();
210
- },
211
-
212
- registerHelper: function (name: string, helper: any) {
213
- helpers[name] = helper;
214
- helperNames = Object.keys(helpers);
215
- helperValues = helperNames.map((n) => helpers[n]);
216
- },
217
- };
218
-
219
- export function plugFatArrowExpansion(impl: (body: string) => string) {
220
- expFatArrows = impl;
221
- }
222
-
223
- export function invalidateExpressionCache() {
224
- expCache = {};
225
- }
226
-
227
- let expCache: Record<string, MemoSelector> = {};
228
-
229
- let getExpressionCache = () => expCache;
230
-
231
- export function setGetExpressionCacheCallback(callback: () => Record<string, MemoSelector>) {
232
- getExpressionCache = callback;
233
- }
1
+ import { computable } from "./computable";
2
+ import { Format } from "../util/Format";
3
+ import { Binding } from "./Binding";
4
+
5
+ import { quoteStr } from "../util/quote";
6
+ import { isFunction } from "../util/isFunction";
7
+ import { isValidIdentifierName } from "../util/isValidIdentifierName";
8
+ import { MemoSelector, Selector } from "./Selector";
9
+
10
+ /*
11
+ Helper usage example
12
+
13
+ Expression.registerHelper('_', _);
14
+ let e = Expression.compile('_.min({data})');
15
+ */
16
+
17
+ let helpers: Record<string, any> = {},
18
+ helperNames: string[] = [],
19
+ helperValues: any[] = [],
20
+ expFatArrows: null | ((body: string) => string) = null;
21
+
22
+ function getExpr(expr: Selector): MemoSelector {
23
+ if (expr.memoize) return expr as MemoSelector;
24
+
25
+ function memoize(): Selector {
26
+ let lastValue: any,
27
+ lastRunBindings: Record<string, Selector> = {},
28
+ lastRunResults: Record<string, any> = {},
29
+ getters: Record<string, Selector> = {},
30
+ currentData: any,
31
+ len = -1;
32
+
33
+ let get = function (bindingWithFormat: string) {
34
+ let getter = getters[bindingWithFormat];
35
+ if (!getter) {
36
+ let binding = bindingWithFormat,
37
+ format;
38
+ let colonIndex = bindingWithFormat.indexOf(":");
39
+ if (colonIndex != -1) {
40
+ format = Format.parse(bindingWithFormat.substring(colonIndex + 1));
41
+ binding = bindingWithFormat.substring(0, colonIndex);
42
+ } else {
43
+ let nullSeparatorIndex = bindingWithFormat.indexOf(":");
44
+ if (nullSeparatorIndex != -1) {
45
+ format = Format.parse(bindingWithFormat.substring(nullSeparatorIndex));
46
+ binding = bindingWithFormat.substring(0, nullSeparatorIndex - 1);
47
+ }
48
+ }
49
+ let b = Binding.get(binding);
50
+ getter = (data) => {
51
+ let value = b.value(data);
52
+ lastRunBindings[len] = b.value;
53
+ lastRunResults[len] = value;
54
+ len++;
55
+ return value;
56
+ };
57
+
58
+ if (format) {
59
+ let valueGetter = getter;
60
+ getter = (data) => format(valueGetter(data));
61
+ }
62
+
63
+ getters[bindingWithFormat] = getter;
64
+ }
65
+ return getter(currentData);
66
+ };
67
+
68
+ return function (data) {
69
+ let i = 0;
70
+ for (; i < len; i++) if (lastRunBindings[i](data) !== lastRunResults[i]) break;
71
+ if (i !== len) {
72
+ len = 0;
73
+ currentData = data;
74
+ lastValue = expr(get);
75
+ }
76
+ return lastValue;
77
+ };
78
+ }
79
+
80
+ let result: Selector = memoize();
81
+ result.memoize = memoize;
82
+ return result as MemoSelector;
83
+ }
84
+
85
+ export function expression(str: string | Selector): MemoSelector {
86
+ if (isFunction(str)) return getExpr(str);
87
+
88
+ let cache = getExpressionCache();
89
+ let r = cache[str];
90
+ if (r) return r;
91
+
92
+ let quote: string | false = false;
93
+
94
+ let termStart = -1,
95
+ curlyBrackets = 0,
96
+ percentExpression;
97
+
98
+ let fb = ["return ("];
99
+
100
+ let args: Record<string, string | Selector> = {};
101
+ let formats = [];
102
+ let subExprCount = 0;
103
+ let invalidNameCount = 0;
104
+
105
+ for (let i = 0; i < str.length; i++) {
106
+ let c = str[i];
107
+ switch (c) {
108
+ case "{":
109
+ if (curlyBrackets > 0 && !quote) curlyBrackets++;
110
+ else if (!quote && termStart < 0 && (str[i + 1] != "{" || str[i - 1] == "%")) {
111
+ termStart = i + 1;
112
+ curlyBrackets = 1;
113
+ percentExpression = str[i - 1] == "%";
114
+ if (percentExpression) fb.pop(); //%
115
+ } else if (termStart < 0 && (quote || str[i - 1] != "{")) fb.push(c);
116
+ break;
117
+
118
+ case "}":
119
+ if (termStart >= 0) {
120
+ if (quote) continue;
121
+ if (--curlyBrackets == 0) {
122
+ let term = str.substring(termStart, i);
123
+ let formatStart = 0;
124
+ if (term[0] == "[") formatStart = term.indexOf("]");
125
+ let colon = term.indexOf(":", formatStart > 0 ? formatStart : 0);
126
+ let binding = colon == -1 ? term : term.substring(0, colon);
127
+ let format = colon == -1 ? null : term.substring(colon + 1);
128
+ if (colon == -1) {
129
+ let nullSepIndex = binding.indexOf("|", formatStart);
130
+ if (nullSepIndex != -1) {
131
+ format = binding.substring(nullSepIndex);
132
+ binding = binding.substring(0, nullSepIndex);
133
+ }
134
+ }
135
+ let argName = binding.replace(/\./g, "_");
136
+ if (!isValidIdentifierName(argName)) argName = "inv" + ++invalidNameCount;
137
+ if (percentExpression || (binding[0] == "[" && binding[binding.length - 1] == "]")) {
138
+ argName = `expr${++subExprCount}`;
139
+ args[argName] = expression(percentExpression ? binding : binding.substring(1, binding.length - 1));
140
+ } else args[argName] = binding;
141
+ if (format) {
142
+ let formatter = "fmt" + formats.length;
143
+ fb.push(formatter, "(", argName, ", ", quoteStr(format), ")");
144
+ formats.push(Format.parse(format));
145
+ } else fb.push(argName);
146
+ termStart = -1;
147
+ }
148
+ } else fb.push(c);
149
+
150
+ break;
151
+
152
+ case '"':
153
+ case "'":
154
+ if (!quote) quote = c;
155
+ else if (quote == c) {
156
+ let at = i - 1;
157
+ let slashCount = 0;
158
+ while (at >= 0 && str[at] === "\\") {
159
+ slashCount++;
160
+ at--;
161
+ }
162
+ if (slashCount % 2 == 0) quote = false;
163
+ }
164
+
165
+ if (curlyBrackets == 0) fb.push(c);
166
+
167
+ break;
168
+
169
+ default:
170
+ if (termStart < 0) fb.push(c);
171
+ break;
172
+ }
173
+ }
174
+
175
+ fb.push(")");
176
+
177
+ let body = fb.join("");
178
+
179
+ if (expFatArrows) body = expFatArrows(body);
180
+
181
+ //console.log(body);
182
+ let keys = Object.keys(args);
183
+
184
+ try {
185
+ let compute = new Function("fmt", ...formats.map((f, i) => "fmt" + i), ...helperNames, ...keys, body).bind(
186
+ Format,
187
+ Format.value,
188
+ ...formats,
189
+ ...helperValues,
190
+ );
191
+
192
+ let selector = computable(...keys.map((k) => args[k]), compute);
193
+ cache[str] = selector;
194
+ return selector;
195
+ } catch (err) {
196
+ throw new Error(`Failed to parse expression: '${str}'. ${err}`);
197
+ }
198
+ }
199
+
200
+ export type GetFunction = (bindingPath: string) => any;
201
+ export type SelectorFunction = (get: GetFunction) => any;
202
+
203
+ export const Expression = {
204
+ get: function (str: string | SelectorFunction): MemoSelector {
205
+ return expression(str);
206
+ },
207
+
208
+ compile: function (str: string | SelectorFunction): Selector {
209
+ return this.get(str).memoize();
210
+ },
211
+
212
+ registerHelper: function (name: string, helper: any) {
213
+ helpers[name] = helper;
214
+ helperNames = Object.keys(helpers);
215
+ helperValues = helperNames.map((n) => helpers[n]);
216
+ },
217
+ };
218
+
219
+ export function plugFatArrowExpansion(impl: (body: string) => string) {
220
+ expFatArrows = impl;
221
+ }
222
+
223
+ export function invalidateExpressionCache() {
224
+ expCache = {};
225
+ }
226
+
227
+ let expCache: Record<string, MemoSelector> = {};
228
+
229
+ let getExpressionCache = () => expCache;
230
+
231
+ export function setGetExpressionCacheCallback(callback: () => Record<string, MemoSelector>) {
232
+ getExpressionCache = callback;
233
+ }