Handle widget hide/show logic

also only create a widget manager once
This commit is contained in:
Jonathan Frederic 2013-10-17 06:13:52 +00:00
parent 4d2bcf02e5
commit 4dc1155c7e

View File

@ -19,9 +19,11 @@
// require(['components/underscore/underscore-min.js',
// 'components/backbone/backbone-min.js'],
var IPython = (function (IPython) {
"use strict";
// Only run once on a notebook.
if (IPython.notebook.widget_manager == undefined) {
//-----------------------------------------------------------------------
// WidgetModel class
//-----------------------------------------------------------------------
@ -148,9 +150,24 @@ var IPython = (function (IPython) {
widget_view.render();
widget_model.views.push(widget_view);
// Handle when the view element is remove from the page.
widget_view.$el.on("remove", function(){
var index = widget_model.views.indexOf(widget_view);
if (index > -1) {
widget_model.views.splice(index, 1);
}
widget_view.remove(); // Clean-up view
// Close the comm if there are no views left.
if (widget_model.views.length()==0) {
widget_model.comm.close();
}
});
// Add the view's element to cell's widget div.
widget_area
.append($("<div />").append(widget_view.$el));
.append($("<div />").append(widget_view.$el))
.parent().show(); // Show the widget_area (parent of widget_subarea)
// Update the view based on the model contents.
widget_view.refresh();
@ -235,7 +252,6 @@ var IPython = (function (IPython) {
IPython.WidgetModel = WidgetModel;
IPython.WidgetView = WidgetView;
IPython.notebook.widget_manager = new WidgetManager(IPython.notebook.kernel.comm_manager);
return IPython;
}(IPython));
}