x4js 2.0.12 → 2.0.13

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 (108) hide show
  1. package/README.md +15 -15
  2. package/lib/README.txt +15 -15
  3. package/lib/cjs/x4.css +1 -1
  4. package/lib/cjs/x4.js +1 -1
  5. package/lib/esm/x4.css +1 -1
  6. package/lib/esm/x4.mjs +1 -1
  7. package/lib/src/components/base.scss +25 -26
  8. package/lib/src/components/boxes/boxes.module.scss +37 -37
  9. package/lib/src/components/boxes/boxes.ts +129 -125
  10. package/lib/src/components/breadcrumb/breadcrumb.scss +28 -0
  11. package/lib/src/components/breadcrumb/breadcrumb.ts +84 -0
  12. package/lib/src/components/breadcrumb/chevron-right.svg +1 -0
  13. package/lib/src/components/btngroup/btngroup.module.scss +28 -28
  14. package/lib/src/components/btngroup/btngroup.ts +119 -101
  15. package/lib/src/components/button/button.module.scss +154 -153
  16. package/lib/src/components/button/button.ts +117 -117
  17. package/lib/src/components/calendar/calendar.module.scss +162 -162
  18. package/lib/src/components/calendar/calendar.ts +326 -325
  19. package/lib/src/components/checkbox/check.svg +3 -3
  20. package/lib/src/components/checkbox/checkbox.module.scss +141 -141
  21. package/lib/src/components/checkbox/checkbox.ts +125 -124
  22. package/lib/src/components/colorinput/colorinput.module.scss +64 -64
  23. package/lib/src/components/colorinput/colorinput.ts +90 -87
  24. package/lib/src/components/colorpicker/colorpicker.module.scss +132 -132
  25. package/lib/src/components/colorpicker/colorpicker.ts +481 -476
  26. package/lib/src/components/combobox/combobox.module.scss +123 -120
  27. package/lib/src/components/combobox/combobox.ts +192 -190
  28. package/lib/src/components/combobox/updown.svg +3 -3
  29. package/lib/src/components/components.ts +34 -0
  30. package/lib/src/components/dialog/dialog.module.scss +71 -71
  31. package/lib/src/components/dialog/dialog.ts +94 -92
  32. package/lib/src/components/form/form.module.scss +34 -34
  33. package/lib/src/components/form/form.ts +41 -36
  34. package/lib/src/components/grid/datastore.ts +1298 -0
  35. package/lib/src/components/grid/gridview.ts +1108 -0
  36. package/lib/src/components/grid/memdb.ts +325 -0
  37. package/lib/src/components/header/header.module.scss +39 -39
  38. package/lib/src/components/header/header.ts +129 -123
  39. package/lib/src/components/icon/icon.module.scss +29 -29
  40. package/lib/src/components/icon/icon.ts +136 -134
  41. package/lib/src/components/image/image.module.scss +20 -20
  42. package/lib/src/components/image/image.ts +68 -66
  43. package/lib/src/components/input/input.module.scss +69 -69
  44. package/lib/src/components/input/input.ts +275 -274
  45. package/lib/src/components/label/label.module.scss +58 -52
  46. package/lib/src/components/label/label.ts +64 -55
  47. package/lib/src/components/link/link.ts +78 -0
  48. package/lib/src/components/listbox/listbox.module.scss +103 -103
  49. package/lib/src/components/listbox/listbox.ts +431 -427
  50. package/lib/src/components/menu/menu.module.scss +107 -107
  51. package/lib/src/components/menu/menu.ts +171 -168
  52. package/lib/src/components/messages/messages.module.scss +48 -47
  53. package/lib/src/components/messages/messages.ts +68 -63
  54. package/lib/src/components/normalize.scss +386 -386
  55. package/lib/src/components/notification/notification.module.scss +81 -81
  56. package/lib/src/components/notification/notification.ts +109 -108
  57. package/lib/src/components/panel/panel.module.scss +47 -47
  58. package/lib/src/components/panel/panel.ts +57 -56
  59. package/lib/src/components/popup/popup.module.scss +43 -43
  60. package/lib/src/components/popup/popup.ts +396 -395
  61. package/lib/src/components/progress/progress.module.scss +56 -56
  62. package/lib/src/components/progress/progress.ts +43 -42
  63. package/lib/src/components/rating/rating.module.scss +22 -22
  64. package/lib/src/components/rating/rating.ts +131 -125
  65. package/lib/src/components/shared.scss +90 -76
  66. package/lib/src/components/sizers/sizer.module.scss +89 -89
  67. package/lib/src/components/sizers/sizer.ts +123 -119
  68. package/lib/src/components/slider/slider.module.scss +70 -70
  69. package/lib/src/components/slider/slider.ts +147 -142
  70. package/lib/src/components/switch/switch.module.scss +126 -126
  71. package/lib/src/components/switch/switch.ts +61 -55
  72. package/lib/src/components/tabs/tabs.module.scss +46 -46
  73. package/lib/src/components/tabs/tabs.ts +168 -157
  74. package/lib/src/components/textarea/textarea.module.scss +59 -59
  75. package/lib/src/components/textarea/textarea.ts +60 -54
  76. package/lib/src/components/textedit/textedit.module.scss +113 -113
  77. package/lib/src/components/textedit/textedit.ts +83 -82
  78. package/lib/src/components/themes.scss +81 -77
  79. package/lib/src/components/tooltips/tooltips.scss +50 -50
  80. package/lib/src/components/tooltips/tooltips.ts +103 -102
  81. package/lib/src/components/treeview/treeview.module.scss +115 -115
  82. package/lib/src/components/treeview/treeview.ts +410 -403
  83. package/lib/src/components/viewport/viewport.module.scss +24 -24
  84. package/lib/src/components/viewport/viewport.ts +41 -38
  85. package/lib/src/core/component.ts +1002 -979
  86. package/lib/src/core/core_application.ts +44 -0
  87. package/lib/src/core/core_colors.ts +249 -249
  88. package/lib/src/core/core_dom.ts +471 -471
  89. package/lib/src/core/core_dragdrop.ts +200 -200
  90. package/lib/src/core/core_element.ts +97 -97
  91. package/lib/src/core/core_events.ts +149 -149
  92. package/lib/src/core/core_i18n.ts +377 -377
  93. package/lib/src/core/core_router.ts +221 -221
  94. package/lib/src/core/core_styles.ts +214 -214
  95. package/lib/src/core/core_svg.ts +550 -550
  96. package/lib/src/core/core_tools.ts +688 -673
  97. package/lib/src/demo/assets/radio.svg +3 -3
  98. package/lib/src/demo/index.html +11 -11
  99. package/lib/src/demo/main.scss +21 -21
  100. package/lib/src/demo/main.tsx +323 -323
  101. package/lib/src/types/scss.d.ts +4 -4
  102. package/lib/src/types/x4react.d.ts +8 -8
  103. package/lib/src/x4.scss +18 -18
  104. package/lib/src/x4.ts +31 -62
  105. package/lib/styles/x4.css +1 -1
  106. package/lib/types/x4js.d.ts +100 -49
  107. package/package.json +2 -3
  108. package/src/x4.ts +31 -62
