-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTableView.jl
320 lines (280 loc) · 11 KB
/
TableView.jl
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
module TableView
using Tables
using WebIO, JSExpr, JSON, Dates, UUIDs
using Observables: @map
export showtable
const ag_grid_imports = []
function __init__()
version = readchomp(joinpath(@__DIR__, "..", "ag-grid.version"))
empty!(ag_grid_imports)
for f in ["ag-grid.js", "ag-grid.css", "ag-theme-balham.css"]
push!(ag_grid_imports, normpath(joinpath(@__DIR__, "..", "deps", "ag-grid-$(version)", f)))
end
pushfirst!(ag_grid_imports, normpath(joinpath(@__DIR__, "rowNumberRenderer.js")))
end
to_css_size(s::AbstractString) = s
to_css_size(s::Real) = "$(s)px"
struct IteratorAndFirst{F, T}
first::F
source::T
len::Int
function IteratorAndFirst(x)
len = Base.haslength(x) ? length(x) : 0
first = iterate(x)
return new{typeof(first), typeof(x)}(first, x, len)
end
function IteratorAndFirst(first, x)
len = Base.haslength(x) ? length(x) + 1 : 1
return new{typeof(first), typeof(x)}(first, x, len)
end
end
Base.IteratorSize(::Type{IteratorAndFirst{F, T}}) where {F, T} = Base.IteratorSize(T)
Base.length(x::IteratorAndFirst) = x.len
Base.IteratorEltype(::Type{IteratorAndFirst{F, T}}) where {F, T} = Base.IteratorEltype(T)
Base.eltype(x::IteratorAndFirst) = eltype(x.source)
Base.iterate(x::IteratorAndFirst) = x.first
function Base.iterate(x::IteratorAndFirst, st)
st === nothing && return nothing
return iterate(x.source, st)
end
showtable(table::AbstractMatrix; kwargs...) = showtable(Tables.table(table); kwargs...)
"""
showtable(table; options::Dict{Symbol, Any} = Dict{Symbol, Any}(), option_mutator! = identity,
dark = false, height = :auto, width = "100%", cell_changed = nothing)
Return a `WebIO.Scope` that displays the provided `table`.
Optional arguments:
- `options`: Directly passed to agGrid's `Grid` constructor. Refer to the
[documentation](https://www.ag-grid.com/documentation/) for more info.
- `options_mutator!`: Runs on the `options` dictionary populated by TableView and allows for
customizing the grid (at your own risk -- you can break the package by
supplying invalid options).
- `dark`: Switch to a dark theme.
- `title`: Displayed above the table if non-empty;
- `height`/`width`: CSS attributes specifying the output height and with.
- `cell_changed`: Either `nothing` or a function that takes a single argument with the fields
`"new"`, `"old"`, `"row"`, and `"col"`. This function is called whenever the
user edits a table field. Note that all values will be strings, so you need to
do the necessary conversions yourself.
"""
function showtable(table;
options::Dict{Symbol, Any} = Dict{Symbol, Any}(),
option_mutator! = identity,
dark::Bool = false,
title::String = "",
height = :auto,
width = "100%",
cell_changed = nothing,
async_threshold = 10_000,
)
rows = Tables.rows(table)
it_sz = Base.IteratorSize(rows)
has_len = it_sz isa Base.HasLength || it_sz isa Base.HasShape
tablelength = has_len ? length(rows) : nothing
if height === :auto
height = 500
if tablelength !== nothing
# header + footer height ≈ 40px, 28px per row
height = min(50 + tablelength*28, height)
end
end
schema = Tables.schema(rows)
if schema === nothing
st = iterate(rows)
rows = IteratorAndFirst(st, rows)
names = Symbol[]
types = []
if st !== nothing
row = st[1]
for nm in propertynames(row)
push!(names, nm)
push!(types, typeof(getproperty(row, nm)))
end
schema = Tables.Schema(names, types)
else
# no schema and no rows
end
else
names = schema.names
types = schema.types
end
async = tablelength === nothing || tablelength > async_threshold
w = Scope(imports = ag_grid_imports)
onCellValueChanged = @js function () end
if cell_changed != nothing
onedit = Observable(w, "onedit", Dict{Any,Any}(
"row" => 0,
"col" => 0,
"old" => 0,
"new" => 0
))
on(onedit) do x
cell_changed(x)
end
onCellValueChanged = @js function (ev)
$onedit[] = Dict(
"row" => ev.rowIndex,
"col" => ev.colDef.headerName,
"old" => ev.oldValue,
"new" => ev.newValue
)
end
end
coldefs = [Dict(
:headerName => string(n),
:editable => cell_changed !== nothing,
:headerTooltip => string(types[i]),
:field => string(n),
:sortable => !async,
:resizable => true,
:type => types[i] <: Union{Missing, T where T <: Number} ? "numericColumn" : nothing,
:filter => async ? false : types[i] <: Union{Missing, T where T <: Dates.Date} ? "agDateColumnFilter" :
types[i] <: Union{Missing, T where T <: Number} ? "agNumberColumnFilter" : true,
) for (i, n) in enumerate(names)]
pushfirst!(coldefs, Dict(
:headerName => "Row",
:editable => false,
:headerTooltip => "",
:field => "__row__",
:sortable => !async,
:resizable => true,
:type => "numericColumn",
:cellRenderer => "rowNumberRenderer",
:filter => false
))
options[:onCellValueChanged] = onCellValueChanged
options[:columnDefs] = coldefs
options[:multiSortKey] = "ctrl"
options[:rowSelection] = "multiple"
for e in ["onCellClicked", "onCellDoubleClicked", "onRowClicked", "onCellFocused", "onCellKeyDown"]
o = Observable{Any}(w, e, nothing)
handler = @js function (ev)
@var x = Dict()
if ev.rowIndex !== undefined
x["rowIndex"] = ev.rowIndex + 1
end
if ev.colDef !== undefined
x["column"] = ev.colDef.headerName
end
$o[] = x
end
options[Symbol(e)] = handler
end
id = string("grid-", string(uuid1())[1:8])
w.dom = dom"div"(
dom"div"(
title,
style = Dict(
"background-color" => dark ? "#1c1f20" : "#F5F7F7",
"color" => dark ? "#F5F7F7" : "#1c1f20",
"height" => isempty(title) ? "0" : "18px",
"padding" => isempty(title) ? "0" : "5px",
"font-family" => """-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif"""
)
),
dom"div"(className = "ag-theme-balham$(dark ? "-dark" : "")",
style = Dict("width" => to_css_size(width),
"height" => to_css_size(height)),
id = id
)
)
showfun = async ? _showtable_async! : _showtable_sync!
showfun(w, schema, types, rows, tablelength, id, options, option_mutator!)
return w
end
function _showtable_sync!(w, schema, types, rows, tablelength, id, options, option_mutator!)
options[:rowData] = JSONText(table2json(schema, rows, types))
option_mutator!(options)
license = get(ENV, "AG_GRID_LICENSE_KEY", nothing)
handler = @js function (RowNumberRenderer, agGrid)
@var gridOptions = $options
@var el = document.getElementById($id)
gridOptions.components = Dict(
"rowNumberRenderer" => RowNumberRenderer
)
if $(license !== nothing)
agGrid.LicenseManager.setLicenseKey($license)
end
this.table = @new agGrid.Grid(el, gridOptions)
gridOptions.columnApi.autoSizeAllColumns()
end
onimport(w, handler)
end
function _showtable_async!(w, schema, types, rows, tablelength, id, options, option_mutator!)
rowparams = Observable(w, "rowparams", Dict("startRow" => 1,
"endRow" => 100,
"successCallback" => @js v -> nothing))
requestedrows = Observable(w, "requestedrows", JSONText("{}"))
on(rowparams) do x
requestedrows[] = JSONText(table2json(schema, rows, types, requested = [x["startRow"] + 1, x["endRow"] + 1]))
end
onjs(requestedrows, @js function (val)
($rowparams[]).successCallback(val, $(tablelength))
end)
options[:maxConcurrentDatasourceRequests] = 1
options[:cacheBlockSize] = 1000
options[:maxBlocksInCache] = 100
options[:rowModelType] = "infinite"
options[:datasource] = Dict(
"getRows" =>
@js function (rowParams)
$rowparams[] = rowParams
end
,
"rowCount" => tablelength
)
license = get(ENV, "AG_GRID_LICENSE_KEY", nothing)
option_mutator!(options)
handler = @js function (RowNumberRenderer, agGrid)
@var gridOptions = $options
@var el = document.getElementById($id)
gridOptions.components = Dict(
"rowNumberRenderer" => RowNumberRenderer
)
if $(license !== nothing)
agGrid.LicenseManager.setLicenseKey($license)
end
this.table = @new agGrid.Grid(el, gridOptions)
gridOptions.columnApi.autoSizeAllColumns()
end
onimport(w, handler)
end
# By default all objects must use repr or sprint
_is_javascript_safe(x::Real) = false
function _is_javascript_safe(x::Integer)
min_safe_int = -(Int64(2)^53-1)
max_safe_int = Int64(2)^53-1
min_safe_int < x < max_safe_int
end
function _is_javascript_safe(x::AbstractFloat)
min_safe_float = -(Float64(2)^53-1)
max_safe_float = Float64(2)^53-1
min_safe_float < x < max_safe_float
end
# directly write JSON instead of allocating temporary dicts etc
function table2json(schema, rows, types; requested = nothing)
io = IOBuffer()
rowwriter = JSON.Writer.CompactContext(io)
JSON.begin_array(rowwriter)
ser = JSON.StandardSerialization()
for (i, row) in enumerate(rows)
if requested != nothing && (i < first(requested) || i > last(requested))
continue
end
JSON.delimit(rowwriter)
columnwriter = JSON.Writer.CompactContext(io)
JSON.begin_object(columnwriter)
Tables.eachcolumn(schema, row) do val, ind, name
if val isa Real && isfinite(val) && _is_javascript_safe(val)
JSON.show_pair(columnwriter, ser, name, val)
elseif val === nothing || val === missing
JSON.show_pair(columnwriter, ser, name, repr(val))
else
JSON.show_pair(columnwriter, ser, name, sprint(print, val))
end
end
JSON.end_object(columnwriter)
end
JSON.end_array(rowwriter)
String(take!(io))
end
end