Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagecell
Path: blob/master/js/interact_controls.js
447 views
1
import $ from "jquery";
2
import utils from "./utils";
3
4
var interact_control_throttle = 100;
5
6
function InteractControl() {
7
return function (session, control_id) {
8
this.session = session;
9
this.control_id = control_id;
10
};
11
}
12
13
/*
14
// To implement a new control, do something like the following.
15
// See below for examples. The Checkbox control is particularly simple.
16
17
MyControl = InteractControl();
18
MyControl.prototype.create = function(data, block_id) {
19
// The create message is in `data`, while the block id to use for `this.session.output` is in block_id.
20
// This method creates the control and registers any change handlers.
21
// Change handlers should send a `variable_update` message back to the server. This message is handled
22
// by the control's python variable_update method.
23
};
24
25
MyControl.prototype.update = function(namespace, variable, control_id) {
26
// If a variable in the namespace is updated (i.e., the client receives a variable update message),
27
// this method is called. The namespace is the UUID of the namespace, the variable is the variable name as a string,
28
// and the control_id is the UUID of the control. This method should send a message and register a handler for the reply
29
// from the control's python update_control method. The reply handler should then update the control appropriately.
30
};
31
*/
32
33
var Slider = InteractControl();
34
Slider.prototype.create = function (data, block_id) {
35
var that = this;
36
this.control = this.session.output(
37
utils.createElement("div", { id: data.control_id }),
38
block_id
39
);
40
this.control.slider({
41
disabled: !data.enabled,
42
min: data.min,
43
max: data.max,
44
step: data.step,
45
slide: utils.throttle(function (event, ui) {
46
if (!event.originalEvent) {
47
return;
48
}
49
that.session.send_message(
50
"variable_update",
51
{ control_id: data.control_id, value: ui.value },
52
{
53
iopub: {
54
output: $.proxy(
55
that.session.handle_output,
56
that.session
57
),
58
},
59
}
60
);
61
}, interact_control_throttle),
62
});
63
};
64
65
Slider.prototype.update = function (namespace, variable, control_id) {
66
var that = this;
67
if (this.control_id !== control_id) {
68
this.session.send_message(
69
"control_update",
70
{
71
control_id: this.control_id,
72
namespace: namespace,
73
variable: variable,
74
},
75
{
76
iopub: {
77
output: $.proxy(this.session.handle_output, this.session),
78
},
79
shell: {
80
control_update_reply: function (content, metadata) {
81
if (content.status === "ok") {
82
that.control.slider("value", content.result.value);
83
}
84
},
85
},
86
}
87
);
88
}
89
};
90
91
var ExpressionBox = InteractControl();
92
ExpressionBox.prototype.create = function (data, block_id) {
93
var that = this;
94
this.control = this.session.output(
95
utils.createElement("input", {
96
id: data.control_id,
97
type: "textbox",
98
}),
99
block_id
100
);
101
this.control.change(function (event) {
102
if (!event.originalEvent) {
103
return;
104
}
105
that.session.send_message(
106
"variable_update",
107
{ control_id: data.control_id, value: $(this).val() },
108
{
109
iopub: {
110
output: $.proxy(that.session.handle_output, that.session),
111
},
112
}
113
);
114
});
115
};
116
117
ExpressionBox.prototype.update = function (namespace, variable, control_id) {
118
var that = this;
119
this.session.send_message(
120
"control_update",
121
{
122
control_id: this.control_id,
123
namespace: namespace,
124
variable: variable,
125
},
126
{
127
iopub: {
128
output: $.proxy(this.session.handle_output, this.session),
129
},
130
shell: {
131
control_update_reply: function (content, metadata) {
132
if (content.status === "ok") {
133
that.control.val(content.result.value);
134
}
135
},
136
},
137
}
138
);
139
};
140
141
var Checkbox = InteractControl();
142
Checkbox.prototype.create = function (data, block_id) {
143
var that = this;
144
this.control = this.session.output(
145
utils.createElement("input", {
146
id: data.control_id,
147
type: "checkbox",
148
}),
149
block_id
150
);
151
this.control.change(function (event) {
152
if (!event.originalEvent) {
153
return;
154
}
155
that.session.send_message(
156
"variable_update",
157
{ control_id: data.control_id, value: $(this).prop("checked") },
158
{
159
iopub: {
160
output: $.proxy(that.session.handle_output, that.session),
161
},
162
}
163
);
164
});
165
};
166
167
Checkbox.prototype.update = function (namespace, variable, control_id) {
168
var that = this;
169
this.session.send_message(
170
"control_update",
171
{
172
control_id: this.control_id,
173
namespace: namespace,
174
variable: variable,
175
},
176
{
177
iopub: {
178
output: $.proxy(this.session.handle_output, this.session),
179
},
180
shell: {
181
control_update_reply: function (content, metadata) {
182
if (content.status === "ok") {
183
that.control.prop("checked", content.result.value);
184
}
185
},
186
},
187
}
188
);
189
};
190
191
var OutputRegion = InteractControl();
192
OutputRegion.prototype.create = function (data, block_id) {
193
var that = this;
194
this.control = this.session.output(
195
utils.createElement("div", { id: data.control_id }),
196
block_id
197
);
198
this.session.output_blocks[this.control_id] = this.control;
199
this.message_number = 1;
200
};
201
202
OutputRegion.prototype.update = function (namespace, variable, control_id) {
203
var that = this;
204
this.message_number += 1;
205
var msg_number = this.message_number;
206
this.session.send_message(
207
"control_update",
208
{
209
control_id: this.control_id,
210
namespace: namespace,
211
variable: variable,
212
},
213
{
214
iopub: {
215
output: function (msg) {
216
if (msg_number === that.message_number) {
217
$.proxy(that.session.handle_output, that.session)(
218
msg,
219
that.control_id
220
);
221
}
222
},
223
},
224
}
225
);
226
};
227
228
export default {
229
Slider: Slider,
230
ExpressionBox: ExpressionBox,
231
Checkbox: Checkbox,
232
OutputRegion: OutputRegion,
233
};
234
235