cx 25.5.0 → 25.5.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.
@@ -1,587 +1,594 @@
1
- import { Widget, VDOM, getContent } from "../ui/Widget";
2
- import { PureContainer } from "../ui/PureContainer";
3
- import { GroupAdapter } from "../ui/adapter/GroupAdapter";
4
- import { Binding, isBinding } from "../data/Binding";
5
- import { Selection } from "../ui/selection/Selection";
6
- import { KeyCode } from "../util/KeyCode";
7
- import { scrollElementIntoView } from "../util/scrollElementIntoView";
8
- import { FocusManager, oneFocusOut, offFocusOut, preventFocusOnTouch } from "../ui/FocusManager";
9
- import { isString } from "../util/isString";
10
- import { isArray } from "../util/isArray";
11
- import { getAccessor } from "../data/getAccessor";
12
- import { batchUpdates } from "../ui/batchUpdates";
13
- import { Container } from "../ui/Container";
14
- import { addEventListenerWithOptions } from "../util/addEventListenerWithOptions";
15
-
16
- /*
17
- - renders list of items
18
- - focusable (keyboard navigation)
19
- - selection
20
- - fake focus - list appears focused and receives keyboard inputs redirected from other control (dropdown scenario)
21
- */
22
-
23
- export class List extends Widget {
24
- init() {
25
- if (this.recordAlias) this.recordName = this.recordAlias;
26
-
27
- if (this.indexAlias) this.indexName = this.indexAlias;
28
-
29
- this.adapter = GroupAdapter.create(this.adapter || GroupAdapter, {
30
- recordName: this.recordName,
31
- indexName: this.indexName,
32
- recordsAccessor: getAccessor(this.records),
33
- keyField: this.keyField,
34
- sortOptions: this.sortOptions,
35
- });
36
-
37
- this.child = ListItem.create({
38
- layout: this.layout,
39
- items: this.items,
40
- children: this.children,
41
- styled: true,
42
- class: this.itemClass,
43
- className: this.itemClassName,
44
- style: this.itemStyle,
45
- disabled: this.itemDisabled,
46
- ...this.item,
47
- });
48
-
49
- delete this.children;
50
-
51
- this.selection = Selection.create(this.selection, {
52
- records: this.records,
53
- });
54
-
55
- super.init();
56
-
57
- if (this.grouping) {
58
- this.groupBy(this.grouping);
59
- }
60
- }
61
-
62
- initInstance(context, instance) {
63
- this.adapter.initInstance(context, instance);
64
- }
65
-
66
- declareData() {
67
- let selection = this.selection.configureWidget(this);
68
-
69
- super.declareData(
70
- selection,
71
- {
72
- records: undefined,
73
- sorters: undefined,
74
- sortField: undefined,
75
- sortDirection: undefined,
76
- filterParams: {
77
- structured: true,
78
- },
79
- itemStyle: {
80
- structured: true,
81
- },
82
- emptyText: undefined,
83
- tabIndex: undefined,
84
- },
85
- ...arguments
86
- );
87
- }
88
-
89
- prepareData(context, instance) {
90
- let { data } = instance;
91
-
92
- if (data.sortField)
93
- data.sorters = [
94
- {
95
- field: data.sortField,
96
- direction: data.sortDirection || "ASC",
97
- },
98
- ];
99
- this.adapter.sort(data.sorters);
100
-
101
- let filter = null;
102
- if (this.onCreateFilter) filter = instance.invoke("onCreateFilter", data.filterParams, instance);
103
- else if (this.filter) filter = (item) => this.filter(item, data.filterParams);
104
- this.adapter.setFilter(filter);
105
- instance.mappedRecords = this.adapter.getRecords(context, instance, data.records, instance.store);
106
-
107
- data.stateMods = Object.assign(data.stateMods || {}, {
108
- selectable: !this.selection.isDummy || this.onItemClick,
109
- empty: instance.mappedRecords.length == 0,
110
- });
111
-
112
- super.prepareData(context, instance);
113
- }
114
-
115
- explore(context, instance, data) {
116
- let instances = [];
117
- let isSelected = this.selection.getIsSelectedDelegate(instance.store);
118
- instance.mappedRecords.forEach((record) => {
119
- if (record.type == "data") {
120
- let itemInstance = instance.getChild(context, this.child, record.key, record.store);
121
- itemInstance.record = record;
122
- itemInstance.selected = isSelected(record.data, record.index);
123
-
124
- let changed = false;
125
- if (itemInstance.cache("recordData", record.data)) changed = true;
126
- if (itemInstance.cache("selected", itemInstance.selected)) changed = true;
127
-
128
- if (this.cached && !changed && itemInstance.visible && !itemInstance.childStateDirty) {
129
- instances.push(itemInstance);
130
- itemInstance.shouldUpdate = false;
131
- } else if (itemInstance.scheduleExploreIfVisible(context)) instances.push(itemInstance);
132
- } else if (record.type == "group-header" && record.grouping.header) {
133
- let itemInstance = instance.getChild(context, record.grouping.header, record.key, record.store);
134
- itemInstance.record = record;
135
- if (itemInstance.scheduleExploreIfVisible(context)) instances.push(itemInstance);
136
- } else if (record.type == "group-footer" && record.grouping.footer) {
137
- let itemInstance = instance.getChild(context, record.grouping.footer, record.key, record.store);
138
- itemInstance.record = record;
139
- if (itemInstance.scheduleExploreIfVisible(context)) instances.push(itemInstance);
140
- }
141
- });
142
- instance.instances = instances;
143
- }
144
-
145
- render(context, instance, key) {
146
- let items = instance.instances.map((x, i) => ({
147
- instance: x,
148
- key: x.record.key,
149
- type: x.record.type,
150
- content: getContent(x.render(context)),
151
- }));
152
- return (
153
- <ListComponent
154
- key={key}
155
- instance={instance}
156
- items={items}
157
- selectable={!this.selection.isDummy || this.onItemClick}
158
- />
159
- );
160
- }
161
-
162
- groupBy(grouping) {
163
- if (!isArray(grouping)) {
164
- if (isString(grouping) || typeof grouping == "object") return this.groupBy([grouping]);
165
- throw new Error("DynamicGrouping should be an array of grouping objects");
166
- }
167
-
168
- grouping = grouping.map((g, i) => {
169
- if (isString(g)) {
170
- return {
171
- key: {
172
- [g]: {
173
- bind: this.recordName + "." + g,
174
- },
175
- },
176
- };
177
- }
178
- return g;
179
- });
180
-
181
- grouping.forEach((g) => {
182
- if (g.header) g.header = Widget.create(g.header);
183
-
184
- if (g.footer) g.footer = Widget.create(g.footer);
185
- });
186
-
187
- this.adapter.groupBy(grouping);
188
- this.update();
189
- }
190
- }
191
-
192
- List.prototype.recordName = "$record";
193
- List.prototype.indexName = "$index";
194
- List.prototype.baseClass = "list";
195
- List.prototype.focusable = true;
196
- List.prototype.focused = false;
197
- List.prototype.itemPad = true;
198
- List.prototype.cached = false;
199
- List.prototype.styled = true;
200
- List.prototype.scrollSelectionIntoView = false;
201
- List.prototype.selectMode = false;
202
- List.prototype.selectOnTab = false;
203
-
204
- Widget.alias("list", List);
205
-
206
- class ListComponent extends VDOM.Component {
207
- constructor(props) {
208
- super(props);
209
- let { widget } = props.instance;
210
- let { focused } = widget;
211
- this.state = {
212
- cursor: focused && props.selectable ? 0 : -1,
213
- focused: focused,
214
- };
215
-
216
- this.handleItemMouseDown = this.handleItemMouseDown.bind(this);
217
- this.handleItemDoubleClick = this.handleItemDoubleClick.bind(this);
218
- this.handleItemClick = this.handleItemClick.bind(this);
219
- }
220
-
221
- shouldComponentUpdate(props, state) {
222
- return props.instance.shouldUpdate || state != this.state;
223
- }
224
-
225
- componentDidMount() {
226
- let { instance } = this.props;
227
- let { widget } = instance;
228
- if (widget.pipeKeyDown) {
229
- instance.invoke("pipeKeyDown", this.handleKeyDown.bind(this), instance);
230
- this.showCursor();
231
- }
232
-
233
- if (widget.autoFocus) FocusManager.focus(this.el);
234
-
235
- if (widget.onScroll) {
236
- this.unsubscribeScroll = addEventListenerWithOptions(
237
- this.el,
238
- "scroll",
239
- (event) => {
240
- instance.invoke("onScroll", event, instance);
241
- },
242
- { passive: true }
243
- );
244
- }
245
-
246
- this.componentDidUpdate();
247
- }
248
-
249
- UNSAFE_componentWillReceiveProps(props) {
250
- if (this.state.focused && props.instance.widget.selectMode) this.showCursor(true, props.items);
251
- else if (this.state.cursor >= props.items.length) this.moveCursor(props.items.length - 1);
252
- else if (this.state.focused && this.state.cursor < 0) this.moveCursor(0);
253
- }
254
-
255
- componentWillUnmount() {
256
- let { instance } = this.props;
257
- let { widget } = instance;
258
- offFocusOut(this);
259
- if (widget.pipeKeyDown) instance.invoke("pipeKeyDown", null, instance);
260
- }
261
-
262
- handleItemMouseDown(e) {
263
- let index = Number(e.currentTarget.dataset.recordIndex);
264
- this.moveCursor(index);
265
- if (e.shiftKey) e.preventDefault();
266
-
267
- this.moveCursor(index, {
268
- select: true,
269
- selectOptions: {
270
- toggle: e.ctrlKey && !e.shiftKey,
271
- add: e.ctrlKey && e.shiftKey,
272
- },
273
- selectRange: e.shiftKey,
274
- });
275
- }
276
-
277
- handleItemClick(e) {
278
- let { instance, items } = this.props;
279
- let index = Number(e.currentTarget.dataset.recordIndex);
280
- let item = items[this.cursorChildIndex[index]];
281
- if (instance.invoke("onItemClick", e, item.instance) === false) return;
282
-
283
- this.moveCursor(index, {
284
- select: true,
285
- selectOptions: {
286
- toggle: e.ctrlKey && !e.shiftKey,
287
- add: e.ctrlKey && e.shiftKey,
288
- },
289
- selectRange: e.shiftKey,
290
- });
291
- }
292
-
293
- handleItemDoubleClick(e) {
294
- let { instance, items } = this.props;
295
- let index = Number(e.currentTarget.dataset.recordIndex);
296
- let item = items[this.cursorChildIndex[index]];
297
- instance.invoke("onItemDoubleClick", e, item.instance);
298
- }
299
-
300
- render() {
301
- let { instance, items, selectable } = this.props;
302
- let { data, widget } = instance;
303
- let { CSS, baseClass } = widget;
304
- let itemStyle = CSS.parseStyle(data.itemStyle);
305
- this.cursorChildIndex = [];
306
- let cursorIndex = 0;
307
-
308
- let onDblClick, onClick;
309
-
310
- if (widget.onItemClick) onClick = this.handleItemClick;
311
-
312
- if (widget.onItemDoubleClick) onDblClick = this.handleItemDoubleClick;
313
-
314
- let children =
315
- items.length > 0 &&
316
- items.map((x, i) => {
317
- let { data, selected } = x.instance;
318
- let className;
319
-
320
- if (x.type == "data") {
321
- let ind = cursorIndex++;
322
-
323
- this.cursorChildIndex.push(i);
324
- className = CSS.element(baseClass, "item", {
325
- selected: selected,
326
- cursor: ind == this.state.cursor,
327
- pad: widget.itemPad,
328
- disabled: data.disabled,
329
- });
330
-
331
- return (
332
- <li
333
- key={x.key}
334
- className={CSS.expand(className, data.classNames)}
335
- style={itemStyle}
336
- data-record-index={ind}
337
- onMouseDown={this.handleItemMouseDown}
338
- onClick={onClick}
339
- onDoubleClick={onDblClick}
340
- >
341
- {x.content}
342
- </li>
343
- );
344
- } else {
345
- return (
346
- <li key={x.key} className={CSS.element(baseClass, x.type)}>
347
- {x.content}
348
- </li>
349
- );
350
- }
351
- });
352
-
353
- if (!children && data.emptyText) {
354
- children = <li className={CSS.element(baseClass, "empty-text")}>{data.emptyText}</li>;
355
- }
356
-
357
- return (
358
- <ul
359
- ref={(el) => {
360
- this.el = el;
361
- }}
362
- className={CSS.expand(data.classNames, CSS.state({ focused: this.state.focused }))}
363
- style={data.style}
364
- tabIndex={widget.focusable && selectable && items.length > 0 ? data.tabIndex || 0 : null}
365
- onMouseDown={preventFocusOnTouch}
366
- onKeyDown={this.handleKeyDown.bind(this)}
367
- onMouseLeave={this.handleMouseLeave.bind(this)}
368
- onFocus={this.onFocus.bind(this)}
369
- onBlur={this.onBlur.bind(this)}
370
- >
371
- {children}
372
- </ul>
373
- );
374
- }
375
-
376
- componentDidUpdate() {
377
- let { widget } = this.props.instance;
378
- if (widget.scrollSelectionIntoView) {
379
- //The timeout is reqired for use-cases when parent needs to do some measuring that affect scrollbars, i.e. LookupField.
380
- setTimeout(() => this.scrollElementIntoView(), 0);
381
- }
382
- }
383
-
384
- scrollElementIntoView() {
385
- if (!this.el) return; //unmount
386
- let { widget } = this.props.instance;
387
- let { CSS, baseClass } = widget;
388
- let selectedRowSelector = `.${CSS.element(baseClass, "item")}.${CSS.state("selected")}`;
389
- let firstSelectedRow = this.el.querySelector(selectedRowSelector);
390
- if (firstSelectedRow != this.selectedEl) {
391
- if (firstSelectedRow) scrollElementIntoView(firstSelectedRow, true, false, 0, this.el);
392
- this.selectedEl = firstSelectedRow;
393
- }
394
- }
395
-
396
- moveCursor(index, { focused, hover, scrollIntoView, select, selectRange, selectOptions } = {}) {
397
- let { instance, selectable } = this.props;
398
- if (!selectable) return;
399
-
400
- let { widget } = instance;
401
- let newState = {};
402
- if (widget.focused) focused = true;
403
-
404
- if (focused != null && this.state.focused != focused) newState.focused = focused;
405
-
406
- //ignore mouse enter/leave events (support with a flag if a feature request comes)
407
- if (!hover) newState.cursor = index;
408
-
409
- //batch updates to avoid flickering between selection and cursor changes
410
- batchUpdates(() => {
411
- if (select || widget.selectMode) {
412
- let start = selectRange && this.state.selectionStart >= 0 ? this.state.selectionStart : index;
413
- if (start < 0) start = index;
414
- this.selectRange(start, index, selectOptions);
415
- if (!selectRange) newState.selectionStart = index;
416
- }
417
- if (Object.keys(newState).length > 0) {
418
- this.setState(newState, () => {
419
- if (scrollIntoView) {
420
- let item = this.el.children[this.cursorChildIndex[index]];
421
- if (item) scrollElementIntoView(item);
422
- }
423
- });
424
- }
425
- });
426
- }
427
-
428
- selectRange(from, to, options) {
429
- let { instance, items } = this.props;
430
- let { widget } = instance;
431
-
432
- if (from > to) {
433
- let tmp = from;
434
- from = to;
435
- to = tmp;
436
- }
437
-
438
- let selection = [],
439
- indexes = [];
440
-
441
- for (let cursor = from; cursor <= to; cursor++) {
442
- let item = items[this.cursorChildIndex[cursor]];
443
- if (item) {
444
- let { record, data } = item.instance;
445
- if (data.disabled) continue;
446
- selection.push(record.data);
447
- indexes.push(record.index);
448
- }
449
- }
450
-
451
- widget.selection.selectMultiple(instance.store, selection, indexes, options);
452
- }
453
-
454
- showCursor(force, newItems) {
455
- if (!force && this.state.cursor >= 0) return;
456
-
457
- let items = newItems || this.props.items;
458
- let index = -1,
459
- firstSelected = -1,
460
- firstValid = -1;
461
- for (let i = 0; i < items.length; i++) {
462
- let item = items[i];
463
- if (isDataItem(item)) {
464
- index++;
465
-
466
- if (!isItemDisabled(item) && firstValid == -1) firstValid = index;
467
- if (item.instance.selected) {
468
- firstSelected = index;
469
- break;
470
- }
471
- }
472
- }
473
- this.moveCursor(firstSelected != -1 ? firstSelected : firstValid, {
474
- focusedport: true,
475
- });
476
- }
477
-
478
- onFocus() {
479
- let { widget } = this.props.instance;
480
-
481
- FocusManager.nudge();
482
- this.showCursor(widget.selectMode);
483
-
484
- if (!widget.focused)
485
- oneFocusOut(this, this.el, () => {
486
- this.moveCursor(-1, { focused: false });
487
- });
488
-
489
- this.setState({
490
- focused: true,
491
- });
492
- }
493
-
494
- onBlur() {
495
- FocusManager.nudge();
496
- }
497
-
498
- handleMouseLeave() {
499
- let { widget } = this.props.instance;
500
- if (!widget.focused) this.moveCursor(-1, { hover: true });
501
- }
502
-
503
- handleKeyDown(e) {
504
- let { instance, items } = this.props;
505
- let { widget } = instance;
506
-
507
- if (this.onKeyDown && instance.invoke("onKeyDown", e, instance) === false) return;
508
-
509
- switch (e.keyCode) {
510
- case KeyCode.tab:
511
- case KeyCode.enter:
512
- if (!widget.selectOnTab && e.keyCode == KeyCode.tab) break;
513
- let item = items[this.cursorChildIndex[this.state.cursor]];
514
- if (item && widget.onItemClick && instance.invoke("onItemClick", e, item.instance) === false) return;
515
- this.moveCursor(this.state.cursor, {
516
- select: true,
517
- selectOptions: {
518
- toggle: e.ctrlKey && !e.shiftKey,
519
- add: e.ctrlKey && e.shiftKey,
520
- },
521
- selectRange: e.shiftKey,
522
- });
523
- break;
524
-
525
- case KeyCode.down:
526
- for (let index = this.state.cursor + 1; index < this.cursorChildIndex.length; index++) {
527
- let item = items[this.cursorChildIndex[index]];
528
- if (!isItemSelectable(item)) continue;
529
- this.moveCursor(index, {
530
- focused: true,
531
- scrollIntoView: true,
532
- select: e.shiftKey,
533
- selectRange: e.shiftKey,
534
- });
535
- e.stopPropagation();
536
- e.preventDefault();
537
- break;
538
- }
539
- break;
540
-
541
- case KeyCode.up:
542
- for (let index = this.state.cursor - 1; index >= 0; index--) {
543
- let item = items[this.cursorChildIndex[index]];
544
- if (!isItemSelectable(item)) continue;
545
- this.moveCursor(index, {
546
- focused: true,
547
- scrollIntoView: true,
548
- select: e.shiftKey,
549
- selectRange: e.shiftKey,
550
- });
551
- e.stopPropagation();
552
- e.preventDefault();
553
- break;
554
- }
555
- break;
556
-
557
- case KeyCode.a:
558
- if (!e.ctrlKey || !widget.selection.multiple) return;
559
-
560
- this.selectRange(0, this.cursorChildIndex.length);
561
-
562
- e.stopPropagation();
563
- e.preventDefault();
564
- break;
565
- }
566
- }
567
- }
568
-
569
- class ListItem extends Container {
570
- declareData(...args) {
571
- super.declareData(...args, {
572
- disabled: undefined,
573
- });
574
- }
575
- }
576
-
577
- function isItemSelectable(item) {
578
- return isDataItem(item) && !isItemDisabled(item);
579
- }
580
-
581
- function isDataItem(item) {
582
- return item?.type == "data";
583
- }
584
-
585
- function isItemDisabled(item) {
586
- return item?.instance.data.disabled;
587
- }
1
+ import { Widget, VDOM, getContent } from "../ui/Widget";
2
+ import { PureContainer } from "../ui/PureContainer";
3
+ import { GroupAdapter } from "../ui/adapter/GroupAdapter";
4
+ import { Binding, isBinding } from "../data/Binding";
5
+ import { Selection } from "../ui/selection/Selection";
6
+ import { KeyCode } from "../util/KeyCode";
7
+ import { scrollElementIntoView } from "../util/scrollElementIntoView";
8
+ import { FocusManager, oneFocusOut, offFocusOut, preventFocusOnTouch } from "../ui/FocusManager";
9
+ import { isString } from "../util/isString";
10
+ import { isArray } from "../util/isArray";
11
+ import { getAccessor } from "../data/getAccessor";
12
+ import { batchUpdates } from "../ui/batchUpdates";
13
+ import { Container } from "../ui/Container";
14
+ import { addEventListenerWithOptions } from "../util/addEventListenerWithOptions";
15
+
16
+ /*
17
+ - renders list of items
18
+ - focusable (keyboard navigation)
19
+ - selection
20
+ - fake focus - list appears focused and receives keyboard inputs redirected from other control (dropdown scenario)
21
+ */
22
+
23
+ export class List extends Widget {
24
+ init() {
25
+ if (this.recordAlias) this.recordName = this.recordAlias;
26
+
27
+ if (this.indexAlias) this.indexName = this.indexAlias;
28
+
29
+ this.adapter = GroupAdapter.create(this.adapter || GroupAdapter, {
30
+ recordName: this.recordName,
31
+ indexName: this.indexName,
32
+ recordsAccessor: getAccessor(this.records),
33
+ keyField: this.keyField,
34
+ sortOptions: this.sortOptions,
35
+ });
36
+
37
+ this.child = ListItem.create({
38
+ layout: this.layout,
39
+ items: this.items,
40
+ children: this.children,
41
+ styled: true,
42
+ class: this.itemClass,
43
+ className: this.itemClassName,
44
+ style: this.itemStyle,
45
+ disabled: this.itemDisabled,
46
+ ...this.item,
47
+ });
48
+
49
+ delete this.children;
50
+
51
+ this.selection = Selection.create(this.selection, {
52
+ records: this.records,
53
+ });
54
+
55
+ super.init();
56
+
57
+ if (this.grouping) {
58
+ this.groupBy(this.grouping);
59
+ }
60
+ }
61
+
62
+ initInstance(context, instance) {
63
+ this.adapter.initInstance(context, instance);
64
+ }
65
+
66
+ declareData() {
67
+ let selection = this.selection.configureWidget(this);
68
+
69
+ super.declareData(
70
+ selection,
71
+ {
72
+ records: undefined,
73
+ sorters: undefined,
74
+ sortField: undefined,
75
+ sortDirection: undefined,
76
+ filterParams: {
77
+ structured: true,
78
+ },
79
+ itemStyle: {
80
+ structured: true,
81
+ },
82
+ emptyText: undefined,
83
+ tabIndex: undefined,
84
+ },
85
+ ...arguments,
86
+ );
87
+ }
88
+
89
+ prepareData(context, instance) {
90
+ let { data } = instance;
91
+
92
+ if (data.sortField)
93
+ data.sorters = [
94
+ {
95
+ field: data.sortField,
96
+ direction: data.sortDirection || "ASC",
97
+ },
98
+ ];
99
+ this.adapter.sort(data.sorters);
100
+
101
+ let filter = null;
102
+ if (this.onCreateFilter) filter = instance.invoke("onCreateFilter", data.filterParams, instance);
103
+ else if (this.filter) filter = (item) => this.filter(item, data.filterParams);
104
+ this.adapter.setFilter(filter);
105
+ instance.mappedRecords = this.adapter.getRecords(context, instance, data.records, instance.store);
106
+
107
+ data.stateMods = Object.assign(data.stateMods || {}, {
108
+ selectable: !this.selection.isDummy || this.onItemClick,
109
+ empty: instance.mappedRecords.length == 0,
110
+ });
111
+
112
+ super.prepareData(context, instance);
113
+ }
114
+
115
+ applyParentStore(instance) {
116
+ super.applyParentStore(instance);
117
+
118
+ // force prepareData to execute again and propagate the store change to the records
119
+ if (instance.cached) delete instance.cached.rawData;
120
+ }
121
+
122
+ explore(context, instance, data) {
123
+ let instances = [];
124
+ let isSelected = this.selection.getIsSelectedDelegate(instance.store);
125
+ instance.mappedRecords.forEach((record) => {
126
+ if (record.type == "data") {
127
+ let itemInstance = instance.getChild(context, this.child, record.key, record.store);
128
+ itemInstance.record = record;
129
+ itemInstance.selected = isSelected(record.data, record.index);
130
+
131
+ let changed = false;
132
+ if (itemInstance.cache("recordData", record.data)) changed = true;
133
+ if (itemInstance.cache("selected", itemInstance.selected)) changed = true;
134
+
135
+ if (this.cached && !changed && itemInstance.visible && !itemInstance.childStateDirty) {
136
+ instances.push(itemInstance);
137
+ itemInstance.shouldUpdate = false;
138
+ } else if (itemInstance.scheduleExploreIfVisible(context)) instances.push(itemInstance);
139
+ } else if (record.type == "group-header" && record.grouping.header) {
140
+ let itemInstance = instance.getChild(context, record.grouping.header, record.key, record.store);
141
+ itemInstance.record = record;
142
+ if (itemInstance.scheduleExploreIfVisible(context)) instances.push(itemInstance);
143
+ } else if (record.type == "group-footer" && record.grouping.footer) {
144
+ let itemInstance = instance.getChild(context, record.grouping.footer, record.key, record.store);
145
+ itemInstance.record = record;
146
+ if (itemInstance.scheduleExploreIfVisible(context)) instances.push(itemInstance);
147
+ }
148
+ });
149
+ instance.instances = instances;
150
+ }
151
+
152
+ render(context, instance, key) {
153
+ let items = instance.instances.map((x, i) => ({
154
+ instance: x,
155
+ key: x.record.key,
156
+ type: x.record.type,
157
+ content: getContent(x.render(context)),
158
+ }));
159
+ return (
160
+ <ListComponent
161
+ key={key}
162
+ instance={instance}
163
+ items={items}
164
+ selectable={!this.selection.isDummy || this.onItemClick}
165
+ />
166
+ );
167
+ }
168
+
169
+ groupBy(grouping) {
170
+ if (!isArray(grouping)) {
171
+ if (isString(grouping) || typeof grouping == "object") return this.groupBy([grouping]);
172
+ throw new Error("DynamicGrouping should be an array of grouping objects");
173
+ }
174
+
175
+ grouping = grouping.map((g, i) => {
176
+ if (isString(g)) {
177
+ return {
178
+ key: {
179
+ [g]: {
180
+ bind: this.recordName + "." + g,
181
+ },
182
+ },
183
+ };
184
+ }
185
+ return g;
186
+ });
187
+
188
+ grouping.forEach((g) => {
189
+ if (g.header) g.header = Widget.create(g.header);
190
+
191
+ if (g.footer) g.footer = Widget.create(g.footer);
192
+ });
193
+
194
+ this.adapter.groupBy(grouping);
195
+ this.update();
196
+ }
197
+ }
198
+
199
+ List.prototype.recordName = "$record";
200
+ List.prototype.indexName = "$index";
201
+ List.prototype.baseClass = "list";
202
+ List.prototype.focusable = true;
203
+ List.prototype.focused = false;
204
+ List.prototype.itemPad = true;
205
+ List.prototype.cached = false;
206
+ List.prototype.styled = true;
207
+ List.prototype.scrollSelectionIntoView = false;
208
+ List.prototype.selectMode = false;
209
+ List.prototype.selectOnTab = false;
210
+
211
+ Widget.alias("list", List);
212
+
213
+ class ListComponent extends VDOM.Component {
214
+ constructor(props) {
215
+ super(props);
216
+ let { widget } = props.instance;
217
+ let { focused } = widget;
218
+ this.state = {
219
+ cursor: focused && props.selectable ? 0 : -1,
220
+ focused: focused,
221
+ };
222
+
223
+ this.handleItemMouseDown = this.handleItemMouseDown.bind(this);
224
+ this.handleItemDoubleClick = this.handleItemDoubleClick.bind(this);
225
+ this.handleItemClick = this.handleItemClick.bind(this);
226
+ }
227
+
228
+ shouldComponentUpdate(props, state) {
229
+ return props.instance.shouldUpdate || state != this.state;
230
+ }
231
+
232
+ componentDidMount() {
233
+ let { instance } = this.props;
234
+ let { widget } = instance;
235
+ if (widget.pipeKeyDown) {
236
+ instance.invoke("pipeKeyDown", this.handleKeyDown.bind(this), instance);
237
+ this.showCursor();
238
+ }
239
+
240
+ if (widget.autoFocus) FocusManager.focus(this.el);
241
+
242
+ if (widget.onScroll) {
243
+ this.unsubscribeScroll = addEventListenerWithOptions(
244
+ this.el,
245
+ "scroll",
246
+ (event) => {
247
+ instance.invoke("onScroll", event, instance);
248
+ },
249
+ { passive: true },
250
+ );
251
+ }
252
+
253
+ this.componentDidUpdate();
254
+ }
255
+
256
+ UNSAFE_componentWillReceiveProps(props) {
257
+ if (this.state.focused && props.instance.widget.selectMode) this.showCursor(true, props.items);
258
+ else if (this.state.cursor >= props.items.length) this.moveCursor(props.items.length - 1);
259
+ else if (this.state.focused && this.state.cursor < 0) this.moveCursor(0);
260
+ }
261
+
262
+ componentWillUnmount() {
263
+ let { instance } = this.props;
264
+ let { widget } = instance;
265
+ offFocusOut(this);
266
+ if (widget.pipeKeyDown) instance.invoke("pipeKeyDown", null, instance);
267
+ }
268
+
269
+ handleItemMouseDown(e) {
270
+ let index = Number(e.currentTarget.dataset.recordIndex);
271
+ this.moveCursor(index);
272
+ if (e.shiftKey) e.preventDefault();
273
+
274
+ this.moveCursor(index, {
275
+ select: true,
276
+ selectOptions: {
277
+ toggle: e.ctrlKey && !e.shiftKey,
278
+ add: e.ctrlKey && e.shiftKey,
279
+ },
280
+ selectRange: e.shiftKey,
281
+ });
282
+ }
283
+
284
+ handleItemClick(e) {
285
+ let { instance, items } = this.props;
286
+ let index = Number(e.currentTarget.dataset.recordIndex);
287
+ let item = items[this.cursorChildIndex[index]];
288
+ if (instance.invoke("onItemClick", e, item.instance) === false) return;
289
+
290
+ this.moveCursor(index, {
291
+ select: true,
292
+ selectOptions: {
293
+ toggle: e.ctrlKey && !e.shiftKey,
294
+ add: e.ctrlKey && e.shiftKey,
295
+ },
296
+ selectRange: e.shiftKey,
297
+ });
298
+ }
299
+
300
+ handleItemDoubleClick(e) {
301
+ let { instance, items } = this.props;
302
+ let index = Number(e.currentTarget.dataset.recordIndex);
303
+ let item = items[this.cursorChildIndex[index]];
304
+ instance.invoke("onItemDoubleClick", e, item.instance);
305
+ }
306
+
307
+ render() {
308
+ let { instance, items, selectable } = this.props;
309
+ let { data, widget } = instance;
310
+ let { CSS, baseClass } = widget;
311
+ let itemStyle = CSS.parseStyle(data.itemStyle);
312
+ this.cursorChildIndex = [];
313
+ let cursorIndex = 0;
314
+
315
+ let onDblClick, onClick;
316
+
317
+ if (widget.onItemClick) onClick = this.handleItemClick;
318
+
319
+ if (widget.onItemDoubleClick) onDblClick = this.handleItemDoubleClick;
320
+
321
+ let children =
322
+ items.length > 0 &&
323
+ items.map((x, i) => {
324
+ let { data, selected } = x.instance;
325
+ let className;
326
+
327
+ if (x.type == "data") {
328
+ let ind = cursorIndex++;
329
+
330
+ this.cursorChildIndex.push(i);
331
+ className = CSS.element(baseClass, "item", {
332
+ selected: selected,
333
+ cursor: ind == this.state.cursor,
334
+ pad: widget.itemPad,
335
+ disabled: data.disabled,
336
+ });
337
+
338
+ return (
339
+ <li
340
+ key={x.key}
341
+ className={CSS.expand(className, data.classNames)}
342
+ style={itemStyle}
343
+ data-record-index={ind}
344
+ onMouseDown={this.handleItemMouseDown}
345
+ onClick={onClick}
346
+ onDoubleClick={onDblClick}
347
+ >
348
+ {x.content}
349
+ </li>
350
+ );
351
+ } else {
352
+ return (
353
+ <li key={x.key} className={CSS.element(baseClass, x.type)}>
354
+ {x.content}
355
+ </li>
356
+ );
357
+ }
358
+ });
359
+
360
+ if (!children && data.emptyText) {
361
+ children = <li className={CSS.element(baseClass, "empty-text")}>{data.emptyText}</li>;
362
+ }
363
+
364
+ return (
365
+ <ul
366
+ ref={(el) => {
367
+ this.el = el;
368
+ }}
369
+ className={CSS.expand(data.classNames, CSS.state({ focused: this.state.focused }))}
370
+ style={data.style}
371
+ tabIndex={widget.focusable && selectable && items.length > 0 ? data.tabIndex || 0 : null}
372
+ onMouseDown={preventFocusOnTouch}
373
+ onKeyDown={this.handleKeyDown.bind(this)}
374
+ onMouseLeave={this.handleMouseLeave.bind(this)}
375
+ onFocus={this.onFocus.bind(this)}
376
+ onBlur={this.onBlur.bind(this)}
377
+ >
378
+ {children}
379
+ </ul>
380
+ );
381
+ }
382
+
383
+ componentDidUpdate() {
384
+ let { widget } = this.props.instance;
385
+ if (widget.scrollSelectionIntoView) {
386
+ //The timeout is reqired for use-cases when parent needs to do some measuring that affect scrollbars, i.e. LookupField.
387
+ setTimeout(() => this.scrollElementIntoView(), 0);
388
+ }
389
+ }
390
+
391
+ scrollElementIntoView() {
392
+ if (!this.el) return; //unmount
393
+ let { widget } = this.props.instance;
394
+ let { CSS, baseClass } = widget;
395
+ let selectedRowSelector = `.${CSS.element(baseClass, "item")}.${CSS.state("selected")}`;
396
+ let firstSelectedRow = this.el.querySelector(selectedRowSelector);
397
+ if (firstSelectedRow != this.selectedEl) {
398
+ if (firstSelectedRow) scrollElementIntoView(firstSelectedRow, true, false, 0, this.el);
399
+ this.selectedEl = firstSelectedRow;
400
+ }
401
+ }
402
+
403
+ moveCursor(index, { focused, hover, scrollIntoView, select, selectRange, selectOptions } = {}) {
404
+ let { instance, selectable } = this.props;
405
+ if (!selectable) return;
406
+
407
+ let { widget } = instance;
408
+ let newState = {};
409
+ if (widget.focused) focused = true;
410
+
411
+ if (focused != null && this.state.focused != focused) newState.focused = focused;
412
+
413
+ //ignore mouse enter/leave events (support with a flag if a feature request comes)
414
+ if (!hover) newState.cursor = index;
415
+
416
+ //batch updates to avoid flickering between selection and cursor changes
417
+ batchUpdates(() => {
418
+ if (select || widget.selectMode) {
419
+ let start = selectRange && this.state.selectionStart >= 0 ? this.state.selectionStart : index;
420
+ if (start < 0) start = index;
421
+ this.selectRange(start, index, selectOptions);
422
+ if (!selectRange) newState.selectionStart = index;
423
+ }
424
+ if (Object.keys(newState).length > 0) {
425
+ this.setState(newState, () => {
426
+ if (scrollIntoView) {
427
+ let item = this.el.children[this.cursorChildIndex[index]];
428
+ if (item) scrollElementIntoView(item);
429
+ }
430
+ });
431
+ }
432
+ });
433
+ }
434
+
435
+ selectRange(from, to, options) {
436
+ let { instance, items } = this.props;
437
+ let { widget } = instance;
438
+
439
+ if (from > to) {
440
+ let tmp = from;
441
+ from = to;
442
+ to = tmp;
443
+ }
444
+
445
+ let selection = [],
446
+ indexes = [];
447
+
448
+ for (let cursor = from; cursor <= to; cursor++) {
449
+ let item = items[this.cursorChildIndex[cursor]];
450
+ if (item) {
451
+ let { record, data } = item.instance;
452
+ if (data.disabled) continue;
453
+ selection.push(record.data);
454
+ indexes.push(record.index);
455
+ }
456
+ }
457
+
458
+ widget.selection.selectMultiple(instance.store, selection, indexes, options);
459
+ }
460
+
461
+ showCursor(force, newItems) {
462
+ if (!force && this.state.cursor >= 0) return;
463
+
464
+ let items = newItems || this.props.items;
465
+ let index = -1,
466
+ firstSelected = -1,
467
+ firstValid = -1;
468
+ for (let i = 0; i < items.length; i++) {
469
+ let item = items[i];
470
+ if (isDataItem(item)) {
471
+ index++;
472
+
473
+ if (!isItemDisabled(item) && firstValid == -1) firstValid = index;
474
+ if (item.instance.selected) {
475
+ firstSelected = index;
476
+ break;
477
+ }
478
+ }
479
+ }
480
+ this.moveCursor(firstSelected != -1 ? firstSelected : firstValid, {
481
+ focusedport: true,
482
+ });
483
+ }
484
+
485
+ onFocus() {
486
+ let { widget } = this.props.instance;
487
+
488
+ FocusManager.nudge();
489
+ this.showCursor(widget.selectMode);
490
+
491
+ if (!widget.focused)
492
+ oneFocusOut(this, this.el, () => {
493
+ this.moveCursor(-1, { focused: false });
494
+ });
495
+
496
+ this.setState({
497
+ focused: true,
498
+ });
499
+ }
500
+
501
+ onBlur() {
502
+ FocusManager.nudge();
503
+ }
504
+
505
+ handleMouseLeave() {
506
+ let { widget } = this.props.instance;
507
+ if (!widget.focused) this.moveCursor(-1, { hover: true });
508
+ }
509
+
510
+ handleKeyDown(e) {
511
+ let { instance, items } = this.props;
512
+ let { widget } = instance;
513
+
514
+ if (this.onKeyDown && instance.invoke("onKeyDown", e, instance) === false) return;
515
+
516
+ switch (e.keyCode) {
517
+ case KeyCode.tab:
518
+ case KeyCode.enter:
519
+ if (!widget.selectOnTab && e.keyCode == KeyCode.tab) break;
520
+ let item = items[this.cursorChildIndex[this.state.cursor]];
521
+ if (item && widget.onItemClick && instance.invoke("onItemClick", e, item.instance) === false) return;
522
+ this.moveCursor(this.state.cursor, {
523
+ select: true,
524
+ selectOptions: {
525
+ toggle: e.ctrlKey && !e.shiftKey,
526
+ add: e.ctrlKey && e.shiftKey,
527
+ },
528
+ selectRange: e.shiftKey,
529
+ });
530
+ break;
531
+
532
+ case KeyCode.down:
533
+ for (let index = this.state.cursor + 1; index < this.cursorChildIndex.length; index++) {
534
+ let item = items[this.cursorChildIndex[index]];
535
+ if (!isItemSelectable(item)) continue;
536
+ this.moveCursor(index, {
537
+ focused: true,
538
+ scrollIntoView: true,
539
+ select: e.shiftKey,
540
+ selectRange: e.shiftKey,
541
+ });
542
+ e.stopPropagation();
543
+ e.preventDefault();
544
+ break;
545
+ }
546
+ break;
547
+
548
+ case KeyCode.up:
549
+ for (let index = this.state.cursor - 1; index >= 0; index--) {
550
+ let item = items[this.cursorChildIndex[index]];
551
+ if (!isItemSelectable(item)) continue;
552
+ this.moveCursor(index, {
553
+ focused: true,
554
+ scrollIntoView: true,
555
+ select: e.shiftKey,
556
+ selectRange: e.shiftKey,
557
+ });
558
+ e.stopPropagation();
559
+ e.preventDefault();
560
+ break;
561
+ }
562
+ break;
563
+
564
+ case KeyCode.a:
565
+ if (!e.ctrlKey || !widget.selection.multiple) return;
566
+
567
+ this.selectRange(0, this.cursorChildIndex.length);
568
+
569
+ e.stopPropagation();
570
+ e.preventDefault();
571
+ break;
572
+ }
573
+ }
574
+ }
575
+
576
+ class ListItem extends Container {
577
+ declareData(...args) {
578
+ super.declareData(...args, {
579
+ disabled: undefined,
580
+ });
581
+ }
582
+ }
583
+
584
+ function isItemSelectable(item) {
585
+ return isDataItem(item) && !isItemDisabled(item);
586
+ }
587
+
588
+ function isDataItem(item) {
589
+ return item?.type == "data";
590
+ }
591
+
592
+ function isItemDisabled(item) {
593
+ return item?.instance.data.disabled;
594
+ }