forked from jheinen/GR.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.jl
440 lines (400 loc) · 12.3 KB
/
js.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
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
module js
import GR
pxwidth = 640
pxheight = 480
id_count = 0
const js_running = Ref(false)
const checking_js = Ref(false)
const port = Ref(0)
const connected = Ref(false)
const connect_cond = Ref(Condition())
conditions = Dict()
l = ReentrantLock()
mutable struct JSTermWidget
identifier::Int
width::Int
height::Int
disp::Int
visible::Bool
end
function inject_js()
global wss
wss = nothing
_gr_js = if isfile(joinpath(ENV["GRDIR"], "lib", "gr.js"))
_gr_js = try
_gr_js = open(joinpath(ENV["GRDIR"], "lib", "gr.js")) do f
_gr_js = read(f, String)
_gr_js
end
catch e
nothing
end
_gr_js
end
if _gr_js === nothing
error(string("Unable to open '", joinpath(ENV["GRDIR"], "lib", "gr.js"), "'."))
else
display(HTML(string("""
<script type="text/javascript" id="jsterm-javascript">
WEB_SOCKET_ADDRESS = 'ws://127.0.0.1:""", port[], """';
if (typeof jsterm === 'undefined') {
""", _gr_js, """
jsterm = new JSTerm();
jsterm.connectWs();
}
</script>
""")))
end
end
function check_js()
if !checking_js[]
checking_js[] = true
d = Dict("text/html"=>string("""
<script type="text/javascript">
WEB_SOCKET_ADDRESS = 'ws://127.0.0.1:""", port[], """';
if (typeof JSTerm !== 'undefined') {
if (typeof jsterm === 'undefined') {
jsterm = new JSTerm();
}
jsterm.connectWs();
} else {
ws = new WebSocket('ws://127.0.0.1:""", port[], """');
ws.onopen = function() {
ws.send("inject-js");
ws.close();
}
}
</script>
"""))
transient = Dict("display_id"=>"jsterm_check")
Main.IJulia.send_ipython(Main.IJulia.publish[], Main.IJulia.msg_pub(Main.IJulia.execute_msg, "display_data", Dict("data"=>d, "metadata"=>Dict(), "transient"=>transient)))
end
end
function JSTermWidget(id::Int, width::Int, height::Int, disp::Int)
global id_count
if GR.isijulia() && GR.displayname() == "js-server"
id_count += 1
if !js_running[]
@async check_js()
end
JSTermWidget(id, width, height, disp, false)
else
error("JSTermWidget is only available in IJulia environments")
end
end
wss = nothing
evthandler = Dict{Int32,Any}()
global_evthandler = nothing
function register_evthandler(f::Function, id)
global evthandler, l
if GR.isijulia()
send_command(Dict("command" => "enable_events"), "cmd", id)
lock(l) do
evthandler[id] = f
end
else
error("register_evthandler is only available in IJulia environments")
end
end
function unregister_evthandler(id)
global evthandler, l
if GR.isijulia()
if global_evthandler === nothing
send_command(Dict("command" => "disable_events"), "cmd", id)
end
lock(l) do
evthandler[id] = nothing
end
else
error("unregister_evthandler is only available in IJulia environments")
end
end
function register_evthandler(f::Function)
global global_evthandler, l
if GR.isijulia()
send_command(Dict("command" => "enable_events"), "cmd", nothing)
lock(l) do
evthandler[id] = f
end
else
error("register_evthandler is only available in IJulia environments")
end
end
function unregister_evthandler()
global global_evthandler, evthandler, l
if GR.isijulia()
send_command(Dict("command" => "disable_events"), "cmd", nothing)
lock(l) do
for key in keys(evthandler)
if evthandler[key] !== nothing
send_command(Dict("command" => "enable_events"), "cmd", key)
end
end
evthandler[id] = nothing
end
else
error("unregister_evthandler is only available in IJulia environments")
end
end
function send_command(msg, msgtype, id=nothing)
global wss, ws
if GR.isijulia() && GR.displayname() == "js-server"
if id !== nothing
m = merge(msg, Dict("type" => msgtype, "id" => id))
else
m = merge(msg, Dict("type" => msgtype))
end
if !js_running[]
conditions["sendonconnect"] = Condition()
@async check_js()
wait(conditions["sendonconnect"])
end
if ws !== nothing
try
HTTP.write(ws, Array{UInt8}(JSON.json(m)))
catch e
ws = nothing
js_running[] = false
end
end
elseif GR.displayname() == "js-server"
error("'js-server' is only available in IJulia environments.")
else
error(str("Display '", GR.displayname(), "' does not support send_command()."))
end
end
function send_evt(msg, id)
if GR.isijulia()
send_command(msg, "evt", id)
else
error("send_evt is only available in IJulia environments")
end
end
function disable_jseventhandling(id)
if GR.isijulia()
send_command(Dict("command" => "disable_jseventhandling"), "cmd", id)
else
error("disable_jseventhandling is only available in IJulia environments")
end
end
function enable_jseventhandling(id)
if GR.isijulia()
send_command(Dict("command" => "enable_jseventhandling"), "cmd", id)
else
error("enable_jseventhandling is only available in IJulia environments")
end
end
function disable_jseventhandling()
if GR.isijulia()
send_command(Dict("command" => "disable_jseventhandling"), "cmd", nothing)
else
error("disable_jseventhandling is only available in IJulia environments")
end
end
function enable_jseventhandling()
global ws
if GR.isijulia()
send_command(Dict("command" => "enable_jseventhandling"), "cmd", nothing)
else
error("enable_jseventhandling is only available in IJulia environments")
end
end
function comm_msg_callback(msg)
global conditions, evthandler, l
data = msg
if haskey(data, "type")
if data["type"] == "save"
d = Dict("text/html"=>string("<script type=\"text/javascript\" class=\"jsterm-data-widget\">", data["content"]["data"]["widget_data"], "</script>"))
transient = Dict("display_id"=>string("save_display_", data["display_id"]))
Main.IJulia.send_ipython(Main.IJulia.publish[], Main.IJulia.msg_pub(Main.IJulia.execute_msg, "update_display_data", Dict("data"=>d, "metadata"=>Dict(), "transient"=>transient)))
elseif data["type"] == "evt"
lock(l) do
if haskey(evthandler, data["id"]) && evthandler[data["id"]] !== nothing
evthandler[data["id"]](data["content"])
end
end
elseif data["type"] == "value"
elseif data["type"] == "createDisplay"
d = Dict("text/html"=>"")
transient = Dict("display_id"=>string("save_display_", data["dispid"]))
Main.IJulia.send_ipython(Main.IJulia.publish[], Main.IJulia.msg_pub(Main.IJulia.execute_msg, "display_data", Dict("data"=>d, "metadata"=>Dict(), "transient"=>transient)))
display(HTML(string("<div style=\"display: none;\" id=\"jsterm-display-", data["dispid"], "\">")))
elseif data["type"] == "ack"
notify(conditions[data["dispid"]])
end
end
end
function settooltip(tthtml, ttdata, id)
send_command(Dict("command" => "settooltip", "html" => tthtml, "data" => ttdata), "cmd", id)
return nothing
end
const pluto_data = Ref("")
const pluto_disp = Ref("")
function get_html()
outp = string("""
<div id="jsterm-display-""", pluto_disp[], """\">
</div>
<script type="text/javascript">
if (typeof jsterm === "undefined") {
var jsterm = null;
}
function run_on_start(data, display) {
if (typeof JSTerm === "undefined") {
setTimeout(function() {run_on_start(data, display)}, 100);
return;
}
if (jsterm === null) {
jsterm = new JSTerm(true);
}
jsterm.draw({
"json": data,
"display": display
})
}
run_on_start('""", pluto_data[], """', '""", pluto_disp[], """');
</script>
""")
if GR.isijulia()
display(HTML(string("""
<script type="text/javascript">
if (typeof JSTerm === "undefined" && document.getElementById('jstermImport') == null) {
let jstermScript = document.createElement("script");
jstermScript.setAttribute("src", \"""", jssource[], """\");
jstermScript.setAttribute("type", "text/javascript")
jstermScript.setAttribute("id", "jstermImport")
document.body.appendChild(jstermScript);
}
</script>
""", outp)))
return
end
if plutoisinit[]
return HTML(outp)
else
return HTML(string("""<script type="text/javascript" src=" """, jssource[], """ "></script>""", outp))
end
end
function jsterm_send(data::String, disp)
global draw_end_condition, ws, conditions
if GR.isijulia() && GR.displayname() == "js-server"
if !js_running[]
conditions["sendonconnect"] = Condition()
@async check_js()
wait(conditions["sendonconnect"])
end
if ws !== nothing
try
conditions[disp] = Condition()
HTTP.write(ws, Array{UInt8}(JSON.json(Dict("json" => data, "type"=>"draw", "display"=>disp))))
wait(conditions[disp])
catch e
ws = nothing
js_running[] = false
end
end
elseif GR.displayname() == "pluto" || GR.displayname() == "js"
pluto_data[] = data
pluto_disp[] = disp
else
error("jsterm_send is only available in IJulia environments and Pluto.jl notebooks")
end
end
function get_prev_plot_id()
send_command(Dict("value"=>"prev_id"), "inq")
end
function set_ref_id(id::Int)
send_command(Dict("id"=>id), "set_ref_id")
end
function recv(name::Cstring, id::Int32, msg::Cstring)
# receives string from C and sends it to JS via Comm
global draw_end_condition
id = string(UUIDs.uuid4());
jsterm_send(unsafe_string(msg), id)
return convert(Int32, 1)
end
function send(name::Cstring, id::Int32)
# Dummy function, not in use
return convert(Cstring, "String")
end
const send_c = Ref(C_NULL)
const recv_c = Ref(C_NULL)
const init = Ref(false)
function ws_cb(webs)
global comm_msg_callback, ws, conditions
check = false
while !eof(webs)
data = readavailable(webs)
if length(data) > 0
data = String(data)
if data == "inject-js"
check = true
inject_js()
d = Dict("text/html"=>string(""))
transient = Dict("display_id"=>"jsterm_check")
Main.IJulia.send_ipython(Main.IJulia.publish[], Main.IJulia.msg_pub(Main.IJulia.execute_msg, "update_display_data", Dict("data"=>d, "metadata"=>Dict(), "transient"=>transient)))
checking_js[] = false
elseif data == "js-running"
d = Dict("text/html"=>string(""))
transient = Dict("display_id"=>"jsterm_check")
Main.IJulia.send_ipython(Main.IJulia.publish[], Main.IJulia.msg_pub(Main.IJulia.execute_msg, "update_display_data", Dict("data"=>d, "metadata"=>Dict(), "transient"=>transient)))
ws = webs
js_running[] = true
if haskey(conditions, "sendonconnect")
notify(conditions["sendonconnect"])
end
else
comm_msg_callback(JSON.parse(data))
end
end
end
if !check
ws = nothing
js_running[] = false
@async check_js()
end
end
const plutoisinit = Ref(false)
const jssource = Ref("https://gr-framework.org/downloads/gr-0.61.0.js")
function init_pluto(source=jssource[]::String)
plutoisinit[] = true
return HTML(string("""
<script type="text/javascript" src=" """, source, """ "></script>
"""))
end
function initjs()
if !init[]
init[] = true
send_c[] = @cfunction(send, Cstring, (Cstring, Int32))
recv_c[] = @cfunction(recv, Int32, (Cstring, Int32, Cstring))
@eval js begin
import UUIDs
import JSON
end
if GR.displayname() == "js-server"
if GR.isijulia()
@eval js begin
import HTTP
import Sockets
end
connect_cond[] = Condition()
connected[] = false
ws_server_task = @async begin
port[], server = Sockets.listenany(8081)
@async HTTP.listen(server=server) do webs
HTTP.WebSockets.upgrade(ws_cb, webs)
end
connected[] = true
notify(connect_cond[])
end
if !connected[]
wait(connect_cond[])
end
else
error("'js-server' is only available in IJulia environments.")
end
end
end
send_c[], recv_c[]
end
precompile(initjs, ())
end # module