cx 25.2.0 → 25.2.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 (107) hide show
  1. package/dist/manifest.js +616 -616
  2. package/dist/widgets.js +8 -2
  3. package/package.json +32 -32
  4. package/src/charts/Legend.js +167 -167
  5. package/src/charts/Legend.scss +40 -40
  6. package/src/charts/LegendEntry.js +128 -128
  7. package/src/charts/LegendEntry.scss +27 -27
  8. package/src/charts/PieChart.d.ts +92 -92
  9. package/src/charts/PieChart.js +529 -529
  10. package/src/charts/axis/Axis.d.ts +113 -113
  11. package/src/charts/axis/Axis.js +280 -280
  12. package/src/charts/axis/CategoryAxis.d.ts +30 -30
  13. package/src/charts/axis/CategoryAxis.js +241 -241
  14. package/src/charts/axis/NumericAxis.js +351 -351
  15. package/src/charts/axis/Stack.js +55 -55
  16. package/src/charts/axis/TimeAxis.d.ts +28 -28
  17. package/src/charts/axis/TimeAxis.js +611 -611
  18. package/src/charts/helpers/PointReducer.js +47 -47
  19. package/src/charts/helpers/SnapPointFinder.js +69 -69
  20. package/src/data/Binding.spec.js +69 -69
  21. package/src/data/Expression.js +229 -229
  22. package/src/data/Expression.spec.js +229 -229
  23. package/src/data/StringTemplate.js +92 -92
  24. package/src/data/StringTemplate.spec.js +132 -132
  25. package/src/data/StructuredSelector.js +132 -132
  26. package/src/data/getAccessor.spec.js +11 -11
  27. package/src/data/getSelector.js +49 -49
  28. package/src/hooks/createLocalStorageRef.d.ts +3 -3
  29. package/src/hooks/createLocalStorageRef.js +20 -20
  30. package/src/index.scss +6 -6
  31. package/src/ui/Culture.d.ts +57 -57
  32. package/src/ui/Culture.js +139 -139
  33. package/src/ui/FocusManager.js +171 -171
  34. package/src/ui/Format.js +108 -108
  35. package/src/ui/HoverSync.js +147 -147
  36. package/src/ui/Instance.d.ts +72 -72
  37. package/src/ui/Instance.js +614 -614
  38. package/src/ui/Repeater.d.ts +61 -61
  39. package/src/ui/index.d.ts +42 -42
  40. package/src/ui/layout/ContentPlaceholder.d.ts +19 -19
  41. package/src/ui/layout/ContentPlaceholder.js +105 -105
  42. package/src/ui/layout/ContentPlaceholder.spec.js +579 -579
  43. package/src/ui/layout/LabelsTopLayout.js +134 -134
  44. package/src/util/Format.js +270 -270
  45. package/src/util/date/encodeDate.d.ts +1 -1
  46. package/src/util/date/encodeDate.js +8 -8
  47. package/src/util/date/encodeDateWithTimezoneOffset.d.ts +1 -1
  48. package/src/util/date/index.d.ts +11 -11
  49. package/src/util/date/index.js +11 -11
  50. package/src/util/date/parseDateInvariant.d.ts +3 -3
  51. package/src/util/date/parseDateInvariant.js +20 -20
  52. package/src/util/getSearchQueryPredicate.js +59 -59
  53. package/src/util/index.d.ts +51 -51
  54. package/src/util/index.js +54 -54
  55. package/src/util/isValidIdentifierName.d.ts +1 -1
  56. package/src/util/isValidIdentifierName.js +5 -5
  57. package/src/util/isValidIdentifierName.spec.js +33 -33
  58. package/src/util/scss/add-rules.scss +38 -38
  59. package/src/widgets/CxCredit.scss +37 -37
  60. package/src/widgets/HighlightedSearchText.js +36 -36
  61. package/src/widgets/HighlightedSearchText.scss +18 -18
  62. package/src/widgets/List.scss +91 -91
  63. package/src/widgets/drag-drop/DropZone.js +214 -214
  64. package/src/widgets/form/Calendar.js +618 -618
  65. package/src/widgets/form/Calendar.scss +196 -196
  66. package/src/widgets/form/Checkbox.scss +127 -127
  67. package/src/widgets/form/ColorField.js +397 -397
  68. package/src/widgets/form/ColorField.scss +96 -96
  69. package/src/widgets/form/ColorPicker.scss +283 -283
  70. package/src/widgets/form/DateTimeField.js +576 -576
  71. package/src/widgets/form/DateTimePicker.js +392 -392
  72. package/src/widgets/form/LookupField.d.ts +179 -179
  73. package/src/widgets/form/LookupField.scss +219 -219
  74. package/src/widgets/form/MonthField.d.ts +99 -99
  75. package/src/widgets/form/MonthField.js +523 -523
  76. package/src/widgets/form/MonthPicker.d.ts +76 -76
  77. package/src/widgets/form/MonthPicker.js +641 -641
  78. package/src/widgets/form/MonthPicker.scss +118 -118
  79. package/src/widgets/form/NumberField.js +459 -459
  80. package/src/widgets/form/NumberField.scss +61 -61
  81. package/src/widgets/form/Radio.scss +121 -121
  82. package/src/widgets/form/Select.scss +99 -99
  83. package/src/widgets/form/Slider.scss +118 -118
  84. package/src/widgets/form/Switch.scss +140 -140
  85. package/src/widgets/form/TextArea.scss +43 -43
  86. package/src/widgets/form/TextField.js +290 -290
  87. package/src/widgets/form/TextField.scss +55 -55
  88. package/src/widgets/form/UploadButton.d.ts +34 -34
  89. package/src/widgets/form/variables.scss +353 -353
  90. package/src/widgets/grid/Grid.d.ts +442 -442
  91. package/src/widgets/grid/Grid.js +3414 -3414
  92. package/src/widgets/grid/GridRow.js +228 -228
  93. package/src/widgets/grid/TreeNode.d.ts +23 -23
  94. package/src/widgets/grid/TreeNode.scss +88 -88
  95. package/src/widgets/grid/variables.scss +133 -133
  96. package/src/widgets/nav/Menu.scss +74 -74
  97. package/src/widgets/overlay/Dropdown.js +612 -612
  98. package/src/widgets/overlay/FlyweightTooltipTracker.js +39 -39
  99. package/src/widgets/overlay/Overlay.d.ts +73 -73
  100. package/src/widgets/overlay/Tooltip.js +308 -303
  101. package/src/widgets/overlay/Window.js +202 -202
  102. package/src/widgets/overlay/captureMouse.js +124 -124
  103. package/src/widgets/overlay/createHotPromiseWindowFactory.d.ts +18 -18
  104. package/src/widgets/overlay/createHotPromiseWindowFactory.js +56 -56
  105. package/src/widgets/overlay/index.d.ts +11 -11
  106. package/src/widgets/overlay/index.js +11 -11
  107. package/src/widgets/variables.scss +144 -144
