##// END OF EJS Templates
Fixed & separated output_area -> widget_area logic
Jonathan Frederic -
Show More
@@ -1,454 +1,463 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 58 for (var output_area in this.views) {
59 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 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 146 var output_area = this._get_output_area(msg.parent_header.msg_id);
147 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 186 for (var output_area in this.views) {
187 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 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 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 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 219 var view = this._create_view(view_name, output_area);
220 220 new_views.push(view);
221 output_area.element.find('.widget-area').find('.widget-subarea')
222 .append(view.$el)
223 .parent().show(); // Show the widget_area (parent of widget_subarea)
221 this._get_widget_area_element(output_area, true)
222 .append(view.$el);
224 223
225 224 }
226 225
227 226 for (var view_index in new_views) {
228 227 var view = new_views[view_index];
229 228 view.update();
230 229 }
231 230 },
232 231
233 232
234 233 // Create a view
235 234 _create_view: function (view_name, output_area) {
236 235 var view = new this.widget_view_types[view_name]({model: this});
237 236 view.render();
238 237 if (this.views[output_area]==undefined) {
239 238 this.views[output_area] = []
240 239 }
241 240 this.views[output_area].push(view);
242 241 view.output_area = output_area;
243 242
244 243 // Handle when the view element is remove from the page.
245 244 var that = this;
246 245 view.$el.on("remove", function(){
247 246 var index = that.views[output_area].indexOf(view);
248 247 if (index > -1) {
249 248 that.views[output_area].splice(index, 1);
250 249 }
251 250 view.remove(); // Clean-up view
252 251 if (that.views[output_area].length()==0) {
253 252 delete that.views[output_area];
254 253 }
255 254
256 255 // Close the comm if there are no views left.
257 256 if (that.views.length()==0) {
258 257 that.comm.close();
259 258 }
260 259 });
261 260 return view;
262 261 },
263 262
264 263
265 264 // Build a callback dict.
266 265 _make_callbacks: function (output_area) {
267 266 var callbacks = {};
268 267 if (output_area != null) {
269 268 var that = this;
270 269 callbacks = {
271 270 iopub : {
272 271 output : $.proxy(output_area.handle_output, output_area),
273 272 clear_output : $.proxy(output_area.handle_clear_output, output_area),
274 273 status : function(msg){
275 274 that.handle_status(output_area, msg);
276 275 },
277 276 get_output_area : function() {
278 277 if (that.last_modified_view != undefined &&
279 278 that.last_modified_view.output_area != undefined) {
280 279 return that.last_modified_view.output_area;
281 280 } else {
282 281 return null
283 282 }
284 283 },
285 284 },
286 285 };
287 286 }
288 287 return callbacks;
289 288 },
290 289
291 290
292 // Get the cell index corresponding to the msg_id.
293 // output_area is a JQuery DOM element handle that has widget_area
294 // and nested widget_subarea elements.
291 // Get the output area corresponding to the msg_id.
292 // output_area is an instance of
295 293 _get_output_area: function (msg_id) {
296 294
297 295 // First, guess cell.execute triggered
298 296 var cells = IPython.notebook.get_cells();
299 297 for (var cell_index in cells) {
300 298 if (cells[cell_index].last_msg_id == msg_id) {
301 299 var cell = IPython.notebook.get_cell(cell_index)
302 300 return cell.output_area;
303 301 }
304 302 }
305 303
306 304 // Second, guess widget triggered
307 305 var callbacks = this.comm_manager.kernel.get_callbacks_for_msg(msg_id)
308 306 if (callbacks != undefined && callbacks.iopub != undefined && callbacks.iopub.get_output_area != undefined) {
309 307 var output_area = callbacks.iopub.get_output_area();
310 308 if (output_area != null) {
311 309 return output_area;
312 310 }
313 311 }
314 312
315 313 // Not triggered by a widget or a cell
316 314 return null;
317 315 },
318 316
317 // Gets widget output area (as a JQuery element) from the
318 // output_area (Ipython.OutputArea instance)
319 _get_widget_area_element: function (output_area, show) {
320 var widget_area = output_area.element
321 .parent() // output_wrapper
322 .parent() // cell
323 .find('.widget-area');
324 if (show) { widget_area.show(); }
325 return widget_area.find('.widget-subarea');
326 },
327
319 328 });
320 329
321 330
322 331 //--------------------------------------------------------------------
323 332 // WidgetView class
324 333 //--------------------------------------------------------------------
325 334 var WidgetView = Backbone.View.extend({
326 335
327 336 initialize: function() {
328 337 this.visible = true;
329 338 this.model.on('change',this.update,this);
330 339 this._add_class_calls = this.model.get('_add_class')[0];
331 340 this._remove_class_calls = this.model.get('_remove_class')[0];
332 341 },
333 342
334 343 update: function() {
335 344 if (this.model.get('visible') != undefined) {
336 345 if (this.visible != this.model.get('visible')) {
337 346 this.visible = this.model.get('visible');
338 347 if (this.visible) {
339 348 this.$el.show();
340 349 } else {
341 350 this.$el.hide();
342 351 }
343 352 }
344 353 }
345 354
346 355 if (this.model.css != undefined) {
347 356 for (var selector in this.model.css) {
348 357 if (this.model.css.hasOwnProperty(selector)) {
349 358
350 359 // Apply the css traits to all elements that match the selector.
351 360 var elements = this.get_selector_element(selector);
352 361 if (elements.length > 0) {
353 362 var css_traits = this.model.css[selector];
354 363 for (var css_key in css_traits) {
355 364 if (css_traits.hasOwnProperty(css_key)) {
356 365 elements.css(css_key, css_traits[css_key]);
357 366 }
358 367 }
359 368 }
360 369 }
361 370 }
362 371 }
363 372
364 373 var add_class = this.model.get('_add_class');
365 374 if (add_class != undefined){
366 375 var add_class_calls = add_class[0];
367 376 if (add_class_calls > this._add_class_calls) {
368 377 this._add_class_calls = add_class_calls;
369 378 var elements = this.get_selector_element(add_class[1]);
370 379 if (elements.length > 0) {
371 380 elements.addClass(add_class[2]);
372 381 }
373 382 }
374 383 }
375 384
376 385 var remove_class = this.model.get('_remove_class');
377 386 if (remove_class != undefined){
378 387 var remove_class_calls = remove_class[0];
379 388 if (remove_class_calls > this._remove_class_calls) {
380 389 this._remove_class_calls = remove_class_calls;
381 390 var elements = this.get_selector_element(remove_class[1]);
382 391 if (elements.length > 0) {
383 392 elements.removeClass(remove_class[2]);
384 393 }
385 394 }
386 395 }
387 396 },
388 397
389 398 get_selector_element: function(selector) {
390 399 // Get the elements via the css selector. If the selector is
391 400 // blank, apply the style to the $el_to_style element. If
392 401 // the $el_to_style element is not defined, use apply the
393 402 // style to the view's element.
394 403 var elements = this.$el.find(selector);
395 404 if (selector=='') {
396 405 if (this.$el_to_style == undefined) {
397 406 elements = this.$el;
398 407 } else {
399 408 elements = this.$el_to_style;
400 409 }
401 410 }
402 411 return elements;
403 412 },
404 413 });
405 414
406 415
407 416 //--------------------------------------------------------------------
408 417 // WidgetManager class
409 418 //--------------------------------------------------------------------
410 419 var WidgetManager = function(comm_manager){
411 420 this.comm_manager = comm_manager;
412 421 this.widget_model_types = {};
413 422 this.widget_view_types = {};
414 423
415 424 var that = this;
416 425 Backbone.sync = function(method, model, options, error) {
417 426 var result = model.handle_sync(method, options);
418 427 if (options.success) {
419 428 options.success(result);
420 429 }
421 430 };
422 431 }
423 432
424 433
425 434 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
426 435 // Register the widget with the comm manager. Make sure to pass this object's context
427 436 // in so `this` works in the call back.
428 437 this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this));
429 438 this.widget_model_types[widget_model_name] = widget_model_type;
430 439 }
431 440
432 441
433 442 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
434 443 this.widget_view_types[widget_view_name] = widget_view_type;
435 444 }
436 445
437 446
438 447 WidgetManager.prototype.handle_com_open = function (comm, msg) {
439 448 var widget_type_name = msg.content.target_name;
440 449 var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this.widget_view_types);
441 450 }
442 451
443 452
444 453 //--------------------------------------------------------------------
445 454 // Init code
446 455 //--------------------------------------------------------------------
447 456 IPython.WidgetManager = WidgetManager;
448 457 IPython.WidgetModel = WidgetModel;
449 458 IPython.WidgetView = WidgetView;
450 459
451 460 IPython.notebook.widget_manager = new WidgetManager(IPython.notebook.kernel.comm_manager);
452 461
453 462 };
454 463 });
General Comments 0
You need to be logged in to leave comments. Login now