manager.js
367 lines
| 14.5 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
"underscore", | ||||
Jonathan Frederic
|
r17199 | "backbone", | ||
Tarun Gaba
|
r17273 | "jquery", | ||
Jonathan Frederic
|
r18882 | "base/js/utils", | ||
Jonathan Frederic
|
r18897 | "base/js/namespace", | ||
Jonathan Frederic
|
r19350 | "services/kernels/comm" | ||
], function (_, Backbone, $, utils, IPython, comm) { | ||||
Thomas Kluyver
|
r18146 | "use strict"; | ||
Jonathan Frederic
|
r17199 | //-------------------------------------------------------------------- | ||
// WidgetManager class | ||||
//-------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r17200 | var WidgetManager = function (comm_manager, notebook) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Public constructor | ||||
*/ | ||||
Jonathan Frederic
|
r17199 | WidgetManager._managers.push(this); | ||
// Attach a comm manager to the | ||||
Jonathan Frederic
|
r17200 | this.keyboard_manager = notebook.keyboard_manager; | ||
Jonathan Frederic
|
r17199 | this.notebook = notebook; | ||
this.comm_manager = comm_manager; | ||||
Jonathan Frederic
|
r19350 | this.comm_target_name = 'ipython.widget'; | ||
Jonathan Frederic
|
r17199 | this._models = {}; /* Dictionary of model ids and model instances */ | ||
Jonathan Frederic
|
r18264 | // Register with the comm manager. | ||
Jonathan Frederic
|
r19350 | this.comm_manager.register_target(this.comm_target_name, $.proxy(this._handle_comm_open, this)); | ||
Jonathan Frederic
|
r17199 | }; | ||
//-------------------------------------------------------------------- | ||||
// 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; | ||||
}; | ||||
WidgetManager.register_widget_view = function (view_name, view_type) { | ||||
// Registers a widget view by name. | ||||
WidgetManager._view_types[view_name] = view_type; | ||||
}; | ||||
//-------------------------------------------------------------------- | ||||
// Instance level | ||||
//-------------------------------------------------------------------- | ||||
WidgetManager.prototype.display_view = function(msg, model) { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* Displays a view for a particular model. | ||||
*/ | ||||
Jonathan Frederic
|
r18898 | var that = this; | ||
Jonathan Frederic
|
r19350 | return new Promise(function(resolve, reject) { | ||
var cell = that.get_msg_cell(msg.parent_header.msg_id); | ||||
if (cell === null) { | ||||
reject(new Error("Could not determine where the display" + | ||||
" message was from. Widget will not be displayed")); | ||||
} else { | ||||
return that.display_view_in_cell(cell, model); | ||||
} | ||||
}); | ||||
}; | ||||
WidgetManager.prototype.display_view_in_cell = function(cell, model) { | ||||
// Displays a view in a cell. | ||||
return new Promise(function(resolve, reject) { | ||||
if (cell.display_widget_view) { | ||||
cell.display_widget_view(that.create_view(model, {cell: cell})) | ||||
.then(function(view) { | ||||
Jonathan Frederic
|
r18895 | that._handle_display_view(view); | ||
view.trigger('displayed'); | ||||
Jonathan Frederic
|
r19350 | resolve(view); | ||
}, function(error) { | ||||
reject(new utils.WrappedError('Could not display view', error)); | ||||
}); | ||||
} else { | ||||
reject(new Error('Cell does not have a `display_widget_view` method.')); | ||||
} | ||||
}); | ||||
Jonathan Frederic
|
r17199 | }; | ||
WidgetManager.prototype._handle_display_view = function (view) { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* Have the IPython keyboard manager disable its event | ||||
* handling so the widget can capture keyboard input. | ||||
* Note, this is only done on the outer most widgets. | ||||
*/ | ||||
Jonathan Frederic
|
r17199 | if (this.keyboard_manager) { | ||
this.keyboard_manager.register_events(view.$el); | ||||
Jonathan Frederic
|
r18882 | if (view.additional_elements) { | ||
for (var i = 0; i < view.additional_elements.length; i++) { | ||||
Jonathan Frederic
|
r18895 | this.keyboard_manager.register_events(view.additional_elements[i]); | ||
Jonathan Frederic
|
r18882 | } | ||
} | ||||
Jonathan Frederic
|
r17199 | } | ||
}; | ||||
Thomas Kluyver
|
r18142 | |||
Thomas Kluyver
|
r18146 | WidgetManager.prototype.create_view = function(model, options) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Creates a promise for a view of a given model | ||||
* | ||||
* Make sure the view creation is not out of order with | ||||
* any state updates. | ||||
*/ | ||||
Jonathan Frederic
|
r18905 | model.state_change = model.state_change.then(function() { | ||
Jonathan Frederic
|
r18906 | |||
Jonathan Frederic
|
r18905 | return utils.load_class(model.get('_view_name'), model.get('_view_module'), | ||
Jonathan Frederic
|
r18894 | WidgetManager._view_types).then(function(ViewType) { | ||
// 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 (options.parent !== undefined) { | ||||
options.cell = options.parent.options.cell; | ||||
} | ||||
// Create and render the view... | ||||
var parameters = {model: model, options: options}; | ||||
var view = new ViewType(parameters); | ||||
view.listenTo(model, 'destroy', view.remove); | ||||
Jason Grout
|
r19158 | return Promise.resolve(view.render()).then(function() {return view;}); | ||
Jason Grout
|
r19080 | }).catch(utils.reject("Couldn't create a view for model id '" + String(model.id) + "'", true)); | ||
Jonathan Frederic
|
r18905 | }); | ||
Jason Grout
|
r19170 | model.views[utils.uuid()] = model.state_change; | ||
Jonathan Frederic
|
r18905 | return model.state_change; | ||
Jonathan Frederic
|
r17199 | }; | ||
WidgetManager.prototype.get_msg_cell = function (msg_id) { | ||||
var cell = null; | ||||
// First, check to see if the msg was triggered by cell execution. | ||||
if (this.notebook) { | ||||
cell = this.notebook.get_msg_cell(msg_id); | ||||
} | ||||
if (cell !== null) { | ||||
return cell; | ||||
} | ||||
// 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. | ||||
var kernel = this.comm_manager.kernel; | ||||
if (kernel) { | ||||
var callbacks = kernel.get_callbacks_for_msg(msg_id); | ||||
if (callbacks && callbacks.iopub && | ||||
callbacks.iopub.get_cell !== undefined) { | ||||
return callbacks.iopub.get_cell(); | ||||
Jonathan Frederic
|
r14534 | } | ||
Jonathan Frederic
|
r17199 | } | ||
// Not triggered by a cell or widget (no get_cell callback | ||||
// exists). | ||||
return null; | ||||
}; | ||||
WidgetManager.prototype.callbacks = function (view) { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* callback handlers specific a view | ||||
*/ | ||||
Jonathan Frederic
|
r17199 | var callbacks = {}; | ||
if (view && view.options.cell) { | ||||
// Try to get output handlers | ||||
var cell = view.options.cell; | ||||
var handle_output = null; | ||||
var handle_clear_output = null; | ||||
if (cell.output_area) { | ||||
handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); | ||||
handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); | ||||
Jonathan Frederic
|
r14469 | } | ||
Jonathan Frederic
|
r18517 | // Create callback dictionary using what is known | ||
Brian E. Granger
|
r16652 | var that = this; | ||
Jonathan Frederic
|
r17199 | 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; | ||||
}; | ||||
WidgetManager.prototype.get_model = function (model_id) { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* Get a promise for a model by model id. | ||||
*/ | ||||
Jonathan Frederic
|
r18898 | return this._models[model_id]; | ||
Jonathan Frederic
|
r17199 | }; | ||
WidgetManager.prototype._handle_comm_open = function (comm, msg) { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* Handle when a comm is opened. | ||||
*/ | ||||
Jonathan Frederic
|
r18916 | return this.create_model({ | ||
Jonathan Frederic
|
r18517 | model_name: msg.content.data.model_name, | ||
model_module: msg.content.data.model_module, | ||||
Jason Grout
|
r19080 | comm: comm}).catch(utils.reject("Couldn't create a model.", true)); | ||
Jonathan Frederic
|
r18506 | }; | ||
Jonathan Frederic
|
r18512 | WidgetManager.prototype.create_model = function (options) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Create and return a promise for a new widget model | ||||
* | ||||
* Minimally, one must provide the model_name and widget_class | ||||
* parameters to create a model from Javascript. | ||||
* | ||||
* Example | ||||
* -------- | ||||
* JS: | ||||
* IPython.notebook.kernel.widget_manager.create_model({ | ||||
* model_name: 'WidgetModel', | ||||
* widget_class: 'IPython.html.widgets.widget_int.IntSlider'}) | ||||
* .then(function(model) { console.log('Create success!', model); }, | ||||
* $.proxy(console.error, console)); | ||||
* | ||||
* Parameters | ||||
* ---------- | ||||
* options: dictionary | ||||
* Dictionary of options with the following contents: | ||||
* model_name: string | ||||
* Target name of the widget model to create. | ||||
* model_module: (optional) string | ||||
* Module name of the widget model to create. | ||||
* widget_class: (optional) string | ||||
* Target name of the widget in the back-end. | ||||
* comm: (optional) Comm | ||||
* | ||||
* Create a comm if it wasn't provided. | ||||
*/ | ||||
Jonathan Frederic
|
r18884 | var comm = options.comm; | ||
if (!comm) { | ||||
comm = this.comm_manager.new_comm('ipython.widget', {'widget_class': options.widget_class}); | ||||
} | ||||
var that = this; | ||||
var model_id = comm.comm_id; | ||||
Jonathan Frederic
|
r18895 | var model_promise = utils.load_class(options.model_name, options.model_module, WidgetManager._model_types) | ||
Jason Grout
|
r18887 | .then(function(ModelType) { | ||
var widget_model = new ModelType(that, model_id, comm); | ||||
widget_model.once('comm:close', function () { | ||||
delete that._models[model_id]; | ||||
}); | ||||
Jonathan Frederic
|
r19350 | widget_model.name = options.model_name; | ||
widget_model.module = options.model_module; | ||||
Jason Grout
|
r18887 | return widget_model; | ||
Jonathan Frederic
|
r18895 | |||
Jason Grout
|
r18889 | }, function(error) { | ||
delete that._models[model_id]; | ||||
Jonathan Frederic
|
r18895 | var wrapped_error = new utils.WrappedError("Couldn't create model", error); | ||
Jonathan Frederic
|
r18907 | return Promise.reject(wrapped_error); | ||
Jason Grout
|
r18887 | }); | ||
this._models[model_id] = model_promise; | ||||
return model_promise; | ||||
Jonathan Frederic
|
r17199 | }; | ||
Jonathan Frederic
|
r17198 | |||
Jonathan Frederic
|
r19350 | WidgetManager.prototype.get_state = function(options) { | ||
// Get the state of the widget manager. | ||||
// | ||||
// This includes all of the widget models and the cells that they are | ||||
// displayed in. | ||||
// | ||||
// Parameters | ||||
// ---------- | ||||
// options: dictionary | ||||
// Dictionary of options with the following contents: | ||||
// only_displayed: (optional) boolean=false | ||||
// Only return models with one or more displayed views. | ||||
// not_alive: (optional) boolean=false | ||||
// Include models that have comms with severed connections. | ||||
return utils.resolve_promise_dict(function(models) { | ||||
var state = {}; | ||||
for (var model_id in models) { | ||||
if (models.hasOwnProperty(model_id)) { | ||||
var model = models[model_id]; | ||||
// If the model has one or more views defined for it, | ||||
// consider it displayed. | ||||
var displayed_flag = !(options && options.only_displayed) || Object.keys(model.views).length > 0; | ||||
var alive_flag = (options && options.not_alive) || model.comm_alive; | ||||
if (displayed_flag && alive_flag) { | ||||
state[model.model_id] = { | ||||
model_name: model.name, | ||||
model_module: model.module, | ||||
views: [], | ||||
}; | ||||
// Get the views that are displayed *now*. | ||||
for (var id in model.views) { | ||||
if (model.views.hasOwnProperty(id)) { | ||||
var view = model.views[id]; | ||||
var cell_index = this.notebook.find_cell_index(view.options.cell); | ||||
state[model.model_id].views.push(cell_index); | ||||
} | ||||
} | ||||
} | ||||
} | ||||
} | ||||
return state; | ||||
}); | ||||
}; | ||||
WidgetManager.prototype.set_state = function(state) { | ||||
// Set the notebook's state. | ||||
// | ||||
// Reconstructs all of the widget models and attempts to redisplay the | ||||
// widgets in the appropriate cells by cell index. | ||||
// Get the kernel when it's available. | ||||
var that = this; | ||||
return (new Promise(function(resolve, reject) { | ||||
if (that.kernel) { | ||||
resolve(that.kernel); | ||||
} else { | ||||
that.events.on('kernel_created.Session', function(event, data) { | ||||
resolve(data.kernel); | ||||
}); | ||||
} | ||||
})).then(function(kernel) { | ||||
// Recreate all the widget models for the given state. | ||||
that.widget_models = []; | ||||
for (var i = 0; i < state.length; i++) { | ||||
// Recreate a comm using the widget's model id (model_id == comm_id). | ||||
var new_comm = new comm.Comm(kernel.widget_manager.comm_target_name, state[i].model_id); | ||||
kernel.comm_manager.register_comm(new_comm); | ||||
// Create the model using the recreated comm. When the model is | ||||
// created we don't know yet if the comm is valid so set_comm_alive | ||||
// false. Once we receive the first state push from the back-end | ||||
// we know the comm is alive. | ||||
var model = kernel.widget_manager.create_model({ | ||||
comm: new_comm, | ||||
model_name: state[i].model_name, | ||||
model_module: state[i].model_module}).then(function(model) { | ||||
model.set_comm_alive(false); | ||||
model.request_state(); | ||||
model.received_state.then(function() { | ||||
model.set_comm_alive(true); | ||||
}); | ||||
return model; | ||||
}); | ||||
that.widget_models.push(model); | ||||
} | ||||
return Promise.all(that.widget_models); | ||||
}); | ||||
}; | ||||
Jonathan Frederic
|
r18882 | // Backwards compatibility. | ||
Jonathan Frederic
|
r17216 | IPython.WidgetManager = WidgetManager; | ||
Jonathan Frederic
|
r17203 | return {'WidgetManager': WidgetManager}; | ||
Jonathan Frederic
|
r17199 | }); | ||