##// END OF EJS Templates
Semicolons
Semicolons

File last commit:

r18517:3dffe4a8
r18834:61a8c008
Show More
manager.js
280 lines | 10.7 KiB | application/javascript | JavascriptLexer
Jonathan Frederic
Almost done!...
r17198 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
"underscore",
Jonathan Frederic
Done with major changes,...
r17199 "backbone",
Tarun Gaba
Added jquery in define
r17273 "jquery",
Jonathan Frederic
Fix all the tests
r17216 "base/js/namespace"
Tarun Gaba
Added jquery in define
r17273 ], function (_, Backbone, $, IPython) {
Thomas Kluyver
Clean up some JS code
r18146 "use strict";
Jonathan Frederic
Done with major changes,...
r17199 //--------------------------------------------------------------------
// WidgetManager class
//--------------------------------------------------------------------
Jonathan Frederic
MWE,...
r17200 var WidgetManager = function (comm_manager, notebook) {
Jonathan Frederic
Done with major changes,...
r17199 // Public constructor
WidgetManager._managers.push(this);
// Attach a comm manager to the
Jonathan Frederic
MWE,...
r17200 this.keyboard_manager = notebook.keyboard_manager;
Jonathan Frederic
Done with major changes,...
r17199 this.notebook = notebook;
this.comm_manager = comm_manager;
this._models = {}; /* Dictionary of model ids and model instances */
Jonathan Frederic
Separate widget model name from com target name.
r18264 // Register with the comm manager.
this.comm_manager.register_target('ipython.widget', $.proxy(this._handle_comm_open, this));
Jonathan Frederic
Done with major changes,...
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 {
Thomas Kluyver
Allow widget views to be loaded from require modules...
r18142 var that = this;
Jonathan Frederic
Rebase fixes
r18517 this.create_view(model, {cell: cell, success: function(view) {
Thomas Kluyver
Allow widget views to be loaded from require modules...
r18142 that._handle_display_view(view);
if (cell.widget_subarea) {
cell.widget_subarea.append(view.$el);
}
view.trigger('displayed');
}});
Jonathan Frederic
Done with major changes,...
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);
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
Completely remove cell from model and view.
r14534 }
Jonathan Frederic
Done with major changes,...
r17199 }
}
};
Thomas Kluyver
Allow widget views to be loaded from require modules...
r18142
Jonathan Frederic
Done with major changes,...
r17199
Thomas Kluyver
Clean up some JS code
r18146 WidgetManager.prototype.create_view = function(model, options) {
Jonathan Frederic
Done with major changes,...
r17199 // Creates a view for a particular model.
Thomas Kluyver
Put callbacks for create_view and create_child_view in options...
r18145
Jonathan Frederic
Done with major changes,...
r17199 var view_name = model.get('_view_name');
Thomas Kluyver
Put callbacks for create_view and create_child_view in options...
r18145 var view_mod = model.get('_view_module');
Jonathan Frederic
Rebase fixes
r18517 var error = options.error || function(error) { console.log(error); };
Thomas Kluyver
Put callbacks for create_view and create_child_view in options...
r18145
Thomas Kluyver
Allow widget views to be loaded from require modules...
r18142 var instantiate_view = function(ViewType) {
if (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 || {};
Thomas Kluyver
Clean up some JS code
r18146 if (options.parent !== undefined) {
options.cell = options.parent.options.cell;
Thomas Kluyver
Allow widget views to be loaded from require modules...
r18142 }
Jonathan Frederic
Done with major changes,...
r17199
Thomas Kluyver
Allow widget views to be loaded from require modules...
r18142 // Create and render the view...
var parameters = {model: model, options: options};
Thomas Kluyver
Clean up some JS code
r18146 var view = new ViewType(parameters);
Thomas Kluyver
Allow widget views to be loaded from require modules...
r18142 view.render();
model.on('destroy', view.remove, view);
Jonathan Frederic
Rebase fixes
r18517 if (options.success) {
options.success(view);
}
Thomas Kluyver
Put callbacks for create_view and create_child_view in options...
r18145 } else {
Jonathan Frederic
Rebase fixes
r18517 error({unknown_view: true, view_name: view_name,
Thomas Kluyver
Clean up some JS code
r18146 view_module: view_mod});
Jonathan Frederic
jshint widget.js
r14457 }
Thomas Kluyver
Clean up some JS code
r18146 };
Jonathan Frederic
Done with major changes,...
r17199
Thomas Kluyver
Fix logic
r18144 if (view_mod) {
Thomas Kluyver
Allow widget views to be loaded from require modules...
r18142 require([view_mod], function(module) {
Thomas Kluyver
Clean up some JS code
r18146 instantiate_view(module[view_name]);
Jonathan Frederic
Rebase fixes
r18517 }, error);
Thomas Kluyver
Allow widget views to be loaded from require modules...
r18142 } else {
instantiate_view(WidgetManager._view_types[view_name]);
Jonathan Frederic
Done with major changes,...
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
Completely remove cell from model and view.
r14534 }
Jonathan Frederic
Done with major changes,...
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
Removed comm dependency of widget model and view
r14469 }
Jonathan Frederic
Rebase fixes
r18517 // Create callback dictionary using what is known
Brian E. Granger
Remove model from WidgetManager._model on comm:close.
r16652 var that = this;
Jonathan Frederic
Done with major changes,...
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
Rebase fixes
r18517 this.create_model({
model_name: msg.content.data.model_name,
model_module: msg.content.data.model_module,
comm: comm});
Jonathan Frederic
Enable widget instanciation from front-end.
r18506 };
Jonathan Frederic
Clarified API for the create_model function,...
r18512 WidgetManager.prototype.create_model = function (options) {
Jonathan Frederic
Enable widget instanciation from front-end.
r18506 // Create and return a new widget model.
//
Jonathan Frederic
s/target_name/widget_class
r18515 // Minimally, one must provide the model_name and widget_class
Jonathan Frederic
Clarified API for the create_model function,...
r18512 // parameters to create a model from Javascript.
//
// Example
// --------
// JS:
Jonathan Frederic
Rebase fixes
r18517 // IPython.notebook.kernel.widget_manager.create_model({
Jonathan Frederic
Clarified API for the create_model function,...
r18512 // model_name: 'WidgetModel',
Jonathan Frederic
s/target_name/widget_class
r18515 // widget_class: 'IPython.html.widgets.widget_int.IntSlider',
Jonathan Frederic
Clarified API for the create_model function,...
r18512 // init_state_callback: function(model) { console.log('Create success!', model); }});
Jonathan Frederic
Enable widget instanciation from front-end.
r18506 //
// Parameters
// ----------
// options: dictionary
// Dictionary of options with the following contents:
// model_name: string
// Target name of the widget model to create.
Jonathan Frederic
Rebase fixes
r18517 // model_module: (optional) string
// Module name of the widget model to create.
Jonathan Frederic
s/target_name/widget_class
r18515 // widget_class: (optional) string
Jonathan Frederic
Enable widget instanciation from front-end.
r18506 // Target name of the widget in the back-end.
// comm: (optional) Comm
Jonathan Frederic
Rebase fixes
r18517 // success: (optional) callback
// Callback for when the model was created successfully.
// error: (optional) callback
// Callback for when the model wasn't created.
Jonathan Frederic
Make Python push initial state....
r18507 // init_state_callback: (optional) callback
// Called when the first state push from the back-end is
Jonathan Frederic
Clarified API for the create_model function,...
r18512 // recieved. Allows you to modify the model after it's
// complete state is filled and synced.
Jonathan Frederic
Enable widget instanciation from front-end.
r18506
Jonathan Frederic
Rebase fixes
r18517 // Make default callbacks if not specified.
var error = options.error || function(error) { console.log(error); };
Jonathan Frederic
Enable widget instanciation from front-end.
r18506 // Create a comm if it wasn't provided.
var comm = options.comm;
if (!comm) {
Jonathan Frederic
s/target_name/widget_class
r18515 comm = this.comm_manager.new_comm('ipython.widget', {'widget_class': options.widget_class});
Jonathan Frederic
Enable widget instanciation from front-end.
r18506 }
Jonathan Frederic
Rebase fixes
r18517 // Create a new model that is connected to the comm.
Jonathan Frederic
Done with major changes,...
r17199 var that = this;
Thomas Kluyver
Support specifying requirejs modules for widget models
r18466 var instantiate_model = function(ModelType) {
var model_id = comm.comm_id;
Jonathan Frederic
Make Python push initial state....
r18507 var widget_model = new ModelType(that, model_id, comm, options.init_state_callback);
widget_model.on('comm:close', function () {
Thomas Kluyver
Support specifying requirejs modules for widget models
r18466 delete that._models[model_id];
});
that._models[model_id] = widget_model;
Jonathan Frederic
Rebase fixes
r18517 if (options.success) {
options.success(widget_model);
}
Thomas Kluyver
Support specifying requirejs modules for widget models
r18466 };
Jonathan Frederic
Rebase fixes
r18517 // Get the model type using require or through the registry.
var widget_type_name = options.model_name;
var widget_module = options.model_module;
Thomas Kluyver
Support specifying requirejs modules for widget models
r18466 if (widget_module) {
Jonathan Frederic
Rebase fixes
r18517
Thomas Kluyver
Support specifying requirejs modules for widget models
r18466 // Load the module containing the widget model
require([widget_module], function(mod) {
if (mod[widget_type_name]) {
instantiate_model(mod[widget_type_name]);
} else {
Jonathan Frederic
Rebase fixes
r18517 error("Error creating widget model: " + widget_type_name
Thomas Kluyver
Support specifying requirejs modules for widget models
r18466 + " not found in " + widget_module);
}
Jonathan Frederic
Rebase fixes
r18517 }, error);
Thomas Kluyver
Support specifying requirejs modules for widget models
r18466 } else {
Jonathan Frederic
Rebase fixes
r18517
Thomas Kluyver
Support specifying requirejs modules for widget models
r18466 // No module specified, load from the global models registry
instantiate_model(WidgetManager._model_types[widget_type_name]);
}
Jonathan Frederic
Done with major changes,...
r17199 };
Jonathan Frederic
Almost done!...
r17198
Jonathan Frederic
Fix all the tests
r17216 // Backwards compatability.
IPython.WidgetManager = WidgetManager;
Jonathan Frederic
Fix all the bugs!
r17203 return {'WidgetManager': WidgetManager};
Jonathan Frederic
Done with major changes,...
r17199 });