x4js 2.0.26 → 2.0.30

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 (73) hide show
  1. package/.vscode/launch.json +14 -0
  2. package/.vscode/settings.json +2 -0
  3. package/ai-comments.txt +97 -0
  4. package/demo/assets/house-light.svg +1 -0
  5. package/demo/assets/radio.svg +4 -0
  6. package/demo/index.html +12 -0
  7. package/demo/main.scss +23 -0
  8. package/demo/main.ts +324 -0
  9. package/demo/package.json +26 -0
  10. package/demo/scss.d.ts +4 -0
  11. package/demo/svg.d.ts +1 -0
  12. package/demo/tsconfig.json +14 -0
  13. package/lib/types/x4js.d.ts +0 -2374
  14. package/package.json +23 -47
  15. package/prepack.mjs +3 -0
  16. package/scripts/prepack.mjs +342 -0
  17. package/src/colors.scss +246 -0
  18. package/src/components/boxes/boxes.module.scss +1 -1
  19. package/src/components/boxes/boxes.ts +139 -28
  20. package/src/components/button/button.ts +80 -33
  21. package/src/components/combobox/combobox.ts +1 -1
  22. package/src/components/dialog/dialog.ts +4 -0
  23. package/src/components/gauge/gauge.module.scss +3 -0
  24. package/src/components/gauge/gauge.ts +1 -1
  25. package/src/components/gridview/gridview.ts +106 -8
  26. package/src/components/icon/icon.ts +42 -14
  27. package/src/components/input/input.ts +155 -76
  28. package/src/components/keyboard/keyboard.module.scss +1 -1
  29. package/src/components/keyboard/keyboard.ts +31 -9
  30. package/src/components/label/label.module.scss +9 -0
  31. package/src/components/label/label.ts +10 -6
  32. package/src/components/link/link.module.scss +44 -0
  33. package/src/components/link/link.ts +7 -1
  34. package/src/components/listbox/listbox.module.scss +18 -4
  35. package/src/components/listbox/listbox.ts +34 -15
  36. package/src/components/menu/menu.module.scss +14 -2
  37. package/src/components/menu/menu.ts +1 -1
  38. package/src/components/messages/messages.ts +13 -5
  39. package/src/components/panel/panel.module.scss +7 -0
  40. package/src/components/popup/popup.ts +14 -10
  41. package/src/components/propgrid/propgrid.ts +13 -3
  42. package/src/components/shared.scss +4 -0
  43. package/src/components/spreadsheet/spreadsheet.module.scss +308 -0
  44. package/src/components/spreadsheet/spreadsheet.ts +1223 -0
  45. package/src/components/tabs/tabs.module.scss +1 -0
  46. package/src/components/textarea/textarea.ts +8 -2
  47. package/src/components/textedit/textedit.ts +7 -0
  48. package/src/components/themes.scss +2 -0
  49. package/src/components/tooltips/tooltips.ts +15 -3
  50. package/src/core/component.ts +358 -162
  51. package/src/core/core_application.ts +129 -32
  52. package/src/core/core_colors.ts +382 -119
  53. package/src/core/core_data.ts +73 -86
  54. package/src/core/core_dom.ts +10 -0
  55. package/src/core/core_dragdrop.ts +32 -7
  56. package/src/core/core_element.ts +111 -4
  57. package/src/core/core_events.ts +48 -11
  58. package/src/core/core_i18n.ts +2 -0
  59. package/src/core/core_pdf.ts +454 -0
  60. package/src/core/core_router.ts +64 -5
  61. package/src/core/core_state.ts +1 -0
  62. package/src/core/core_styles.ts +11 -12
  63. package/src/core/core_svg.ts +348 -51
  64. package/src/core/core_tools.ts +105 -17
  65. package/src/x4.ts +1 -0
  66. package/src/x4tsx.d.ts +2 -1
  67. package/tsconfig.json +11 -0
  68. package/lib/README.txt +0 -20
  69. package/lib/cjs/x4.css +0 -1
  70. package/lib/cjs/x4.js +0 -2
  71. package/lib/esm/x4.css +0 -1
  72. package/lib/esm/x4.mjs +0 -2
  73. package/lib/styles/x4.css +0 -1
