manager.js
246 lines
| 9.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
|
r17216 | "base/js/namespace" | ||
Jonathan Frederic
|
r18882 | ], function (_, Backbone, $, utils, IPython) { | ||
Thomas Kluyver
|
r18146 | "use strict"; | ||
Jonathan Frederic
|
r17199 | //-------------------------------------------------------------------- | ||
// WidgetManager class | ||||
//-------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r17200 | var WidgetManager = function (comm_manager, notebook) { | ||
Jonathan Frederic
|
r17199 | // Public constructor | ||
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; | ||||
this._models = {}; /* Dictionary of model ids and model instances */ | ||||
Jonathan Frederic
|
r18264 | // Register with the comm manager. | ||
this.comm_manager.register_target('ipython.widget', $.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) { | ||||
// Displays a view for a particular model. | ||||
var cell = this.get_msg_cell(msg.parent_header.msg_id); | ||||
if (cell === null) { | ||||
console.log("Could not determine where the display" + | ||||
" message was from. Widget will not be displayed"); | ||||
} else { | ||||
Jonathan Frederic
|
r18882 | var dummy = null; | ||
if (cell.widget_subarea) { | ||||
dummy = $('<div />'); | ||||
cell.widget_subarea.append(dummy); | ||||
} | ||||
Thomas Kluyver
|
r18142 | var that = this; | ||
Jonathan Frederic
|
r18882 | this.create_view(model, {cell: cell}).then(function(view) { | ||
Thomas Kluyver
|
r18142 | that._handle_display_view(view); | ||
Jonathan Frederic
|
r18882 | if (dummy) { | ||
dummy.replaceWith(view.$el); | ||||
Thomas Kluyver
|
r18142 | } | ||
view.trigger('displayed'); | ||||
Jonathan Frederic
|
r18882 | }, function(error) { console.error(error); }); | ||
Jonathan Frederic
|
r17199 | } | ||
}; | ||||
WidgetManager.prototype._handle_display_view = function (view) { | ||||
// 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. | ||||
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++) { | ||||
this.keyboard_manager.register_events(view.additional_elements[i]); | ||||
} | ||||
} | ||||
Jonathan Frederic
|
r17199 | } | ||
}; | ||||
Thomas Kluyver
|
r18142 | |||
Jonathan Frederic
|
r17199 | |||
Thomas Kluyver
|
r18146 | WidgetManager.prototype.create_view = function(model, options) { | ||
Jonathan Frederic
|
r17199 | // Creates a view for a particular model. | ||
Jason Grout
|
r18881 | return new Promise(function(resolve, reject) { | ||
Jonathan Frederic
|
r18882 | var view_name = model.get('_view_name'); | ||
var view_module = model.get('_view_module'); | ||||
utils.try_load(view_name, view_module, WidgetManager._view_types).then(function(ViewType){ | ||||
Thomas Kluyver
|
r18145 | |||
Thomas Kluyver
|
r18142 | // If a view is passed into the method, use that view's cell as | ||
// the cell for the view that is created. | ||||
Jonathan Frederic
|
r18882 | options = options || {}; | ||
Thomas Kluyver
|
r18146 | if (options.parent !== undefined) { | ||
options.cell = options.parent.options.cell; | ||||
Thomas Kluyver
|
r18142 | } | ||
Jonathan Frederic
|
r17199 | |||
Thomas Kluyver
|
r18142 | // Create and render the view... | ||
var parameters = {model: model, options: options}; | ||||
Thomas Kluyver
|
r18146 | var view = new ViewType(parameters); | ||
Thomas Kluyver
|
r18142 | view.render(); | ||
Jonathan Frederic
|
r18882 | model.on('destroy', view.remove, view); | ||
Jason Grout
|
r18881 | resolve(view); | ||
Jonathan Frederic
|
r18882 | }, reject); | ||
}); | ||||
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) { | ||||
// callback handlers specific a view | ||||
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) { | ||||
// Look-up a model instance by its id. | ||||
var model = this._models[model_id]; | ||||
if (model !== undefined && model.id == model_id) { | ||||
return model; | ||||
} | ||||
return null; | ||||
}; | ||||
WidgetManager.prototype._handle_comm_open = function (comm, msg) { | ||||
// Handle when a comm is opened. | ||||
Jonathan Frederic
|
r18517 | this.create_model({ | ||
model_name: msg.content.data.model_name, | ||||
model_module: msg.content.data.model_module, | ||||
comm: comm}); | ||||
Jonathan Frederic
|
r18506 | }; | ||
Jonathan Frederic
|
r18512 | WidgetManager.prototype.create_model = function (options) { | ||
Jason Grout
|
r18881 | // Create and return a promise to create a new widget model. | ||
Jonathan Frederic
|
r18506 | // | ||
Jonathan Frederic
|
r18515 | // Minimally, one must provide the model_name and widget_class | ||
Jonathan Frederic
|
r18512 | // parameters to create a model from Javascript. | ||
// | ||||
// Example | ||||
// -------- | ||||
// JS: | ||||
Jonathan Frederic
|
r18517 | // IPython.notebook.kernel.widget_manager.create_model({ | ||
Jonathan Frederic
|
r18512 | // model_name: 'WidgetModel', | ||
Jonathan Frederic
|
r18882 | // widget_class: 'IPython.html.widgets.widget_int.IntSlider'}) | ||
// .then(function(model) { console.log('Create success!', model); }, | ||||
// function(error) { console.error(error); }); | ||||
Jonathan Frederic
|
r18506 | // | ||
// Parameters | ||||
// ---------- | ||||
// options: dictionary | ||||
// Dictionary of options with the following contents: | ||||
// model_name: string | ||||
// Target name of the widget model to create. | ||||
Jonathan Frederic
|
r18517 | // model_module: (optional) string | ||
// Module name of the widget model to create. | ||||
Jonathan Frederic
|
r18515 | // widget_class: (optional) string | ||
Jonathan Frederic
|
r18506 | // Target name of the widget in the back-end. | ||
// comm: (optional) Comm | ||||
Jason Grout
|
r18881 | return new Promise(function(resolve, reject) { | ||
Jonathan Frederic
|
r18882 | |||
Jason Grout
|
r18881 | // Get the model type using require or through the registry. | ||
var widget_type_name = options.model_name; | ||||
var widget_module = options.model_module; | ||||
Jonathan Frederic
|
r18882 | var that = this; | ||
utils.try_load(widget_type_name, widget_module, WidgetManager._model_types) | ||||
.then(function(ModelType) { | ||||
// Create a comm if it wasn't provided. | ||||
var comm = options.comm; | ||||
if (!comm) { | ||||
comm = that.comm_manager.new_comm('ipython.widget', {'widget_class': options.widget_class}); | ||||
} | ||||
var model_id = comm.comm_id; | ||||
var widget_model = new ModelType(that, model_id, comm); | ||||
widget_model.on('comm:close', function () { | ||||
delete that._models[model_id]; | ||||
}); | ||||
that._models[model_id] = widget_model; | ||||
reolve(widget_model); | ||||
Jason Grout
|
r18881 | }, reject); | ||
Jonathan Frederic
|
r18882 | }); | ||
Jonathan Frederic
|
r17199 | }; | ||
Jonathan Frederic
|
r17198 | |||
Jonathan Frederic
|
r18882 | // Backwards compatibility. | ||
Jonathan Frederic
|
r17216 | IPython.WidgetManager = WidgetManager; | ||
Jonathan Frederic
|
r17203 | return {'WidgetManager': WidgetManager}; | ||
Jonathan Frederic
|
r17199 | }); | ||