Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagecell
Path: blob/master/js/widgets.js
447 views
1
import $ from "jquery";
2
import utils from "./utils";
3
4
// Creates global namespace
5
import "mpl";
6
import { console } from "./console";
7
8
export const widgets = {
9
Graphics: function (session) {
10
return function (comm, msg) {
11
var callbacks = {
12
iopub: { output: $.proxy(session.handle_output, session) },
13
};
14
var filename = msg.content.data.filename;
15
var filepath = session.kernel.kernel_url + "/files/";
16
var img = utils.createElement("img", {
17
src: filepath + filename,
18
});
19
var block_id = msg.metadata.interact_id || null;
20
21
session.output(img, block_id);
22
// Handle clicks inside the image
23
$(img).click(function (e) {
24
var offset = $(this).offset();
25
var x = (e.pageX - offset.left) / img.clientWidth;
26
var y = (e.pageY - offset.top) / img.clientHeight;
27
comm.send({ x: x, y: y, eventType: "click" }, callbacks);
28
});
29
// Handle mousemove inside the image
30
$(img).mousemove(function (e) {
31
var offset = $(this).offset();
32
var x = (e.pageX - offset.left) / img.clientWidth;
33
var y = (e.pageY - offset.top) / img.clientHeight;
34
comm.send({ x: x, y: y, eventType: "mousemove" }, callbacks);
35
});
36
37
// For messages from Python to javascript; we don't use this in this example
38
//comm.on_msg(function(msg) {console.log(msg)});
39
};
40
},
41
ThreeJS: function (session) {
42
return function (comm, msg) {
43
var that = this;
44
var callbacks = {
45
iopub: { output: $.proxy(session.handle_output, session) },
46
};
47
var div = utils.createElement("div", {
48
style: "border: 2px solid blue;margin:0;padding:0;",
49
});
50
var block_id = msg.metadata.interact_id || null;
51
52
$(div).salvus_threejs(msg.content.data);
53
54
that.obj = utils.proxy([
55
"add_3dgraphics_obj",
56
"render_scene",
57
"set_frame",
58
"animate",
59
]);
60
run_when_defined({
61
fn: function () {
62
return $(div).data("salvus-threejs");
63
},
64
cb: function (result) {
65
that.obj._run_callbacks(result);
66
that.obj = result;
67
},
68
err: function (err) {
69
comm.close();
70
console.log(err);
71
},
72
});
73
74
session.output(div, block_id);
75
76
comm.on_msg(function (msg) {
77
var data = msg.content.data;
78
var type = data.msg_type;
79
delete data.msg_type;
80
if (type === "add") {
81
that.obj.add_3dgraphics_obj(data);
82
} else if (type === "render") {
83
that.obj.render_scene(data);
84
} else if (type === "set_frame") {
85
that.obj.set_frame(data);
86
} else if (type === "animate") {
87
that.obj.animate(data);
88
} else if (type === "lights") {
89
that.obj.add_lights(data);
90
}
91
});
92
};
93
},
94
MPL: function (session) {
95
var callbacks = {
96
iopub: { output: $.proxy(session.handle_output, session) },
97
};
98
var comm_websocket = function (comm) {
99
var ws = {};
100
// MPL assumes we have a websocket that is not open yet
101
// so we run the onopen handler after they have a chance
102
// to set it.
103
ws.onopen = function () {};
104
setTimeout(ws.onopen(), 0);
105
ws.close = function () {
106
comm.close();
107
};
108
ws.send = function (m) {
109
comm.send(m, callbacks);
110
console.log("sending", m);
111
};
112
comm.on_msg(function (msg) {
113
console.log("receiving", msg);
114
ws.onmessage(msg["content"]["data"]);
115
});
116
return ws;
117
};
118
return function (comm, msg) {
119
var id = msg.content.data.id;
120
var div = utils.createElement("div", {
121
style: "border: 2px solid blue;margin:0;padding:0;",
122
});
123
var block_id = msg.metadata.interact_id || null;
124
session.output(div, block_id);
125
var c = comm_websocket(comm);
126
var m = new mpl.figure(
127
id,
128
c,
129
function () {
130
console.log("download");
131
},
132
div
133
);
134
};
135
},
136
};
137
138
export default widgets;
139
140