@@ -0,0 +1,1223 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file spreadsheet.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
+
18
+ import { Component, ComponentContent, ComponentEvents, ComponentProps, EvClick, EvContextMenu, EvDblClick, EvSelectionChange, componentFromDOM } from '../../core/component';
19
+ import { GridColumn } from '../gridview/gridview'
20
+
21
+ import { class_ns, isNumber, isString, UnsafeHtml } from '../../core/core_tools';
22
+ import { CoreEvent, EventCallback, EventMap } from '../../core/core_events';
23
+ import { kbNav } from '../../core/core_tools';
24
+
25
+ import { Icon } from '../icon/icon';
26
+ import { Image } from '../image/image'
27
+ import { Box } from '../boxes/boxes';
28
+ import { CSizer } from '../sizers/sizer'
29
+ import { Viewport } from '../viewport/viewport';
30
+ import { SimpleText } from '../label/label';
31
+
32
+ import check_icon from "../checkbox/check.svg";
33
+ import "./spreadsheet.module.scss"
34
+ import { CoreElement } from '../../x4.js';
35
+
36
+ interface CellRef {
37
+ col: number;
38
+ row: number;
39
+ }
40
+
41
+ export type CellClassifier = ( row: number, col: number ) => string; // return the cell computed class
42
+ export type RowClassifier = (row: number ) => string; // return the row computed class
43
+ export type CellRenderer = (row: number, col: number, content: any) => Component;
44
+
45
+ export interface SpreadsheetColumn extends Omit<GridColumn,"classifier"> {
46
+ cellClassifier?: CellClassifier;
47
+ }
48
+
49
+
50
+ function mkid(row: number, col: number) {
51
+ return ((row & 0xfffff) << 12) | (col & 0xfff);
52
+ }
53
+
54
+ /**
55
+ *
56
+ */
57
+
58
+ export interface EvChange extends CoreEvent {
59
+ }
60
+
61
+ export interface StoreEvents extends EventMap {
62
+ changed: EvChange;
63
+ }
64
+
65
+ export class Store extends CoreElement<StoreEvents> {
66
+ private _maxrows: number;
67
+ private _data: Map<number, any>;
68
+ private _lock: number; // lock
69
+ private _change: boolean;
70
+
71
+ constructor() {
72
+ super();
73
+
74
+ this._data = new Map();
75
+ this._lock = 0;
76
+ this._change = false;
77
+ this._maxrows = 0;
78
+ }
79
+
80
+ setMaxRowCount(rows: number) {
81
+
82
+ if (this._maxrows == rows) {
83
+ return
84
+ }
85
+
86
+ if (rows < this._maxrows) {
87
+ const n = new Map<number, any>();
88
+ this._data.forEach((v, k) => {
89
+ const row = k >> 12;
90
+ if (row <= rows) {
91
+ n.set(k, v);
92
+ }
93
+ });
94
+ this._data = n;
95
+ }
96
+
97
+ this._maxrows = rows;
98
+ this._changed()
99
+ }
100
+
101
+ getRowCount(): number {
102
+ return this._maxrows;
103
+ }
104
+
105
+ setData(row: number, col: number, data: any) {
106
+ this._data.set(mkid(row, col), data);
107
+ if (row > this._maxrows) {
108
+ this._maxrows = row;
109
+ }
110
+
111
+ this._changed();
112
+ }
113
+
114
+ getData(row: number, col: number) {
115
+ return this._data.get(mkid(row, col));
116
+ }
117
+
118
+ hasData( row: number, col?: number ) {
119
+ return this._data.has( col===undefined ? row : mkid(row, col));
120
+ }
121
+
122
+ lock() {
123
+ this._lock++;
124
+ }
125
+
126
+ unlock() {
127
+ if (this._lock) {
128
+ this._lock--;
129
+ if (!this._lock && this._change) {
130
+ this._changed();
131
+ }
132
+ }
133
+ }
134
+
135
+ removeRow( row_num: number ) {
136
+
137
+ if( row_num>=this._maxrows ) {
138
+ return;
139
+ }
140
+
141
+ const n = new Map<number, any>();
142
+ this._data.forEach( (v, k) => {
143
+ const row = k >> 12;
144
+ if (row != row_num) {
145
+ if( row>row_num ) {
146
+ k = mkid(row-1,k&0xfff)
147
+ }
148
+
149
+ n.set(k, v);
150
+ }
151
+ } );
152
+ this._data = n;
153
+
154
+ this._maxrows--;
155
+ this._changed( );
156
+ }
157
+
158
+ private _changed() {
159
+ if (!this._lock) {
160
+ this.fire("changed", {});
161
+ this._change = false;
162
+ }
163
+ else {
164
+ this._change = true;
165
+ }
166
+ }
167
+
168
+ clear( ) {
169
+ this._data = new Map();
170
+ this._maxrows = 0;
171
+ this._changed( );
172
+ }
173
+ }
174
+
175
+
176
+ /**
177
+ *
178
+ */
179
+
180
+ const SCROLL_LIMIT = 200;
181
+
182
+ export interface SpreadsheetEvents extends ComponentEvents {
183
+ click?: EvClick;
184
+ dblClick?: EvDblClick;
185
+ contextMenu?: EvContextMenu;
186
+ selectionChange?: EvSelectionChange;
187
+ }
188
+
189
+ export interface SpreadsheetProps extends ComponentProps {
190
+ footer?: boolean;
191
+ store: Store;
192
+ columns: SpreadsheetColumn[];
193
+ rowClassifier?: RowClassifier;
194
+
195
+
196
+ click?: EventCallback<EvClick>;
197
+ dblClick?: EventCallback<EvDblClick>;
198
+ contextMenu?: EventCallback<EvContextMenu>;
199
+ selectionChange?: EventCallback<EvSelectionChange>;
200
+ }
201
+
202
+ /**
203
+ * we can handle
204
+ * 4_095 cols and (1_048_575-1)/2 rows (this is a chrome limitation max pixels of scrollbars )
205
+ */
206
+
207
+ /**
208
+ *
209
+ */
210
+
211
+ @class_ns("x4")
212
+ export class Spreadsheet<P extends SpreadsheetProps = SpreadsheetProps, E extends SpreadsheetEvents = SpreadsheetEvents> extends Component<P, E> {
213
+
214
+ private _columns: SpreadsheetColumn[];
215
+ private _store: Store;
216
+
217
+ private _lock: number;
218
+ private _dirty: number;
219
+
220
+ private _row_height: number;
221
+
222
+ private _left: number;
223
+ private _top: number;
224
+
225
+ private _body: Component;
226
+ private _viewport: Component;
227
+
228
+ private _fheader: Box; // fixed col header
229
+ private _hheader: Box; // col header
230
+ private _vheader: Box; // vertical row header
231
+ private _ffooter: Box; // fixed footer
232
+ private _footer: Box; // footer
233
+
234
+ private _vis_rows: Map<number, { h: Component, r: Component }>;
235
+ private _start: number;
236
+ private _end: number;
237
+
238
+ private _selection: Set<number>;
239
+ private _num_fmt = new Intl.NumberFormat('fr-FR');
240
+ private _mny_fmt = new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR' });
241
+ private _dte_fmt = new Intl.DateTimeFormat('fr-FR', {});
242
+
243
+ private _has_fixed: boolean;
244
+ private _has_footer: boolean;
245
+
246
+ constructor(props: P) {
247
+ super(props);
248
+
249
+ this._lock = 0;
250
+ this._dirty = 0;
251
+
252
+ this._row_height = 32;
253
+
254
+ this._left = 0;
255
+ this._top = 0;
256
+
257
+ this._vis_rows = new Map();
258
+ this._selection = new Set();
259
+ this._has_fixed = false;
260
+ this._has_footer = props.footer;
261
+
262
+ this._columns = props.columns.map(x => x);
263
+
264
+ this.mapPropEvents(props, "click", "dblClick", "contextMenu", "selectionChange");
265
+
266
+ this.lock(true);
267
+ this.setAttribute("tabindex", 0);
268
+
269
+ this.addDOMEvent("created", () => {
270
+ this._init();
271
+ this._dirty = 1;
272
+ this.lock(false);
273
+ });
274
+
275
+ this.addDOMEvent("resized", () => {
276
+ this._updateFlexs();
277
+ this._computeFullSize();
278
+ this._update(true);
279
+ });
280
+
281
+ this.addDOMEvent("keydown", (e) => {
282
+ this._on_key(e);
283
+ })
284
+
285
+ if (props.store) {
286
+ this.setStore(props.store);
287
+ }
288
+
289
+ }
290
+
291
+ /**
292
+ *
293
+ */
294
+
295
+ private _on_key(ev: KeyboardEvent) {
296
+
297
+ if (this.isDisabled()) {
298
+ return;
299
+ }
300
+
301
+ switch (ev.key) {
302
+ case "ArrowDown": {
303
+ this.navigate(kbNav.next);
304
+ break;
305
+ }
306
+
307
+ case "ArrowUp": {
308
+ this.navigate(kbNav.prev);
309
+ break;
310
+ }
311
+
312
+ case "ArrowLeft": {
313
+ this.navigate(kbNav.left);
314
+ break;
315
+ }
316
+
317
+ case "ArrowRight": {
318
+ this.navigate(kbNav.right);
319
+ break;
320
+ }
321
+
322
+ case "Home": {
323
+ this.navigate(kbNav.first);
324
+ break;
325
+ }
326
+
327
+ case "End": {
328
+ this.navigate(kbNav.last);
329
+ break;
330
+ }
331
+
332
+ case "PageDown": {
333
+ this.navigate(kbNav.pgdn);
334
+ break;
335
+ }
336
+
337
+ case "PageUp": {
338
+ this.navigate(kbNav.pgup);
339
+ break;
340
+ }
341
+
342
+ default:
343
+ return;
344
+ }
345
+
346
+ ev.preventDefault();
347
+ ev.stopPropagation();
348
+ }
349
+
350
+ /**
351
+ *
352
+ */
353
+
354
+ navigate(sens: kbNav) {
355
+ if (!this._selection.size) {
356
+ if (sens == kbNav.next || sens == kbNav.pgdn) {
357
+ sens = kbNav.first;
358
+ }
359
+ else {
360
+ sens = kbNav.last;
361
+ }
362
+ }
363
+
364
+ const getLineSel = ( top: boolean ) => {
365
+ let m: number, M: number;
366
+ let col: number;
367
+
368
+ this._selection.forEach( x => {
369
+ const row = x>>12;
370
+ if( m===undefined || m>row ) { m = row; col=x&0xff; }
371
+ if( M===undefined || M<row ) { M = row; col=x&0xff; }
372
+ } );
373
+
374
+ return [top ? m : M, col]
375
+ }
376
+
377
+ if (sens == kbNav.first || sens == kbNav.last) {
378
+ let nline = sens == kbNav.first ? 0 : this._store.getRowCount() - 1;
379
+ this._clearSelection(false);
380
+ this._addSelection(mkid(nline,0), true);
381
+ this._scrollToIndex(nline);
382
+ return true;
383
+ }
384
+ else if (sens == kbNav.prev || sens == kbNav.next) {
385
+
386
+ const [fline,col] = getLineSel( sens == kbNav.prev );
387
+
388
+ let nline = sens == kbNav.next ? fline + 1 : fline - 1;
389
+ if (nline >= 0 && nline < this._store.getRowCount()) {
390
+ this._clearSelection(false);
391
+ this._addSelection( mkid(nline,col), true);
392
+ this._scrollToIndex( nline );
393
+ return true;
394
+ }
395
+ }
396
+ else if (sens == kbNav.pgdn || sens == kbNav.pgup) {
397
+ const pgh = this._vis_rows.size;
398
+ const [fline,col] = getLineSel( sens == kbNav.pgup );
399
+
400
+ let sby = sens == kbNav.pgdn ? pgh : -pgh;
401
+ let nline = fline + sby;
402
+
403
+ if (nline < 0) {
404
+ nline = 0;
405
+ }
406
+ else if (nline >= this._store.getRowCount()) {
407
+ nline = this._store.getRowCount() - 1;
408
+ }
409
+
410
+ if (nline != fline) {
411
+ this._clearSelection( false );
412
+ this._addSelection(mkid(nline,col), true);
413
+
414
+ if (this._store.getRowCount() < SCROLL_LIMIT) {
415
+ sby *= this._row_height;
416
+ }
417
+
418
+ this._viewport.dom.scrollBy(0, sby);
419
+ return true;
420
+ }
421
+ }
422
+ else if( sens==kbNav.left || sens==kbNav.right ) {
423
+ const [fline,col] = getLineSel( sens == kbNav.left );
424
+
425
+ let ncol = sens == kbNav.right ? col+1 : col-1;
426
+ if (ncol >= 0 && ncol < this._columns.length ) {
427
+ this._clearSelection(false);
428
+ this._addSelection( mkid(fline,ncol), true);
429
+ //this._scrollToIndex( nline );
430
+ return true;
431
+ }
432
+ }
433
+
434
+ return false;
435
+ }
436
+
437
+ /**
438
+ *
439
+ */
440
+
441
+ private _scrollToIndex(index: number, block = 'nearest') {
442
+
443
+ // is it already visible ?
444
+ let ref = mkid(index,0);
445
+ let rows = this.queryAll(`.cell[data-ref="${ref}"]`);
446
+ if (rows.length) {
447
+ rows[0].scrollIntoView({ block: block as any });
448
+ }
449
+ // nope, refill
450
+ else {
451
+ let top = index;
452
+ if (this._store.getRowCount() < SCROLL_LIMIT) {
453
+ top *= this._row_height;
454
+ }
455
+
456
+ this._viewport.dom.scrollTo(0, top);
457
+ }
458
+ }
459
+
460
+ /**
461
+ *
462
+ */
463
+
464
+ setStore(store: Store) {
465
+
466
+ const on_change = (ev: EvChange) => {
467
+ if (!this._viewport) {
468
+ // not created
469
+ return;
470
+ }
471
+
472
+ // try to keep selection
473
+ if (ev.type == 'changed' && this._selection.size ) {
474
+ const nsel = new Set<number>();
475
+ this._selection.forEach(x => {
476
+ if( this._store.hasData( x ) ) {
477
+ nsel.add( x );
478
+ }
479
+ });
480
+
481
+ this._selection = nsel;
482
+ }
483
+
484
+ this._updateFlexs();
485
+ this._computeFullSize();
486
+ this._update(true);
487
+ }
488
+
489
+ // unlink previous observer
490
+ if (this._store) {
491
+ this._store.off('changed', on_change);
492
+ }
493
+
494
+ if (store) {
495
+ this._store = store;
496
+ this._store.on('changed', on_change);
497
+ }
498
+ else {
499
+ this._store = null;
500
+ }
501
+ }
502
+
503
+ /**
504
+ *
505
+ */
506
+
507
+ lock(lock: boolean) {
508
+ if (lock) {
509
+ this._lock++;
510
+ }
511
+ else {
512
+ if (--this._lock == 0 && this._dirty) {
513
+ this._update(true);
514
+ }
515
+ }
516
+ }
517
+
518
+ private _getColCount() {
519
+ return this._columns.length;
520
+ }
521
+
522
+ private _getCol(index: number) {
523
+ return this._columns[index];
524
+ }
525
+
526
+ /**
527
+ *
528
+ */
529
+
530
+ private _buildColHeader(fixed: boolean) {
531
+ // row header
532
+ const els: Component[] = [];
533
+
534
+ const count = this._getColCount();
535
+ for (let col = 0; col < count; col++) {
536
+ const cdata = this._getCol(col);
537
+ if ((!!cdata.fixed) != fixed) {
538
+ continue;
539
+ }
540
+
541
+ const sizer = new CSizer("right");
542
+
543
+ sizer.on("stop", () => {
544
+ this._updateFlexs();
545
+ })
546
+
547
+ sizer.on("resize", (ev) => {
548
+ cdata.width = ev.size;
549
+ cdata.flex = 0;
550
+
551
+ const cols = this.queryAll(`[data-col="${col}"]`)
552
+ cols.forEach(c => {
553
+ c.setStyleValue("width", ev.size + "px");
554
+ });
555
+
556
+ const rh = header.getBoundingRect();
557
+
558
+ if (!fixed) {
559
+ this._body.setStyleValue("width", rh.width + "px");
560
+ }
561
+ else {
562
+ this.setStyleVariable("--fixed-width", rh.width + "px");
563
+ }
564
+ })
565
+
566
+ const cell = new Component({
567
+ cls: `cell`,
568
+ attrs: { "data-col": col },
569
+ style: { width: cdata.width ? cdata.width + "px" : undefined },
570
+ content: [
571
+ new SimpleText({ text: cdata.title, align: cdata.header_align ?? "left" }),
572
+ new Component({ cls: "sorter" }),
573
+ sizer
574
+ ]
575
+ });
576
+
577
+ /*
578
+ cell.addDOMEvent("touchend", () => {
579
+ const last = cell.getInternalData("touchend");
580
+ const now = Date.now();
581
+ const delta = last ? now - last : 0;
582
+ if (delta > 30 && delta < 300) {
583
+ this._sortCol(col);
584
+ }
585
+ else {
586
+ cell.setInternalData("touchend", now);
587
+ }
588
+ })
589
+
590
+ cell.addDOMEvent("dblclick", () => {
591
+ this._sortCol(col);
592
+ });
593
+ */
594
+
595
+ els.push(cell);
596
+ }
597
+
598
+ if (fixed && els.length == 0) {
599
+ return null;
600
+ }
601
+
602
+ const header = new Box({ cls: "col-header", content: els });
603
+ header.setClass("fixed", fixed);
604
+
605
+ return header;
606
+ }
607
+
608
+ /**
609
+ *
610
+ */
611
+
612
+ private _buildColFooter(fixed: boolean) {
613
+ // row header
614
+ const els: Component[] = [];
615
+
616
+ const count = this._getColCount();
617
+ for (let col = 0; col < count; col++) {
618
+ const cdata = this._getCol(col);
619
+ if ((!!cdata.fixed) != fixed) {
620
+ continue;
621
+ }
622
+
623
+ const cell = new Component({
624
+ cls: `cell`,
625
+ attrs: { "data-col": col },
626
+ style: { width: cdata.width ? cdata.width + "px" : undefined },
627
+ content: [
628
+ new SimpleText({ text: cdata.footer_val }),
629
+ ]
630
+ });
631
+
632
+ /*
633
+ cell.addDOMEvent("dblclick", () => {
634
+ this._sortCol(col);
635
+ });
636
+ */
637
+
638
+ els.push(cell);
639
+ }
640
+
641
+ if (fixed && els.length == 0) {
642
+ return null;
643
+ }
644
+
645
+ const header = new Box({ cls: "col-footer", content: els });
646
+ header.setClass("fixed", fixed);
647
+
648
+ return header;
649
+ }
650
+ /**
651
+ * extra_cls est input/output
652
+ */
653
+
654
+ private _renderCell(row: number, column: SpreadsheetColumn, extra_cls: string[]): ComponentContent {
655
+
656
+ const col = column.id;
657
+ const type = column.type;
658
+
659
+ let data = this._store.getData( row, col );
660
+ if (data === undefined || data === null) {
661
+ return null;
662
+ }
663
+
664
+ let cls = "";
665
+ if( column.cellClassifier ) {
666
+ extra_cls.push( column.cellClassifier( row, col ) );
667
+ }
668
+
669
+ if( data instanceof UnsafeHtml ) {
670
+ return data;
671
+ }
672
+
673
+ if (column.formatter) {
674
+ return column.formatter(data);
675
+ }
676
+
677
+ switch (type) {
678
+ case "checkbox": {
679
+ if (data) {
680
+ return new Icon({ cls: "cell-check" + cls, iconId: check_icon });
681
+ }
682
+
683
+ return undefined;
684
+ }
685
+
686
+ case "image": {
687
+ if (isString(data)) {
688
+ return new Image({ cls, src: data, fit: "scale-down" });
689
+ }
690
+
691
+ return undefined;
692
+ }
693
+
694
+ case "number": {
695
+ if (!isNumber(data)) {
696
+ return "NaN";
697
+ }
698
+
699
+ data = this._num_fmt.format(data as number);
700
+ break;
701
+ }
702
+
703
+ case "money": {
704
+ if (!isNumber(data)) {
705
+ return "NaN";
706
+ }
707
+
708
+ data = this._mny_fmt.format(data as number);
709
+ break;
710
+ }
711
+
712
+ case "percent": {
713
+ return new Box({
714
+ cls: "percent " + cls,
715
+ content: new Component({ cls: "bar", width: data + "%" })
716
+ });
717
+ }
718
+
719
+ case "icon": {
720
+ return new Icon({ cls, iconId: data + "" });
721
+ }
722
+
723
+ case "date": {
724
+ data = this._dte_fmt.format(data as Date);
725
+ break;
726
+ }
727
+
728
+ default: {
729
+ data = data + "";
730
+ break;
731
+ }
732
+ }
733
+
734
+ return new Component({ tag: "span", cls, content: data });
735
+ }
736
+
737
+ /**
738
+ *
739
+ */
740
+
741
+ private _buildRow(rowid: number, top: number) {
742
+
743
+ const els: Component[] = [];
744
+ const count = this._getColCount();
745
+
746
+ for (let col = 0; col < count; col++) {
747
+ const cdata = this._getCol(col);
748
+ if (cdata.fixed) {
749
+ continue;
750
+ }
751
+
752
+ const extra: string[] = []
753
+ const content = this._renderCell(rowid, cdata, extra);
754
+
755
+ const el = new Component({
756
+ cls: "cell",
757
+ attrs: { "data-col": col },
758
+ style: { width: cdata?.width ? cdata.width + "px" : undefined },
759
+ content
760
+ });
761
+
762
+ switch (cdata.align) {
763
+ case "center": el.addClass( "align-center" ); break;
764
+ case "right": el.addClass( "align-right" ); break;
765
+ }
766
+
767
+ if (extra.length) {
768
+ el.addClass(extra.join(' '));
769
+ }
770
+
771
+ if (cdata.type) {
772
+ el.addClass(cdata.type);
773
+ }
774
+
775
+ const ref = mkid(rowid, col);
776
+ if (this._selection.has(ref)) {
777
+ el.addClass("selected");
778
+ }
779
+
780
+ el.setInternalData("col", col);
781
+ el.setInternalData("row", rowid)
782
+ el.setData("ref", ref + "");
783
+
784
+ els.push(el);
785
+ }
786
+
787
+ let row_cls = 'row';
788
+ if( this.props.rowClassifier ) {
789
+ const xtra = this.props.rowClassifier( rowid );
790
+ if( xtra ) {
791
+ row_cls += ' ' + xtra.trim();
792
+ }
793
+ }
794
+
795
+ return new Box({ cls: row_cls, style: { top: top.toFixed(2) + "px" }, content: els });
796
+ }
797
+
798
+ /**
799
+ *
800
+ */
801
+
802
+ private _buildRowHeader(rowid: number, top: number) {
803
+
804
+ const cols: Component[] = [];
805
+ const count = this._getColCount();
806
+
807
+ for (let col = 0; col < count; col++) {
808
+ const cdata = this._getCol(col);
809
+ if (!cdata?.fixed) {
810
+ continue;
811
+ }
812
+
813
+ const content = this._renderCell(rowid, cdata, [cdata.type]);
814
+
815
+ let align = "start";
816
+ switch (cdata.align) {
817
+ default: align = "start"; break;
818
+ case "center": align = "center"; break;
819
+ case "right": align = "end"; break;
820
+ }
821
+
822
+ const el = new Component({
823
+ cls: "cell",
824
+ style: { width: cdata?.width ? cdata.width + "px" : undefined, justifyContent: align },
825
+ content
826
+ });
827
+
828
+ if (cdata.type) {
829
+ el.addClass(cdata.type);
830
+ }
831
+
832
+ el.setInternalData("col", col);
833
+ el.setInternalData("row", rowid);
834
+ el.setData("ref", mkid(rowid, col) + "");
835
+
836
+ if (this._selection.has(mkid(col, rowid))) {
837
+ el.addClass("selected");
838
+ }
839
+
840
+
841
+
842
+ cols.push(el);
843
+ }
844
+
845
+ return new Box({ cls: "row", style: { top: top + "px" }, content: cols });
846
+ }
847
+
848
+ /**
849
+ *
850
+ */
851
+
852
+ private _updateFlexs() {
853
+ let maxw = 0;
854
+ let flexc = 0;
855
+
856
+ const ccount = this._getColCount();
857
+
858
+ for (let x = 0; x < ccount; x++) {
859
+ const cdata = this._getCol(x);
860
+
861
+ if (!cdata.fixed && cdata.flex) {
862
+ flexc += cdata.flex;
863
+ }
864
+ else {
865
+ maxw += cdata.width;
866
+ }
867
+ }
868
+
869
+ if (flexc) {
870
+ const width = this._viewport.dom.clientWidth;
871
+ const delta = width - maxw;
872
+ const fw = delta / flexc;
873
+
874
+ for (let col = 0; col < ccount; col++) {
875
+ const cdata = this._getCol(col);
876
+ if (!cdata.fixed && cdata.flex) {
877
+ cdata.width = Math.max(cdata.flex * fw, 32);
878
+
879
+ const cols = this.queryAll(`[data-col="${col}"]`)
880
+ cols.forEach(c => {
881
+ c.setStyleValue("width", cdata.width + "px");
882
+ });
883
+ }
884
+ }
885
+ }
886
+ }
887
+
888
+ /**
889
+ *
890
+ */
891
+
892
+ private _computeFullSize() {
893
+
894
+ let maxw = 0;
895
+ let maxfw = 0;
896
+
897
+ const ccount = this._getColCount();
898
+
899
+ for (let x = 0; x < ccount; x++) {
900
+ const cdata = this._getCol(x);
901
+ let w = 0;
902
+
903
+ if (cdata.fixed) {
904
+ this._has_fixed = true;
905
+ }
906
+
907
+ if (cdata.width) {
908
+ w += cdata.width;
909
+ }
910
+
911
+ if (cdata.fixed) {
912
+ maxfw += w;
913
+ }
914
+ else {
915
+ maxw += w;
916
+ }
917
+ }
918
+
919
+ const maxr = this._store ? this._store.getRowCount() : 0;
920
+ let maxh = maxr;
921
+
922
+ if (maxr < SCROLL_LIMIT) {
923
+ maxh *= this._row_height;
924
+ }
925
+ else {
926
+ const height = this._body.dom.parentElement.clientHeight;
927
+ const npage = height / this._row_height;
928
+ maxh = maxr - Math.floor(npage) + npage * this._row_height;
929
+ }
930
+
931
+ this.setStyleVariable("--fixed-width", maxfw + "px");
932
+ this._body.setStyleValue("height", maxh + "px");
933
+ this._body.setStyleValue("width", maxw + "px");
934
+ this._vheader.setStyleValue("height", maxh + "px");
935
+ }
936
+
937
+ /**
938
+ *
939
+ */
940
+
941
+ private _init() {
942
+ this._body = new Component({ cls: "body" });
943
+
944
+ this._viewport = new Viewport({ content: this._body });
945
+
946
+ if (!this._has_footer) {
947
+ this.setStyleVariable("--footer-height", "0");
948
+ }
949
+
950
+ // SCROLL
951
+ this._viewport.addDOMEvent("scroll", (ev) => {
952
+ // sync horz & vert elements
953
+ this._left = this._viewport.dom.scrollLeft;
954
+ this.setStyleVariable("--left", -this._left + "px");
955
+
956
+ this._top = this._viewport.dom.scrollTop;
957
+ this.setStyleVariable("--top", -this._top + "px");
958
+
959
+ //this.setTimeout( "update", 0, ( ) => this._update( ) );
960
+ this._update()
961
+ });
962
+
963
+ // WHEEL
964
+ this.addDOMEvent("wheel", (ev: WheelEvent) => {
965
+ if (ev.deltaY && this._store && this._store.getRowCount() >= SCROLL_LIMIT) {
966
+ this._viewport.dom.scrollBy(0, ev.deltaY < 0 ? -1 : 1);
967
+ ev.stopPropagation();
968
+ ev.preventDefault();
969
+ }
970
+
971
+ if (this._has_fixed && ev.deltaY) {
972
+ // wheel on fixed part
973
+ // fixed part do not have scrollbar, so we need to handle it by hand
974
+ let t = ev.target as Node;
975
+ while (t != this.dom) {
976
+ if (t == this._vheader.dom) {
977
+ this._viewport.dom.scrollBy(0, ev.deltaY < 0 ? -this._row_height : this._row_height);
978
+ ev.stopPropagation();
979
+ ev.preventDefault();
980
+ break;
981
+ }
982
+
983
+ t = t.parentNode;
984
+ }
985
+ }
986
+ })
987
+
988
+ const targetCell = (e: MouseEvent) => {
989
+ let el = e.target as Element;
990
+ while (el && !el.classList.contains("cell")) {
991
+ el = el.parentElement;
992
+ }
993
+
994
+ if (el) {
995
+ const cel = componentFromDOM(el);
996
+ return {
997
+ ref: cel.getIntData("ref"),
998
+ row: cel.getInternalData("row"),
999
+ col: cel.getInternalData("col"),
1000
+ }
1001
+ }
1002
+
1003
+ return undefined;
1004
+ }
1005
+
1006
+
1007
+ // CLICK
1008
+ this.addDOMEvent("click", (e) => {
1009
+ const ref = targetCell(e);
1010
+ if (ref) {
1011
+ //TODO: multiselection
1012
+ if (!this._selection.has(ref.ref)) {
1013
+ this._clearSelection( false );
1014
+ this._addSelection(ref.ref,true);
1015
+ }
1016
+ }
1017
+ });
1018
+
1019
+ // DBLCLICK
1020
+ this.addDOMEvent("dblclick", (e) => {
1021
+ const ref = targetCell(e);
1022
+ if (ref) {
1023
+ //TODO: multiselection
1024
+ if (!this._selection.has(ref.ref)) {
1025
+ this._clearSelection(false);
1026
+ this._addSelection(ref.ref,true);
1027
+ }
1028
+
1029
+ this.fire( "dblClick", { context: { row: ref.row, col: ref.col } } );
1030
+ }
1031
+ });
1032
+
1033
+ // CONTEXT
1034
+ this.addDOMEvent("contextmenu", (e) => {
1035
+ const ref = targetCell(e);
1036
+ if (ref) {
1037
+ //TODO: multiselection
1038
+ if (!this._selection.has(ref.ref)) {
1039
+ this._clearSelection( false );
1040
+ this._addSelection(ref.ref, true );
1041
+ }
1042
+
1043
+ this.fire( "contextMenu", { uievent: e, context: { row: ref.row, col: ref.col } } );
1044
+ }
1045
+ else {
1046
+ this.fire( "contextMenu", { uievent: e, context: null } );
1047
+ }
1048
+
1049
+ e.preventDefault();
1050
+ e.stopPropagation();
1051
+ });
1052
+
1053
+ this._updateFlexs();
1054
+
1055
+ this._fheader = this._buildColHeader(true);
1056
+ this._hheader = this._buildColHeader(false);
1057
+ this._vheader = new Box({ cls: "row-header" })
1058
+
1059
+ if (this._has_footer) {
1060
+ this._ffooter = this._buildColFooter(true);
1061
+ this._footer = this._buildColFooter(false);
1062
+ }
1063
+
1064
+ this.setContent([this._viewport, this._fheader, this._hheader, this._ffooter, this._footer, this._vheader]);
1065
+
1066
+ // compute misc variables
1067
+ {
1068
+ const rh = this.getStyleVariable("--row-height");
1069
+ this._row_height = parseInt(rh);
1070
+ }
1071
+
1072
+ this._computeFullSize();
1073
+ }
1074
+
1075
+ /**
1076
+ *
1077
+ */
1078
+
1079
+ private _update(force = false) {
1080
+
1081
+ if (!this._lock) {
1082
+ const rc = this.getBoundingRect();
1083
+
1084
+ // rows
1085
+ const rowc = this._store ? this._store.getRowCount() : 0;
1086
+ const mul = rowc < SCROLL_LIMIT ? this._row_height : 1;
1087
+
1088
+ const start = Math.floor(this._top / mul);
1089
+ const end = start + Math.ceil(rc.height / this._row_height);
1090
+ const hasFixed = this._has_fixed;
1091
+
1092
+ if (this._start != start || this._end != end || force) {
1093
+
1094
+ const rows: Component[] = [];
1095
+ const headers: Component[] = [];
1096
+
1097
+ if (force) {
1098
+ this._vis_rows.clear();
1099
+ }
1100
+
1101
+ let newvis: typeof this._vis_rows = new Map();
1102
+
1103
+ let y = start * mul;
1104
+
1105
+ for (let row = start; row < end && row < rowc; row++, y += this._row_height) {
1106
+
1107
+ let el = this._vis_rows.get(row);
1108
+ //const rec = this._store.getByIndex(row);
1109
+
1110
+ if (hasFixed) {
1111
+ if (!el) {
1112
+ el = {
1113
+ h: this._buildRowHeader(row, y),
1114
+ r: this._buildRow(row, y),
1115
+ };
1116
+ }
1117
+ else {
1118
+ el.h.setStyleValue("top", y + "px");
1119
+ el.r.setStyleValue("top", y + "px");
1120
+ }
1121
+
1122
+ headers.push(el.h);
1123
+ }
1124
+ else {
1125
+ if (!el) {
1126
+ el = { h: null, r: this._buildRow(row, y), };
1127
+ }
1128
+ else {
1129
+ el.r.setStyleValue("top", y + "px");
1130
+ }
1131
+ }
1132
+
1133
+ rows.push(el.r);
1134
+ newvis.set(row, el);
1135
+ }
1136
+
1137
+ if (hasFixed) {
1138
+ headers.push(new Component({ cls: "cell-out", style: { top: y + "px" } }));
1139
+ }
1140
+
1141
+ this._vis_rows = newvis;
1142
+ this._start = start;
1143
+ this._end = end;
1144
+
1145
+ this._body.setContent(rows);
1146
+
1147
+ if (hasFixed) {
1148
+ this._vheader.removeClass("@hidden");
1149
+ this._vheader.setContent(headers);
1150
+ }
1151
+ else {
1152
+ this._vheader.addClass("@hidden");
1153
+ }
1154
+ }
1155
+ }
1156
+ }
1157
+ /**
1158
+ *
1159
+ */
1160
+
1161
+ private _clearSelection(notify = true) {
1162
+ for (const ref of this._selection.keys()) {
1163
+ const els = this.queryAll(`.cell[data-ref="${ref}"]`)
1164
+ els.forEach(el => {
1165
+ el.removeClass("selected");
1166
+ })
1167
+ }
1168
+
1169
+ this._selection.clear();
1170
+
1171
+ if (notify) {
1172
+ this.fire("selectionChange", { selection: [], empty: true });
1173
+ }
1174
+ }
1175
+
1176
+ /**
1177
+ *
1178
+ */
1179
+
1180
+ private _addSelection(ref: number, notify = true) {
1181
+ this._selection.add(ref)
1182
+
1183
+ const els = this.queryAll(`.cell[data-ref="${ref}"]`)
1184
+ els.forEach(el => {
1185
+ el.addClass("selected");
1186
+ });
1187
+
1188
+ if (notify) {
1189
+ const selection = this.getSelection();
1190
+ this.fire("selectionChange", { selection, empty: selection.length != 0 });
1191
+ }
1192
+ }
1193
+
1194
+ /**
1195
+ *
1196
+ */
1197
+
1198
+ getSelection(): CellRef[] {
1199
+ const selection: CellRef[] = [];
1200
+
1201
+ this._selection.forEach(x => {
1202
+ selection.push({
1203
+ row: x >> 12,
1204
+ col: x & 0xfff,
1205
+ })
1206
+ });
1207
+
1208
+ return selection;
1209
+ }
1210
+
1211
+ /**
1212
+ *
1213
+ */
1214
+
1215
+ selectItem(row: number, col: number, append = false) {
1216
+ if (!append) {
1217
+ this._clearSelection(false);
1218
+ }
1219
+
1220
+ this._addSelection(mkid(row, col), true);
1221
+ }
1222
+ }
1223
+