1+ // Copyright (c) Jupyter Development Team.
2+ // Distributed under the terms of the Modified BSD License.
13
2- // Events that this code should rely on.
3- // delete.Cell
4- // selected_cell_type_changed.Notebook
5- // create.Cell
6-
4+ define ( [ 'jquery' , 'base/js/events' ] , function ( $ , events ) {
5+
6+ /**
7+ * WidgetArea
8+ */
9+ var WidgetArea = function ( cell ) {
710 this . widget_views = [ ] ;
11+
12+ this . _cell = cell ;
813 this . _widgets_live = true ;
14+ this . disposed = false ;
15+
16+ this . _create_elements ( ) ;
17+ this . _bind_events ( ) ;
18+ }
19+
20+ /**
21+ * Display a widget view in the cell.
22+ */
23+ WidgetArea . prototype . display_widget_view = function ( view_promise ) {
24+
25+ // Display a dummy element
26+ var dummy = $ ( '<div/>' ) ;
27+ this . widget_subarea . append ( dummy ) ;
28+
29+ // Display the view.
30+ var that = this ;
31+ return view_promise . then ( function ( view ) {
32+ that . widget_area . show ( ) ;
33+ dummy . replaceWith ( view . $el ) ;
34+ that . widget_views . push ( view ) ;
35+
36+ // Check the live state of the view's model.
37+ if ( view . model . comm_live ) {
38+ that . _widget_live ( view ) ;
39+ } else {
40+ that . _widget_dead ( view ) ;
41+ }
42+
43+ // Listen to comm live events for the view.
44+ view . on ( 'comm:live' , that . _widget_live , that ) ;
45+ view . on ( 'comm:dead' , that . _widget_dead , that ) ;
46+ return view ;
47+ } ) ;
48+ } ;
49+
50+ /**
51+ * Disposes of the widget area and its widgets.
52+ */
53+ WidgetArea . prototype . dispose = function ( ) {
54+ this . _clear ( ) ;
55+ this . disposed = true ;
56+ } ;
957
58+ /**
59+ * Creates the elements of the widget area and appends them
60+ * to the associated cell.
61+ */
62+ WidgetArea . prototype . _create_elements = function ( ) {
1063 var widget_area = $ ( '<div/>' )
1164 . addClass ( 'widget-area' )
1265 . hide ( ) ;
3891 } )
3992 . appendTo ( widget_prompt ) ;
4093
94+ if ( this . _cell . input ) {
95+ this . _cell . input . after ( widget_area ) ;
96+ } else {
97+ throw new Error ( 'Cell does not have an `input` element. Is it not a CodeCell?' ) ;
98+ }
99+ }
41100
42101 /**
43- * Display a widget view in the cell.
44- */
45- CodeCell . prototype . display_widget_view = function ( view_promise ) {
46-
47- // Display a dummy element
48- var dummy = $ ( '<div/>' ) ;
49- this . widget_subarea . append ( dummy ) ;
50-
51- // Display the view.
102+ * Listens to events of the cell.
103+ */
104+ WidgetArea . prototype . _bind_events = function ( ) {
52105 var that = this ;
53- return view_promise . then ( function ( view ) {
54- that . widget_area . show ( ) ;
55- dummy . replaceWith ( view . $el ) ;
56- that . widget_views . push ( view ) ;
57-
58- // Check the live state of the view's model.
59- if ( view . model . comm_live ) {
60- that . _widget_live ( view ) ;
61- } else {
62- that . _widget_dead ( view ) ;
106+ events . on ( 'execute.CodeCell' , function ( event , data ) {
107+ if ( data . cell === that . _cell ) {
108+ that . _clear ( ) ;
63109 }
64-
65- // Listen to comm live events for the view.
66- view . on ( 'comm:live' , that . _widget_live , that ) ;
67- view . on ( 'comm:dead' , that . _widget_dead , that ) ;
68- return view ;
69110 } ) ;
70111 } ;
71112
72113 /**
73114 * Handles when a widget loses it's comm connection.
74- * @param {WidgetView } view
115+ * @param {WidgetView } view
75116 */
76- CodeCell . prototype . _widget_dead = function ( view ) {
117+ WidgetArea . prototype . _widget_dead = function ( view ) {
77118 if ( this . _widgets_live ) {
78119 this . _widgets_live = false ;
79120 this . widget_area . addClass ( 'connection-problems' ) ;
83124
84125 /**
85126 * Handles when a widget is connected to a live comm.
86- * @param {WidgetView } view
127+ * @param {WidgetView } view
87128 */
88- CodeCell . prototype . _widget_live = function ( view ) {
129+ WidgetArea . prototype . _widget_live = function ( view ) {
89130 if ( ! this . _widgets_live ) {
90131 // Check that the other widgets are live too. O(N) operation.
91132 // Abort the function at the first dead widget found.
97138 }
98139 } ;
99140
100-
101- // TODO: on event execute.CodeCell
141+ /**
142+ * Clears the widgets in the widget area.
143+ */
144+ WidgetArea . prototype . _clear = function ( ) {
102145 // Clear widget area
103146 for ( var i = 0 ; i < this . widget_views . length ; i ++ ) {
104147 var view = this . widget_views [ i ] ;
113156 this . widget_subarea . height ( '' ) ;
114157 this . widget_area . height ( '' ) ;
115158 this . widget_area . hide ( ) ;
159+ } ;
160+
161+ return { WidgetArea : WidgetArea } ;
162+ } ) ;
116163
0 commit comments