##// END OF EJS Templates
Moved get_msg_cell which depends on notebook specific logic...
Jonathan Frederic -
Show More
@@ -29,9 +29,9 b' define(["components/underscore/underscore-min",'
29 // WidgetModel class
29 // WidgetModel class
30 //--------------------------------------------------------------------
30 //--------------------------------------------------------------------
31 var WidgetModel = Backbone.Model.extend({
31 var WidgetModel = Backbone.Model.extend({
32 constructor: function(comm_manager, comm, widget_view_types) {
32 constructor: function(comm_manager, comm, widget_manager) {
33 this.comm_manager = comm_manager;
33 this.comm_manager = comm_manager;
34 this.widget_view_types = widget_view_types;
34 this.widget_manager = widget_manager;
35 this.pending_msgs = 0;
35 this.pending_msgs = 0;
36 this.msg_throttle = 3;
36 this.msg_throttle = 3;
37 this.msg_buffer = null;
37 this.msg_buffer = null;
@@ -53,8 +53,8 b' define(["components/underscore/underscore-min",'
53 this.last_modified_view = caller;
53 this.last_modified_view = caller;
54 this.save(this.changedAttributes(), {patch: true});
54 this.save(this.changedAttributes(), {patch: true});
55
55
56 for (var output_area in this.views) {
56 for (var cell in this.views) {
57 var views = this.views[output_area];
57 var views = this.views[cell];
58 for (var view_index in views) {
58 for (var view_index in views) {
59 var view = views[view_index];
59 var view = views[view_index];
60 if (view !== caller) {
60 if (view !== caller) {
@@ -65,7 +65,7 b' define(["components/underscore/underscore-min",'
65 },
65 },
66
66
67
67
68 handle_status: function (output_area, msg) {
68 handle_status: function (cell, msg) {
69 //execution_state : ('busy', 'idle', 'starting')
69 //execution_state : ('busy', 'idle', 'starting')
70 if (msg.content.execution_state=='idle') {
70 if (msg.content.execution_state=='idle') {
71
71
@@ -74,8 +74,8 b' define(["components/underscore/underscore-min",'
74 if (this.msg_buffer != null &&
74 if (this.msg_buffer != null &&
75 this.msg_throttle == this.pending_msgs) {
75 this.msg_throttle == this.pending_msgs) {
76
76
77 var output_area = this._get_output_area(msg.parent_header.msg_id);
77 var cell = this._get_msg_cell(msg.parent_header.msg_id);
78 var callbacks = this._make_callbacks(output_area);
78 var callbacks = this._make_callbacks(cell);
79 var data = {sync_method: 'update', sync_data: this.msg_buffer};
79 var data = {sync_method: 'update', sync_data: this.msg_buffer};
80 this.comm.send(data, callbacks);
80 this.comm.send(data, callbacks);
81 this.msg_buffer = null;
81 this.msg_buffer = null;
@@ -125,12 +125,12 b' define(["components/underscore/underscore-min",'
125
125
126 var data = {sync_method: method, sync_data: send_json};
126 var data = {sync_method: method, sync_data: send_json};
127
127
128 var output_area = null;
128 var cell = null;
129 if (this.last_modified_view != undefined && this.last_modified_view != null) {
129 if (this.last_modified_view != undefined && this.last_modified_view != null) {
130 output_area = this.last_modified_view.output_area;
130 cell = this.last_modified_view.cell;
131 }
131 }
132
132
133 var callbacks = this._make_callbacks(output_area);
133 var callbacks = this._make_callbacks(cell);
134 this.comm.send(data, callbacks);
134 this.comm.send(data, callbacks);
135 this.pending_msgs++;
135 this.pending_msgs++;
136 }
136 }
@@ -149,14 +149,14 b' define(["components/underscore/underscore-min",'
149 case 'display':
149 case 'display':
150
150
151 // Try to get the cell index.
151 // Try to get the cell index.
152 var output_area = this._get_output_area(msg.parent_header.msg_id);
152 var cell = this._get_msg_cell(msg.parent_header.msg_id);
153 if (output_area == null) {
153 if (cell == null) {
154 console.log("Could not determine where the display" +
154 console.log("Could not determine where the display" +
155 " message was from. Widget will not be displayed")
155 " message was from. Widget will not be displayed")
156 } else {
156 } else {
157 this.display_view(msg.content.data.view_name,
157 this.display_view(msg.content.data.view_name,
158 msg.content.data.parent,
158 msg.content.data.parent,
159 output_area);
159 cell);
160 }
160 }
161 break;
161 break;
162 case 'update':
162 case 'update':
@@ -189,8 +189,8 b' define(["components/underscore/underscore-min",'
189
189
190 // Handle when a widget is closed.
190 // Handle when a widget is closed.
191 handle_comm_closed: function (msg) {
191 handle_comm_closed: function (msg) {
192 for (var output_area in this.views) {
192 for (var cell in this.views) {
193 var views = this.views[output_area];
193 var views = this.views[cell];
194 for (var view_index in views) {
194 for (var view_index in views) {
195 var view = views[view_index];
195 var view = views[view_index];
196 view.remove();
196 view.remove();
@@ -200,18 +200,18 b' define(["components/underscore/underscore-min",'
200
200
201
201
202 // Create view that represents the model.
202 // Create view that represents the model.
203 display_view: function (view_name, parent_comm_id, output_area) {
203 display_view: function (view_name, parent_comm_id, cell) {
204 var new_views = [];
204 var new_views = [];
205
205
206 var displayed = false;
206 var displayed = false;
207 if (parent_comm_id != undefined) {
207 if (parent_comm_id != undefined) {
208 var parent_comm = this.comm_manager.comms[parent_comm_id];
208 var parent_comm = this.comm_manager.comms[parent_comm_id];
209 var parent_model = parent_comm.model;
209 var parent_model = parent_comm.model;
210 var parent_views = parent_model.views[output_area];
210 var parent_views = parent_model.views[cell];
211 for (var parent_view_index in parent_views) {
211 for (var parent_view_index in parent_views) {
212 var parent_view = parent_views[parent_view_index];
212 var parent_view = parent_views[parent_view_index];
213 if (parent_view.display_child != undefined) {
213 if (parent_view.display_child != undefined) {
214 var view = this._create_view(view_name, output_area);
214 var view = this._create_view(view_name, cell);
215 new_views.push(view);
215 new_views.push(view);
216 parent_view.display_child(view);
216 parent_view.display_child(view);
217 displayed = true;
217 displayed = true;
@@ -222,11 +222,13 b' define(["components/underscore/underscore-min",'
222 if (!displayed) {
222 if (!displayed) {
223 // No parent view is defined or exists. Add the view's
223 // No parent view is defined or exists. Add the view's
224 // element to cell's widget div.
224 // element to cell's widget div.
225 var view = this._create_view(view_name, output_area);
225 var view = this._create_view(view_name, cell);
226 new_views.push(view);
226 new_views.push(view);
227 this._get_widget_area_element(output_area, true)
227
228 .append(view.$el);
228 if (cell.widget_subarea != undefined && cell.widget_subarea != null) {
229
229 cell.widget_area.show();
230 cell.widget_subarea.append(view.$el);
231 }
230 }
232 }
231
233
232 for (var view_index in new_views) {
234 for (var view_index in new_views) {
@@ -237,25 +239,25 b' define(["components/underscore/underscore-min",'
237
239
238
240
239 // Create a view
241 // Create a view
240 _create_view: function (view_name, output_area) {
242 _create_view: function (view_name, cell) {
241 var view = new this.widget_view_types[view_name]({model: this});
243 var view = new this.widget_manager.widget_view_types[view_name]({model: this});
242 view.render();
244 view.render();
243 if (this.views[output_area]==undefined) {
245 if (this.views[cell]==undefined) {
244 this.views[output_area] = []
246 this.views[cell] = []
245 }
247 }
246 this.views[output_area].push(view);
248 this.views[cell].push(view);
247 view.output_area = output_area;
249 view.cell = cell;
248
250
249 // Handle when the view element is remove from the page.
251 // Handle when the view element is remove from the page.
250 var that = this;
252 var that = this;
251 view.$el.on("remove", function(){
253 view.$el.on("remove", function(){
252 var index = that.views[output_area].indexOf(view);
254 var index = that.views[cell].indexOf(view);
253 if (index > -1) {
255 if (index > -1) {
254 that.views[output_area].splice(index, 1);
256 that.views[cell].splice(index, 1);
255 }
257 }
256 view.remove(); // Clean-up view
258 view.remove(); // Clean-up view
257 if (that.views[output_area].length()==0) {
259 if (that.views[cell].length()==0) {
258 delete that.views[output_area];
260 delete that.views[cell];
259 }
261 }
260
262
261 // Close the comm if there are no views left.
263 // Close the comm if there are no views left.
@@ -268,21 +270,21 b' define(["components/underscore/underscore-min",'
268
270
269
271
270 // Build a callback dict.
272 // Build a callback dict.
271 _make_callbacks: function (output_area) {
273 _make_callbacks: function (cell) {
272 var callbacks = {};
274 var callbacks = {};
273 if (output_area != null) {
275 if (cell != null && cell.output_area != undefined && cell.output_area != null) {
274 var that = this;
276 var that = this;
275 callbacks = {
277 callbacks = {
276 iopub : {
278 iopub : {
277 output : $.proxy(output_area.handle_output, output_area),
279 output : $.proxy(cell.output_area.handle_output, cell.output_area),
278 clear_output : $.proxy(output_area.handle_clear_output, output_area),
280 clear_output : $.proxy(cell.output_area.handle_clear_output, cell.output_area),
279 status : function(msg){
281 status : function(msg){
280 that.handle_status(output_area, msg);
282 that.handle_status(cell, msg);
281 },
283 },
282 get_output_area : function() {
284 get_cell : function() {
283 if (that.last_modified_view != undefined &&
285 if (that.last_modified_view != undefined &&
284 that.last_modified_view.output_area != undefined) {
286 that.last_modified_view.cell != undefined) {
285 return that.last_modified_view.output_area;
287 return that.last_modified_view.cell;
286 } else {
288 } else {
287 return null
289 return null
288 }
290 }
@@ -295,47 +297,33 b' define(["components/underscore/underscore-min",'
295
297
296
298
297 // Get the output area corresponding to the msg_id.
299 // Get the output area corresponding to the msg_id.
298 // output_area is an instance of Ipython.OutputArea
300 // cell is an instance of IPython.Cell
299 _get_output_area: function (msg_id) {
301 _get_msg_cell: function (msg_id) {
300
302
301 // First, check to see if the msg was triggered by cell execution.
303 // First, check to see if the msg was triggered by cell execution.
302 var cell = IPython.notebook.get_msg_cell(msg_id);
304 var cell = this.widget_manager.get_msg_cell(msg_id);
303 if (cell != null) {
305 if (cell != null) {
304 return cell.output_area;
306 return cell;
305 }
307 }
306
308
307 // Second, check to see if a get_output_area callback was defined
309 // Second, check to see if a get_cell callback was defined
308 // for the message. get_output_area callbacks are registered for
310 // for the message. get_cell callbacks are registered for
309 // widget messages, so this block is actually checking to see if the
311 // widget messages, so this block is actually checking to see if the
310 // message was triggered by a widget.
312 // message was triggered by a widget.
311 var kernel = this.comm_manager.kernel;
313 var kernel = this.comm_manager.kernel;
312 var callbacks = kernel.get_callbacks_for_msg(msg_id);
314 var callbacks = kernel.get_callbacks_for_msg(msg_id);
313 if (callbacks != undefined &&
315 if (callbacks != undefined &&
314 callbacks.iopub != undefined &&
316 callbacks.iopub != undefined &&
315 callbacks.iopub.get_output_area != undefined) {
317 callbacks.iopub.get_cell != undefined) {
316
318
317 var output_area = callbacks.iopub.get_output_area();
319 return callbacks.iopub.get_cell();
318 if (output_area != null) {
319 return output_area;
320 }
321 }
320 }
322
321
323 // Not triggered by a cell or widget (no get_output_area callback
322 // Not triggered by a cell or widget (no get_cell callback
324 // exists).
323 // exists).
325 return null;
324 return null;
326 },
325 },
327
326
328 // Gets widget output area (as a JQuery element) from the
329 // output_area (Ipython.OutputArea instance)
330 _get_widget_area_element: function (output_area, show) {
331 var widget_area = output_area.element
332 .parent() // output_wrapper
333 .parent() // cell
334 .find('.widget-area');
335 if (show) { widget_area.show(); }
336 return widget_area.find('.widget-subarea');
337 },
338
339 });
327 });
340
328
341
329
@@ -469,7 +457,14 b' define(["components/underscore/underscore-min",'
469
457
470 WidgetManager.prototype.handle_com_open = function (comm, msg) {
458 WidgetManager.prototype.handle_com_open = function (comm, msg) {
471 var widget_type_name = msg.content.target_name;
459 var widget_type_name = msg.content.target_name;
472 var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this.widget_view_types);
460 var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this);
461 }
462
463
464 WidgetManager.prototype.get_msg_cell = function (msg_id) {
465 if (IPython.notebook != undefined && IPython.notebook != null) {
466 return IPython.notebook.get_msg_cell(msg_id);
467 }
473 }
468 }
474
469
475
470
General Comments 0
You need to be logged in to leave comments. Login now