##// END OF EJS Templates
Merge pull request #5172 from jdfreder/print-breaks...
Merge pull request #5172 from jdfreder/print-breaks Prevent page breaks when printing notebooks via print-view.

File last commit:

r15044:dd905d92
r15468:b02e1c96 merge
Show More
widgetmanager.js
210 lines | 8.7 KiB | application/javascript | JavascriptLexer
Jonathan Frederic
Added widjet.js...
r14224 //----------------------------------------------------------------------------
// Copyright (C) 2013 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------
//============================================================================
// WidgetModel, WidgetView, and WidgetManager
//============================================================================
/**
* Base Widget classes
* @module IPython
* @namespace IPython
* @submodule widget
*/
Jonathan Frederic
jshint widget.js
r14457 (function () {
"use strict";
// Use require.js 'define' method so that require.js is intelligent enough to
// syncronously load everything within this file when it is being 'required'
// elsewhere.
Jonathan Frederic
Added require.js shims for underscore and backbone...
r14483 define(["underscore",
"backbone",
Jonathan Frederic
s/Underscore/_
r14897 ], function (_, Backbone) {
Jonathan Frederic
Added support for multiple kernels.
r14624
Jonathan Frederic
jshint widget.js
r14457 //--------------------------------------------------------------------
// WidgetManager class
//--------------------------------------------------------------------
Jonathan Frederic
Added support for multiple kernels.
r14624 var WidgetManager = function (comm_manager) {
// Public constructor
WidgetManager._managers.push(this);
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342
Jonathan Frederic
Added support for multiple kernels.
r14624 // Attach a comm manager to the
Jonathan Frederic
jshint widget.js
r14457 this.comm_manager = comm_manager;
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 this._models = {}; /* Dictionary of model ids and model instances */
Jonathan Frederic
jshint widget.js
r14457
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 // Register already-registered widget model types with the comm manager.
Jonathan Frederic
Fixed some typos related to _.each loops
r14678 var that = this;
Jonathan Frederic
Fixed context errors and a couple of typos to get the tests working again
r14686 _.each(WidgetManager._model_types, function(model_type, model_name) {
that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that));
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 });
Jonathan Frederic
jshint widget.js
r14457 };
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342
Jonathan Frederic
Added support for multiple kernels.
r14624 //--------------------------------------------------------------------
// Class level
//--------------------------------------------------------------------
WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */
WidgetManager._view_types = {}; /* Dictionary of view names and view types. */
WidgetManager._managers = []; /* List of widget managers */
WidgetManager.register_widget_model = function (model_name, model_type) {
// Registers a widget model by name.
WidgetManager._model_types[model_name] = model_type;
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342
Jonathan Frederic
jshint widget.js
r14457 // Register the widget with the comm manager. Make sure to pass this object's context
// in so `this` works in the call back.
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 _.each(WidgetManager._managers, function(instance, i) {
Jonathan Frederic
Added support for multiple kernels.
r14624 if (instance.comm_manager !== null) {
instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance));
}
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 });
Jonathan Frederic
jshint widget.js
r14457 };
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342
Jonathan Frederic
Added support for multiple kernels.
r14624 WidgetManager.register_widget_view = function (view_name, view_type) {
// Registers a widget view by name.
WidgetManager._view_types[view_name] = view_type;
Jonathan Frederic
jshint widget.js
r14457 };
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512
Jonathan Frederic
Added support for multiple kernels.
r14624 //--------------------------------------------------------------------
// Instance level
//--------------------------------------------------------------------
Jason Grout
Pass the whole message into the widget manager display_view call...
r14620 WidgetManager.prototype.display_view = function(msg, model) {
Jonathan Frederic
Moved keyboard_manager logic into a sep. function.
r14734 // Displays a view for a particular model.
Jason Grout
Pass the whole message into the widget manager display_view call...
r14620 var cell = this.get_msg_cell(msg.parent_header.msg_id);
Jonathan Frederic
handle_msg a display_model method.
r14559 if (cell === null) {
console.log("Could not determine where the display" +
" message was from. Widget will not be displayed");
} else {
Jason Grout
Fix the cell reference in views...
r14617 var view = this.create_view(model, {cell: cell});
Jonathan Frederic
Explicitly return null if there aren't any results
r14666 if (view === null) {
Jason Grout
Pass the whole message into the widget manager display_view call...
r14620 console.error("View creation failed", model);
}
MinRK
review pass on widgetmanager.js
r14791 if (cell.widget_subarea) {
Jonathan Frederic
handle_msg a display_model method.
r14559 cell.widget_area.show();
Brian E. Granger
Rename _handle_new_view->_handle_display_view.
r14948 this._handle_display_view(view);
Jonathan Frederic
handle_msg a display_model method.
r14559 cell.widget_subarea.append(view.$el);
}
Jonathan Frederic
Converted tabs to spaces
r14506 }
Jonathan Frederic
Moved keyboard_manager logic into a sep. function.
r14734 };
Jonathan Frederic
handle_msg a display_model method.
r14559
Brian E. Granger
Rename _handle_new_view->_handle_display_view.
r14948 WidgetManager.prototype._handle_display_view = function (view) {
// Have the IPython keyboard manager disable its event
// handling so the widget can capture keyboard input.
Jonathan Frederic
Fix command mode & popup view bug...
r15022 // Note, this is only done on the outer most widgets.
Jonathan Frederic
Unconditionally register $el with keyboard manager...
r15044 IPython.keyboard_manager.register_events(view.$el);
if (view.additional_elements) {
for (var i = 0; i < view.additional_elements.length; i++) {
IPython.keyboard_manager.register_events(view.additional_elements[i]);
Jonathan Frederic
Fix command mode & popup view bug...
r15022 }
Jonathan Frederic
Unconditionally register $el with keyboard manager...
r15044 }
Brian E. Granger
Rename _handle_new_view->_handle_display_view.
r14948 };
Jonathan Frederic
Fixed bug where views child to other views would not have cell information
r14689 WidgetManager.prototype.create_view = function(model, options, view) {
Jonathan Frederic
Moved keyboard_manager logic into a sep. function.
r14734 // Creates a view for a particular model.
Jonathan Frederic
s/view_name/_view_name
r14701 var view_name = model.get('_view_name');
Jonathan Frederic
Added support for multiple kernels.
r14624 var ViewType = WidgetManager._view_types[view_name];
MinRK
review pass on widgetmanager.js
r14791 if (ViewType) {
Jonathan Frederic
Fixed bug where views child to other views would not have cell information
r14689
// If a view is passed into the method, use that view's cell as
// the cell for the view that is created.
options = options || {};
if (view !== undefined) {
options.cell = view.options.cell;
}
Jonathan Frederic
Moved keyboard_manager logic into a sep. function.
r14734 // Create and render the view...
Jonathan Frederic
un-nest options.options
r14565 var parameters = {model: model, options: options};
MinRK
review pass on widgetmanager.js
r14791 view = new ViewType(parameters);
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 view.render();
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512 model.views.push(view);
model.on('destroy', view.remove, view);
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 return view;
}
Jonathan Frederic
Explicitly return null if there aren't any results
r14666 return null;
Jonathan Frederic
Moved keyboard_manager logic into a sep. function.
r14734 };
Jonathan Frederic
jshint widget.js
r14457 WidgetManager.prototype.get_msg_cell = function (msg_id) {
Jonathan Frederic
Completely remove cell from model and view.
r14534 var cell = null;
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 // First, check to see if the msg was triggered by cell execution.
MinRK
review pass on widgetmanager.js
r14791 if (IPython.notebook) {
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 cell = IPython.notebook.get_msg_cell(msg_id);
}
Jonathan Frederic
Completely remove cell from model and view.
r14534 if (cell !== null) {
MinRK
review pass on widgetmanager.js
r14791 return cell;
Jonathan Frederic
Completely remove cell from model and view.
r14534 }
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 // Second, check to see if a get_cell callback was defined
// for the message. get_cell callbacks are registered for
// widget messages, so this block is actually checking to see if the
// message was triggered by a widget.
Jonathan Frederic
Added support for multiple kernels.
r14624 var kernel = this.comm_manager.kernel;
MinRK
review pass on widgetmanager.js
r14791 if (kernel) {
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 var callbacks = kernel.get_callbacks_for_msg(msg_id);
MinRK
review pass on widgetmanager.js
r14791 if (callbacks && callbacks.iopub &&
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 callbacks.iopub.get_cell !== undefined) {
return callbacks.iopub.get_cell();
MinRK
review pass on widgetmanager.js
r14791 }
Jonathan Frederic
jshint widget.js
r14457 }
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486
// Not triggered by a cell or widget (no get_cell callback
// exists).
return null;
Jonathan Frederic
jshint widget.js
r14457 };
Jonathan Frederic
Removed comm dependency of widget model and view
r14469
Jonathan Frederic
Completely remove cell from model and view.
r14534 WidgetManager.prototype.callbacks = function (view) {
// callback handlers specific a view
var callbacks = {};
MinRK
review pass on widgetmanager.js
r14791 if (view && view.options.cell) {
Jonathan Frederic
Fixed bug in throttling code.
r14741
Jonathan Frederic
Completely remove cell from model and view.
r14534 // Try to get output handlers
Jonathan Frederic
Fixed bug in throttling code.
r14741 var cell = view.options.cell;
Jonathan Frederic
Completely remove cell from model and view.
r14534 var handle_output = null;
var handle_clear_output = null;
MinRK
review pass on widgetmanager.js
r14791 if (cell.output_area) {
Jonathan Frederic
Completely remove cell from model and view.
r14534 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
}
// Create callback dict using what is known
var that = this;
callbacks = {
iopub : {
output : handle_output,
clear_output : handle_clear_output,
// Special function only registered by widget messages.
// Allows us to get the cell for a message so we know
// where to add widgets if the code requires it.
get_cell : function () {
return cell;
},
},
};
}
return callbacks;
};
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512 WidgetManager.prototype.get_model = function (model_id) {
Jonathan Frederic
Added some small comments to widget code
r14668 // Look-up a model instance by its id.
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 var model = this._models[model_id];
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512 if (model !== undefined && model.id == model_id) {
Jonathan Frederic
Removed comm dependency of widget model and view
r14469 return model;
}
return null;
};
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
Jonathan Frederic
Added some small comments to widget code
r14668 // Handle when a comm is opened.
Jonathan Frederic
Added support for multiple kernels.
r14624 var model_id = comm.comm_id;
Jonathan Frederic
jshint widget.js
r14457 var widget_type_name = msg.content.target_name;
Jonathan Frederic
Added support for multiple kernels.
r14624 var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 this._models[model_id] = widget_model;
Jonathan Frederic
jshint widget.js
r14457 };
IPython.WidgetManager = WidgetManager;
Jonathan Frederic
Added support for multiple kernels.
r14624 return IPython.WidgetManager;
Jonathan Frederic
jshint widget.js
r14457 });
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 }());