-
Notifications
You must be signed in to change notification settings - Fork 8
/
tabulator_model.ts
249 lines (220 loc) · 7.99 KB
/
tabulator_model.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
// See https://docs.bokeh.org/en/latest/docs/reference/models/layouts.html
import { HTMLBox, HTMLBoxView } from "@bokehjs/models/layouts/html_box"
import {div} from "@bokehjs/core/dom"
// See https://docs.bokeh.org/en/latest/docs/reference/core/properties.html
import * as p from "@bokehjs/core/properties";
import {ColumnDataSource} from "@bokehjs/models/sources/column_data_source";
export function set_size(el: HTMLElement, model: HTMLBox): void {
let width_policy = model.width != null ? "fixed" : "fit"
let height_policy = model.height != null ? "fixed" : "fit"
const {sizing_mode} = model
if (sizing_mode != null) {
if (sizing_mode == "fixed")
width_policy = height_policy = "fixed"
else if (sizing_mode == "stretch_both")
width_policy = height_policy = "max"
else if (sizing_mode == "stretch_width")
width_policy = "max"
else if (sizing_mode == "stretch_height")
height_policy = "max"
else {
switch (sizing_mode) {
case "scale_width":
width_policy = "max"
height_policy = "min"
break
case "scale_height":
width_policy = "min"
height_policy = "max"
break
case "scale_both":
width_policy = "max"
height_policy = "max"
break
default:
throw new Error("unreachable")
}
}
}
if (width_policy == "fixed" && model.width)
el.style.width = model.width + "px";
else if (width_policy == "max")
el.style.width = "100%";
if (height_policy == "fixed" && model.height)
el.style.height = model.height + "px";
else if (height_policy == "max")
el.style.height = "100%";
}
function transform_cds_to_records(cds: ColumnDataSource): any {
const data: any = []
const columns = cds.columns()
const cdsLength = cds.get_length()
if (columns.length === 0||cdsLength === null) {
return [];
}
for (let i = 0; i < cdsLength; i++) {
const item: any = {}
for (const column of columns) {
let array: any = cds.get_array(column);
const shape = array[0].shape == null ? null : array[0].shape;
if ((shape != null) && (shape.length > 1) && (typeof shape[0] == "number"))
item[column] = array.slice(i*shape[1], i*shape[1]+shape[1])
else
item[column] = array[i]
}
data.push(item)
}
return data
}
declare var Tabulator: any;
// declare const requirejs: any;
declare const require: any;
// The view of the Bokeh extension/ HTML element
// Here you can define how to render the model as well as react to model changes or View events.
export class TabulatorModelView extends HTMLBoxView {
model: TabulatorModel;
tabulator: any;
_tabulator_cell_updating: boolean=false;
// objectElement: any // Element
connect_signals(): void {
super.connect_signals()
this.connect(this.model.properties.configuration.change, () => {
this.render();
})
// this.connect(this.model.source.change, () => this.setData())
this.connect(this.model.source.properties.data.change, () => {
this.setData();
})
this.connect(this.model.source.streaming, () => this.addData())
this.connect(this.model.source.patching, () => this.updateOrAddData())
// this.connect(this.model.source.selected.change, () => this.updateSelection())
this.connect(this.model.source.selected.properties.indices.change, () => this.updateSelection())
}
render(): void {
super.render()
console.log("render");
const container = div({class: "pnx-tabulator"});
set_size(container, this.model)
let configuration = this.getConfiguration();
// I'm working on getting this working in the notebook but have not yet found the solution
// See [Issue 1529](https://github.com/holoviz/panel/issues/15299)
if (typeof Tabulator === 'undefined'){
// Tabulator=require("tabulator-tables")
// requirejs(["https://unpkg.com/tabulator-tables"]);
// Tabulator=requirejs("https://unpkg.com/tabulator-tables");
console.log("Tabulator not loaded. See https://github.com/holoviz/panel/issues/15299");
}
console.log(Tabulator);
this.tabulator = new Tabulator(container, configuration)
this.el.appendChild(container)
}
getConfiguration(): any {
// I refer to this via _view because this is the tabulator element when cellEdited is used
let _view = this;
function rowSelectionChanged(data: any, _: any): void {
console.log("rowSelectionChanged")
let indices: any = data.map((row: any) => row.index)
_view.model.source.selected.indices = indices;
}
function startUpdating(): void {
_view._tabulator_cell_updating = true;
}
function endUpdating(): void {
_view._tabulator_cell_updating = false;
}
function cellEdited(cell: any){
console.log("cellEdited");
const field = cell._cell.column.field;
const index = cell._cell.row.data.index;
const value = cell._cell.value;
startUpdating();
_view.model.source.patch({[field]: [[index, value]]});
_view.model._cell_change = {"c": field, "i": index, "v": value}
endUpdating();
}
let default_configuration = {
"rowSelectionChanged": rowSelectionChanged,
"cellEdited": cellEdited,
"index": "index",
}
let configuration = {
...this.model.configuration,
...default_configuration
}
let data = this.model.source;
if (data ===null || Object.keys(data.data).length===0){
return configuration;
}
else {
console.log("adding data to configuration");
data = transform_cds_to_records(data)
return {
...configuration,
"data": data,
}
}
}
after_layout(): void {
console.log("after_layout");
super.after_layout()
this.tabulator.redraw(true);
}
setData(): void {
console.log("setData");
let data = transform_cds_to_records(this.model.source);
this.tabulator.setData(data);
}
addData(): void {
console.log("addData");
let data = transform_cds_to_records(this.model.source);
this.tabulator.setData(data);
}
updateOrAddData(): void {
// To avoid double updating the tabulator data
if (this._tabulator_cell_updating===true){return;}
console.log("updateData");
let data = transform_cds_to_records(this.model.source);
this.tabulator.setData(data);
}
updateSelection(): void {
console.log("updateSelection");
if (this.tabulator==null){return}
let indices: number[]= this.model.source.selected.indices;
let selectedRows: any = this.tabulator.getSelectedRows();
for (let row of selectedRows){
if (!indices.includes(row.getData().index)){
row.toggleSelect();
}
}
for (let index of indices){
// Improve this
// Maybe tabulator should use id as index?
this.tabulator.selectRow(index);
}
}
}
export namespace TabulatorModel {
export type Attrs = p.AttrsOf<Props>
export type Props = HTMLBox.Props & {
configuration: p.Property<any>,
source: p.Property<ColumnDataSource>,
_cell_change: p.Property<any>,
}
}
export interface TabulatorModel extends TabulatorModel.Attrs { }
// The Bokeh .ts model corresponding to the Bokeh .py model
export class TabulatorModel extends HTMLBox {
properties: TabulatorModel.Props
constructor(attrs?: Partial<TabulatorModel.Attrs>) {
super(attrs)
}
static __module__ = "awesome_panel_extensions.bokeh_extensions.tabulator_model"
static init_TabulatorModel(): void {
this.prototype.default_view = TabulatorModelView;
this.define<TabulatorModel.Props>({
configuration: [p.Any, ],
source: [p.Any, ],
_cell_change: [p.Any, ],
})
}
}