/
tabulator.ts
1267 lines (1146 loc) · 40.4 KB
/
tabulator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {undisplay} from "@bokehjs/core/dom"
import {isArray} from "@bokehjs/core/util/types"
import {ModelEvent} from "@bokehjs/core/bokeh_events"
import {div} from "@bokehjs/core/dom"
import {Enum} from "@bokehjs/core/kinds"
import * as p from "@bokehjs/core/properties";
import {LayoutDOM} from "@bokehjs/models/layouts/layout_dom"
import {ColumnDataSource} from "@bokehjs/models/sources/column_data_source"
import {TableColumn} from "@bokehjs/models/widgets/tables"
import type {Attrs} from "@bokehjs/core/types"
import {debounce} from "debounce"
import {comm_settings} from "./comm_manager"
import {transform_cds_to_records} from "./data"
import {HTMLBox, HTMLBoxView} from "./layout"
export class TableEditEvent extends ModelEvent {
constructor(readonly column: string, readonly row: number, readonly pre: boolean) {
super()
}
protected get event_values(): Attrs {
return {model: this.origin, column: this.column, row: this.row, pre: this.pre}
}
static {
this.prototype.event_name = "table-edit"
}
}
export class CellClickEvent extends ModelEvent {
constructor(readonly column: string, readonly row: number) {
super()
}
protected get event_values(): Attrs {
return {model: this.origin, column: this.column, row: this.row}
}
static {
this.prototype.event_name = "cell-click"
}
}
export class SelectionEvent extends ModelEvent {
constructor(readonly indices: number[], readonly selected: boolean, readonly flush: boolean = false) {
super()
}
protected get event_values(): Attrs {
return {model: this.origin, indices: this.indices, selected: this.selected, flush: this.flush}
}
static {
this.prototype.event_name = "selection-change"
}
}
declare const Tabulator: any;
function find_group(key: any, value: string, records: any[]): any {
for (const record of records) {
if (record[key] == value)
return record
}
return null
}
function summarize(grouped: any[], columns: any[], aggregators: string[], depth: number = 0): any {
const summary: any = {}
if (grouped.length == 0)
return summary
const agg = aggregators[depth]
for (const group of grouped) {
const subsummary = summarize(group._children, columns, aggregators, depth+1)
for (const col in subsummary) {
if (isArray(subsummary[col]))
group[col] = subsummary[col].reduce((a: any, b: any) => a + b, 0) / subsummary[col].length
else
group[col] = subsummary[col]
}
for (const column of columns.slice(1)) {
const val = group[column.field]
if (column.field in summary) {
const old_val = summary[column.field]
if (agg === 'min')
summary[column.field] = Math.min(val, old_val)
else if (agg === 'max')
summary[column.field] = Math.max(val, old_val)
else if (agg === 'sum')
summary[column.field] = val + old_val
else if (agg === 'mean') {
if (isArray(summary[column.field]))
summary[column.field].push(val)
else
summary[column.field] = [old_val, val]
}
} else
summary[column.field] = val
}
}
return summary
}
function group_data(records: any[], columns: any[], indexes: string[], aggregators: any): any[] {
const grouped = []
const index_field = columns[0].field
for (const record of records) {
const value = record[indexes[0]]
let group = find_group(index_field, value, grouped)
if (group == null) {
group = {_children: []}
group[index_field] = value
grouped.push(group)
}
let subgroup = group
const groups: any = {}
for (const index of indexes.slice(1)) {
subgroup = find_group(index_field, record[index], subgroup._children)
if (subgroup == null) {
subgroup = {_children: []}
subgroup[index_field] = record[index]
group._children.push(subgroup)
}
groups[index] = group
for (const column of columns.slice(1))
subgroup[column.field] = record[column]
group = subgroup
}
for (const column of columns.slice(1))
subgroup[column.field] = record[column.field]
}
const aggs = []
for (const index of indexes)
aggs.push((index in aggregators) ? aggregators[index] : 'sum')
summarize(grouped, columns, aggs)
return grouped
}
const timestampSorter = function(a: any, b: any, _aRow: any, _bRow: any, _column: any, _dir: any, _params: any){
// Bokeh serializes datetime objects as UNIX timestamps.
//a, b - the two values being compared
//aRow, bRow - the row components for the values being compared (useful if you need to access additional fields in the row data for the sort)
//column - the column component for the column being sorted
//dir - the direction of the sort ("asc" or "desc")
//sorterParams - sorterParams object from column definition array
// Added an _ in front of some parameters as they're unused and the Typescript compiler was complaining about it.
// const alignEmptyValues = params.alignEmptyValues
let emptyAlign: any
emptyAlign = 0
const opts = {zone: new (window as any).luxon.IANAZone('UTC')}
// NaN values are serialized to -9223372036854776 by Bokeh
if (String(a) == '-9223372036854776') {
a = (window as any).luxon.DateTime.fromISO('invalid')
} else {
a = (window as any).luxon.DateTime.fromMillis(a, opts)
}
if (String(b) == '-9223372036854776') {
b = (window as any).luxon.DateTime.fromISO('invalid')
} else {
b = (window as any).luxon.DateTime.fromMillis(b, opts)
}
if(!a.isValid){
emptyAlign = !b.isValid ? 0 : -1;
}else if(!b.isValid){
emptyAlign = 1;
}else{
//compare valid values
return a - b;
}
// Invalid (e.g. NaN) always at the bottom
emptyAlign *= -1
return emptyAlign;
}
const dateEditor = function(cell: any, onRendered: any, success: any, cancel: any) {
//cell - the cell component for the editable cell
//onRendered - function to call when the editor has been rendered
//success - function to call to pass the successfully updated value to Tabulator
//cancel - function to call to abort the edit and return to a normal cell
//create and style input
const rawValue = cell.getValue()
const opts = {zone: new (window as any).luxon.IANAZone('UTC')}
let cellValue: any
if (rawValue === 'NaN' || rawValue === null)
cellValue = null
else
cellValue = (window as any).luxon.DateTime.fromMillis(rawValue, opts).toFormat("yyyy-MM-dd")
const input = document.createElement("input")
input.setAttribute("type", "date");
input.style.padding = "4px";
input.style.width = "100%";
input.style.boxSizing = "border-box";
input.value = cellValue;
onRendered(() => {
input.focus();
input.style.height = "100%";
});
function onChange() {
const new_val = (window as any).luxon.DateTime.fromFormat(input.value, "yyyy-MM-dd", opts).toMillis()
if (new_val != cellValue)
success(new_val)
else
cancel()
}
//submit new value on blur or change
input.addEventListener("blur", onChange);
//submit new value on enter
input.addEventListener("keydown", (e) => {
if (e.key == "Enter")
setTimeout(onChange, 100)
if (e.key == "Escape")
setTimeout(cancel, 100)
});
return input;
};
const datetimeEditor = function(cell: any, onRendered: any, success: any, cancel: any) {
//cell - the cell component for the editable cell
//onRendered - function to call when the editor has been rendered
//success - function to call to pass the successfully updated value to Tabulator
//cancel - function to call to abort the edit and return to a normal cell
//create and style input
const rawValue = cell.getValue()
const opts = {zone: new (window as any).luxon.IANAZone('UTC')}
let cellValue: any
if (rawValue === 'NaN' || rawValue === null)
cellValue = null
else
cellValue = (window as any).luxon.DateTime.fromMillis(rawValue, opts).toFormat("yyyy-MM-dd'T'T")
const input = document.createElement("input")
input.setAttribute("type", "datetime-local");
input.style.padding = "4px";
input.style.width = "100%";
input.style.boxSizing = "border-box";
input.value = cellValue;
onRendered(() => {
input.focus();
input.style.height = "100%";
});
function onChange() {
const new_val = (window as any).luxon.DateTime.fromFormat(input.value, "yyyy-MM-dd'T'T", opts).toMillis()
if (new_val != cellValue)
success(new_val)
else
cancel()
}
//submit new value on blur or change
input.addEventListener("blur", onChange);
//submit new value on enter
input.addEventListener("keydown", (e) => {
if (e.key == "Enter")
setTimeout(onChange, 100)
if (e.key == "Escape")
setTimeout(cancel, 100)
});
return input;
};
export class DataTabulatorView extends HTMLBoxView {
model: DataTabulator;
tabulator: any;
columns: Map<string, any> = new Map();
_tabulator_cell_updating: boolean=false
_updating_page: boolean = false
_updating_sort: boolean = false
_selection_updating: boolean = false
_initializing: boolean
_lastVerticalScrollbarTopPosition: number = 0;
_applied_styles: boolean = false
_building: boolean = false
connect_signals(): void {
super.connect_signals()
const p = this.model.properties
const {configuration, layout, columns, groupby} = p;
this.on_change([configuration, layout, groupby], debounce(() => {
this.invalidate_render()
}, 20, false))
this.connect(this.model.properties.visible.change, () => {
if (this.model.visible)
this.tabulator.element.style.visibility = 'visible';
})
this.on_change([columns], () => {
this.tabulator.setColumns(this.getColumns())
this.setHidden()
})
this.connect(p.download.change, () => {
const ftype = this.model.filename.endsWith('.json') ? "json" : "csv"
this.tabulator.download(ftype, this.model.filename)
})
this.connect(p.children.change, () => this.renderChildren())
this.connect(p.expanded.change, () => {
// The first cell is the cell of the frozen _index column.
for (const row of this.tabulator.rowManager.getRows()) {
if (row.cells.length > 0)
row.cells[0].layoutElement()
}
// Make sure the expand icon is changed when expanded is
// changed from Python.
for (const row of this.tabulator.rowManager.getRows()) {
if (row.cells.length > 0) {
const index = row.data._index
const icon = this.model.expanded.indexOf(index) < 0 ? "►" : "▼"
row.cells[1].element.innerText = icon
}
}
})
this.connect(p.cell_styles.change, () => {
if (this._applied_styles)
this.tabulator.redraw(true)
this.setStyles()
})
this.connect(p.hidden_columns.change, () => {
this.setHidden()
this.tabulator.redraw(true)
})
this.connect(p.page_size.change, () => this.setPageSize())
this.connect(p.page.change, () => {
if (!this._updating_page)
this.setPage()
})
this.connect(p.visible.change, () => this.setVisibility())
this.connect(p.max_page.change, () => this.setMaxPage())
this.connect(p.frozen_rows.change, () => this.setFrozen())
this.connect(p.sorters.change, () => this.setSorters())
this.connect(p.theme_classes.change, () => this.setCSSClasses(this.tabulator.element))
this.connect(this.model.source.properties.data.change, () => {
if (this.tabulator === undefined)
return
this._selection_updating = true
this.setData()
this._selection_updating = false
this.postUpdate()
})
this.connect(this.model.source.streaming, () => this.addData())
this.connect(this.model.source.patching, () => {
const inds = this.model.source.selected.indices
this.updateOrAddData();
this.tabulator.rowManager.element.scrollTop = this._lastVerticalScrollbarTopPosition;
// Restore indices since updating data may have reset checkbox column
this.model.source.selected.indices = inds;
})
this.connect(this.model.source.selected.change, () => this.setSelection())
this.connect(this.model.source.selected.properties.indices.change, () => this.setSelection())
}
get groupBy(): boolean | ((data: any) => string) {
const groupby = (data: any) => {
const groups = []
for (const g of this.model.groupby) {
const group = g + ': ' + data[g]
groups.push(group)
}
return groups.join(', ')
}
return this.model.groupby.length ? groupby : false
}
get sorters(): any[] {
const sorters = []
if (this.model.sorters.length)
sorters.push({column: '_index', dir: 'asc'})
for (const sort of this.model.sorters.reverse()) {
if (sort.column === undefined)
sort.column = sort.field
sorters.push(sort)
}
return sorters
}
invalidate_render(): void {
this.tabulator.destroy()
this.tabulator = null
this.render()
}
redraw(): void {
if (this._building)
return
if (this.tabulator.columnManager.element != null) {
this.tabulator.columnManager.redraw(true);
}
if (this.tabulator.rowManager.renderer != null) {
this.tabulator.rowManager.redraw(true)
this.renderChildren()
this.setStyles()
}
}
after_layout(): void {
super.after_layout()
if (this.tabulator != null && this._initializing)
this.redraw()
this._initializing = false
}
setCSSClasses(el: HTMLDivElement): void {
el.className = "pnx-tabulator tabulator"
for (const cls of this.model.theme_classes)
el.classList.add(cls)
}
render(): void {
if (this.tabulator != null)
this.tabulator.destroy()
super.render()
this._initializing = true
const container = div({style: "display: contents;"})
const el = div({style: "width: 100%; height: 100%; visibility: hidden;"})
this.setCSSClasses(el)
container.appendChild(el)
this.shadow_el.appendChild(container)
let configuration = this.getConfiguration()
this.tabulator = new Tabulator(el, configuration)
this.watch_stylesheets()
this.init_callbacks()
}
style_redraw(): void {
if (this.model.visible)
this.tabulator.element.style.visibility = 'visible';
if (!this._initializing && !this._building)
this.redraw()
}
tableInit(): void {
this._building = true
// Patch the ajax request and page data parsing methods
const ajax = this.tabulator.modules.ajax
ajax.sendRequest = (_url: any, params: any, _config: any) => {
return this.requestPage(params.page, params.sort)
}
this.tabulator.modules.page._parseRemoteData = (): boolean => {
return false
}
}
init_callbacks(): void {
// Initialization
this.tabulator.on("tableBuilding", () => this.tableInit())
this.tabulator.on("tableBuilt", () => this.tableBuilt())
// Rendering callbacks
this.tabulator.on("selectableCheck", (row: any) => {
const selectable = this.model.selectable_rows
return (selectable == null) || selectable.includes(row._row.data._index)
})
this.tabulator.on("tooltips", (cell: any) => {
return cell.getColumn().getField() + ": " + cell.getValue();
})
this.tabulator.on("scrollVertical", debounce(() => {
this.setStyles()
}, 50, false))
// Sync state with model
this.tabulator.on("rowSelectionChanged", (data: any, rows: any, selected: any, deselected: any) => this.rowSelectionChanged(data, rows, selected, deselected))
this.tabulator.on("rowClick", (e: any, row: any) => this.rowClicked(e, row))
this.tabulator.on("cellEdited", (cell: any) => this.cellEdited(cell))
this.tabulator.on("dataFiltering", (filters: any) => {
this.model.filters = filters
})
this.tabulator.on("dataFiltered", (_: any, rows: any[]) => {
if (this._building)
return
// Ensure that after filtering empty scroll renders
if (rows.length === 0)
this.tabulator.rowManager.renderEmptyScroll()
// Ensure that after filtering the page is updated
this.updatePage(this.tabulator.getPage())
})
this.tabulator.on("pageLoaded", (pageno: number) => {
this.updatePage(pageno)
})
this.tabulator.on("renderComplete", () => {
if (this._building)
return
this.postUpdate()
});
this.tabulator.on("dataSorting", (sorters: any[]) => {
const sorts = []
for (const s of sorters) {
if (s.field !== '_index')
sorts.push({field: s.field, dir: s.dir})
}
if (this.model.pagination !== 'remote') {
this._updating_sort = true
this.model.sorters = sorts
this._updating_sort = false
}
})
}
tableBuilt(): void {
this._building = false
this.setSelection()
this.renderChildren()
this.setStyles()
if (this.model.pagination) {
this.setMaxPage()
this.tabulator.setPage(this.model.page)
}
}
requestPage(page: number, sorters: any[]): Promise<void> {
return new Promise((resolve: any, reject: any) => {
try {
if (page != null && sorters != null) {
this._updating_sort = true
const sorts = []
for (const s of sorters) {
if (s.field !== '_index')
sorts.push({field: s.field, dir: s.dir})
}
this.model.sorters = sorts
this._updating_sort = false
this._updating_page = true
try {
this.model.page = page || 1
} finally {
this._updating_page = false
}
}
resolve([])
} catch(err) {
reject(err)
}
})
}
getLayout(): string {
let layout = this.model.layout
switch (layout) {
case "fit_data":
return "fitData"
case "fit_data_fill":
return "fitDataFill"
case "fit_data_stretch":
return "fitDataStretch"
case "fit_data_table":
return "fitDataTable"
case "fit_columns":
return "fitColumns"
}
}
getConfiguration(): any {
// Only use selectable mode if explicitly requested otherwise manually handle selections
let selectable = this.model.select_mode === 'toggle' ? true : NaN
let configuration = {
...this.model.configuration,
index: "_index",
nestedFieldSeparator: false,
movableColumns: false,
selectable: selectable,
columns: this.getColumns(),
initialSort: this.sorters,
layout: this.getLayout(),
pagination: this.model.pagination != null,
paginationMode: this.model.pagination,
paginationSize: this.model.page_size,
paginationInitialPage: 1,
groupBy: this.groupBy,
rowFormatter: (row: any) => this._render_row(row),
frozenRows: (row: any) => {
return this.model.frozen_rows.length ? this.model.frozen_rows.includes(row._row.data._index) : false
}
}
if (this.model.pagination === "remote") {
configuration['ajaxURL'] = "http://panel.pyviz.org"
configuration['sortMode'] = "remote"
}
const cds: any = this.model.source;
let data: any[]
if (cds === null || (cds.columns().length === 0))
data = []
else
data = transform_cds_to_records(cds, true)
if (configuration.dataTree)
data = group_data(data, this.model.columns, this.model.indexes, this.model.aggregators)
return {
...configuration,
"data": data,
}
}
get child_models(): LayoutDOM[] {
const children: LayoutDOM[] = []
for (const idx of this.model.expanded) {
const child = this.model.children.get?.(idx)
if (child != null)
children.push(child)
}
return children
}
renderChildren(): void {
new Promise(async (resolve: any) => {
await this.build_child_views()
resolve(null)
}).then(() => {
for (const r of this.model.expanded) {
const row = this.tabulator.getRow(r)
this._render_row(row, false)
}
this._update_children()
if (this.tabulator.rowManager.renderer != null)
this.tabulator.rowManager.adjustTableSize()
this.invalidate_layout()
})
}
_render_row(row: any, resize: boolean = true): void {
const index = row._row?.data._index
if (!this.model.expanded.includes(index) || this.model.children.get(index) == null)
return
const model = this.model.children.get(index)
const view = model == null ? null : this._child_views.get(model)
if (view == null)
return
const rowEl = row.getElement()
const style = getComputedStyle(this.tabulator.element.children[1].children[0])
const bg = style.backgroundColor
const neg_margin = rowEl.style.paddingLeft ? "-" + rowEl.style.paddingLeft : '0';
const viewEl = div({style: "background-color: " + bg +"; margin-left:" + neg_margin + "; max-width: 100%; overflow-x: hidden;"})
viewEl.appendChild(view.el)
rowEl.appendChild(viewEl)
if (!view.has_finished()) {
view.render()
view.after_render()
}
if (resize) {
this._update_children()
this.tabulator.rowManager.adjustTableSize()
this.invalidate_layout()
}
}
_expand_render(cell: any): string {
const index = cell._cell.row.data._index
const icon = this.model.expanded.indexOf(index) < 0 ? "►" : "▼"
return "<i>" + icon + "</i>"
}
_update_expand(cell: any): void {
const index = cell._cell.row.data._index
const expanded = [...this.model.expanded]
const exp_index = expanded.indexOf(index)
if (exp_index < 0)
expanded.push(index)
else {
const removed = expanded.splice(exp_index, 1)[0]
const model = this.model.children.get?.(removed)
if (model != null) {
const view = this._child_views.get(model)
if (view !== undefined && view.el != null)
undisplay(view.el)
}
}
this.model.expanded = expanded
if (expanded.indexOf(index) < 0)
return
let ready = true
for (const idx of this.model.expanded) {
if (this.model.children.get?.(idx) == null) {
ready = false
break
}
}
if (ready)
this.renderChildren()
}
getData(): any[] {
let data = transform_cds_to_records(this.model.source, true)
if (this.model.configuration.dataTree)
data = group_data(data, this.model.columns, this.model.indexes, this.model.aggregators)
return data
}
getColumns(): any {
this.columns = new Map()
const config_columns: (any[] | undefined) = this.model.configuration?.columns;
let columns = []
columns.push({field: '_index', frozen: true, visible: false})
if (config_columns != null) {
for (const column of config_columns)
if (column.columns != null) {
const group_columns = []
for (const col of column.columns)
group_columns.push({...col})
columns.push({...column, columns: group_columns})
} else if (column.formatter === "expand") {
const expand = {
hozAlign: "center",
cellClick: (_: any, cell: any) => { this._update_expand(cell) },
formatter: (cell: any) => { return this._expand_render(cell) },
width:40,
frozen: true
}
columns.push(expand)
} else {
const new_column = {...column}
if (new_column.formatter === "rowSelection") {
new_column.cellClick = (_: any, cell: any) => {
cell.getRow().toggleSelect();
}
}
columns.push(new_column)
}
}
for (const column of this.model.columns) {
let tab_column: any = null
if (config_columns != null) {
for (const col of columns) {
if (col.columns != null) {
for (const c of col.columns) {
if (column.field === c.field) {
tab_column = c
break
}
}
if (tab_column != null)
break
} else if (column.field === col.field) {
tab_column = col
break
}
}
}
if (tab_column == null)
tab_column = {field: column.field}
this.columns.set(column.field, tab_column)
if (tab_column.title == null)
tab_column.title = column.title
if (tab_column.width == null && column.width != null && column.width != 0)
tab_column.width = column.width
if (tab_column.formatter == null && column.formatter != null) {
const formatter: any = column.formatter
const ftype = formatter.type
if (ftype === "BooleanFormatter")
tab_column.formatter = "tickCross"
else {
tab_column.formatter = (cell: any) => {
const formatted = column.formatter.doFormat(cell.getRow(), cell, cell.getValue(), null, null)
if (column.formatter.type === 'HTMLTemplateFormatter')
return formatted
const node = div()
node.innerHTML = formatted
const child = node.children[0]
if (child.innerHTML === "function(){return c.convert(arguments)}") // If the formatter fails
return ''
return child
}
}
}
if (tab_column.sorter == 'timestamp') {
tab_column.sorter = timestampSorter
}
if (tab_column.sorter === undefined) {
tab_column.sorter = "string"
}
const editor: any = column.editor
const ctype = editor.type
if (tab_column.editor != null) {
if (tab_column.editor === 'date') {
tab_column.editor = dateEditor
} else if (tab_column.editor === 'datetime') {
tab_column.editor = datetimeEditor
}
} else if (ctype === "StringEditor") {
if (editor.completions.length > 0) {
tab_column.editor = "list"
tab_column.editorParams = {values: editor.completions, autocomplete:true, listOnEmpty:true}
} else
tab_column.editor = "input"
} else if (ctype === "TextEditor")
tab_column.editor = "textarea"
else if (ctype === "IntEditor" || ctype === "NumberEditor") {
tab_column.editor = "number"
tab_column.editorParams = {step: editor.step}
if (ctype === "IntEditor")
tab_column.validator = "integer"
else
tab_column.validator = "numeric"
} else if (ctype === "CheckboxEditor") {
tab_column.editor = "tickCross"
} else if (ctype === "DateEditor") {
tab_column.editor = dateEditor
} else if (ctype === "SelectEditor") {
tab_column.editor = "list"
tab_column.editorParams = {values: editor.options}
} else if (editor != null && editor.default_view != null) {
tab_column.editor = (cell: any, onRendered: any, success: any, cancel: any) => {
this.renderEditor(column, cell, onRendered, success, cancel)
}
}
tab_column.visible = (tab_column.visible != false && !this.model.hidden_columns.includes(column.field))
tab_column.editable = () => (this.model.editable && (editor.default_view != null))
if (tab_column.headerFilter) {
if ((typeof tab_column.headerFilter) === 'boolean' &&
(typeof tab_column.editor) === 'string') {
tab_column.headerFilter = tab_column.editor
tab_column.headerFilterParams = tab_column.editorParams
}
}
for (const sort of this.model.sorters) {
if (tab_column.field === sort.field)
tab_column.headerSortStartingDir = sort.dir
}
tab_column.cellClick = (_: any, cell: any) => {
const index = cell.getData()._index
const event = new CellClickEvent(column.field, index)
this.model.trigger_event(event)
}
if (config_columns == null)
columns.push(tab_column)
}
for (const col in this.model.buttons) {
const button_formatter = () => {
return this.model.buttons[col];
};
const button_column = {
formatter: button_formatter,
hozAlign: "center",
cellClick: (_: any, cell: any) => {
const index = cell.getData()._index
const event = new CellClickEvent(col, index)
this.model.trigger_event(event)
}
}
columns.push(button_column)
}
return columns
}
renderEditor(column: any, cell: any, onRendered: any, success: any, cancel: any): any {
const editor = column.editor
const view = new editor.default_view({column: column, model: editor, parent: this, container: cell._cell.element})
view.initialize()
view.connect_signals()
onRendered(() => {
view.setValue(cell.getValue())
})
view.inputEl.addEventListener('input', () => {
const value = view.serializeValue()
const old_value = cell.getValue()
const validation = view.validate()
if (!validation.valid)
cancel(validation.msg)
if (old_value != null && typeof value != typeof old_value)
cancel("Mismatching type")
else
success(view.serializeValue())
});
return view.inputEl
}
// Update table
setData(): void {
if (this._initializing || this._building || !this.tabulator.initialized)
return
const data = this.getData()
if (this.model.pagination != null)
this.tabulator.rowManager.setData(data, true, false)
else
this.tabulator.setData(data)
}
addData(): void {
const rows = this.tabulator.rowManager.getRows()
const last_row = rows[rows.length-1]
const start = ((last_row?.data._index) || 0)
this.setData()
if (this.model.follow && last_row)
this.tabulator.scrollToRow(start, "top", false)
}
postUpdate(): void {
this.setSelection()
this.setStyles()
}
updateOrAddData(): void {
// To avoid double updating the tabulator data
if (this._tabulator_cell_updating)
return
// Temporarily set minHeight to avoid "scroll-to-top" issues caused
// by Tabulator JS entirely destroying the table when .setData is called.
// Inspired by https://github.com/olifolkerd/tabulator/issues/4155
const prev_minheight = this.tabulator.element.style.minHeight
this.tabulator.element.style.minHeight = this.tabulator.element.offsetHeight + "px"
let data = transform_cds_to_records(this.model.source, true)
this.tabulator.setData(data).then(() => {
this.tabulator.element.style.minHeight = prev_minheight
})
}
setFrozen(): void {
for (const row of this.model.frozen_rows) {
this.tabulator.getRow(row).freeze()
}
}
setVisibility(): void {
if (this.tabulator == null)
return
this.tabulator.element.style.visibility = this.model.visible ? 'visible' : 'hidden';
}
updatePage(pageno: number): void {
if (this.model.pagination === 'local' && this.model.page !== pageno) {
this._updating_page = true
this.model.page = pageno
this._updating_page = false
this.setStyles()
}
}
setGroupBy(): void {
this.tabulator.setGroupBy(this.groupBy)
}
setSorters(): void {
if (this._updating_sort)
return
this.tabulator.setSort(this.sorters)
}
setStyles(): void {
const style_data = this.model.cell_styles.data
if (this.tabulator == null || this.tabulator.getDataCount() == 0 || style_data == null || !style_data.size)
return
this._applied_styles = false
for (const r of style_data.keys()) {
const row_style = style_data.get(r)
const row = this.tabulator.getRow(r)
if (!row)
continue
const cells = row._row.cells
for (const c of row_style.keys()) {
const style = row_style.get(c)
const cell = cells[c]
if (cell == null || !style.length)
continue
const element = cell.element
for (const s of style) {
let prop, value
if (isArray(s))
[prop, value] = s
else if (!s.includes(':'))
continue