@@ -0,0 +1,1108 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file gridview.ts
9
+ * @author Etienne Cochard
10
+ *
11
+ * @copyright (c) 2024 R-libre ingenierie
12
+ *
13
+ * Use of this source code is governed by an MIT-style license
14
+ * that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
15
+ **/
16
+
17
+ import { Component, ComponentEvents, componentFromDOM, ComponentProps, EvContextMenu, EvDblClick, EvSelectionChange, wrapDOM } from '@core/component.js';
18
+ import { CoreEvent } from '@core/core_events.js';
19
+ import { Label } from '../label/label.js';
20
+ import { Icon } from '../icon/icon.js';
21
+
22
+ import { HBox, VBox } from '../boxes/boxes.js';
23
+ import { _tr } from '@core/core_i18n.js';
24
+ import { isFunction, unsafeHtml, UnsafeHtml } from '@core/core_tools.js';
25
+
26
+ import icon_arrow_up from "./icon-arrow-up.svg"
27
+ import icon_arrow_down from "./icon-arrow-down.svg"
28
+ import { DataStore, DataView } from './datastore.js';
29
+
30
+
31
+ interface Record {}
32
+
33
+
34
+ export interface EvGridCheck extends CoreEvent {
35
+ rec: Record;
36
+ chk: boolean;
37
+ }
38
+
39
+
40
+
41
+
42
+
43
+ /**
44
+ *
45
+ */
46
+
47
+ interface GridColumn {
48
+ id: any;
49
+ title: string;
50
+ width: number;
51
+ flex?: number;
52
+ align?: 'left' | 'center' | 'right';
53
+ renderer?: CellRenderer;
54
+ //formatter?: FormatFunc;
55
+ cls?: string;
56
+ sortable?: boolean;
57
+ }
58
+
59
+ interface GridColumnInternal extends GridColumn {
60
+ $hdr: ColHeader;
61
+ $ftr: Component;
62
+ }
63
+
64
+ export type CellRenderer = (rec: Record) => Component;
65
+ export type RowClassifier = (rec: Record, Row: Component) => void;
66
+ export type ContextMenuGridItem = (event: MouseEvent, item: Record, grid: GridView) => any;
67
+
68
+ type emptyFn = () => string;
69
+
70
+ interface GridViewEvents extends ComponentEvents {
71
+ dblClick?: EvDblClick;
72
+ selectionChange?: EvSelectionChange;
73
+ contextMenu?: EvContextMenu;
74
+ gridCheck?: EvGridCheck;
75
+ }
76
+
77
+ export interface GridViewProps extends ComponentProps {
78
+ store: DataStore | DataView;
79
+ columns: GridColumn[];
80
+ calcRowClass?: RowClassifier;
81
+ empty_text?: string | emptyFn; // set or return '' to avoid message
82
+ hasMarks?: boolean; // if true add a checkbox on left side cf. clearMarks, getMarksIds
83
+ hasFooter?: boolean;
84
+ }
85
+
86
+ /**
87
+ *
88
+ */
89
+
90
+ class ColHeader extends Component<GridViewProps> {
91
+
92
+ private m_sens: "up" | "dn";
93
+ private m_sorted: boolean;
94
+ private m_sorter: Icon
95
+
96
+ constructor( props: GridViewProps, title: string ) {
97
+ super( props );
98
+
99
+ this.m_sorted = false;
100
+ this.m_sens = 'dn';
101
+
102
+ this.setContent( [
103
+ new Label({
104
+ tag: 'span',
105
+ content: title
106
+ }),
107
+ this.m_sorter = new Icon( {
108
+ cls: 'sort',
109
+ hidden: true,
110
+ iconId: icon_arrow_down
111
+ })
112
+ ]);
113
+ }
114
+
115
+ isSorted( ) {
116
+ return this.m_sorted;
117
+ }
118
+
119
+ //set sorted( v ) {
120
+ // this.m_sorted = v;
121
+ // this.m_sens = 'dn';
122
+ // this.itemWithRef<Icon>( 'sorter' ).show( v );
123
+ //}
124
+
125
+ sort( v: boolean, sens: "up" | "dn" ) {
126
+ this.m_sorted = v;
127
+ this.m_sens = sens;
128
+
129
+ this.m_sorter.setIcon( this.m_sens == 'up' ? icon_arrow_down : icon_arrow_up );
130
+ this.m_sorter.show(v);
131
+ }
132
+
133
+ get sens( ) {
134
+ return this.m_sens;
135
+ }
136
+
137
+ toggleSens( ) {
138
+ this.m_sens = this.m_sens=='up' ? 'dn' : 'up';
139
+ this.m_sorter.setIcon( this.m_sens == 'up' ? icon_arrow_down : icon_arrow_up );
140
+ }
141
+ }
142
+
143
+
144
+
145
+ /**
146
+ * gridview class
147
+ */
148
+
149
+ export class GridView extends VBox<GridViewProps, GridViewEvents> {
150
+
151
+ protected m_dataview: DataView;
152
+ protected m_data_cx: EventDisposer;
153
+
154
+ protected m_columns: GridColumn[];
155
+
156
+ protected m_view_el: Component;
157
+ protected m_container: Component;
158
+ protected m_header: Component;
159
+
160
+ protected m_footer: Component;
161
+
162
+ protected m_empty_msg: Label;
163
+ protected m_empty_text: string | emptyFn;
164
+
165
+ protected m_selection: any;
166
+ private m_itemHeight: number;
167
+ private m_topIndex: number;
168
+ protected m_visible_rows: Component[]; // shown elements
169
+
170
+ protected m_hasMarks: boolean;
171
+ protected m_marks: Set<any>; // checked elements
172
+
173
+ private m_recycler: Component[];
174
+
175
+ private m_rowClassifier: RowClassifier;
176
+
177
+ constructor(props: GridViewProps) {
178
+ super(props);
179
+
180
+ this.m_columns = props.columns;
181
+ this.m_hasMarks = props.hasMarks ?? false;
182
+ this.m_marks = new Set<any>();
183
+
184
+ // prepend the checkable column
185
+ if (this.m_hasMarks) {
186
+ this.m_columns.unshift({
187
+ id: 'id',
188
+ title: '',
189
+ width: 30,
190
+ renderer: (e) => this._renderCheck( e )
191
+ });
192
+ }
193
+
194
+ this.setAttribute('tabindex', 0);
195
+
196
+ this.m_topIndex = 0;
197
+ this.m_itemHeight = 0;
198
+ this.m_recycler = [];
199
+ this.m_rowClassifier = props.calcRowClass;
200
+
201
+ this.m_empty_text = props.empty_text ?? _tr.global.empty_list;
202
+
203
+ this.addDOMEvent('created', () => this._on_create() );
204
+ this.addDOMEvent('click', (e)=>this._on_click(e));
205
+ this.addDOMEvent('dblclick', (e)=>this._on_dblclick(e));
206
+ this.addDOMEvent('contextmenu', (e)=>this._on_menu(e));
207
+ this.addDOMEvent('keydown', (e)=>this._on_key(e));
208
+
209
+ this.setStore(props.store);
210
+ }
211
+
212
+ _on_create() {
213
+ this._updateScroll(true);
214
+ }
215
+
216
+ /**
217
+ *
218
+ */
219
+
220
+ private _moveSel(sens: number, select = true) {
221
+
222
+ let sel = this.m_selection;
223
+ let scrolltype = null;
224
+
225
+ if (sel === undefined) {
226
+ sel = this.m_dataview.getByIndex(0).getID();
227
+ }
228
+ else {
229
+
230
+ let index = this.m_dataview.indexOfId(this.m_selection);
231
+
232
+ if (sens == 1) {
233
+ index++;
234
+ }
235
+ else if (sens == -1) {
236
+ index--;
237
+ }
238
+ else if (sens == 2) {
239
+ index += this.m_visible_rows.length - 1;
240
+ }
241
+ else if (sens == -2) {
242
+ index -= this.m_visible_rows.length - 1;
243
+ }
244
+
245
+ if (sens < 0) {
246
+ scrolltype = 'start';
247
+ }
248
+ else {
249
+ scrolltype = 'end';
250
+ }
251
+
252
+ if (index < 0) {
253
+ index = 0;
254
+ }
255
+ else if (index >= this.m_dataview.getCount()) {
256
+ index = this.m_dataview.getCount() - 1;
257
+ }
258
+
259
+ sel = this.m_dataview.getByIndex(index).getID();
260
+ }
261
+
262
+ if (this.m_selection != sel && select) {
263
+ this._selectItem(sel, null, scrolltype);
264
+ }
265
+
266
+ return sel;
267
+ }
268
+
269
+ /**
270
+ *
271
+ */
272
+
273
+ private _on_key(event: KeyboardEvent) {
274
+ if (!this.m_dataview || this.m_dataview.getCount() == 0) {
275
+ return;
276
+ }
277
+
278
+ switch (event.key) {
279
+ case 'ArrowDown':
280
+ case 'Down': {
281
+ this._moveSel(1);
282
+ break;
283
+ }
284
+
285
+ case 'ArrowUp':
286
+ case 'Up': {
287
+ this._moveSel(-1);
288
+ break;
289
+ }
290
+
291
+ case 'PageUp': {
292
+ this._moveSel(-2);
293
+ break;
294
+ }
295
+
296
+ case 'PageDown': {
297
+ this._moveSel(2);
298
+ break;
299
+ }
300
+ }
301
+ }
302
+
303
+ /**
304
+ *
305
+ */
306
+
307
+ getNextSel(sens: number) {
308
+ return this._moveSel(sens, false);
309
+ }
310
+
311
+ private _scrollIntoView(id: any, sens?: string) {
312
+
313
+ let itm = this._findItem(id);
314
+ if (itm) {
315
+ itm.scrollIntoView({
316
+ block: 'center' //<ScrollLogicalPosition>sens ?? 'nearest'
317
+ });
318
+ }
319
+ else {
320
+ this.m_topIndex = this.m_dataview.indexOfId(id);
321
+ this.m_view_el.dom.scrollTop = this.m_topIndex * this.m_itemHeight;
322
+ this._buildItems();
323
+
324
+ this._scrollIntoView(id);
325
+ }
326
+
327
+ }
328
+
329
+ /**
330
+ * change the list of item displayed
331
+ * @param items - new array of items
332
+ */
333
+
334
+ public setStore(store: DataStore | DataView) {
335
+
336
+ this.m_selection = undefined;
337
+
338
+ if (store instanceof DataStore) {
339
+ this.m_dataview = store.createView();
340
+ }
341
+ else {
342
+ this.m_dataview = store;
343
+ }
344
+
345
+ if( this.m_hasMarks ) {
346
+ this.clearMarks( );
347
+ }
348
+
349
+ // unlink previous observer
350
+ if (this.m_data_cx) {
351
+ this.m_data_cx.dispose( );
352
+ }
353
+
354
+ if (this.m_dataview) {
355
+
356
+ this.m_data_cx = this.m_dataview.on( 'view_change', ( ev ) => {
357
+ if (ev.change_type == 'change') {
358
+ this.m_selection = undefined;
359
+ }
360
+
361
+ this._updateScroll(true);
362
+ });
363
+
364
+ //this.update( );
365
+ this._updateScroll(true);
366
+ }
367
+ }
368
+
369
+ public getView(): DataView {
370
+ return this.m_dataview;
371
+ }
372
+
373
+ /**
374
+ * return the current selection (row id) or null
375
+ */
376
+
377
+ public getSelection(): any {
378
+ return this.m_selection;
379
+ }
380
+
381
+ public getSelRec(): Record {
382
+ if (this.m_selection) {
383
+ return this.m_dataview.getById(this.m_selection);
384
+ }
385
+
386
+ return null;
387
+ }
388
+
389
+ public setSelection(recId: any) {
390
+ this._selectItem(recId, null, 'center');
391
+ }
392
+
393
+ /** @ignore */
394
+ render() {
395
+
396
+ this.m_recycler = [];
397
+ this.m_container = new Component({
398
+ cls: 'content',
399
+ });
400
+
401
+ this.m_empty_msg = new Label({
402
+ cls: 'empty-msg',
403
+ text: ''
404
+ });
405
+
406
+ this.m_view_el = new Component({
407
+ cls: '@scroll-view',
408
+ flex: 1,
409
+ dom_events: {
410
+ sizechange: ( ) => this._updateScroll(true),
411
+ scroll: ( ) => this._updateScroll(false)
412
+ },
413
+ content: this.m_container
414
+ });
415
+
416
+ let flex = false;
417
+ let cols = this.m_columns.map((col, index) => {
418
+
419
+ let cls = '@cell';
420
+ if (col.cls) {
421
+ cls += ' ' + col.cls;
422
+ }
423
+
424
+ let comp = new ColHeader({
425
+ cls,
426
+ flex: col.flex,
427
+ sizable: 'right',
428
+ style: {
429
+ width: col.width+"px"
430
+ },
431
+ dom_events: {
432
+ click: (ev: MouseEvent) => {
433
+ let t = wrapDOM(<HTMLElement>ev.target);
434
+ if (!t.hasClass('@sizer-overlay')) { // avoid sizer click
435
+ this._sortCol(col);
436
+ ev.preventDefault();
437
+ }
438
+ }
439
+ }
440
+ }, col.title );
441
+
442
+ const resizeCol = ( ev: EvSize ) => {
443
+ this._on_col_resize(index, ev.size.width);
444
+
445
+ if( this.m_footer ) {
446
+ let col = componentFromDOM( this.m_footer.dom.childNodes[index] as HTMLElement );
447
+ if( col ) {
448
+ col.setStyleValue( 'width', ev.size.width );
449
+ }
450
+ }
451
+ }
452
+
453
+ new SizerOverlay({
454
+ target: comp,
455
+ sens: 'right',
456
+ events: {resize: ( e ) => resizeCol(e )}
457
+ });
458
+
459
+ if( col.flex ) {
460
+ flex = true;
461
+ }
462
+
463
+ (<any>col).$hdr = comp;
464
+ return comp;
465
+ });
466
+
467
+ (cols as any).push( new Flex( {
468
+ ref: 'flex',
469
+ cls: flex ? '@hidden' : ''
470
+ } ) );
471
+
472
+ // compute full width
473
+ let full_width = 0;
474
+ this.m_columns.forEach((col) => {
475
+ full_width += col.width ?? 0;
476
+ });
477
+
478
+ this.m_header = new HBox({
479
+ cls: '@header',
480
+ content: <any>cols,
481
+ style: {
482
+ minWidth: full_width+"px"
483
+ }
484
+ });
485
+
486
+ if( this.props.hasFooter ) {
487
+ let foots = this.m_columns.map((col, index) => {
488
+
489
+ let cls = '@cell';
490
+
491
+ if (col.align) {
492
+ cls += ' ' + col.align;
493
+ }
494
+
495
+ if (col.cls) {
496
+ cls += ' ' + col.cls;
497
+ }
498
+
499
+ let comp = new Component({
500
+ cls,
501
+ data: { col: index },
502
+ flex: col.flex,
503
+ style: {
504
+ width: col.width+"px"
505
+ }
506
+ });
507
+
508
+ (col as GridColumnInternal).$ftr = comp;
509
+ return comp;
510
+ });
511
+
512
+ (foots as any).push( new Flex( {
513
+ ref: 'flex',
514
+ cls: flex ? '@hidden' : ''
515
+ } ) );
516
+
517
+ this.m_footer = new HBox({
518
+ cls: '@footer',
519
+ content: <any>foots,
520
+ style: {
521
+ minWidth: full_width+"px"
522
+ }
523
+ });
524
+ }
525
+ else {
526
+ this.m_footer = null;
527
+ }
528
+
529
+ this.setContent([
530
+ this.m_header,
531
+ this.m_view_el,
532
+ this.m_footer,
533
+ this.m_empty_msg,
534
+ ]);
535
+
536
+ }
537
+
538
+ private _on_col_resize(col: number, width: number) {
539
+
540
+ const _col = this.m_columns[col] as GridColumnInternal;
541
+
542
+ let updateFlex = false;
543
+
544
+ if( width>=0 ) {
545
+ _col.width = width;
546
+ if( _col.flex ) {
547
+ _col.$hdr.removeClass( '@flex' );
548
+ _col.$ftr?.removeClass( '@flex' );
549
+ _col.flex = undefined;
550
+ updateFlex = true;
551
+ }
552
+ }
553
+ else if( width<0 && !_col.flex ) {
554
+ _col.$hdr.addClass( '@flex' );
555
+ _col.$ftr?.addClass( '@flex' );
556
+ _col.flex = 1;
557
+ updateFlex = true;
558
+ }
559
+
560
+ if( updateFlex ) {
561
+ let flex = false;
562
+ this.m_columns.forEach( c => {
563
+ if( c.flex ) {
564
+ flex = true;
565
+ }
566
+ });
567
+
568
+ this.m_header.itemWithRef( 'flex' )?.show( flex ? false : true );
569
+ if( this.m_footer ) {
570
+ this.m_footer.itemWithRef( 'flex' )?.show( flex ? false : true );
571
+ }
572
+ }
573
+
574
+ this._updateScroll(true);
575
+ }
576
+
577
+ /**
578
+ *
579
+ */
580
+
581
+ sortCol( name: string, asc = true ) {
582
+ const col = this.m_columns.find(c => c.id==name );
583
+ if( col===undefined ) {
584
+ console.assert( false, "unknown field "+name+" in grid.sortCol" );
585
+ return;
586
+ }
587
+
588
+ this._sortCol( col, asc ? "dn" : "up" );
589
+ }
590
+
591
+ /**
592
+ *
593
+ */
594
+
595
+ private _sortCol(col: GridColumn, sens: "up" | "dn" = "up" ) {
596
+
597
+ if (col.sortable === false) {
598
+ return;
599
+ }
600
+
601
+ this.m_columns.forEach((c) => {
602
+ if (c !== col) {
603
+ (c as GridColumnInternal).$hdr.sort( false, "dn" );
604
+ }
605
+ });
606
+
607
+ const $hdr = (col as GridColumnInternal).$hdr;
608
+
609
+ if ($hdr.isSorted()) {
610
+ $hdr.toggleSens( );
611
+ }
612
+ else {
613
+ $hdr.sort( true, sens );
614
+ }
615
+
616
+ if (this.m_dataview) {
617
+ this.m_dataview.sort([
618
+ { field: col.id, ascending: $hdr.sens=='dn' ? false : true }
619
+ ]);
620
+ }
621
+ }
622
+
623
+ /**
624
+ *
625
+ */
626
+
627
+ private _computeItemHeight() {
628
+ let gr = document.createElement('div');
629
+ gr.classList.add('x-row');
630
+
631
+ let gv = document.createElement('div');
632
+ gv.classList.add('x-grid-view');
633
+ gv.style.position = 'absolute';
634
+ gv.style.top = '-1000px';
635
+ gv.appendChild(gr);
636
+
637
+ this.dom.appendChild(gv);
638
+ let rc = gr.getBoundingClientRect();
639
+ this.dom.removeChild(gv);
640
+
641
+ this.m_itemHeight = rc.height;
642
+ }
643
+
644
+ private _createRow( props: ComponentProps ): Component {
645
+
646
+ let row: Component;
647
+ if (this.m_recycler.length) {
648
+ row = this.m_recycler.pop();
649
+ row.clearClasses();
650
+ row.addClass( props.cls );
651
+ row.setContent( props.content );
652
+ row.setStyle( props.style );
653
+
654
+ for( let n in props.data ) {
655
+ row.setData( n, props.data[n] );
656
+ }
657
+ }
658
+ else {
659
+ row = new HBox( props );
660
+ }
661
+
662
+ if (!row.dom) {
663
+ this.m_container.appendContent(row);
664
+ }
665
+
666
+ return row;
667
+ }
668
+
669
+ private _buildItems( canOpt = true ) {
670
+ let rc = this.getBoundingRect();
671
+ let rh = this.m_header.getBoundingRect();
672
+ let height = rc.height - rh.height + this.m_itemHeight;
673
+
674
+ if (this.m_itemHeight == 0) {
675
+ this._computeItemHeight();
676
+ }
677
+
678
+ let top = this.m_topIndex * this.m_itemHeight;
679
+ let y = 0;
680
+ let cidx = 0;
681
+ let index = this.m_topIndex;
682
+ let count = this.m_dataview ? this.m_dataview.getCount() : 0;
683
+
684
+ let full_width = 0; // todo: +4 pixel of left border
685
+ let even = this.m_topIndex & 1 ? true : false;
686
+
687
+ // compute full width
688
+ this.m_columns.forEach((col) => {
689
+ full_width += col.width ?? 0;
690
+ });
691
+
692
+ // if items height make scroll visible, update header width
693
+ if (((count + 1) * this.m_itemHeight) >= height) {
694
+ let w = Component.getScrollbarSize();
695
+ this.m_header.setStyleValue("paddingRight", w);
696
+ this.m_footer?.setStyleValue("paddingRight", w);
697
+ }
698
+ else {
699
+ this.m_header.setStyleValue("paddingRight", 0);
700
+ this.m_footer?.setStyleValue("paddingRight", 0);
701
+ }
702
+
703
+ // passe 0 - all created cells are moved to the recycler
704
+ if( this.m_visible_rows ) {
705
+ this.m_visible_rows.forEach((c) => {
706
+ this.m_recycler.push(c);
707
+ });
708
+ }
709
+
710
+ this.m_visible_rows = [];
711
+ let limit = 100;
712
+ while (y < height && index < count && --limit > 0) {
713
+
714
+ let rec = this.m_dataview.getByIndex(index);
715
+ let rowid = rec.getID();
716
+
717
+ let crow = canOpt ? this.m_recycler.findIndex( ( r ) => r.getData('row-id')==rowid ) : -1;
718
+ if( crow>=0 ) {
719
+ let rrow = this.m_recycler.splice( crow, 1 )[ 0 ];
720
+ rrow.setStyle( {
721
+ top: (y + top)+"px",
722
+ minWidth: full_width+"px",
723
+ } );
724
+
725
+ if (this.m_hasMarks) {
726
+ rrow.setClass( '@marked', this.m_marks.has(rowid) );
727
+ }
728
+
729
+ rrow.removeClass( '@hidden' );
730
+ rrow.setClass( '@selected', this.m_selection === rowid );
731
+
732
+ this.m_visible_rows[cidx] = rrow;
733
+ }
734
+ else {
735
+ let cols = this.m_columns.map( col => {
736
+
737
+ let cls = '@cell';
738
+ if (col.align) {
739
+ cls += ' ' + col.align;
740
+ }
741
+
742
+ if (col.cls) {
743
+ cls += ' ' + col.cls;
744
+ }
745
+
746
+ let cell: Component;
747
+ if (col.renderer) {
748
+ cell = col.renderer(rec);
749
+ if (cell) {
750
+ cell.addClass(cls);
751
+ cell.setStyleValue('width', col.width);
752
+ if (col.flex !== undefined) {
753
+ cell.addClass('@flex');
754
+ cell.setStyleValue('flex', col.flex);
755
+ }
756
+ }
757
+ }
758
+ else {
759
+
760
+ let fmt = col.formatter;
761
+ let text;
762
+
763
+ if (fmt && fmt instanceof Function) {
764
+ text = fmt(rec.getRaw(col.id), rec);
765
+ }
766
+ else {
767
+ text = rec.getField(col.id);
768
+ }
769
+
770
+ cell = new Component({
771
+ cls,
772
+ width: col.width,
773
+ content: unsafeHtml( `<span>${text}</span>` ),
774
+ flex: col.flex
775
+ })
776
+ }
777
+
778
+ return cell;
779
+ });
780
+
781
+ let cls = '@row @hlayout center';
782
+ if (this.m_hasMarks) {
783
+ if (this.m_marks.has(rowid)) {
784
+ cls += ' @marked';
785
+ }
786
+ }
787
+
788
+ if (this.m_selection === rowid) {
789
+ cls += ' @selected';
790
+ }
791
+
792
+ let row = this.m_visible_rows[cidx] = this._createRow( {
793
+ cls,
794
+ content: <any>cols,
795
+ style: {
796
+ top: (y + top)+"px",
797
+ minWidth: full_width+"px",
798
+ },
799
+ data: {
800
+ 'row-id': rowid,
801
+ 'row-idx': index
802
+ }
803
+ });
804
+
805
+ row.addClass(even ? 'even' : 'odd');
806
+ even = !even;
807
+
808
+ if (this.m_rowClassifier) {
809
+ this.m_rowClassifier(rec, row);
810
+ }
811
+ }
812
+
813
+ y += this.m_itemHeight;
814
+
815
+ index++;
816
+ cidx++;
817
+ }
818
+
819
+ // if some cells are still in cache, hide them
820
+ this.m_recycler.forEach((c) => {
821
+ c.addClass('@hidden');
822
+ })
823
+
824
+ //this.m_container.setContent(<ComponentContent>this.m_visible_rows);
825
+
826
+ let show = !count;
827
+ let msg = (this.m_empty_text instanceof Function) ? this.m_empty_text() : this.m_empty_text;
828
+ this.m_empty_msg.setText( msg );
829
+
830
+ if (show && msg.length == 0) {
831
+ show = false;
832
+ }
833
+
834
+ this.m_empty_msg.show(show);
835
+
836
+ if (full_width < rc.width) {
837
+ this.m_header.setStyleValue('width', null);
838
+ this.m_footer?.setStyleValue('width', null);
839
+ this.m_container.setStyle({
840
+ height: (count * this.m_itemHeight)+"px",
841
+ width: null
842
+ });
843
+ }
844
+ else {
845
+ this.m_header.setStyleValue('width', full_width + 1000 );
846
+ this.m_footer?.setStyleValue('width', full_width + 1000 );
847
+ this.m_container.setStyle({
848
+ height: (count * this.m_itemHeight)+"px",
849
+ width: full_width+"px"
850
+ });
851
+ }
852
+ }
853
+
854
+ /**
855
+ *
856
+ */
857
+
858
+ private _updateScroll(forceUpdate: boolean ) {
859
+ if (!this.m_view_el || !this.m_view_el.dom) {
860
+ return;
861
+ }
862
+
863
+ const update = () => {
864
+
865
+ // element destroyed between updateScroll and now
866
+ if( !this.dom ) {
867
+ return;
868
+ }
869
+
870
+ let newTop = Math.floor(this.m_view_el.dom.scrollTop / (this.m_itemHeight || 1));
871
+
872
+ if (newTop != this.m_topIndex || forceUpdate) {
873
+ this.m_topIndex = newTop;
874
+ this._buildItems( !forceUpdate );
875
+ }
876
+
877
+ let newLeft = this.m_view_el.dom.scrollLeft;
878
+ this.m_header.setStyleValue('left', -newLeft);
879
+ this.m_footer?.setStyleValue('left', -newLeft);
880
+ }
881
+
882
+ if (forceUpdate) {
883
+ this.singleShot( update, 10 );
884
+ }
885
+ else {
886
+ update();
887
+ }
888
+ }
889
+
890
+ /** @ignore */
891
+
892
+ private _rowFromTarget(dom: Element ) {
893
+ let self = this.dom;
894
+
895
+ while (dom && dom != self) {
896
+ let itm = componentFromDOM(dom);
897
+
898
+ if (itm) {
899
+ let id = itm.getData('row-id');
900
+ if (id !== undefined) {
901
+ return { id, itm };
902
+ }
903
+ }
904
+
905
+ dom = dom.parentElement;
906
+ }
907
+
908
+ return undefined;
909
+ }
910
+
911
+ private _on_click(e: MouseEvent) {
912
+ let hit = this._rowFromTarget(e.target as Element);
913
+ if (hit) {
914
+ this._selectItem(hit.id, hit.itm);
915
+ }
916
+ else {
917
+ this._selectItem(undefined, undefined);
918
+ }
919
+ }
920
+
921
+ private _on_dblclick(e: MouseEvent) {
922
+ let hit = this._rowFromTarget(e.target as Element);
923
+ if (hit) {
924
+ this._selectItem(hit.id, hit.itm);
925
+
926
+ let rec = this.m_dataview.getById(hit.id);
927
+ this.fire( 'dblClick', {context:rec} );
928
+
929
+ if (this.m_hasMarks) {
930
+ this._toggleMark(rec);
931
+ }
932
+ }
933
+
934
+ }
935
+
936
+ /** @ignore */
937
+ private _on_menu(e: MouseEvent) {
938
+
939
+ let dom = e.target as HTMLElement,
940
+ self = this.dom;
941
+
942
+ while (dom && dom != self) {
943
+ let itm = componentFromDOM(dom),
944
+ id = itm?.getData('row-id');
945
+
946
+ if (id !== undefined) {
947
+ this._selectItem(id, itm);
948
+
949
+ let idx = itm.getData('row-idx');
950
+ let rec = this.m_dataview.getByIndex(idx);
951
+
952
+ this._showItemContextMenu(e, rec);
953
+ e.preventDefault();
954
+ return;
955
+ }
956
+
957
+ dom = dom.parentElement;
958
+ }
959
+ }
960
+
961
+ /**
962
+ *
963
+ */
964
+
965
+ private _findItem(id: string): Component {
966
+
967
+ for (let i = 0; i < this.m_visible_rows.length; i++) {
968
+ let itm = this.m_visible_rows[i];
969
+ if (itm.getData('row-id') === id) {
970
+ return itm;
971
+ }
972
+ }
973
+
974
+ return null;
975
+ }
976
+
977
+ /**
978
+ * @ignore
979
+ * called when an item is selected by mouse
980
+ */
981
+
982
+ protected _selectItem(item: any, dom_item: Component, scrollIntoView?: string) {
983
+
984
+ if (this.m_selection !== undefined) {
985
+ let old = this._findItem(this.m_selection);
986
+ if (old) {
987
+ old.removeClass('@selected');
988
+ }
989
+ }
990
+
991
+ this.m_selection = item;
992
+
993
+ if (item) {
994
+ if (scrollIntoView) {
995
+ this._scrollIntoView(item, scrollIntoView);
996
+ }
997
+
998
+ if (!dom_item) {
999
+ dom_item = this._findItem(item);
1000
+ }
1001
+
1002
+ if (dom_item) {
1003
+ dom_item.addClass('@selected');
1004
+ }
1005
+
1006
+ let rec = this.m_dataview.getById(item);
1007
+ this.fire( 'selectionChange', {selection:rec});
1008
+ }
1009
+ else {
1010
+ this.fire( 'selectionChange', {selection:null});
1011
+ }
1012
+ }
1013
+
1014
+ /**
1015
+ *
1016
+ */
1017
+
1018
+ protected _showItemContextMenu(event: MouseEvent, item: Record) {
1019
+ this.fire( 'contextMenu', {uievent:event,context:item});
1020
+ }
1021
+
1022
+ /**
1023
+ *
1024
+ */
1025
+
1026
+ public clearSelection() {
1027
+ this._selectItem(null, null);
1028
+ }
1029
+
1030
+ setEmptyText(text: string | UnsafeHtml) {
1031
+ this.m_empty_msg.setText( text );
1032
+ }
1033
+
1034
+ private _renderCheck(rec: Record) {
1035
+ let icon = '--x4-icon-square';
1036
+ if (this.m_marks.has(rec.getID())) {
1037
+ icon = '--x4-icon-square-check';
1038
+ }
1039
+
1040
+ return new Icon({ iconId: `var(${icon})` });
1041
+ }
1042
+
1043
+ private _toggleMark(rec: Record) {
1044
+
1045
+ let id = rec.getID();
1046
+ let chk = false;
1047
+
1048
+ if (this.m_marks.has(id)) {
1049
+ this.m_marks.delete(id);
1050
+ }
1051
+ else {
1052
+ this.m_marks.add(id);
1053
+ chk = true;
1054
+ }
1055
+
1056
+ this.fire( 'gridCheck',{rec:rec, chk:chk });
1057
+ this._buildItems( false );
1058
+ }
1059
+
1060
+ public getMarks(): any[] {
1061
+ let ids = [];
1062
+ for (const v of this.m_marks.values()) {
1063
+ ids.push(v);
1064
+ }
1065
+
1066
+ return ids;
1067
+ }
1068
+
1069
+ public clearMarks() {
1070
+ if (this.m_marks.size) {
1071
+ this.m_marks = new Set<any>();
1072
+ this._buildItems( false );
1073
+ }
1074
+ }
1075
+
1076
+ public setFooterData( rec: any ) {
1077
+ if( !this.m_footer ) {
1078
+ return;
1079
+ }
1080
+
1081
+ this.m_footer.enumChilds( (c) => {
1082
+ let cid = c.getData( 'col' );
1083
+ if( cid ) {
1084
+ let col = this.m_columns[cid];
1085
+
1086
+ let value = rec[col.id];
1087
+ if( value!==undefined ) {
1088
+ if( isFunction(value) ) { // FooterRenderer
1089
+ value( c );
1090
+ }
1091
+ else {
1092
+ let text;
1093
+ const fmt = col.formatter;
1094
+ if (fmt && fmt instanceof Function) {
1095
+ text = fmt(value, rec);
1096
+ }
1097
+ else {
1098
+ text = value;
1099
+ }
1100
+
1101
+ c.setContent( text, false );
1102
+ }
1103
+ }
1104
+ }
1105
+ });
1106
+ }
1107
+ }
1108
+