|
|
|
|
@ -1,12 +1,65 @@
|
|
|
|
|
// Copyright (c) Jupyter Development Team.
|
|
|
|
|
// Distributed under the terms of the Modified BSD License.
|
|
|
|
|
|
|
|
|
|
// Events that this code should rely on.
|
|
|
|
|
// delete.Cell
|
|
|
|
|
// selected_cell_type_changed.Notebook
|
|
|
|
|
// create.Cell
|
|
|
|
|
|
|
|
|
|
define(['jquery', 'base/js/events'], function($, events) {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WidgetArea
|
|
|
|
|
*/
|
|
|
|
|
var WidgetArea = function(cell) {
|
|
|
|
|
this.widget_views = [];
|
|
|
|
|
|
|
|
|
|
this._cell = cell;
|
|
|
|
|
this._widgets_live = true;
|
|
|
|
|
this.disposed = false;
|
|
|
|
|
|
|
|
|
|
this._create_elements();
|
|
|
|
|
this._bind_events();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display a widget view in the cell.
|
|
|
|
|
*/
|
|
|
|
|
WidgetArea.prototype.display_widget_view = function(view_promise) {
|
|
|
|
|
|
|
|
|
|
// Display a dummy element
|
|
|
|
|
var dummy = $('<div/>');
|
|
|
|
|
this.widget_subarea.append(dummy);
|
|
|
|
|
|
|
|
|
|
// Display the view.
|
|
|
|
|
var that = this;
|
|
|
|
|
return view_promise.then(function(view) {
|
|
|
|
|
that.widget_area.show();
|
|
|
|
|
dummy.replaceWith(view.$el);
|
|
|
|
|
that.widget_views.push(view);
|
|
|
|
|
|
|
|
|
|
// Check the live state of the view's model.
|
|
|
|
|
if (view.model.comm_live) {
|
|
|
|
|
that._widget_live(view);
|
|
|
|
|
} else {
|
|
|
|
|
that._widget_dead(view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Listen to comm live events for the view.
|
|
|
|
|
view.on('comm:live', that._widget_live, that);
|
|
|
|
|
view.on('comm:dead', that._widget_dead, that);
|
|
|
|
|
return view;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disposes of the widget area and its widgets.
|
|
|
|
|
*/
|
|
|
|
|
WidgetArea.prototype.dispose = function() {
|
|
|
|
|
this._clear();
|
|
|
|
|
this.disposed = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates the elements of the widget area and appends them
|
|
|
|
|
* to the associated cell.
|
|
|
|
|
*/
|
|
|
|
|
WidgetArea.prototype._create_elements = function() {
|
|
|
|
|
var widget_area = $('<div/>')
|
|
|
|
|
.addClass('widget-area')
|
|
|
|
|
.hide();
|
|
|
|
|
@ -38,42 +91,30 @@
|
|
|
|
|
})
|
|
|
|
|
.appendTo(widget_prompt);
|
|
|
|
|
|
|
|
|
|
if (this._cell.input) {
|
|
|
|
|
this._cell.input.after(widget_area);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error('Cell does not have an `input` element. Is it not a CodeCell?');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display a widget view in the cell.
|
|
|
|
|
*/
|
|
|
|
|
CodeCell.prototype.display_widget_view = function(view_promise) {
|
|
|
|
|
|
|
|
|
|
// Display a dummy element
|
|
|
|
|
var dummy = $('<div/>');
|
|
|
|
|
this.widget_subarea.append(dummy);
|
|
|
|
|
|
|
|
|
|
// Display the view.
|
|
|
|
|
* Listens to events of the cell.
|
|
|
|
|
*/
|
|
|
|
|
WidgetArea.prototype._bind_events = function() {
|
|
|
|
|
var that = this;
|
|
|
|
|
return view_promise.then(function(view) {
|
|
|
|
|
that.widget_area.show();
|
|
|
|
|
dummy.replaceWith(view.$el);
|
|
|
|
|
that.widget_views.push(view);
|
|
|
|
|
|
|
|
|
|
// Check the live state of the view's model.
|
|
|
|
|
if (view.model.comm_live) {
|
|
|
|
|
that._widget_live(view);
|
|
|
|
|
} else {
|
|
|
|
|
that._widget_dead(view);
|
|
|
|
|
events.on('execute.CodeCell', function(event, data) {
|
|
|
|
|
if (data.cell===that._cell) {
|
|
|
|
|
that._clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Listen to comm live events for the view.
|
|
|
|
|
view.on('comm:live', that._widget_live, that);
|
|
|
|
|
view.on('comm:dead', that._widget_dead, that);
|
|
|
|
|
return view;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles when a widget loses it's comm connection.
|
|
|
|
|
* @param {WidgetView} view
|
|
|
|
|
* @param {WidgetView} view
|
|
|
|
|
*/
|
|
|
|
|
CodeCell.prototype._widget_dead = function(view) {
|
|
|
|
|
WidgetArea.prototype._widget_dead = function(view) {
|
|
|
|
|
if (this._widgets_live) {
|
|
|
|
|
this._widgets_live = false;
|
|
|
|
|
this.widget_area.addClass('connection-problems');
|
|
|
|
|
@ -83,9 +124,9 @@
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles when a widget is connected to a live comm.
|
|
|
|
|
* @param {WidgetView} view
|
|
|
|
|
* @param {WidgetView} view
|
|
|
|
|
*/
|
|
|
|
|
CodeCell.prototype._widget_live = function(view) {
|
|
|
|
|
WidgetArea.prototype._widget_live = function(view) {
|
|
|
|
|
if (!this._widgets_live) {
|
|
|
|
|
// Check that the other widgets are live too. O(N) operation.
|
|
|
|
|
// Abort the function at the first dead widget found.
|
|
|
|
|
@ -97,8 +138,10 @@
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: on event execute.CodeCell
|
|
|
|
|
/**
|
|
|
|
|
* Clears the widgets in the widget area.
|
|
|
|
|
*/
|
|
|
|
|
WidgetArea.prototype._clear = function() {
|
|
|
|
|
// Clear widget area
|
|
|
|
|
for (var i = 0; i < this.widget_views.length; i++) {
|
|
|
|
|
var view = this.widget_views[i];
|
|
|
|
|
@ -113,4 +156,8 @@
|
|
|
|
|
this.widget_subarea.height('');
|
|
|
|
|
this.widget_area.height('');
|
|
|
|
|
this.widget_area.hide();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {WidgetArea: WidgetArea};
|
|
|
|
|
});
|
|
|
|
|
|