@ -29,9 +29,9 @@ define(["components/underscore/underscore-min",
// WidgetModel class
//--------------------------------------------------------------------
var WidgetModel = Backbone . Model . extend ( {
constructor : function ( comm _manager , comm , widget _ view_types ) {
constructor : function ( comm _manager , comm , widget _ manager ) {
this . comm _manager = comm _manager ;
this . widget _ view_types = widget _view _types ;
this . widget _ manager = widget _manager ;
this . pending _msgs = 0 ;
this . msg _throttle = 3 ;
this . msg _buffer = null ;
@ -53,8 +53,8 @@ define(["components/underscore/underscore-min",
this . last _modified _view = caller ;
this . save ( this . changedAttributes ( ) , { patch : true } ) ;
for ( var output_area in this . views ) {
var views = this . views [ output_area ] ;
for ( var cell in this . views ) {
var views = this . views [ cell ] ;
for ( var view _index in views ) {
var view = views [ view _index ] ;
if ( view !== caller ) {
@ -65,7 +65,7 @@ define(["components/underscore/underscore-min",
} ,
handle _status : function ( output_area , msg ) {
handle _status : function ( cell , msg ) {
//execution_state : ('busy', 'idle', 'starting')
if ( msg . content . execution _state == 'idle' ) {
@ -74,8 +74,8 @@ define(["components/underscore/underscore-min",
if ( this . msg _buffer != null &&
this . msg _throttle == this . pending _msgs ) {
var output_area = this . _get _output _area ( msg . parent _header . msg _id ) ;
var callbacks = this . _make _callbacks ( output_area ) ;
var cell = this . _get _msg _cell ( msg . parent _header . msg _id ) ;
var callbacks = this . _make _callbacks ( cell ) ;
var data = { sync _method : 'update' , sync _data : this . msg _buffer } ;
this . comm . send ( data , callbacks ) ;
this . msg _buffer = null ;
@ -125,12 +125,12 @@ define(["components/underscore/underscore-min",
var data = { sync _method : method , sync _data : send _json } ;
var output_area = null ;
var cell = null ;
if ( this . last _modified _view != undefined && this . last _modified _view != null ) {
output_area = this . last _modified _view . output _area ;
cell = this . last _modified _view . cell ;
}
var callbacks = this . _make _callbacks ( output_area ) ;
var callbacks = this . _make _callbacks ( cell ) ;
this . comm . send ( data , callbacks ) ;
this . pending _msgs ++ ;
}
@ -149,14 +149,14 @@ define(["components/underscore/underscore-min",
case 'display' :
// Try to get the cell index.
var output_area = this . _get _output _area ( msg . parent _header . msg _id ) ;
if ( output_area == null ) {
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 {
this . display _view ( msg . content . data . view _name ,
msg . content . data . parent ,
output_area ) ;
cell ) ;
}
break ;
case 'update' :
@ -189,8 +189,8 @@ define(["components/underscore/underscore-min",
// Handle when a widget is closed.
handle _comm _closed : function ( msg ) {
for ( var output_area in this . views ) {
var views = this . views [ output_area ] ;
for ( var cell in this . views ) {
var views = this . views [ cell ] ;
for ( var view _index in views ) {
var view = views [ view _index ] ;
view . remove ( ) ;
@ -200,18 +200,18 @@ define(["components/underscore/underscore-min",
// Create view that represents the model.
display _view : function ( view _name , parent _comm _id , output_area ) {
display _view : function ( view _name , parent _comm _id , cell ) {
var new _views = [ ] ;
var displayed = false ;
if ( parent _comm _id != undefined ) {
var parent _comm = this . comm _manager . comms [ parent _comm _id ] ;
var parent _model = parent _comm . model ;
var parent _views = parent _model . views [ output_area ] ;
var parent _views = parent _model . views [ cell ] ;
for ( var parent _view _index in parent _views ) {
var parent _view = parent _views [ parent _view _index ] ;
if ( parent _view . display _child != undefined ) {
var view = this . _create _view ( view _name , output_area ) ;
var view = this . _create _view ( view _name , cell ) ;
new _views . push ( view ) ;
parent _view . display _child ( view ) ;
displayed = true ;
@ -222,11 +222,13 @@ define(["components/underscore/underscore-min",
if ( ! displayed ) {
// No parent view is defined or exists. Add the view's
// element to cell's widget div.
var view = this . _create _view ( view _name , output_area ) ;
var view = this . _create _view ( view _name , cell ) ;
new _views . push ( view ) ;
this . _get _widget _area _element ( output _area , true )
. append ( view . $el ) ;
if ( cell . widget _subarea != undefined && cell . widget _subarea != null ) {
cell . widget _area . show ( ) ;
cell . widget _subarea . append ( view . $el ) ;
}
}
for ( var view _index in new _views ) {
@ -237,25 +239,25 @@ define(["components/underscore/underscore-min",
// Create a view
_create _view : function ( view _name , output_area ) {
var view = new this . widget _ view_types [ view _name ] ( { model : this } ) ;
_create _view : function ( view _name , cell ) {
var view = new this . widget _ manager. widget _ view_types [ view _name ] ( { model : this } ) ;
view . render ( ) ;
if ( this . views [ output_area ] == undefined ) {
this . views [ output_area ] = [ ]
if ( this . views [ cell ] == undefined ) {
this . views [ cell ] = [ ]
}
this . views [ output_area ] . push ( view ) ;
view . output_area = output _area ;
this . views [ cell ] . push ( view ) ;
view . cell = cell ;
// Handle when the view element is remove from the page.
var that = this ;
view . $el . on ( "remove" , function ( ) {
var index = that . views [ output_area ] . indexOf ( view ) ;
var index = that . views [ cell ] . indexOf ( view ) ;
if ( index > - 1 ) {
that . views [ output_area ] . splice ( index , 1 ) ;
that . views [ cell ] . splice ( index , 1 ) ;
}
view . remove ( ) ; // Clean-up view
if ( that . views [ output_area ] . length ( ) == 0 ) {
delete that . views [ output_area ] ;
if ( that . views [ cell ] . length ( ) == 0 ) {
delete that . views [ cell ] ;
}
// Close the comm if there are no views left.
@ -268,21 +270,21 @@ define(["components/underscore/underscore-min",
// Build a callback dict.
_make _callbacks : function ( output_area ) {
_make _callbacks : function ( cell ) {
var callbacks = { } ;
if ( output_area != null ) {
if ( cell != null && cell . output _area != undefined && cell . output_area != null ) {
var that = this ;
callbacks = {
iopub : {
output : $ . proxy ( output_area . handle _output , output _area ) ,
clear _output : $ . proxy ( output_area . handle _clear _output , output _area ) ,
output : $ . proxy ( cell. output_area . handle _output , cell . output _area ) ,
clear _output : $ . proxy ( cell. output_area . handle _clear _output , cell . output _area ) ,
status : function ( msg ) {
that . handle _status ( output_area , msg ) ;
that . handle _status ( cell , msg ) ;
} ,
get _ output_area : function ( ) {
get _ cell : function ( ) {
if ( that . last _modified _view != undefined &&
that . last _modified _view . output_area != undefined ) {
return that . last _modified _view . output_area ;
that . last _modified _view . cell != undefined ) {
return that . last _modified _view . cell ;
} else {
return null
}
@ -295,47 +297,33 @@ define(["components/underscore/underscore-min",
// Get the output area corresponding to the msg_id.
// output_area is an instance of Ipython.OutputArea
_get _ output_area : function ( msg _id ) {
// cell is an instance of IPython.Cell
_get _ msg_cell : function ( msg _id ) {
// First, check to see if the msg was triggered by cell execution.
var cell = IPython . notebook . get _msg _cell ( msg _id ) ;
var cell = this . widget _manager . get _msg _cell ( msg _id ) ;
if ( cell != null ) {
return cell .output _area ;
return cell ;
}
// Second, check to see if a get_ output_area callback was defined
// for the message. get_ output_area callbacks are registered for
// 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 ;
var callbacks = kernel . get _callbacks _for _msg ( msg _id ) ;
if ( callbacks != undefined &&
callbacks . iopub != undefined &&
callbacks . iopub . get _ output_area != undefined ) {
callbacks . iopub . get _ cell != undefined ) {
var output _area = callbacks . iopub . get _output _area ( ) ;
if ( output _area != null ) {
return output _area ;
}
return callbacks . iopub . get _cell ( ) ;
}
// Not triggered by a cell or widget (no get_ output_area callback
// Not triggered by a cell or widget (no get_cell callback
// exists).
return null ;
} ,
// Gets widget output area (as a JQuery element) from the
// output_area (Ipython.OutputArea instance)
_get _widget _area _element : function ( output _area , show ) {
var widget _area = output _area . element
. parent ( ) // output_wrapper
. parent ( ) // cell
. find ( '.widget-area' ) ;
if ( show ) { widget _area . show ( ) ; }
return widget _area . find ( '.widget-subarea' ) ;
} ,
} ) ;
@ -469,7 +457,14 @@ define(["components/underscore/underscore-min",
WidgetManager . prototype . handle _com _open = function ( comm , msg ) {
var widget _type _name = msg . content . target _name ;
var widget _model = new this . widget _model _types [ widget _type _name ] ( this . comm _manager , comm , this . widget _view _types ) ;
var widget _model = new this . widget _model _types [ widget _type _name ] ( this . comm _manager , comm , this ) ;
}
WidgetManager . prototype . get _msg _cell = function ( msg _id ) {
if ( IPython . notebook != undefined && IPython . notebook != null ) {
return IPython . notebook . get _msg _cell ( msg _id ) ;
}
}