##// END OF EJS Templates
Handle widget hide/show logic...
Jonathan Frederic -
Show More
@@ -19,8 +19,10 b''
19 // require(['components/underscore/underscore-min.js',
19 // require(['components/underscore/underscore-min.js',
20 // 'components/backbone/backbone-min.js'],
20 // 'components/backbone/backbone-min.js'],
21
21
22 var IPython = (function (IPython) {
22 "use strict";
23 "use strict";
23
24 // Only run once on a notebook.
25 if (IPython.notebook.widget_manager == undefined) {
24
26
25 //-----------------------------------------------------------------------
27 //-----------------------------------------------------------------------
26 // WidgetModel class
28 // WidgetModel class
@@ -120,21 +122,21 b' var IPython = (function (IPython) {'
120
122
121 // Create the corresponding widget model.
123 // Create the corresponding widget model.
122 var widget_model = new this.widget_model_types[widget_type_name];
124 var widget_model = new this.widget_model_types[widget_type_name];
123
125
124 // Remember comm associated with the model.
126 // Remember comm associated with the model.
125 widget_model.comm = comm;
127 widget_model.comm = comm;
126 comm.model = widget_model;
128 comm.model = widget_model;
127
129
128 // Create an array to remember the views associated with the model.
130 // Create an array to remember the views associated with the model.
129 widget_model.views = [];
131 widget_model.views = [];
130
132
131 // Add a handle to delete the control when the comm is closed.
133 // Add a handle to delete the control when the comm is closed.
132 var that = this;
134 var that = this;
133 var handle_close = function(msg) {
135 var handle_close = function(msg) {
134 that.handle_comm_closed(comm, msg);
136 that.handle_comm_closed(comm, msg);
135 }
137 }
136 comm.on_close(handle_close);
138 comm.on_close(handle_close);
137
139
138 // Handle incomming messages.
140 // Handle incomming messages.
139 var handle_msg = function(msg) {
141 var handle_msg = function(msg) {
140 that.handle_comm_msg(comm, msg);
142 that.handle_comm_msg(comm, msg);
@@ -147,11 +149,26 b' var IPython = (function (IPython) {'
147 var widget_view = new this.widget_view_types[widget_view_name]({model: widget_model});
149 var widget_view = new this.widget_view_types[widget_view_name]({model: widget_model});
148 widget_view.render();
150 widget_view.render();
149 widget_model.views.push(widget_view);
151 widget_model.views.push(widget_view);
150
152
153 // Handle when the view element is remove from the page.
154 widget_view.$el.on("remove", function(){
155 var index = widget_model.views.indexOf(widget_view);
156 if (index > -1) {
157 widget_model.views.splice(index, 1);
158 }
159 widget_view.remove(); // Clean-up view
160
161 // Close the comm if there are no views left.
162 if (widget_model.views.length()==0) {
163 widget_model.comm.close();
164 }
165 });
166
151 // Add the view's element to cell's widget div.
167 // Add the view's element to cell's widget div.
152 widget_area
168 widget_area
153 .append($("<div />").append(widget_view.$el));
169 .append($("<div />").append(widget_view.$el))
154
170 .parent().show(); // Show the widget_area (parent of widget_subarea)
171
155 // Update the view based on the model contents.
172 // Update the view based on the model contents.
156 widget_view.refresh();
173 widget_view.refresh();
157 }
174 }
@@ -162,18 +179,18 b' var IPython = (function (IPython) {'
162 var method = msg.content.data.method;
179 var method = msg.content.data.method;
163 switch (method){
180 switch (method){
164 case 'show':
181 case 'show':
165
182
166 // TODO: Get cell from registered output handler.
183 // TODO: Get cell from registered output handler.
167 var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);
184 var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);
168 var widget_subarea = cell.element.find('.widget_area').find('.widget_subarea');
185 var widget_subarea = cell.element.find('.widget_area').find('.widget_subarea');
169
186
170 if (msg.content.data.parent != undefined) {
187 if (msg.content.data.parent != undefined) {
171 var find_results = widget_subarea.find("." + msg.content.data.parent);
188 var find_results = widget_subarea.find("." + msg.content.data.parent);
172 if (find_results.length > 0) {
189 if (find_results.length > 0) {
173 widget_subarea = find_results;
190 widget_subarea = find_results;
174 }
191 }
175 }
192 }
176
193
177 this.show_view(widget_subarea, comm.model, msg.content.data.view_name);
194 this.show_view(widget_subarea, comm.model, msg.content.data.view_name);
178 break;
195 break;
179 case 'update':
196 case 'update':
@@ -235,7 +252,6 b' var IPython = (function (IPython) {'
235 IPython.WidgetModel = WidgetModel;
252 IPython.WidgetModel = WidgetModel;
236 IPython.WidgetView = WidgetView;
253 IPython.WidgetView = WidgetView;
237
254
238
255 IPython.notebook.widget_manager = new WidgetManager(IPython.notebook.kernel.comm_manager);
239 return IPython;
240
256
241 }(IPython));
257 }
General Comments 0
You need to be logged in to leave comments. Login now