-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathwidgets.js
138 lines (129 loc) · 4.77 KB
/
widgets.js
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
import $ from "jquery";
import utils from "./utils";
// Creates global namespace
import "mpl";
import { console } from "./console";
export const widgets = {
Graphics: function (session) {
return function (comm, msg) {
var callbacks = {
iopub: { output: $.proxy(session.handle_output, session) },
};
var filename = msg.content.data.filename;
var filepath = session.kernel.kernel_url + "/files/";
var img = utils.createElement("img", {
src: filepath + filename,
});
var block_id = msg.metadata.interact_id || null;
session.output(img, block_id);
// Handle clicks inside the image
$(img).click(function (e) {
var offset = $(this).offset();
var x = (e.pageX - offset.left) / img.clientWidth;
var y = (e.pageY - offset.top) / img.clientHeight;
comm.send({ x: x, y: y, eventType: "click" }, callbacks);
});
// Handle mousemove inside the image
$(img).mousemove(function (e) {
var offset = $(this).offset();
var x = (e.pageX - offset.left) / img.clientWidth;
var y = (e.pageY - offset.top) / img.clientHeight;
comm.send({ x: x, y: y, eventType: "mousemove" }, callbacks);
});
// For messages from Python to javascript; we don't use this in this example
//comm.on_msg(function(msg) {console.log(msg)});
};
},
ThreeJS: function (session) {
return function (comm, msg) {
var that = this;
var callbacks = {
iopub: { output: $.proxy(session.handle_output, session) },
};
var div = utils.createElement("div", {
style: "border: 2px solid blue;margin:0;padding:0;",
});
var block_id = msg.metadata.interact_id || null;
$(div).salvus_threejs(msg.content.data);
that.obj = utils.proxy([
"add_3dgraphics_obj",
"render_scene",
"set_frame",
"animate",
]);
run_when_defined({
fn: function () {
return $(div).data("salvus-threejs");
},
cb: function (result) {
that.obj._run_callbacks(result);
that.obj = result;
},
err: function (err) {
comm.close();
console.log(err);
},
});
session.output(div, block_id);
comm.on_msg(function (msg) {
var data = msg.content.data;
var type = data.msg_type;
delete data.msg_type;
if (type === "add") {
that.obj.add_3dgraphics_obj(data);
} else if (type === "render") {
that.obj.render_scene(data);
} else if (type === "set_frame") {
that.obj.set_frame(data);
} else if (type === "animate") {
that.obj.animate(data);
} else if (type === "lights") {
that.obj.add_lights(data);
}
});
};
},
MPL: function (session) {
var callbacks = {
iopub: { output: $.proxy(session.handle_output, session) },
};
var comm_websocket = function (comm) {
var ws = {};
// MPL assumes we have a websocket that is not open yet
// so we run the onopen handler after they have a chance
// to set it.
ws.onopen = function () {};
setTimeout(ws.onopen(), 0);
ws.close = function () {
comm.close();
};
ws.send = function (m) {
comm.send(m, callbacks);
console.log("sending", m);
};
comm.on_msg(function (msg) {
console.log("receiving", msg);
ws.onmessage(msg["content"]["data"]);
});
return ws;
};
return function (comm, msg) {
var id = msg.content.data.id;
var div = utils.createElement("div", {
style: "border: 2px solid blue;margin:0;padding:0;",
});
var block_id = msg.metadata.interact_id || null;
session.output(div, block_id);
var c = comm_websocket(comm);
var m = new mpl.figure(
id,
c,
function () {
console.log("download");
},
div
);
};
},
};
export default widgets;