##// END OF EJS Templates
jshint widget.js
Jonathan Frederic -
Show More
@@ -15,6 +15,7 b''
15 * @submodule widget
15 * @submodule widget
16 */
16 */
17
17
18 (function () {
18 "use strict";
19 "use strict";
19
20
20 // Use require.js 'define' method so that require.js is intelligent enough to
21 // Use require.js 'define' method so that require.js is intelligent enough to
@@ -69,8 +70,8 b' define(["components/underscore/underscore-min",'
69 // will insure that any python code triggered by the sent message
70 // will insure that any python code triggered by the sent message
70 // can create and display widgets and output.
71 // can create and display widgets and output.
71 if (cell === undefined) {
72 if (cell === undefined) {
72 if (this.last_modified_view != undefined &&
73 if (this.last_modified_view !== undefined &&
73 this.last_modified_view.cell != undefined) {
74 this.last_modified_view.cell !== undefined) {
74 cell = this.last_modified_view.cell;
75 cell = this.last_modified_view.cell;
75 }
76 }
76 }
77 }
@@ -104,7 +105,7 b' define(["components/underscore/underscore-min",'
104 this._custom_msg_callbacks.splice(found_index, 1);
105 this._custom_msg_callbacks.splice(found_index, 1);
105 }
106 }
106 } else {
107 } else {
107 this._custom_msg_callbacks.push(callback)
108 this._custom_msg_callbacks.push(callback);
108 }
109 }
109 },
110 },
110
111
@@ -135,9 +136,9 b' define(["components/underscore/underscore-min",'
135
136
136 // Try to get the cell.
137 // Try to get the cell.
137 var cell = this._get_msg_cell(msg.parent_header.msg_id);
138 var cell = this._get_msg_cell(msg.parent_header.msg_id);
138 if (cell == null) {
139 if (cell === null) {
139 console.log("Could not determine where the display" +
140 console.log("Could not determine where the display" +
140 " message was from. Widget will not be displayed")
141 " message was from. Widget will not be displayed");
141 } else {
142 } else {
142 this._display_view(msg.content.data.view_name,
143 this._display_view(msg.content.data.view_name,
143 msg.content.data.parent,
144 msg.content.data.parent,
@@ -198,10 +199,9 b' define(["components/underscore/underscore-min",'
198
199
199 // Send buffer if this message caused another message to be
200 // Send buffer if this message caused another message to be
200 // throttled.
201 // throttled.
201 if (this.msg_buffer != null &&
202 if (this.msg_buffer !== null &&
202 this.msg_throttle == this.pending_msgs) {
203 this.msg_throttle == this.pending_msgs) {
203
204
204 var cell = this._get_msg_cell(msg.parent_header.msg_id);
205 var callbacks = this._make_callbacks(cell);
205 var callbacks = this._make_callbacks(cell);
206 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
206 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
207 this.comm.send(data, callbacks);
207 this.comm.send(data, callbacks);
@@ -219,6 +219,7 b' define(["components/underscore/underscore-min",'
219 // Custom syncronization logic.
219 // Custom syncronization logic.
220 _handle_sync: function (method, options) {
220 _handle_sync: function (method, options) {
221 var model_json = this.toJSON();
221 var model_json = this.toJSON();
222 var attr;
222
223
223 // Only send updated state if the state hasn't been changed
224 // Only send updated state if the state hasn't been changed
224 // during an update.
225 // during an update.
@@ -228,10 +229,10 b' define(["components/underscore/underscore-min",'
228 // it can be sent once the kernel has finished processing
229 // it can be sent once the kernel has finished processing
229 // some of the existing messages.
230 // some of the existing messages.
230 if (method=='patch') {
231 if (method=='patch') {
231 if (this.msg_buffer == null) {
232 if (this.msg_buffer === null) {
232 this.msg_buffer = $.extend({}, model_json); // Copy
233 this.msg_buffer = $.extend({}, model_json); // Copy
233 }
234 }
234 for (var attr in options.attrs) {
235 for (attr in options.attrs) {
235 this.msg_buffer[attr] = options.attrs[attr];
236 this.msg_buffer[attr] = options.attrs[attr];
236 }
237 }
237 } else {
238 } else {
@@ -245,7 +246,7 b' define(["components/underscore/underscore-min",'
245 var send_json = model_json;
246 var send_json = model_json;
246 if (method=='patch') {
247 if (method=='patch') {
247 send_json = {};
248 send_json = {};
248 for (var attr in options.attrs) {
249 for (attr in options.attrs) {
249 send_json[attr] = options.attrs[attr];
250 send_json[attr] = options.attrs[attr];
250 }
251 }
251 }
252 }
@@ -253,7 +254,7 b' define(["components/underscore/underscore-min",'
253 var data = {method: 'backbone', sync_method: method, sync_data: send_json};
254 var data = {method: 'backbone', sync_method: method, sync_data: send_json};
254
255
255 var cell = null;
256 var cell = null;
256 if (this.last_modified_view != undefined && this.last_modified_view != null) {
257 if (this.last_modified_view !== undefined && this.last_modified_view !== null) {
257 cell = this.last_modified_view.cell;
258 cell = this.last_modified_view.cell;
258 }
259 }
259
260
@@ -272,7 +273,7 b' define(["components/underscore/underscore-min",'
272 _handle_view_displayed: function(view) {
273 _handle_view_displayed: function(view) {
273 if (this._view_displayed_callback) {
274 if (this._view_displayed_callback) {
274 try {
275 try {
275 this._view_displayed_callback(view)
276 this._view_displayed_callback(view);
276 } catch (e) {
277 } catch (e) {
277 console.log("Exception in widget model view displayed callback", e, view, this);
278 console.log("Exception in widget model view displayed callback", e, view, this);
278 }
279 }
@@ -302,19 +303,20 b' define(["components/underscore/underscore-min",'
302 // Create view that represents the model.
303 // Create view that represents the model.
303 _display_view: function (view_name, parent_comm_id, cell) {
304 _display_view: function (view_name, parent_comm_id, cell) {
304 var new_views = [];
305 var new_views = [];
306 var view;
305
307
306 // Try creating and adding the view to it's parent.
308 // Try creating and adding the view to it's parent.
307 var displayed = false;
309 var displayed = false;
308 if (parent_comm_id != undefined) {
310 if (parent_comm_id !== undefined) {
309 var parent_comm = this.comm_manager.comms[parent_comm_id];
311 var parent_comm = this.comm_manager.comms[parent_comm_id];
310 var parent_model = parent_comm.model;
312 var parent_model = parent_comm.model;
311 var parent_views = parent_model.views;
313 var parent_views = parent_model.views;
312 for (var parent_view_index in parent_views) {
314 for (var parent_view_index in parent_views) {
313 var parent_view = parent_views[parent_view_index];
315 var parent_view = parent_views[parent_view_index];
314 if (parent_view.cell === cell) {
316 if (parent_view.cell === cell) {
315 if (parent_view.display_child != undefined) {
317 if (parent_view.display_child !== undefined) {
316 var view = this._create_view(view_name, cell);
318 view = this._create_view(view_name, cell);
317 if (view != null) {
319 if (view !== null) {
318 new_views.push(view);
320 new_views.push(view);
319 parent_view.display_child(view);
321 parent_view.display_child(view);
320 displayed = true;
322 displayed = true;
@@ -328,11 +330,11 b' define(["components/underscore/underscore-min",'
328 // If no parent view is defined or exists. Add the view's
330 // If no parent view is defined or exists. Add the view's
329 // element to cell's widget div.
331 // element to cell's widget div.
330 if (!displayed) {
332 if (!displayed) {
331 var view = this._create_view(view_name, cell);
333 view = this._create_view(view_name, cell);
332 if (view != null) {
334 if (view !== null) {
333 new_views.push(view);
335 new_views.push(view);
334
336
335 if (cell.widget_subarea != undefined && cell.widget_subarea != null) {
337 if (cell.widget_subarea !== undefined && cell.widget_subarea !== null) {
336 cell.widget_area.show();
338 cell.widget_area.show();
337 cell.widget_subarea.append(view.$el);
339 cell.widget_subarea.append(view.$el);
338 this._handle_view_displayed(view);
340 this._handle_view_displayed(view);
@@ -342,7 +344,7 b' define(["components/underscore/underscore-min",'
342
344
343 // Force the new view(s) to update their selves
345 // Force the new view(s) to update their selves
344 for (var view_index in new_views) {
346 for (var view_index in new_views) {
345 var view = new_views[view_index];
347 view = new_views[view_index];
346 view.update();
348 view.update();
347 }
349 }
348 },
350 },
@@ -350,9 +352,9 b' define(["components/underscore/underscore-min",'
350
352
351 // Create a view
353 // Create a view
352 _create_view: function (view_name, cell) {
354 _create_view: function (view_name, cell) {
353 var view_type = this.widget_manager.widget_view_types[view_name];
355 var ViewType = this.widget_manager.widget_view_types[view_name];
354 if (view_type != undefined && view_type != null) {
356 if (ViewType !== undefined && ViewType !== null) {
355 var view = new view_type({model: this});
357 var view = new ViewType({model: this});
356 view.render();
358 view.render();
357 this.views.push(view);
359 this.views.push(view);
358 view.cell = cell;
360 view.cell = cell;
@@ -367,10 +369,10 b' define(["components/underscore/underscore-min",'
367 view.remove(); // Clean-up view
369 view.remove(); // Clean-up view
368
370
369 // Close the comm if there are no views left.
371 // Close the comm if there are no views left.
370 if (that.views.length()==0) {
372 if (that.views.length() === 0) {
371 if (that._close_callback) {
373 if (that._close_callback) {
372 try {
374 try {
373 that._close_callback(that)
375 that._close_callback(that);
374 } catch (e) {
376 } catch (e) {
375 console.log("Exception in widget model close callback", e, that);
377 console.log("Exception in widget model close callback", e, that);
376 }
378 }
@@ -388,12 +390,12 b' define(["components/underscore/underscore-min",'
388 // Build a callback dict.
390 // Build a callback dict.
389 _make_callbacks: function (cell) {
391 _make_callbacks: function (cell) {
390 var callbacks = {};
392 var callbacks = {};
391 if (cell != null) {
393 if (cell !== null) {
392
394
393 // Try to get output handlers
395 // Try to get output handlers
394 var handle_output = null;
396 var handle_output = null;
395 var handle_clear_output = null;
397 var handle_clear_output = null;
396 if (cell.output_area != undefined && cell.output_area != null) {
398 if (cell.output_area !== undefined && cell.output_area !== null) {
397 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
399 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
398 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
400 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
399 }
401 }
@@ -428,7 +430,7 b' define(["components/underscore/underscore-min",'
428
430
429 // First, check to see if the msg was triggered by cell execution.
431 // First, check to see if the msg was triggered by cell execution.
430 var cell = this.widget_manager.get_msg_cell(msg_id);
432 var cell = this.widget_manager.get_msg_cell(msg_id);
431 if (cell != null) {
433 if (cell !== null) {
432 return cell;
434 return cell;
433 }
435 }
434
436
@@ -438,9 +440,9 b' define(["components/underscore/underscore-min",'
438 // message was triggered by a widget.
440 // message was triggered by a widget.
439 var kernel = this.comm_manager.kernel;
441 var kernel = this.comm_manager.kernel;
440 var callbacks = kernel.get_callbacks_for_msg(msg_id);
442 var callbacks = kernel.get_callbacks_for_msg(msg_id);
441 if (callbacks != undefined &&
443 if (callbacks !== undefined &&
442 callbacks.iopub != undefined &&
444 callbacks.iopub !== undefined &&
443 callbacks.iopub.get_cell != undefined) {
445 callbacks.iopub.get_cell !== undefined) {
444
446
445 return callbacks.iopub.get_cell();
447 return callbacks.iopub.get_cell();
446 }
448 }
@@ -483,7 +485,7 b' define(["components/underscore/underscore-min",'
483 },
485 },
484
486
485 update: function() {
487 update: function() {
486 if (this.model.get('visible') != undefined) {
488 if (this.model.get('visible') !== undefined) {
487 if (this.visible != this.model.get('visible')) {
489 if (this.visible != this.model.get('visible')) {
488 this.visible = this.model.get('visible');
490 this.visible = this.model.get('visible');
489 if (this.visible) {
491 if (this.visible) {
@@ -494,7 +496,7 b' define(["components/underscore/underscore-min",'
494 }
496 }
495 }
497 }
496
498
497 if (this.model.css != undefined) {
499 if (this.model.css !== undefined) {
498 for (var selector in this.model.css) {
500 for (var selector in this.model.css) {
499 if (this.model.css.hasOwnProperty(selector)) {
501 if (this.model.css.hasOwnProperty(selector)) {
500
502
@@ -519,8 +521,8 b' define(["components/underscore/underscore-min",'
519 // the $el_to_style element is not defined, use apply the
521 // the $el_to_style element is not defined, use apply the
520 // style to the view's element.
522 // style to the view's element.
521 var elements = this.$el.find(selector);
523 var elements = this.$el.find(selector);
522 if (selector===undefined || selector===null || selector=='') {
524 if (selector === undefined || selector === null || selector === '') {
523 if (this.$el_to_style == undefined) {
525 if (this.$el_to_style === undefined) {
524 elements = this.$el;
526 elements = this.$el;
525 } else {
527 } else {
526 elements = this.$el_to_style;
528 elements = this.$el_to_style;
@@ -546,7 +548,7 b' define(["components/underscore/underscore-min",'
546 options.success(result);
548 options.success(result);
547 }
549 }
548 };
550 };
549 }
551 };
550
552
551
553
552 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
554 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
@@ -556,34 +558,34 b' define(["components/underscore/underscore-min",'
556 for (var widget_model_name in this.widget_model_types) {
558 for (var widget_model_name in this.widget_model_types) {
557 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
559 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
558 }
560 }
559 }
561 };
560
562
561
563
562 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
564 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
563 // Register the widget with the comm manager. Make sure to pass this object's context
565 // Register the widget with the comm manager. Make sure to pass this object's context
564 // in so `this` works in the call back.
566 // in so `this` works in the call back.
565 if (this.comm_manager!=null) {
567 if (this.comm_manager !== null) {
566 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
568 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
567 }
569 }
568 this.widget_model_types[widget_model_name] = widget_model_type;
570 this.widget_model_types[widget_model_name] = widget_model_type;
569 }
571 };
570
572
571
573
572 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
574 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
573 this.widget_view_types[widget_view_name] = widget_view_type;
575 this.widget_view_types[widget_view_name] = widget_view_type;
574 }
576 };
575
577
576
578
577 WidgetManager.prototype.get_msg_cell = function (msg_id) {
579 WidgetManager.prototype.get_msg_cell = function (msg_id) {
578 if (IPython.notebook != undefined && IPython.notebook != null) {
580 if (IPython.notebook !== undefined && IPython.notebook !== null) {
579 return IPython.notebook.get_msg_cell(msg_id);
581 return IPython.notebook.get_msg_cell(msg_id);
580 }
582 }
581 }
583 };
582
584
583
585
584 WidgetManager.prototype.on_create_widget = function (callback) {
586 WidgetManager.prototype.on_create_widget = function (callback) {
585 this._create_widget_callback = callback;
587 this._create_widget_callback = callback;
586 }
588 };
587
589
588
590
589 WidgetManager.prototype._handle_create_widget = function (widget_model) {
591 WidgetManager.prototype._handle_create_widget = function (widget_model) {
@@ -594,14 +596,14 b' define(["components/underscore/underscore-min",'
594 console.log("Exception in WidgetManager callback", e, widget_model);
596 console.log("Exception in WidgetManager callback", e, widget_model);
595 }
597 }
596 }
598 }
597 }
599 };
598
600
599
601
600 WidgetManager.prototype._handle_com_open = function (comm, msg) {
602 WidgetManager.prototype._handle_com_open = function (comm, msg) {
601 var widget_type_name = msg.content.target_name;
603 var widget_type_name = msg.content.target_name;
602 var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this);
604 var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this);
603 this._handle_create_widget(widget_model);
605 this._handle_create_widget(widget_model);
604 }
606 };
605
607
606
608
607 //--------------------------------------------------------------------
609 //--------------------------------------------------------------------
@@ -611,9 +613,10 b' define(["components/underscore/underscore-min",'
611 IPython.WidgetModel = WidgetModel;
613 IPython.WidgetModel = WidgetModel;
612 IPython.WidgetView = WidgetView;
614 IPython.WidgetView = WidgetView;
613
615
614 if (IPython.widget_manager==undefined || IPython.widget_manager==null) {
616 if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
615 IPython.widget_manager = new WidgetManager();
617 IPython.widget_manager = new WidgetManager();
616 }
618 }
617
619
618 return IPython.widget_manager;
620 return IPython.widget_manager;
619 });
621 });
622 }()); No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now