@@ -1,614 +1,614 @@
1
- import { Controller } from "./Controller";
2
- import { debug, renderFlag, processDataFlag, destroyFlag } from "../util/Debug";
3
- import { GlobalCacheIdentifier } from "../util/GlobalCacheIdentifier";
4
- import { throttle } from "../util/throttle";
5
- import { validatedDebounce } from "../util/validatedDebounce";
6
- import { batchUpdates } from "./batchUpdates";
7
- import { isString } from "../util/isString";
8
- import { isFunction } from "../util/isFunction";
9
- import { isDefined } from "../util/isDefined";
10
- import { isArray } from "../util/isArray";
11
- import { isObject } from "../util/isObject";
12
- import { isNonEmptyArray } from "../util/isNonEmptyArray";
13
- import { isUndefined } from "../util/isUndefined";
14
- import { isAccessorChain } from "../data/createAccessorModelProxy";
15
-
16
- let instanceId = 1000;
17
-
18
- export class Instance {
19
- constructor(widget, key, parent, store) {
20
- this.widget = widget;
21
- this.key = key;
22
- this.id = String(++instanceId);
23
- this.cached = {};
24
- this.parent = parent;
25
- this.store = store;
26
- }
27
-
28
- setStore(store) {
29
- this.store = store;
30
- }
31
-
32
- init(context) {
33
- //widget is initialized when first instance is initialized
34
- if (!this.widget.initialized) {
35
- this.widget.init(context);
36
- this.widget.initialized = true;
37
- }
38
-
39
- if (!this.dataSelector) {
40
- this.widget.selector.init(this.store);
41
- this.dataSelector = this.widget.selector.createStoreSelector();
42
- }
43
-
44
- //init instance might change the store, so it must go before the controller
45
- this.widget.initInstance(context, this);
46
- this.widget.initState(context, this);
47
-
48
- if (this.widget.controller)
49
- this.controller = Controller.create(this.widget.controller, {
50
- widget: this.widget,
51
- instance: this,
52
- store: this.store,
53
- });
54
-
55
- if (
56
- this.widget.exploreCleanup ||
57
- this.widget.outerLayout ||
58
- this.widget.isContent ||
59
- this.widget.controller ||
60
- this.widget.prepareCleanup
61
- )
62
- this.needsExploreCleanup = true;
63
- if (this.widget.prepare || this.widget.controller) this.needsPrepare = true;
64
- if (this.widget.cleanup || this.widget.controller) this.needsCleanup = true;
65
- this.initialized = true;
66
- }
67
-
68
- checkVisible(context) {
69
- if (!this.initialized) this.init(context);
70
-
71
- let wasVisible = this.visible;
72
- this.rawData = this.dataSelector(this.store);
73
- this.visible = this.widget.checkVisible(context, this, this.rawData);
74
- if (this.visible && !this.detached) this.parent.instanceCache.addChild(this);
75
- this.explored = false;
76
- this.prepared = false;
77
-
78
- if (!this.visible && wasVisible) this.destroy();
79
-
80
- return this.visible;
81
- }
82
-
83
- scheduleExploreIfVisible(context) {
84
- if (this.checkVisible(context)) {
85
- context.exploreStack.push(this);
86
-
87
- if (this.needsExploreCleanup) context.exploreStack.push(this);
88
-
89
- return true;
90
- }
91
- return false;
92
- }
93
-
94
- cache(key, value) {
95
- let oldValue = this.cached[key];
96
- if (oldValue === value) return false;
97
-
98
- if (!this.cacheList) this.cacheList = {};
99
- this.cacheList[key] = value;
100
- return true;
101
- }
102
-
103
- markShouldUpdate(context) {
104
- let ins = this;
105
- let renderList = this.renderList;
106
- renderList.markReverseIndex();
107
-
108
- //notify all parents that child state changed to bust up caching
109
- while (ins && !ins.shouldUpdate && ins.explored) {
110
- if (ins.renderList !== renderList) {
111
- renderList.reverse();
112
- renderList = ins.renderList;
113
- renderList.markReverseIndex();
114
- }
115
- ins.shouldUpdate = true;
116
- renderList.data.push(ins);
117
- ins = ins.widget.isContent
118
- ? ins.contentPlaceholder
119
- : ins.parent.outerLayout === ins
120
- ? ins.parent.parent
121
- : ins.parent;
122
- }
123
- renderList.reverse();
124
- }
125
-
126
- explore(context) {
127
- if (!this.visible) throw new Error("Explore invisible!");
128
-
129
- if (this.explored) {
130
- if (this.widget.prepareCleanup) context.prepareList.push(this);
131
-
132
- if (this.widget.exploreCleanup) this.widget.exploreCleanup(context, this);
133
-
134
- if (this.parent.outerLayout === this) context.popNamedValue("content", "body");
135
-
136
- if (this.widget.controller) context.pop("controller");
137
-
138
- return;
139
- }
140
-
141
- this.explored = true;
142
-
143
- if (this.needsPrepare) context.prepareList.push(this);
144
- else this.prepared = true;
145
-
146
- if (this.needsCleanup) context.cleanupList.push(this);
147
-
148
- if (this.instanceCache) this.instanceCache.mark();
149
-
150
- //controller may reconfigure the widget and need to go before shouldUpdate calculation
151
- this.parentOptions = context.parentOptions;
152
-
153
- if (!this.controller) {
154
- if (context.controller) this.controller = context.controller;
155
- else if (this.parent.controller) this.controller = this.parent.controller;
156
- }
157
-
158
- this.destroyTracked = false;
159
-
160
- if (this.controller) {
161
- if (this.widget.controller) {
162
- if (!this.controller.initialized) {
163
- this.controller.init(context);
164
- this.controller.initialized = true;
165
- }
166
- context.push("controller", this.controller);
167
- this.controller.explore(context);
168
- if (this.controller.onDestroy && this.controller.widget == this.widget) this.trackDestroy();
169
- }
170
- }
171
-
172
- if (this.widget.onDestroy || isNonEmptyArray(this.destroySubscriptions)) this.trackDestroy();
173
-
174
- this.renderList = this.assignedRenderList || this.parent.renderList || context.getRootRenderList();
175
-
176
- let shouldUpdate =
177
- this.rawData !== this.cached.rawData ||
178
- this.state !== this.cached.state ||
179
- this.widget.version !== this.cached.widgetVersion ||
180
- this.cached.globalCacheIdentifier !== GlobalCacheIdentifier.get();
181
-
182
- if (shouldUpdate) {
183
- this.data = { ...this.rawData };
184
- this.widget.prepareData(context, this);
185
- debug(processDataFlag, this.widget);
186
- }
187
-
188
- //onExplore might set the outer layout
189
- if (this.widget.onExplore) this.widget.onExplore(context, this);
190
-
191
- if (this.parent.outerLayout === this) {
192
- this.renderList = this.renderList.insertRight();
193
- context.pushNamedValue("content", "body", this.parent);
194
- }
195
-
196
- if (this.widget.outerLayout) {
197
- this.outerLayout = this.getChild(context, this.widget.outerLayout, null, this.store);
198
- this.outerLayout.scheduleExploreIfVisible(context);
199
- this.renderList = this.renderList.insertLeft();
200
- }
201
-
202
- if (this.widget.isContent) {
203
- this.contentPlaceholder = context.contentPlaceholder && context.contentPlaceholder[this.widget.putInto];
204
- if (this.contentPlaceholder) context.contentPlaceholder[this.widget.putInto](this);
205
- else {
206
- this.renderList = this.renderList.insertLeft();
207
- context.pushNamedValue("content", this.widget.putInto, this);
208
- if (!context.contentList) context.contentList = {};
209
- let list = context.contentList[this.widget.putInto];
210
- if (!list) list = context.contentList[this.widget.putInto] = [];
211
- list.push(this);
212
- }
213
- }
214
-
215
- this.shouldUpdate = false;
216
- if (shouldUpdate || this.childStateDirty || !this.widget.memoize) this.markShouldUpdate(context);
217
-
218
- context.exploreStack.hop();
219
-
220
- if (this.widget.helpers) {
221
- this.helpers = {};
222
- for (let cmp in this.widget.helpers) {
223
- let helper = this.widget.helpers[cmp];
224
- if (helper) {
225
- let ins = this.getChild(context, helper);
226
- if (ins.scheduleExploreIfVisible(context)) this.helpers[cmp] = ins;
227
- }
228
- }
229
- }
230
-
231
- this.widget.explore(context, this, this.data);
232
- }
233
-
234
- prepare(context) {
235
- if (!this.visible) throw new Error("Prepare invisible!");
236
-
237
- if (this.prepared) {
238
- if (this.widget.prepareCleanup) this.widget.prepareCleanup(context, this);
239
- return;
240
- }
241
-
242
- this.prepared = true;
243
- if (this.widget.prepare) this.widget.prepare(context, this);
244
-
245
- if (this.widget.controller && this.controller.prepare) this.controller.prepare(context);
246
- }
247
-
248
- render(context) {
249
- if (!this.visible) throw new Error("Render invisible!");
250
-
251
- if (this.shouldUpdate) {
252
- debug(renderFlag, this.widget, this.key);
253
- let vdom = renderResultFix(this.widget.render(context, this, this.key));
254
- if (this.widget.isContent || this.outerLayout) this.contentVDOM = vdom;
255
- else this.vdom = vdom;
256
- }
257
-
258
- if (this.cacheList) for (let key in this.cacheList) this.cached[key] = this.cacheList[key];
259
-
260
- this.cacheList = null;
261
-
262
- this.cached.rawData = this.rawData;
263
- this.cached.data = this.data;
264
- this.cached.state = this.state;
265
- this.cached.widgetVersion = this.widget.version;
266
- this.cached.globalCacheIdentifier = GlobalCacheIdentifier.get();
267
- this.childStateDirty = false;
268
-
269
- if (this.instanceCache) this.instanceCache.sweep();
270
-
271
- if (this.parent.outerLayout === this) {
272
- //if outer layouts are chained we need to find the originating element (last element with OL set)
273
- let parent = this.parent;
274
- while (parent.parent.outerLayout == parent) parent = parent.parent;
275
- parent.vdom = this.vdom;
276
- }
277
-
278
- return this.vdom;
279
- }
280
-
281
- cleanup(context) {
282
- if (this.widget.controller && this.controller.cleanup) this.controller.cleanup(context);
283
-
284
- if (this.widget.cleanup) this.widget.cleanup(context, this);
285
- }
286
-
287
- trackDestroy() {
288
- if (!this.destroyTracked) {
289
- this.destroyTracked = true;
290
- if (this.parent && !this.detached) this.parent.trackDestroyableChild(this);
291
- }
292
- }
293
-
294
- trackDestroyableChild(child) {
295
- this.instanceCache.trackDestroy(child);
296
- this.trackDestroy();
297
- }
298
-
299
- subscribeOnDestroy(callback) {
300
- if (!this.destroySubscriptions) this.destroySubscriptions = [];
301
- this.destroySubscriptions.push(callback);
302
- this.trackDestroy();
303
- return () => {
304
- this.destroySubscriptions && this.destroySubscriptions.filter((cb) => cb != callback);
305
- };
306
- }
307
-
308
- destroy() {
309
- if (this.instanceCache) {
310
- this.instanceCache.destroy();
311
- this.instanceCache = null;
312
- }
313
-
314
- if (this.destroySubscriptions) {
315
- this.destroySubscriptions.forEach((cb) => cb());
316
- this.destroySubscriptions = null;
317
- }
318
-
319
- if (this.destroyTracked) {
320
- debug(destroyFlag, this);
321
-
322
- if (this.widget.onDestroy) this.widget.onDestroy(this);
323
-
324
- if (
325
- this.widget.controller &&
326
- this.controller &&
327
- this.controller.onDestroy &&
328
- this.controller.widget == this.widget
329
- )
330
- this.controller.onDestroy();
331
-
332
- this.destroyTracked = false;
333
- }
334
- }
335
-
336
- setState(state) {
337
- let skip = !!this.state;
338
- if (this.state)
339
- for (let k in state) {
340
- if (this.state[k] !== state[k]) {
341
- skip = false;
342
- break;
343
- }
344
- }
345
-
346
- if (skip) return;
347
-
348
- this.state = Object.assign({}, this.state, state);
349
- let parent = this.parent;
350
- //notify all parents that child state change to bust up caching
351
- while (parent) {
352
- parent.childStateDirty = true;
353
- parent = parent.parent;
354
- }
355
- batchUpdates(() => {
356
- this.store.notify();
357
- });
358
- }
359
-
360
- set(prop, value, options = {}) {
361
- //skip re-rendering (used for reading state from uncontrolled components)
362
- if (options.internal && this.rawData) {
363
- this.rawData[prop] = value;
364
- this.data[prop] = value;
365
- }
366
-
367
- let setter = this.setters && this.setters[prop];
368
- if (setter) {
369
- if (options.immediate && isFunction(setter.reset)) setter.reset(value);
370
- else setter(value);
371
- return true;
372
- }
373
-
374
- let p = this.widget[prop];
375
- if (p && typeof p == "object") {
376
- if (p.debounce) {
377
- this.definePropertySetter(
378
- prop,
379
- validatedDebounce(
380
- (value) => this.doSet(prop, value),
381
- () => this.dataSelector(this.store)[prop],
382
- p.debounce,
383
- ),
384
- );
385
- this.set(prop, value, options);
386
- return true;
387
- }
388
-
389
- if (p.throttle) {
390
- this.definePropertySetter(
391
- prop,
392
- throttle((value) => this.doSet(prop, value), p.throttle),
393
- );
394
- this.set(prop, value, options);
395
- return true;
396
- }
397
- }
398
-
399
- return this.doSet(prop, value);
400
- }
401
-
402
- definePropertySetter(prop, setter) {
403
- if (!this.setters) this.setters = {};
404
- this.setters[prop] = setter;
405
- }
406
-
407
- doSet(prop, value) {
408
- let changed = false;
409
- batchUpdates(() => {
410
- let p = this.widget[prop];
411
- if (isObject(p)) {
412
- if (p.set) {
413
- if (isFunction(p.set)) {
414
- p.set(value, this);
415
- changed = true;
416
- } else if (isString(p.set)) {
417
- this.controller[p.set](value, this);
418
- changed = true;
419
- }
420
- } else if (p.action) {
421
- let action = p.action(value, this);
422
- this.store.dispatch(action);
423
- changed = true;
424
- } else if (isString(p.bind) || isAccessorChain(p.bind)) {
425
- changed = this.store.set(p.bind, value);
426
- }
427
- } else if (isAccessorChain(p)) changed = this.store.set(p.toString(), value);
428
- });
429
- return changed;
430
- }
431
-
432
- nestedDataSet(key, value, dataConfig, useParentStore) {
433
- let config = dataConfig[key];
434
- if (!config)
435
- throw new Error(`Unknown nested data key ${key}. Known keys are ${Object.keys(dataConfig).join(", ")}.`);
436
-
437
- if (isAccessorChain(config)) config = { bind: config.toString() };
438
-
439
- if (config.bind) {
440
- let store = this.store;
441
- //in case of Rescope or DataProxy, bindings point to the data in the parent store
442
- if (useParentStore && store.store) store = store.store;
443
- return isUndefined(value) ? store.deleteItem(config.bind) : store.setItem(config.bind, value);
444
- }
445
-
446
- if (!config.set)
447
- throw new Error(
448
- `Cannot change nested data value for ${key} as it's read-only. Either define it as a binding or define a set function.`,
449
- );
450
- if (isString(config.set)) this.getControllerMethod(config.set)(value, this);
451
- else if (isFunction(config.set)) config.set(value, this);
452
- else
453
- throw new Error(
454
- `Cannot change nested data value for ${key} the defined setter is neither a function nor a controller method.`,
455
- );
456
-
457
- return true;
458
- }
459
-
460
- replaceState(state) {
461
- this.cached.state = this.state;
462
- this.state = state;
463
- this.store.notify();
464
- }
465
-
466
- getInstanceCache() {
467
- if (!this.instanceCache)
468
- this.instanceCache = new InstanceCache(this, this.widget.isPureContainer ? this.key : null);
469
- return this.instanceCache;
470
- }
471
-
472
- clearChildrenCache() {
473
- if (this.instanceCache) this.instanceCache.destroy();
474
- }
475
-
476
- getChild(context, widget, key, store) {
477
- return this.getInstanceCache().getChild(widget, store || this.store, key);
478
- }
479
-
480
- getDetachedChild(widget, key, store) {
481
- let child = new Instance(widget, key, this, store || this.store);
482
- child.detached = true;
483
- return child;
484
- }
485
-
486
- prepareRenderCleanupChild(widget, store, keyPrefix, options) {
487
- return widget.prepareRenderCleanup(store || this.store, options, keyPrefix, this);
488
- }
489
-
490
- getJsxEventProps() {
491
- let { widget } = this;
492
-
493
- if (!isArray(widget.jsxAttributes)) return null;
494
-
495
- let props = {};
496
- widget.jsxAttributes.forEach((attr) => {
497
- if (attr.indexOf("on") == 0 && attr.length > 2) props[attr] = (e) => this.invoke(attr, e, this);
498
- });
499
- return props;
500
- }
501
-
502
- getCallback(methodName) {
503
- let scope = this.widget;
504
- let callback = scope[methodName];
505
-
506
- if (typeof callback === "string") return this.getControllerMethod(callback);
507
-
508
- if (typeof callback !== "function")
509
- throw new Error(`Cannot invoke callback method ${methodName} as assigned value is not a function.`);
510
-
511
- return callback.bind(scope);
512
- }
513
-
514
- getControllerMethod(methodName) {
515
- if (!this.controller)
516
- throw new Error(
517
- `Cannot invoke controller method "${methodName}" as controller is not assigned to the widget.`,
518
- );
519
-
520
- let at = this;
521
- while (at != null && at.controller && !at.controller[methodName]) at = at.parent;
522
-
523
- if (!at || !at.controller || !at.controller[methodName])
524
- throw new Error(
525
- `Cannot invoke controller method "${methodName}". The method cannot be found in any of the assigned controllers.`,
526
- );
527
-
528
- return at.controller[methodName].bind(at.controller);
529
- }
530
-
531
- invoke(methodName, ...args) {
532
- return this.getCallback(methodName).apply(null, args);
533
- }
534
-
535
- invokeControllerMethod(methodName, ...args) {
536
- return this.getControllerMethod(methodName).apply(null, args);
537
- }
538
- }
539
-
540
- function renderResultFix(res) {
541
- return res != null && isDefined(res.content) ? res : { content: res };
542
- }
543
-
544
- export class InstanceCache {
545
- constructor(parent, keyPrefix) {
546
- this.children = {};
547
- this.parent = parent;
548
- this.marked = {};
549
- this.monitored = null;
550
- this.keyPrefix = keyPrefix != null ? keyPrefix + "-" : "";
551
- }
552
-
553
- getChild(widget, store, key) {
554
- let k = this.keyPrefix + (key != null ? key : widget.vdomKey || widget.widgetId);
555
- let instance = this.children[k];
556
-
557
- if (
558
- !instance ||
559
- instance.widget !== widget ||
560
- (!instance.visible && (instance.widget.controller || instance.widget.onInit))
561
- ) {
562
- instance = new Instance(widget, k, this.parent);
563
- this.children[k] = instance;
564
- }
565
- if (instance.store !== store) {
566
- instance.setStore(store);
567
- if (instance.cached) delete instance.cached.rawData; // force prepareData to execute again
568
- }
569
-
570
- return instance;
571
- }
572
-
573
- addChild(instance) {
574
- this.marked[instance.key] = instance;
575
- }
576
-
577
- mark() {
578
- this.marked = {};
579
- }
580
-
581
- trackDestroy(instance) {
582
- if (!this.monitored) this.monitored = {};
583
- this.monitored[instance.key] = instance;
584
- }
585
-
586
- destroy() {
587
- this.children = {};
588
- this.marked = {};
589
-
590
- if (!this.monitored) return;
591
-
592
- for (let key in this.monitored) {
593
- this.monitored[key].destroy();
594
- }
595
-
596
- this.monitored = null;
597
- }
598
-
599
- sweep() {
600
- this.children = this.marked;
601
- if (!this.monitored) return;
602
- let activeCount = 0;
603
- for (let key in this.monitored) {
604
- let monitoredChild = this.monitored[key];
605
- let child = this.children[key];
606
- if (child !== monitoredChild || !monitoredChild.visible) {
607
- monitoredChild.destroy();
608
- delete this.monitored[key];
609
- if (child === monitoredChild) delete this.children[key];
610
- } else activeCount++;
611
- }
612
- if (activeCount === 0) this.monitored = null;
613
- }
614
- }
1
+ import { Controller } from "./Controller";
2
+ import { debug, renderFlag, processDataFlag, destroyFlag } from "../util/Debug";
3
+ import { GlobalCacheIdentifier } from "../util/GlobalCacheIdentifier";
4
+ import { throttle } from "../util/throttle";
5
+ import { validatedDebounce } from "../util/validatedDebounce";
6
+ import { batchUpdates } from "./batchUpdates";
7
+ import { isString } from "../util/isString";
8
+ import { isFunction } from "../util/isFunction";
9
+ import { isDefined } from "../util/isDefined";
10
+ import { isArray } from "../util/isArray";
11
+ import { isObject } from "../util/isObject";
12
+ import { isNonEmptyArray } from "../util/isNonEmptyArray";
13
+ import { isUndefined } from "../util/isUndefined";
14
+ import { isAccessorChain } from "../data/createAccessorModelProxy";
15
+
16
+ let instanceId = 1000;
17
+
18
+ export class Instance {
19
+ constructor(widget, key, parent, store) {
20
+ this.widget = widget;
21
+ this.key = key;
22
+ this.id = String(++instanceId);
23
+ this.cached = {};
24
+ this.parent = parent;
25
+ this.store = store;
26
+ }
27
+
28
+ setStore(store) {
29
+ this.store = store;
30
+ }
31
+
32
+ init(context) {
33
+ //widget is initialized when first instance is initialized
34
+ if (!this.widget.initialized) {
35
+ this.widget.init(context);
36
+ this.widget.initialized = true;
37
+ }
38
+
39
+ if (!this.dataSelector) {
40
+ this.widget.selector.init(this.store);
41
+ this.dataSelector = this.widget.selector.createStoreSelector();
42
+ }
43
+
44
+ //init instance might change the store, so it must go before the controller
45
+ this.widget.initInstance(context, this);
46
+ this.widget.initState(context, this);
47
+
48
+ if (this.widget.controller)
49
+ this.controller = Controller.create(this.widget.controller, {
50
+ widget: this.widget,
51
+ instance: this,
52
+ store: this.store,
53
+ });
54
+
55
+ if (
56
+ this.widget.exploreCleanup ||
57
+ this.widget.outerLayout ||
58
+ this.widget.isContent ||
59
+ this.widget.controller ||
60
+ this.widget.prepareCleanup
61
+ )
62
+ this.needsExploreCleanup = true;
63
+ if (this.widget.prepare || this.widget.controller) this.needsPrepare = true;
64
+ if (this.widget.cleanup || this.widget.controller) this.needsCleanup = true;
65
+ this.initialized = true;
66
+ }
67
+
68
+ checkVisible(context) {
69
+ if (!this.initialized) this.init(context);
70
+
71
+ let wasVisible = this.visible;
72
+ this.rawData = this.dataSelector(this.store);
73
+ this.visible = this.widget.checkVisible(context, this, this.rawData);
74
+ if (this.visible && !this.detached) this.parent.instanceCache.addChild(this);
75
+ this.explored = false;
76
+ this.prepared = false;
77
+
78
+ if (!this.visible && wasVisible) this.destroy();
79
+
80
+ return this.visible;
81
+ }
82
+
83
+ scheduleExploreIfVisible(context) {
84
+ if (this.checkVisible(context)) {
85
+ context.exploreStack.push(this);
86
+
87
+ if (this.needsExploreCleanup) context.exploreStack.push(this);
88
+
89
+ return true;
90
+ }
91
+ return false;
92
+ }
93
+
94
+ cache(key, value) {
95
+ let oldValue = this.cached[key];
96
+ if (oldValue === value) return false;
97
+
98
+ if (!this.cacheList) this.cacheList = {};
99
+ this.cacheList[key] = value;
100
+ return true;
101
+ }
102
+
103
+ markShouldUpdate(context) {
104
+ let ins = this;
105
+ let renderList = this.renderList;
106
+ renderList.markReverseIndex();
107
+
108
+ //notify all parents that child state changed to bust up caching
109
+ while (ins && !ins.shouldUpdate && ins.explored) {
110
+ if (ins.renderList !== renderList) {
111
+ renderList.reverse();
112
+ renderList = ins.renderList;
113
+ renderList.markReverseIndex();
114
+ }
115
+ ins.shouldUpdate = true;
116
+ renderList.data.push(ins);
117
+ ins = ins.widget.isContent
118
+ ? ins.contentPlaceholder
119
+ : ins.parent.outerLayout === ins
120
+ ? ins.parent.parent
121
+ : ins.parent;
122
+ }
123
+ renderList.reverse();
124
+ }
125
+
126
+ explore(context) {
127
+ if (!this.visible) throw new Error("Explore invisible!");
128
+
129
+ if (this.explored) {
130
+ if (this.widget.prepareCleanup) context.prepareList.push(this);
131
+
132
+ if (this.widget.exploreCleanup) this.widget.exploreCleanup(context, this);
133
+
134
+ if (this.parent.outerLayout === this) context.popNamedValue("content", "body");
135
+
136
+ if (this.widget.controller) context.pop("controller");
137
+
138
+ return;
139
+ }
140
+
141
+ this.explored = true;
142
+
143
+ if (this.needsPrepare) context.prepareList.push(this);
144
+ else this.prepared = true;
145
+
146
+ if (this.needsCleanup) context.cleanupList.push(this);
147
+
148
+ if (this.instanceCache) this.instanceCache.mark();
149
+
150
+ //controller may reconfigure the widget and need to go before shouldUpdate calculation
151
+ this.parentOptions = context.parentOptions;
152
+
153
+ if (!this.controller) {
154
+ if (context.controller) this.controller = context.controller;
155
+ else if (this.parent.controller) this.controller = this.parent.controller;
156
+ }
157
+
158
+ this.destroyTracked = false;
159
+
160
+ if (this.controller) {
161
+ if (this.widget.controller) {
162
+ if (!this.controller.initialized) {
163
+ this.controller.init(context);
164
+ this.controller.initialized = true;
165
+ }
166
+ context.push("controller", this.controller);
167
+ this.controller.explore(context);
168
+ if (this.controller.onDestroy && this.controller.widget == this.widget) this.trackDestroy();
169
+ }
170
+ }
171
+
172
+ if (this.widget.onDestroy || isNonEmptyArray(this.destroySubscriptions)) this.trackDestroy();
173
+
174
+ this.renderList = this.assignedRenderList || this.parent.renderList || context.getRootRenderList();
175
+
176
+ let shouldUpdate =
177
+ this.rawData !== this.cached.rawData ||
178
+ this.state !== this.cached.state ||
179
+ this.widget.version !== this.cached.widgetVersion ||
180
+ this.cached.globalCacheIdentifier !== GlobalCacheIdentifier.get();
181
+
182
+ if (shouldUpdate) {
183
+ this.data = { ...this.rawData };
184
+ this.widget.prepareData(context, this);
185
+ debug(processDataFlag, this.widget);
186
+ }
187
+
188
+ //onExplore might set the outer layout
189
+ if (this.widget.onExplore) this.widget.onExplore(context, this);
190
+
191
+ if (this.parent.outerLayout === this) {
192
+ this.renderList = this.renderList.insertRight();
193
+ context.pushNamedValue("content", "body", this.parent);
194
+ }
195
+
196
+ if (this.widget.outerLayout) {
197
+ this.outerLayout = this.getChild(context, this.widget.outerLayout, null, this.store);
198
+ this.outerLayout.scheduleExploreIfVisible(context);
199
+ this.renderList = this.renderList.insertLeft();
200
+ }
201
+
202
+ if (this.widget.isContent) {
203
+ this.contentPlaceholder = context.contentPlaceholder && context.contentPlaceholder[this.widget.putInto];
204
+ if (this.contentPlaceholder) context.contentPlaceholder[this.widget.putInto](this);
205
+ else {
206
+ this.renderList = this.renderList.insertLeft();
207
+ context.pushNamedValue("content", this.widget.putInto, this);
208
+ if (!context.contentList) context.contentList = {};
209
+ let list = context.contentList[this.widget.putInto];
210
+ if (!list) list = context.contentList[this.widget.putInto] = [];
211
+ list.push(this);
212
+ }
213
+ }
214
+
215
+ this.shouldUpdate = false;
216
+ if (shouldUpdate || this.childStateDirty || !this.widget.memoize) this.markShouldUpdate(context);
217
+
218
+ context.exploreStack.hop();
219
+
220
+ if (this.widget.helpers) {
221
+ this.helpers = {};
222
+ for (let cmp in this.widget.helpers) {
223
+ let helper = this.widget.helpers[cmp];
224
+ if (helper) {
225
+ let ins = this.getChild(context, helper);
226
+ if (ins.scheduleExploreIfVisible(context)) this.helpers[cmp] = ins;
227
+ }
228
+ }
229
+ }
230
+
231
+ this.widget.explore(context, this, this.data);
232
+ }
233
+
234
+ prepare(context) {
235
+ if (!this.visible) throw new Error("Prepare invisible!");
236
+
237
+ if (this.prepared) {
238
+ if (this.widget.prepareCleanup) this.widget.prepareCleanup(context, this);
239
+ return;
240
+ }
241
+
242
+ this.prepared = true;
243
+ if (this.widget.prepare) this.widget.prepare(context, this);
244
+
245
+ if (this.widget.controller && this.controller.prepare) this.controller.prepare(context);
246
+ }
247
+
248
+ render(context) {
249
+ if (!this.visible) throw new Error("Render invisible!");
250
+
251
+ if (this.shouldUpdate) {
252
+ debug(renderFlag, this.widget, this.key);
253
+ let vdom = renderResultFix(this.widget.render(context, this, this.key));
254
+ if (this.widget.isContent || this.outerLayout) this.contentVDOM = vdom;
255
+ else this.vdom = vdom;
256
+ }
257
+
258
+ if (this.cacheList) for (let key in this.cacheList) this.cached[key] = this.cacheList[key];
259
+
260
+ this.cacheList = null;
261
+
262
+ this.cached.rawData = this.rawData;
263
+ this.cached.data = this.data;
264
+ this.cached.state = this.state;
265
+ this.cached.widgetVersion = this.widget.version;
266
+ this.cached.globalCacheIdentifier = GlobalCacheIdentifier.get();
267
+ this.childStateDirty = false;
268
+
269
+ if (this.instanceCache) this.instanceCache.sweep();
270
+
271
+ if (this.parent.outerLayout === this) {
272
+ //if outer layouts are chained we need to find the originating element (last element with OL set)
273
+ let parent = this.parent;
274
+ while (parent.parent.outerLayout == parent) parent = parent.parent;
275
+ parent.vdom = this.vdom;
276
+ }
277
+
278
+ return this.vdom;
279
+ }
280
+
281
+ cleanup(context) {
282
+ if (this.widget.controller && this.controller.cleanup) this.controller.cleanup(context);
283
+
284
+ if (this.widget.cleanup) this.widget.cleanup(context, this);
285
+ }
286
+
287
+ trackDestroy() {
288
+ if (!this.destroyTracked) {
289
+ this.destroyTracked = true;
290
+ if (this.parent && !this.detached) this.parent.trackDestroyableChild(this);
291
+ }
292
+ }
293
+
294
+ trackDestroyableChild(child) {
295
+ this.instanceCache.trackDestroy(child);
296
+ this.trackDestroy();
297
+ }
298
+
299
+ subscribeOnDestroy(callback) {
300
+ if (!this.destroySubscriptions) this.destroySubscriptions = [];
301
+ this.destroySubscriptions.push(callback);
302
+ this.trackDestroy();
303
+ return () => {
304
+ this.destroySubscriptions && this.destroySubscriptions.filter((cb) => cb != callback);
305
+ };
306
+ }
307
+
308
+ destroy() {
309
+ if (this.instanceCache) {
310
+ this.instanceCache.destroy();
311
+ this.instanceCache = null;
312
+ }
313
+
314
+ if (this.destroySubscriptions) {
315
+ this.destroySubscriptions.forEach((cb) => cb());
316
+ this.destroySubscriptions = null;
317
+ }
318
+
319
+ if (this.destroyTracked) {
320
+ debug(destroyFlag, this);
321
+
322
+ if (this.widget.onDestroy) this.widget.onDestroy(this);
323
+
324
+ if (
325
+ this.widget.controller &&
326
+ this.controller &&
327
+ this.controller.onDestroy &&
328
+ this.controller.widget == this.widget
329
+ )
330
+ this.controller.onDestroy();
331
+
332
+ this.destroyTracked = false;
333
+ }
334
+ }
335
+
336
+ setState(state) {
337
+ let skip = !!this.state;
338
+ if (this.state)
339
+ for (let k in state) {
340
+ if (this.state[k] !== state[k]) {
341
+ skip = false;
342
+ break;
343
+ }
344
+ }
345
+
346
+ if (skip) return;
347
+
348
+ this.state = Object.assign({}, this.state, state);
349
+ let parent = this.parent;
350
+ //notify all parents that child state change to bust up caching
351
+ while (parent) {
352
+ parent.childStateDirty = true;
353
+ parent = parent.parent;
354
+ }
355
+ batchUpdates(() => {
356
+ this.store.notify();
357
+ });
358
+ }
359
+
360
+ set(prop, value, options = {}) {
361
+ //skip re-rendering (used for reading state from uncontrolled components)
362
+ if (options.internal && this.rawData) {
363
+ this.rawData[prop] = value;
364
+ this.data[prop] = value;
365
+ }
366
+
367
+ let setter = this.setters && this.setters[prop];
368
+ if (setter) {
369
+ if (options.immediate && isFunction(setter.reset)) setter.reset(value);
370
+ else setter(value);
371
+ return true;
372
+ }
373
+
374
+ let p = this.widget[prop];
375
+ if (p && typeof p == "object") {
376
+ if (p.debounce) {
377
+ this.definePropertySetter(
378
+ prop,
379
+ validatedDebounce(
380
+ (value) => this.doSet(prop, value),
381
+ () => this.dataSelector(this.store)[prop],
382
+ p.debounce,
383
+ ),
384
+ );
385
+ this.set(prop, value, options);
386
+ return true;
387
+ }
388
+
389
+ if (p.throttle) {
390
+ this.definePropertySetter(
391
+ prop,
392
+ throttle((value) => this.doSet(prop, value), p.throttle),
393
+ );
394
+ this.set(prop, value, options);
395
+ return true;
396
+ }
397
+ }
398
+
399
+ return this.doSet(prop, value);
400
+ }
401
+
402
+ definePropertySetter(prop, setter) {
403
+ if (!this.setters) this.setters = {};
404
+ this.setters[prop] = setter;
405
+ }
406
+
407
+ doSet(prop, value) {
408
+ let changed = false;
409
+ batchUpdates(() => {
410
+ let p = this.widget[prop];
411
+ if (isObject(p)) {
412
+ if (p.set) {
413
+ if (isFunction(p.set)) {
414
+ p.set(value, this);
415
+ changed = true;
416
+ } else if (isString(p.set)) {
417
+ this.controller[p.set](value, this);
418
+ changed = true;
419
+ }
420
+ } else if (p.action) {
421
+ let action = p.action(value, this);
422
+ this.store.dispatch(action);
423
+ changed = true;
424
+ } else if (isString(p.bind) || isAccessorChain(p.bind)) {
425
+ changed = this.store.set(p.bind, value);
426
+ }
427
+ } else if (isAccessorChain(p)) changed = this.store.set(p.toString(), value);
428
+ });
429
+ return changed;
430
+ }
431
+
432
+ nestedDataSet(key, value, dataConfig, useParentStore) {
433
+ let config = dataConfig[key];
434
+ if (!config)
435
+ throw new Error(`Unknown nested data key ${key}. Known keys are ${Object.keys(dataConfig).join(", ")}.`);
436
+
437
+ if (isAccessorChain(config)) config = { bind: config.toString() };
438
+
439
+ if (config.bind) {
440
+ let store = this.store;
441
+ //in case of Rescope or DataProxy, bindings point to the data in the parent store
442
+ if (useParentStore && store.store) store = store.store;
443
+ return isUndefined(value) ? store.deleteItem(config.bind) : store.setItem(config.bind, value);
444
+ }
445
+
446
+ if (!config.set)
447
+ throw new Error(
448
+ `Cannot change nested data value for ${key} as it's read-only. Either define it as a binding or define a set function.`,
449
+ );
450
+ if (isString(config.set)) this.getControllerMethod(config.set)(value, this);
451
+ else if (isFunction(config.set)) config.set(value, this);
452
+ else
453
+ throw new Error(
454
+ `Cannot change nested data value for ${key} the defined setter is neither a function nor a controller method.`,
455
+ );
456
+
457
+ return true;
458
+ }
459
+
460
+ replaceState(state) {
461
+ this.cached.state = this.state;
462
+ this.state = state;
463
+ this.store.notify();
464
+ }
465
+
466
+ getInstanceCache() {
467
+ if (!this.instanceCache)
468
+ this.instanceCache = new InstanceCache(this, this.widget.isPureContainer ? this.key : null);
469
+ return this.instanceCache;
470
+ }
471
+
472
+ clearChildrenCache() {
473
+ if (this.instanceCache) this.instanceCache.destroy();
474
+ }
475
+
476
+ getChild(context, widget, key, store) {
477
+ return this.getInstanceCache().getChild(widget, store || this.store, key);
478
+ }
479
+
480
+ getDetachedChild(widget, key, store) {
481
+ let child = new Instance(widget, key, this, store || this.store);
482
+ child.detached = true;
483
+ return child;
484
+ }
485
+
486
+ prepareRenderCleanupChild(widget, store, keyPrefix, options) {
487
+ return widget.prepareRenderCleanup(store || this.store, options, keyPrefix, this);
488
+ }
489
+
490
+ getJsxEventProps() {
491
+ let { widget } = this;
492
+
493
+ if (!isArray(widget.jsxAttributes)) return null;
494
+
495
+ let props = {};
496
+ widget.jsxAttributes.forEach((attr) => {
497
+ if (attr.indexOf("on") == 0 && attr.length > 2) props[attr] = (e) => this.invoke(attr, e, this);
498
+ });
499
+ return props;
500
+ }
501
+
502
+ getCallback(methodName) {
503
+ let scope = this.widget;
504
+ let callback = scope[methodName];
505
+
506
+ if (typeof callback === "string") return this.getControllerMethod(callback);
507
+
508
+ if (typeof callback !== "function")
509
+ throw new Error(`Cannot invoke callback method ${methodName} as assigned value is not a function.`);
510
+
511
+ return callback.bind(scope);
512
+ }
513
+
514
+ getControllerMethod(methodName) {
515
+ if (!this.controller)
516
+ throw new Error(
517
+ `Cannot invoke controller method "${methodName}" as controller is not assigned to the widget.`,
518
+ );
519
+
520
+ let at = this;
521
+ while (at != null && at.controller && !at.controller[methodName]) at = at.parent;
522
+
523
+ if (!at || !at.controller || !at.controller[methodName])
524
+ throw new Error(
525
+ `Cannot invoke controller method "${methodName}". The method cannot be found in any of the assigned controllers.`,
526
+ );
527
+
528
+ return at.controller[methodName].bind(at.controller);
529
+ }
530
+
531
+ invoke(methodName, ...args) {
532
+ return this.getCallback(methodName).apply(null, args);
533
+ }
534
+
535
+ invokeControllerMethod(methodName, ...args) {
536
+ return this.getControllerMethod(methodName).apply(null, args);
537
+ }
538
+ }
539
+
540
+ function renderResultFix(res) {
541
+ return res != null && isDefined(res.content) ? res : { content: res };
542
+ }
543
+
544
+ export class InstanceCache {
545
+ constructor(parent, keyPrefix) {
546
+ this.children = {};
547
+ this.parent = parent;
548
+ this.marked = {};
549
+ this.monitored = null;
550
+ this.keyPrefix = keyPrefix != null ? keyPrefix + "-" : "";
551
+ }
552
+
553
+ getChild(widget, store, key) {
554
+ let k = this.keyPrefix + (key != null ? key : widget.vdomKey || widget.widgetId);
555
+ let instance = this.children[k];
556
+
557
+ if (
558
+ !instance ||
559
+ instance.widget !== widget ||
560
+ (!instance.visible && (instance.widget.controller || instance.widget.onInit))
561
+ ) {
562
+ instance = new Instance(widget, k, this.parent);
563
+ this.children[k] = instance;
564
+ }
565
+ if (instance.store !== store) {
566
+ instance.setStore(store);
567
+ if (instance.cached) delete instance.cached.rawData; // force prepareData to execute again
568
+ }
569
+
570
+ return instance;
571
+ }
572
+
573
+ addChild(instance) {
574
+ this.marked[instance.key] = instance;
575
+ }
576
+
577
+ mark() {
578
+ this.marked = {};
579
+ }
580
+
581
+ trackDestroy(instance) {
582
+ if (!this.monitored) this.monitored = {};
583
+ this.monitored[instance.key] = instance;
584
+ }
585
+
586
+ destroy() {
587
+ this.children = {};
588
+ this.marked = {};
589
+
590
+ if (!this.monitored) return;
591
+
592
+ for (let key in this.monitored) {
593
+ this.monitored[key].destroy();
594
+ }
595
+
596
+ this.monitored = null;
597
+ }
598
+
599
+ sweep() {
600
+ this.children = this.marked;
601
+ if (!this.monitored) return;
602
+ let activeCount = 0;
603
+ for (let key in this.monitored) {
604
+ let monitoredChild = this.monitored[key];
605
+ let child = this.children[key];
606
+ if (child !== monitoredChild || !monitoredChild.visible) {
607
+ monitoredChild.destroy();
608
+ delete this.monitored[key];
609
+ if (child === monitoredChild) delete this.children[key];
610
+ } else activeCount++;
611
+ }
612
+ if (activeCount === 0) this.monitored = null;
613
+ }
614
+ }