mirror of
https://github.com/jupyter/notebook.git
synced 2025-04-24 14:20:54 +08:00
Done with major changes,
fixed widget IPython. references
This commit is contained in:
parent
516958ac07
commit
0da779d101
@ -3,194 +3,195 @@
|
||||
|
||||
define([
|
||||
"underscore",
|
||||
"backbone",
|
||||
], function (_, Backbone) {
|
||||
"backbone",
|
||||
], function (_, Backbone) {
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// WidgetManager class
|
||||
//--------------------------------------------------------------------
|
||||
var WidgetManager = function (comm_manager) {
|
||||
// Public constructor
|
||||
WidgetManager._managers.push(this);
|
||||
//--------------------------------------------------------------------
|
||||
// WidgetManager class
|
||||
//--------------------------------------------------------------------
|
||||
var WidgetManager = function (comm_manager, keyboard_manager, notebook) {
|
||||
// Public constructor
|
||||
WidgetManager._managers.push(this);
|
||||
|
||||
// Attach a comm manager to the
|
||||
this.comm_manager = comm_manager;
|
||||
this._models = {}; /* Dictionary of model ids and model instances */
|
||||
// Attach a comm manager to the
|
||||
this.keyboard_manager = keyboard_manager;
|
||||
this.notebook = notebook;
|
||||
this.comm_manager = comm_manager;
|
||||
this._models = {}; /* Dictionary of model ids and model instances */
|
||||
|
||||
// Register already-registered widget model types with the comm manager.
|
||||
var that = this;
|
||||
_.each(WidgetManager._model_types, function(model_type, model_name) {
|
||||
that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that));
|
||||
});
|
||||
};
|
||||
// Register already-registered widget model types with the comm manager.
|
||||
var that = this;
|
||||
_.each(WidgetManager._model_types, function(model_type, model_name) {
|
||||
that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that));
|
||||
});
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// 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 */
|
||||
//--------------------------------------------------------------------
|
||||
// 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_model = function (model_name, model_type) {
|
||||
// Registers a widget model by name.
|
||||
WidgetManager._model_types[model_name] = model_type;
|
||||
|
||||
// Register the widget with the comm manager. Make sure to pass this object's context
|
||||
// in so `this` works in the call back.
|
||||
_.each(WidgetManager._managers, function(instance, i) {
|
||||
if (instance.comm_manager !== null) {
|
||||
instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance));
|
||||
}
|
||||
});
|
||||
};
|
||||
// Register the widget with the comm manager. Make sure to pass this object's context
|
||||
// in so `this` works in the call back.
|
||||
_.each(WidgetManager._managers, function(instance, i) {
|
||||
if (instance.comm_manager !== null) {
|
||||
instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
WidgetManager.register_widget_view = function (view_name, view_type) {
|
||||
// Registers a widget view by name.
|
||||
WidgetManager._view_types[view_name] = view_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 {
|
||||
var view = this.create_view(model, {cell: cell});
|
||||
if (view === null) {
|
||||
console.error("View creation failed", model);
|
||||
}
|
||||
if (cell.widget_subarea) {
|
||||
cell.widget_area.show();
|
||||
this._handle_display_view(view);
|
||||
cell.widget_subarea.append(view.$el);
|
||||
//--------------------------------------------------------------------
|
||||
// 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 {
|
||||
var view = this.create_view(model, {cell: cell});
|
||||
if (view === null) {
|
||||
console.error("View creation failed", model);
|
||||
}
|
||||
if (cell.widget_subarea) {
|
||||
cell.widget_area.show();
|
||||
this._handle_display_view(view);
|
||||
cell.widget_subarea.append(view.$el);
|
||||
view.trigger('displayed');
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
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.
|
||||
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]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
WidgetManager.prototype.create_view = function(model, options, view) {
|
||||
// Creates a view for a particular model.
|
||||
var view_name = model.get('_view_name');
|
||||
var ViewType = WidgetManager._view_types[view_name];
|
||||
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 || {};
|
||||
if (view !== undefined) {
|
||||
options.cell = view.options.cell;
|
||||
}
|
||||
|
||||
// Create and render the view...
|
||||
var parameters = {model: model, options: options};
|
||||
view = new ViewType(parameters);
|
||||
view.render();
|
||||
model.on('destroy', view.remove, view);
|
||||
return view;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
WidgetManager.prototype.get_msg_cell = function (msg_id) {
|
||||
var cell = null;
|
||||
// First, check to see if the msg was triggered by cell execution.
|
||||
if (IPython.notebook) {
|
||||
cell = IPython.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();
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
||||
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.
|
||||
var that = this;
|
||||
var model_id = comm.comm_id;
|
||||
var widget_type_name = msg.content.target_name;
|
||||
var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
|
||||
widget_model.on('comm:close', function () {
|
||||
delete that._models[model_id];
|
||||
});
|
||||
this._models[model_id] = widget_model;
|
||||
};
|
||||
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);
|
||||
|
||||
// For backwards compatability.
|
||||
IPython.WidgetManager = WidgetManager;
|
||||
if (view.additional_elements) {
|
||||
for (var i = 0; i < view.additional_elements.length; i++) {
|
||||
this.keyboard_manager.register_events(view.additional_elements[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
WidgetManager.prototype.create_view = function(model, options, view) {
|
||||
// Creates a view for a particular model.
|
||||
var view_name = model.get('_view_name');
|
||||
var ViewType = WidgetManager._view_types[view_name];
|
||||
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 || {};
|
||||
if (view !== undefined) {
|
||||
options.cell = view.options.cell;
|
||||
}
|
||||
|
||||
// Create and render the view...
|
||||
var parameters = {model: model, options: options};
|
||||
view = new ViewType(parameters);
|
||||
view.render();
|
||||
model.on('destroy', view.remove, view);
|
||||
return view;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
||||
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.
|
||||
var that = this;
|
||||
var model_id = comm.comm_id;
|
||||
var widget_type_name = msg.content.target_name;
|
||||
var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
|
||||
widget_model.on('comm:close', function () {
|
||||
delete that._models[model_id];
|
||||
});
|
||||
this._models[model_id] = widget_model;
|
||||
};
|
||||
|
||||
return WidgetManager;
|
||||
});
|
||||
});
|
||||
|
@ -208,20 +208,20 @@ function(WidgetManager, _, Backbone){
|
||||
|
||||
_pack_models: function(value) {
|
||||
// Replace models with model ids recursively.
|
||||
var that = this;
|
||||
var packed;
|
||||
if (value instanceof Backbone.Model) {
|
||||
return value.id;
|
||||
|
||||
} else if ($.isArray(value)) {
|
||||
var packed = [];
|
||||
var that = this;
|
||||
packed = [];
|
||||
_.each(value, function(sub_value, key) {
|
||||
packed.push(that._pack_models(sub_value));
|
||||
});
|
||||
return packed;
|
||||
|
||||
} else if (value instanceof Object) {
|
||||
var packed = {};
|
||||
var that = this;
|
||||
packed = {};
|
||||
_.each(value, function(sub_value, key) {
|
||||
packed[key] = that._pack_models(sub_value);
|
||||
});
|
||||
|
@ -3,7 +3,8 @@
|
||||
|
||||
define([
|
||||
"widgets/js/widget",
|
||||
], function(widget){
|
||||
"base/js/utils",
|
||||
], function(widget, utils){
|
||||
|
||||
var DropdownView = widget.DOMWidgetView.extend({
|
||||
render : function(){
|
||||
@ -226,7 +227,7 @@ define([
|
||||
if (item.trim().length == 0) {
|
||||
item_html = " ";
|
||||
} else {
|
||||
item_html = IPython.utils.escape_html(item);
|
||||
item_html = utils.escape_html(item);
|
||||
}
|
||||
var item_query = '[data-value="' + item + '"]';
|
||||
var $item_element = that.$buttongroup.find(item_query);
|
||||
|
@ -3,12 +3,13 @@
|
||||
|
||||
define([
|
||||
"widgets/js/widget",
|
||||
], function(widget){
|
||||
"base/js/utils",
|
||||
], function(widget, utils){
|
||||
|
||||
var AccordionView = widget.DOMWidgetView.extend({
|
||||
render: function(){
|
||||
// Called when view is rendered.
|
||||
var guid = 'panel-group' + IPython.utils.uuid();
|
||||
var guid = 'panel-group' + utils.uuid();
|
||||
this.$el
|
||||
.attr('id', guid)
|
||||
.addClass('panel-group');
|
||||
@ -88,7 +89,7 @@ define([
|
||||
// Called when a child is added to children list.
|
||||
var view = this.create_child_view(model);
|
||||
var index = this.containers.length;
|
||||
var uuid = IPython.utils.uuid();
|
||||
var uuid = utils.uuid();
|
||||
var accordion_group = $('<div />')
|
||||
.addClass('panel panel-default')
|
||||
.appendTo(this.$el);
|
||||
@ -141,7 +142,7 @@ define([
|
||||
|
||||
render: function(){
|
||||
// Called when view is rendered.
|
||||
var uuid = 'tabs'+IPython.utils.uuid();
|
||||
var uuid = 'tabs'+utils.uuid();
|
||||
var that = this;
|
||||
this.$tabs = $('<div />', {id: uuid})
|
||||
.addClass('nav')
|
||||
@ -189,7 +190,7 @@ define([
|
||||
// Called when a child is added to children list.
|
||||
var view = this.create_child_view(model);
|
||||
var index = this.containers.length;
|
||||
var uuid = IPython.utils.uuid();
|
||||
var uuid = utils.uuid();
|
||||
|
||||
var that = this;
|
||||
var tab = $('<li />')
|
||||
|
Loading…
x
Reference in New Issue
Block a user