Show More
@@ -1,464 +1,454 b'' | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2013 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // WidgetModel, WidgetView, and WidgetManager |
|
10 | 10 | //============================================================================ |
|
11 | 11 | /** |
|
12 | 12 | * Base Widget classes |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | * @submodule widget |
|
16 | 16 | */ |
|
17 | 17 | |
|
18 | 18 | "use strict"; |
|
19 | 19 | |
|
20 | 20 | // Use require.js 'define' method so that require.js is intelligent enough to |
|
21 | 21 | // syncronously load everything within this file when it is being 'required' |
|
22 | 22 | // elsewhere. |
|
23 | 23 | define(["components/underscore/underscore-min", |
|
24 | 24 | "components/backbone/backbone-min", |
|
25 | 25 | ], function(){ |
|
26 | 26 | |
|
27 | 27 | // Only run once on a notebook. |
|
28 | 28 | if (IPython.notebook.widget_manager == undefined) { |
|
29 | 29 | |
|
30 | 30 | //-------------------------------------------------------------------- |
|
31 | 31 | // WidgetModel class |
|
32 | 32 | //-------------------------------------------------------------------- |
|
33 | 33 | var WidgetModel = Backbone.Model.extend({ |
|
34 | 34 | constructor: function(comm_manager, comm, widget_view_types) { |
|
35 | 35 | this.comm_manager = comm_manager; |
|
36 | 36 | this.widget_view_types = widget_view_types; |
|
37 | 37 | this.pending_msgs = 0; |
|
38 | 38 | this.msg_throttle = 3; |
|
39 | 39 | this.msg_buffer = {}; |
|
40 | 40 | this.views = {}; |
|
41 | 41 | |
|
42 | 42 | // Remember comm associated with the model. |
|
43 | 43 | this.comm = comm; |
|
44 | 44 | comm.model = this; |
|
45 | 45 | |
|
46 | 46 | // Hook comm messages up to model. |
|
47 | 47 | comm.on_close($.proxy(this.handle_comm_closed, this)); |
|
48 | 48 | comm.on_msg($.proxy(this.handle_comm_msg, this)); |
|
49 | 49 | |
|
50 | 50 | return Backbone.Model.apply(this); |
|
51 | 51 | }, |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | update_other_views: function(caller) { |
|
55 | 55 | this.last_modified_view = caller; |
|
56 | 56 | this.save(this.changedAttributes(), {patch: true}); |
|
57 | 57 | |
|
58 |
for (var |
|
|
59 |
var views = this.views[ |
|
|
58 | for (var output_area in this.views) { | |
|
59 | var views = this.views[output_area]; | |
|
60 | 60 | for (var view_index in views) { |
|
61 | 61 | var view = views[view_index]; |
|
62 | 62 | if (view !== caller) { |
|
63 | 63 | view.update(); |
|
64 | 64 | } |
|
65 | 65 | } |
|
66 | 66 | } |
|
67 | 67 | }, |
|
68 | 68 | |
|
69 | 69 | |
|
70 | 70 | handle_status: function (output_area, msg) { |
|
71 | 71 | //execution_state : ('busy', 'idle', 'starting') |
|
72 | 72 | if (msg.content.execution_state=='idle') { |
|
73 | 73 | |
|
74 | 74 | // Send buffer if this message caused another message to be |
|
75 | 75 | // throttled. |
|
76 | 76 | if (this.msg_throttle == this.pending_msgs && |
|
77 | 77 | this.msg_buffer.length > 0) { |
|
78 | 78 | |
|
79 | 79 | var output_area = this._get_msg_output_area(msg); |
|
80 | 80 | var callbacks = this._make_callbacks(output_area); |
|
81 | 81 | var data = {sync_method: 'patch', sync_data: this.msg_buffer}; |
|
82 | 82 | comm.send(data, callbacks); |
|
83 | 83 | this.msg_buffer = {}; |
|
84 | 84 | } else { |
|
85 | 85 | |
|
86 | 86 | // Only decrease the pending message count if the buffer |
|
87 | 87 | // doesn't get flushed (sent). |
|
88 | 88 | --this.pending_msgs; |
|
89 | 89 | } |
|
90 | 90 | } |
|
91 | 91 | }, |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | // Custom syncronization logic. |
|
95 | 95 | handle_sync: function (method, options) { |
|
96 | 96 | var model_json = this.toJSON(); |
|
97 | 97 | |
|
98 | 98 | // Only send updated state if the state hasn't been changed |
|
99 | 99 | // during an update. |
|
100 | 100 | if (!this.updating) { |
|
101 | 101 | if (this.pending_msgs >= this.msg_throttle) { |
|
102 | 102 | // The throttle has been exceeded, buffer the current msg so |
|
103 | 103 | // it can be sent once the kernel has finished processing |
|
104 | 104 | // some of the existing messages. |
|
105 | 105 | if (method=='patch') { |
|
106 | 106 | for (var attr in options.attrs) { |
|
107 | 107 | this.msg_buffer[attr] = options.attrs[attr]; |
|
108 | 108 | } |
|
109 | 109 | } else { |
|
110 | 110 | this.msg_buffer = $.extend({}, model_json); // Copy |
|
111 | 111 | } |
|
112 | 112 | |
|
113 | 113 | } else { |
|
114 | 114 | // We haven't exceeded the throttle, send the message like |
|
115 | 115 | // normal. If this is a patch operation, just send the |
|
116 | 116 | // changes. |
|
117 | 117 | var send_json = model_json; |
|
118 | 118 | if (method=='patch') { |
|
119 | 119 | send_json = {}; |
|
120 | 120 | for (var attr in options.attrs) { |
|
121 | 121 | send_json[attr] = options.attrs[attr]; |
|
122 | 122 | } |
|
123 | 123 | } |
|
124 | 124 | |
|
125 | 125 | var data = {sync_method: method, sync_data: send_json}; |
|
126 |
var output_area = this. |
|
|
126 | var output_area = this.last_modified_view.output_area; | |
|
127 | 127 | var callbacks = this._make_callbacks(output_area); |
|
128 | 128 | this.comm.send(data, callbacks); |
|
129 | 129 | this.pending_msgs++; |
|
130 | 130 | } |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | // Since the comm is a one-way communication, assume the message |
|
134 | 134 | // arrived. |
|
135 | 135 | return model_json; |
|
136 | 136 | }, |
|
137 | 137 | |
|
138 | 138 | |
|
139 | 139 | // Handle incomming comm msg. |
|
140 | 140 | handle_comm_msg: function (msg) { |
|
141 | 141 | var method = msg.content.data.method; |
|
142 | 142 | switch (method){ |
|
143 | 143 | case 'display': |
|
144 | 144 | |
|
145 | 145 | // Try to get the cell index. |
|
146 |
var |
|
|
147 |
if ( |
|
|
146 | var output_area = this._get_output_area(msg.parent_header.msg_id); | |
|
147 | if (output_area == null) { | |
|
148 | 148 | console.log("Could not determine where the display" + |
|
149 | 149 | " message was from. Widget will not be displayed") |
|
150 | 150 | } else { |
|
151 | 151 | this.display_view(msg.content.data.view_name, |
|
152 | 152 | msg.content.data.parent, |
|
153 |
|
|
|
153 | output_area); | |
|
154 | 154 | } |
|
155 | 155 | break; |
|
156 | 156 | case 'update': |
|
157 | 157 | this.handle_update(msg.content.data.state); |
|
158 | 158 | break; |
|
159 | 159 | } |
|
160 | 160 | }, |
|
161 | 161 | |
|
162 | 162 | |
|
163 | 163 | // Handle when a widget is updated via the python side. |
|
164 | 164 | handle_update: function (state) { |
|
165 | 165 | this.updating = true; |
|
166 | 166 | try { |
|
167 | 167 | for (var key in state) { |
|
168 | 168 | if (state.hasOwnProperty(key)) { |
|
169 | 169 | if (key == "_css"){ |
|
170 | 170 | this.css = state[key]; |
|
171 | 171 | } else { |
|
172 | 172 | this.set(key, state[key]); |
|
173 | 173 | } |
|
174 | 174 | } |
|
175 | 175 | } |
|
176 | 176 | this.id = this.comm.comm_id; |
|
177 | 177 | this.save(); |
|
178 | 178 | } finally { |
|
179 | 179 | this.updating = false; |
|
180 | 180 | } |
|
181 | 181 | }, |
|
182 | 182 | |
|
183 | 183 | |
|
184 | 184 | // Handle when a widget is closed. |
|
185 | 185 | handle_comm_closed: function (msg) { |
|
186 |
for (var |
|
|
187 |
var views = this.views[ |
|
|
186 | for (var output_area in this.views) { | |
|
187 | var views = this.views[output_area]; | |
|
188 | 188 | for (var view_index in views) { |
|
189 | 189 | var view = views[view_index]; |
|
190 | 190 | view.remove(); |
|
191 | 191 | } |
|
192 | 192 | } |
|
193 | 193 | }, |
|
194 | 194 | |
|
195 | 195 | |
|
196 | 196 | // Create view that represents the model. |
|
197 |
display_view: function (view_name, parent_comm_id, |
|
|
197 | display_view: function (view_name, parent_comm_id, output_area) { | |
|
198 | 198 | var new_views = []; |
|
199 | 199 | |
|
200 | 200 | var displayed = false; |
|
201 | 201 | if (parent_comm_id != undefined) { |
|
202 | 202 | var parent_comm = this.comm_manager.comms[parent_comm_id]; |
|
203 | 203 | var parent_model = parent_comm.model; |
|
204 |
var parent_views = parent_model.views[ |
|
|
204 | var parent_views = parent_model.views[output_area]; | |
|
205 | 205 | for (var parent_view_index in parent_views) { |
|
206 | 206 | var parent_view = parent_views[parent_view_index]; |
|
207 | 207 | if (parent_view.display_child != undefined) { |
|
208 |
var view = this._create_view(view_name, |
|
|
208 | var view = this._create_view(view_name, output_area); | |
|
209 | 209 | new_views.push(view); |
|
210 | 210 | parent_view.display_child(view); |
|
211 | 211 | displayed = true; |
|
212 | 212 | } |
|
213 | 213 | } |
|
214 | 214 | } |
|
215 | 215 | |
|
216 | 216 | if (!displayed) { |
|
217 | 217 | // No parent view is defined or exists. Add the view's |
|
218 | 218 | // element to cell's widget div. |
|
219 |
var view = this._create_view(view_name, |
|
|
219 | var view = this._create_view(view_name, output_area); | |
|
220 | 220 | new_views.push(view); |
|
221 | var cell = IPython.notebook.get_cell(cell_index); | |
|
222 | cell.element.find('.widget-area').find('.widget-subarea') | |
|
221 | output_area.element.find('.widget-area').find('.widget-subarea') | |
|
223 | 222 | .append(view.$el) |
|
224 | 223 | .parent().show(); // Show the widget_area (parent of widget_subarea) |
|
225 | 224 | |
|
226 | 225 | } |
|
227 | 226 | |
|
228 | 227 | for (var view_index in new_views) { |
|
229 | 228 | var view = new_views[view_index]; |
|
230 | 229 | view.update(); |
|
231 | 230 | } |
|
232 | 231 | }, |
|
233 | 232 | |
|
234 | 233 | |
|
235 | 234 | // Create a view |
|
236 |
_create_view: function (view_name, |
|
|
235 | _create_view: function (view_name, output_area) { | |
|
237 | 236 | var view = new this.widget_view_types[view_name]({model: this}); |
|
238 | 237 | view.render(); |
|
239 |
if (this.views[ |
|
|
240 |
this.views[ |
|
|
238 | if (this.views[output_area]==undefined) { | |
|
239 | this.views[output_area] = [] | |
|
241 | 240 | } |
|
242 |
this.views[ |
|
|
243 | view.cell_index = cell_index; | |
|
241 | this.views[output_area].push(view); | |
|
242 | view.output_area = output_area; | |
|
244 | 243 | |
|
245 | 244 | // Handle when the view element is remove from the page. |
|
246 | 245 | var that = this; |
|
247 | 246 | view.$el.on("remove", function(){ |
|
248 |
var index = that.views[ |
|
|
247 | var index = that.views[output_area].indexOf(view); | |
|
249 | 248 | if (index > -1) { |
|
250 |
that.views[ |
|
|
249 | that.views[output_area].splice(index, 1); | |
|
251 | 250 | } |
|
252 | 251 | view.remove(); // Clean-up view |
|
253 |
if (that.views[ |
|
|
254 |
delete that.views[ |
|
|
252 | if (that.views[output_area].length()==0) { | |
|
253 | delete that.views[output_area]; | |
|
255 | 254 | } |
|
256 | 255 | |
|
257 | 256 | // Close the comm if there are no views left. |
|
258 | 257 | if (that.views.length()==0) { |
|
259 | 258 | that.comm.close(); |
|
260 | 259 | } |
|
261 | 260 | }); |
|
262 | 261 | return view; |
|
263 | 262 | }, |
|
264 | 263 | |
|
265 | 264 | |
|
266 | 265 | // Build a callback dict. |
|
267 | 266 | _make_callbacks: function (output_area) { |
|
268 | 267 | var callbacks = {}; |
|
269 | 268 | if (output_area != null) { |
|
270 | 269 | var that = this; |
|
271 | 270 | callbacks = { |
|
272 | 271 | iopub : { |
|
273 | 272 | output : $.proxy(output_area.handle_output, output_area), |
|
274 | 273 | clear_output : $.proxy(output_area.handle_clear_output, output_area), |
|
275 | 274 | status : function(msg){ |
|
276 | 275 | that.handle_status(output_area, msg); |
|
277 | 276 | }, |
|
278 |
get_ |
|
|
277 | get_output_area : function() { | |
|
279 | 278 | if (that.last_modified_view != undefined && |
|
280 |
that.last_modified_view. |
|
|
281 |
return that.last_modified_view. |
|
|
279 | that.last_modified_view.output_area != undefined) { | |
|
280 | return that.last_modified_view.output_area; | |
|
282 | 281 | } else { |
|
283 |
return |
|
|
282 | return null | |
|
284 | 283 | } |
|
285 | 284 | }, |
|
286 | 285 | }, |
|
287 | 286 | }; |
|
288 | 287 | } |
|
289 | 288 | return callbacks; |
|
290 | 289 | }, |
|
291 | 290 | |
|
292 | 291 | |
|
293 | 292 | // Get the cell index corresponding to the msg_id. |
|
294 | _get_cell_index: function (msg_id) { | |
|
293 | // output_area is a JQuery DOM element handle that has widget_area | |
|
294 | // and nested widget_subarea elements. | |
|
295 | _get_output_area: function (msg_id) { | |
|
295 | 296 | |
|
296 | 297 | // First, guess cell.execute triggered |
|
297 | 298 | var cells = IPython.notebook.get_cells(); |
|
298 | 299 | for (var cell_index in cells) { |
|
299 | 300 | if (cells[cell_index].last_msg_id == msg_id) { |
|
300 |
r |
|
|
301 | var cell = IPython.notebook.get_cell(cell_index) | |
|
302 | return cell.output_area; | |
|
301 | 303 | } |
|
302 | 304 | } |
|
303 | 305 | |
|
304 | 306 | // Second, guess widget triggered |
|
305 | 307 | var callbacks = this.comm_manager.kernel.get_callbacks_for_msg(msg_id) |
|
306 |
if (callbacks != undefined && callbacks.iopub != undefined && callbacks.iopub.get_ |
|
|
307 |
var |
|
|
308 |
if ( |
|
|
309 |
return |
|
|
308 | if (callbacks != undefined && callbacks.iopub != undefined && callbacks.iopub.get_output_area != undefined) { | |
|
309 | var output_area = callbacks.iopub.get_output_area(); | |
|
310 | if (output_area != null) { | |
|
311 | return output_area; | |
|
310 | 312 | } |
|
311 | 313 | } |
|
312 | 314 | |
|
313 | 315 | // Not triggered by a widget or a cell |
|
314 |
return |
|
|
315 | }, | |
|
316 | ||
|
317 | ||
|
318 | // Get the cell output area corresponding to the view. | |
|
319 | _get_view_output_area: function (view) { | |
|
320 | return this._get_cell_output_area(view.cell_index); | |
|
316 | return null; | |
|
321 | 317 | }, |
|
322 | 318 | |
|
323 | ||
|
324 | // Get the cell output area corresponding to the cell index. | |
|
325 | _get_cell_output_area: function (cell_index) { | |
|
326 | var cell = IPython.notebook.get_cell(cell_index) | |
|
327 | return cell.output_area; | |
|
328 | }, | |
|
329 | 319 | }); |
|
330 | 320 | |
|
331 | 321 | |
|
332 | 322 | //-------------------------------------------------------------------- |
|
333 | 323 | // WidgetView class |
|
334 | 324 | //-------------------------------------------------------------------- |
|
335 | 325 | var WidgetView = Backbone.View.extend({ |
|
336 | 326 | |
|
337 | 327 | initialize: function() { |
|
338 | 328 | this.visible = true; |
|
339 | 329 | this.model.on('change',this.update,this); |
|
340 | 330 | this._add_class_calls = this.model.get('_add_class')[0]; |
|
341 | 331 | this._remove_class_calls = this.model.get('_remove_class')[0]; |
|
342 | 332 | }, |
|
343 | 333 | |
|
344 | 334 | update: function() { |
|
345 | 335 | if (this.model.get('visible') != undefined) { |
|
346 | 336 | if (this.visible != this.model.get('visible')) { |
|
347 | 337 | this.visible = this.model.get('visible'); |
|
348 | 338 | if (this.visible) { |
|
349 | 339 | this.$el.show(); |
|
350 | 340 | } else { |
|
351 | 341 | this.$el.hide(); |
|
352 | 342 | } |
|
353 | 343 | } |
|
354 | 344 | } |
|
355 | 345 | |
|
356 | 346 | if (this.model.css != undefined) { |
|
357 | 347 | for (var selector in this.model.css) { |
|
358 | 348 | if (this.model.css.hasOwnProperty(selector)) { |
|
359 | 349 | |
|
360 | 350 | // Apply the css traits to all elements that match the selector. |
|
361 | 351 | var elements = this.get_selector_element(selector); |
|
362 | 352 | if (elements.length > 0) { |
|
363 | 353 | var css_traits = this.model.css[selector]; |
|
364 | 354 | for (var css_key in css_traits) { |
|
365 | 355 | if (css_traits.hasOwnProperty(css_key)) { |
|
366 | 356 | elements.css(css_key, css_traits[css_key]); |
|
367 | 357 | } |
|
368 | 358 | } |
|
369 | 359 | } |
|
370 | 360 | } |
|
371 | 361 | } |
|
372 | 362 | } |
|
373 | 363 | |
|
374 | 364 | var add_class = this.model.get('_add_class'); |
|
375 | 365 | if (add_class != undefined){ |
|
376 | 366 | var add_class_calls = add_class[0]; |
|
377 | 367 | if (add_class_calls > this._add_class_calls) { |
|
378 | 368 | this._add_class_calls = add_class_calls; |
|
379 | 369 | var elements = this.get_selector_element(add_class[1]); |
|
380 | 370 | if (elements.length > 0) { |
|
381 | 371 | elements.addClass(add_class[2]); |
|
382 | 372 | } |
|
383 | 373 | } |
|
384 | 374 | } |
|
385 | 375 | |
|
386 | 376 | var remove_class = this.model.get('_remove_class'); |
|
387 | 377 | if (remove_class != undefined){ |
|
388 | 378 | var remove_class_calls = remove_class[0]; |
|
389 | 379 | if (remove_class_calls > this._remove_class_calls) { |
|
390 | 380 | this._remove_class_calls = remove_class_calls; |
|
391 | 381 | var elements = this.get_selector_element(remove_class[1]); |
|
392 | 382 | if (elements.length > 0) { |
|
393 | 383 | elements.removeClass(remove_class[2]); |
|
394 | 384 | } |
|
395 | 385 | } |
|
396 | 386 | } |
|
397 | 387 | }, |
|
398 | 388 | |
|
399 | 389 | get_selector_element: function(selector) { |
|
400 | 390 | // Get the elements via the css selector. If the selector is |
|
401 | 391 | // blank, apply the style to the $el_to_style element. If |
|
402 | 392 | // the $el_to_style element is not defined, use apply the |
|
403 | 393 | // style to the view's element. |
|
404 | 394 | var elements = this.$el.find(selector); |
|
405 | 395 | if (selector=='') { |
|
406 | 396 | if (this.$el_to_style == undefined) { |
|
407 | 397 | elements = this.$el; |
|
408 | 398 | } else { |
|
409 | 399 | elements = this.$el_to_style; |
|
410 | 400 | } |
|
411 | 401 | } |
|
412 | 402 | return elements; |
|
413 | 403 | }, |
|
414 | 404 | }); |
|
415 | 405 | |
|
416 | 406 | |
|
417 | 407 | //-------------------------------------------------------------------- |
|
418 | 408 | // WidgetManager class |
|
419 | 409 | //-------------------------------------------------------------------- |
|
420 | 410 | var WidgetManager = function(comm_manager){ |
|
421 | 411 | this.comm_manager = comm_manager; |
|
422 | 412 | this.widget_model_types = {}; |
|
423 | 413 | this.widget_view_types = {}; |
|
424 | 414 | |
|
425 | 415 | var that = this; |
|
426 | 416 | Backbone.sync = function(method, model, options, error) { |
|
427 | 417 | var result = model.handle_sync(method, options); |
|
428 | 418 | if (options.success) { |
|
429 | 419 | options.success(result); |
|
430 | 420 | } |
|
431 | 421 | }; |
|
432 | 422 | } |
|
433 | 423 | |
|
434 | 424 | |
|
435 | 425 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { |
|
436 | 426 | // Register the widget with the comm manager. Make sure to pass this object's context |
|
437 | 427 | // in so `this` works in the call back. |
|
438 | 428 | this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this)); |
|
439 | 429 | this.widget_model_types[widget_model_name] = widget_model_type; |
|
440 | 430 | } |
|
441 | 431 | |
|
442 | 432 | |
|
443 | 433 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { |
|
444 | 434 | this.widget_view_types[widget_view_name] = widget_view_type; |
|
445 | 435 | } |
|
446 | 436 | |
|
447 | 437 | |
|
448 | 438 | WidgetManager.prototype.handle_com_open = function (comm, msg) { |
|
449 | 439 | var widget_type_name = msg.content.target_name; |
|
450 | 440 | var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this.widget_view_types); |
|
451 | 441 | } |
|
452 | 442 | |
|
453 | 443 | |
|
454 | 444 | //-------------------------------------------------------------------- |
|
455 | 445 | // Init code |
|
456 | 446 | //-------------------------------------------------------------------- |
|
457 | 447 | IPython.WidgetManager = WidgetManager; |
|
458 | 448 | IPython.WidgetModel = WidgetModel; |
|
459 | 449 | IPython.WidgetView = WidgetView; |
|
460 | 450 | |
|
461 | 451 | IPython.notebook.widget_manager = new WidgetManager(IPython.notebook.kernel.comm_manager); |
|
462 | 452 | |
|
463 | 453 | }; |
|
464 | 454 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now