##// END OF EJS Templates
Added on_create_widget callback to WidgetManager
Jonathan Frederic -
Show More
@@ -1,483 +1,500 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2013 The IPython Development Team
2 // Copyright (C) 2013 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // WidgetModel, WidgetView, and WidgetManager
9 // WidgetModel, WidgetView, and WidgetManager
10 //============================================================================
10 //============================================================================
11 /**
11 /**
12 * Base Widget classes
12 * Base Widget classes
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 * @submodule widget
15 * @submodule widget
16 */
16 */
17
17
18 "use strict";
18 "use strict";
19
19
20 // Use require.js 'define' method so that require.js is intelligent enough to
20 // Use require.js 'define' method so that require.js is intelligent enough to
21 // syncronously load everything within this file when it is being 'required'
21 // syncronously load everything within this file when it is being 'required'
22 // elsewhere.
22 // elsewhere.
23 define(["components/underscore/underscore-min",
23 define(["components/underscore/underscore-min",
24 "components/backbone/backbone-min",
24 "components/backbone/backbone-min",
25 ], function(underscore, backbone){
25 ], function(underscore, backbone){
26
26
27
27
28 //--------------------------------------------------------------------
28 //--------------------------------------------------------------------
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_manager) {
32 constructor: function(comm_manager, comm, widget_manager) {
33 this.comm_manager = comm_manager;
33 this.comm_manager = comm_manager;
34 this.widget_manager = widget_manager;
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;
38 this.views = {};
38 this.views = {};
39
39
40 // Remember comm associated with the model.
40 // Remember comm associated with the model.
41 this.comm = comm;
41 this.comm = comm;
42 comm.model = this;
42 comm.model = this;
43
43
44 // Hook comm messages up to model.
44 // Hook comm messages up to model.
45 comm.on_close($.proxy(this._handle_comm_closed, this));
45 comm.on_close($.proxy(this._handle_comm_closed, this));
46 comm.on_msg($.proxy(this._handle_comm_msg, this));
46 comm.on_msg($.proxy(this._handle_comm_msg, this));
47
47
48 return Backbone.Model.apply(this);
48 return Backbone.Model.apply(this);
49 },
49 },
50
50
51
51
52 update_other_views: function(caller) {
52 update_other_views: function(caller) {
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 cell in this.views) {
56 for (var cell in this.views) {
57 var views = this.views[cell];
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) {
61 view.update();
61 view.update();
62 }
62 }
63 }
63 }
64 }
64 }
65 },
65 },
66
66
67
67
68 // Handle when a widget is closed.
68 // Handle when a widget is closed.
69 _handle_comm_closed: function (msg) {
69 _handle_comm_closed: function (msg) {
70 for (var cell in this.views) {
70 for (var cell in this.views) {
71 var views = this.views[cell];
71 var views = this.views[cell];
72 for (var view_index in views) {
72 for (var view_index in views) {
73 var view = views[view_index];
73 var view = views[view_index];
74 view.remove();
74 view.remove();
75 }
75 }
76 }
76 }
77 },
77 },
78
78
79
79
80 // Handle incomming comm msg.
80 // Handle incomming comm msg.
81 _handle_comm_msg: function (msg) {
81 _handle_comm_msg: function (msg) {
82 var method = msg.content.data.method;
82 var method = msg.content.data.method;
83 switch (method){
83 switch (method){
84 case 'display':
84 case 'display':
85
85
86 // Try to get the cell index.
86 // Try to get the cell index.
87 var cell = this._get_msg_cell(msg.parent_header.msg_id);
87 var cell = this._get_msg_cell(msg.parent_header.msg_id);
88 if (cell == null) {
88 if (cell == null) {
89 console.log("Could not determine where the display" +
89 console.log("Could not determine where the display" +
90 " message was from. Widget will not be displayed")
90 " message was from. Widget will not be displayed")
91 } else {
91 } else {
92 this._display_view(msg.content.data.view_name,
92 this._display_view(msg.content.data.view_name,
93 msg.content.data.parent,
93 msg.content.data.parent,
94 cell);
94 cell);
95 }
95 }
96 break;
96 break;
97 case 'update':
97 case 'update':
98 this._handle_update(msg.content.data.state);
98 this._handle_update(msg.content.data.state);
99 break;
99 break;
100 }
100 }
101 },
101 },
102
102
103
103
104 // Handle when a widget is updated via the python side.
104 // Handle when a widget is updated via the python side.
105 _handle_update: function (state) {
105 _handle_update: function (state) {
106 this.updating = true;
106 this.updating = true;
107 try {
107 try {
108 for (var key in state) {
108 for (var key in state) {
109 if (state.hasOwnProperty(key)) {
109 if (state.hasOwnProperty(key)) {
110 if (key == "_css"){
110 if (key == "_css"){
111 this.css = state[key];
111 this.css = state[key];
112 } else {
112 } else {
113 this.set(key, state[key]);
113 this.set(key, state[key]);
114 }
114 }
115 }
115 }
116 }
116 }
117 this.id = this.comm.comm_id;
117 this.id = this.comm.comm_id;
118 this.save();
118 this.save();
119 } finally {
119 } finally {
120 this.updating = false;
120 this.updating = false;
121 }
121 }
122 },
122 },
123
123
124
124
125 _handle_status: function (cell, msg) {
125 _handle_status: function (cell, msg) {
126 //execution_state : ('busy', 'idle', 'starting')
126 //execution_state : ('busy', 'idle', 'starting')
127 if (msg.content.execution_state=='idle') {
127 if (msg.content.execution_state=='idle') {
128
128
129 // Send buffer if this message caused another message to be
129 // Send buffer if this message caused another message to be
130 // throttled.
130 // throttled.
131 if (this.msg_buffer != null &&
131 if (this.msg_buffer != null &&
132 this.msg_throttle == this.pending_msgs) {
132 this.msg_throttle == this.pending_msgs) {
133
133
134 var cell = this._get_msg_cell(msg.parent_header.msg_id);
134 var cell = this._get_msg_cell(msg.parent_header.msg_id);
135 var callbacks = this._make_callbacks(cell);
135 var callbacks = this._make_callbacks(cell);
136 var data = {sync_method: 'update', sync_data: this.msg_buffer};
136 var data = {sync_method: 'update', sync_data: this.msg_buffer};
137 this.comm.send(data, callbacks);
137 this.comm.send(data, callbacks);
138 this.msg_buffer = null;
138 this.msg_buffer = null;
139 } else {
139 } else {
140
140
141 // Only decrease the pending message count if the buffer
141 // Only decrease the pending message count if the buffer
142 // doesn't get flushed (sent).
142 // doesn't get flushed (sent).
143 --this.pending_msgs;
143 --this.pending_msgs;
144 }
144 }
145 }
145 }
146 },
146 },
147
147
148
148
149 // Custom syncronization logic.
149 // Custom syncronization logic.
150 _handle_sync: function (method, options) {
150 _handle_sync: function (method, options) {
151 var model_json = this.toJSON();
151 var model_json = this.toJSON();
152
152
153 // Only send updated state if the state hasn't been changed
153 // Only send updated state if the state hasn't been changed
154 // during an update.
154 // during an update.
155 if (!this.updating) {
155 if (!this.updating) {
156 if (this.pending_msgs >= this.msg_throttle) {
156 if (this.pending_msgs >= this.msg_throttle) {
157 // The throttle has been exceeded, buffer the current msg so
157 // The throttle has been exceeded, buffer the current msg so
158 // it can be sent once the kernel has finished processing
158 // it can be sent once the kernel has finished processing
159 // some of the existing messages.
159 // some of the existing messages.
160 if (method=='patch') {
160 if (method=='patch') {
161 if (this.msg_buffer == null) {
161 if (this.msg_buffer == null) {
162 this.msg_buffer = $.extend({}, model_json); // Copy
162 this.msg_buffer = $.extend({}, model_json); // Copy
163 }
163 }
164 for (var attr in options.attrs) {
164 for (var attr in options.attrs) {
165 this.msg_buffer[attr] = options.attrs[attr];
165 this.msg_buffer[attr] = options.attrs[attr];
166 }
166 }
167 } else {
167 } else {
168 this.msg_buffer = $.extend({}, model_json); // Copy
168 this.msg_buffer = $.extend({}, model_json); // Copy
169 }
169 }
170
170
171 } else {
171 } else {
172 // We haven't exceeded the throttle, send the message like
172 // We haven't exceeded the throttle, send the message like
173 // normal. If this is a patch operation, just send the
173 // normal. If this is a patch operation, just send the
174 // changes.
174 // changes.
175 var send_json = model_json;
175 var send_json = model_json;
176 if (method=='patch') {
176 if (method=='patch') {
177 send_json = {};
177 send_json = {};
178 for (var attr in options.attrs) {
178 for (var attr in options.attrs) {
179 send_json[attr] = options.attrs[attr];
179 send_json[attr] = options.attrs[attr];
180 }
180 }
181 }
181 }
182
182
183 var data = {sync_method: method, sync_data: send_json};
183 var data = {sync_method: method, sync_data: send_json};
184
184
185 var cell = null;
185 var cell = null;
186 if (this.last_modified_view != undefined && this.last_modified_view != null) {
186 if (this.last_modified_view != undefined && this.last_modified_view != null) {
187 cell = this.last_modified_view.cell;
187 cell = this.last_modified_view.cell;
188 }
188 }
189
189
190 var callbacks = this._make_callbacks(cell);
190 var callbacks = this._make_callbacks(cell);
191 this.comm.send(data, callbacks);
191 this.comm.send(data, callbacks);
192 this.pending_msgs++;
192 this.pending_msgs++;
193 }
193 }
194 }
194 }
195
195
196 // Since the comm is a one-way communication, assume the message
196 // Since the comm is a one-way communication, assume the message
197 // arrived.
197 // arrived.
198 return model_json;
198 return model_json;
199 },
199 },
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, cell) {
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[cell];
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, cell);
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;
218 }
218 }
219 }
219 }
220 }
220 }
221
221
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, cell);
225 var view = this._create_view(view_name, cell);
226 new_views.push(view);
226 new_views.push(view);
227
227
228 if (cell.widget_subarea != undefined && cell.widget_subarea != null) {
228 if (cell.widget_subarea != undefined && cell.widget_subarea != null) {
229 cell.widget_area.show();
229 cell.widget_area.show();
230 cell.widget_subarea.append(view.$el);
230 cell.widget_subarea.append(view.$el);
231 }
231 }
232 }
232 }
233
233
234 for (var view_index in new_views) {
234 for (var view_index in new_views) {
235 var view = new_views[view_index];
235 var view = new_views[view_index];
236 view.update();
236 view.update();
237 }
237 }
238 },
238 },
239
239
240
240
241 // Create a view
241 // Create a view
242 _create_view: function (view_name, cell) {
242 _create_view: function (view_name, cell) {
243 var view = new this.widget_manager.widget_view_types[view_name]({model: this});
243 var view = new this.widget_manager.widget_view_types[view_name]({model: this});
244 view.render();
244 view.render();
245 if (this.views[cell]==undefined) {
245 if (this.views[cell]==undefined) {
246 this.views[cell] = []
246 this.views[cell] = []
247 }
247 }
248 this.views[cell].push(view);
248 this.views[cell].push(view);
249 view.cell = cell;
249 view.cell = cell;
250
250
251 // Handle when the view element is remove from the page.
251 // Handle when the view element is remove from the page.
252 var that = this;
252 var that = this;
253 view.$el.on("remove", function(){
253 view.$el.on("remove", function(){
254 var index = that.views[cell].indexOf(view);
254 var index = that.views[cell].indexOf(view);
255 if (index > -1) {
255 if (index > -1) {
256 that.views[cell].splice(index, 1);
256 that.views[cell].splice(index, 1);
257 }
257 }
258 view.remove(); // Clean-up view
258 view.remove(); // Clean-up view
259 if (that.views[cell].length()==0) {
259 if (that.views[cell].length()==0) {
260 delete that.views[cell];
260 delete that.views[cell];
261 }
261 }
262
262
263 // Close the comm if there are no views left.
263 // Close the comm if there are no views left.
264 if (that.views.length()==0) {
264 if (that.views.length()==0) {
265 that.comm.close();
265 that.comm.close();
266 }
266 }
267 });
267 });
268 return view;
268 return view;
269 },
269 },
270
270
271
271
272 // Build a callback dict.
272 // Build a callback dict.
273 _make_callbacks: function (cell) {
273 _make_callbacks: function (cell) {
274 var callbacks = {};
274 var callbacks = {};
275 if (cell != null && cell.output_area != undefined && cell.output_area != null) {
275 if (cell != null && cell.output_area != undefined && cell.output_area != null) {
276 var that = this;
276 var that = this;
277 callbacks = {
277 callbacks = {
278 iopub : {
278 iopub : {
279 output : $.proxy(cell.output_area.handle_output, cell.output_area),
279 output : $.proxy(cell.output_area.handle_output, cell.output_area),
280 clear_output : $.proxy(cell.output_area.handle_clear_output, cell.output_area),
280 clear_output : $.proxy(cell.output_area.handle_clear_output, cell.output_area),
281 status : function(msg){
281 status : function(msg){
282 that._handle_status(cell, msg);
282 that._handle_status(cell, msg);
283 },
283 },
284 get_cell : function() {
284 get_cell : function() {
285 if (that.last_modified_view != undefined &&
285 if (that.last_modified_view != undefined &&
286 that.last_modified_view.cell != undefined) {
286 that.last_modified_view.cell != undefined) {
287 return that.last_modified_view.cell;
287 return that.last_modified_view.cell;
288 } else {
288 } else {
289 return null
289 return null
290 }
290 }
291 },
291 },
292 },
292 },
293 };
293 };
294 }
294 }
295 return callbacks;
295 return callbacks;
296 },
296 },
297
297
298
298
299 // Get the output area corresponding to the msg_id.
299 // Get the output area corresponding to the msg_id.
300 // cell is an instance of IPython.Cell
300 // cell is an instance of IPython.Cell
301 _get_msg_cell: function (msg_id) {
301 _get_msg_cell: function (msg_id) {
302
302
303 // 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.
304 var cell = this.widget_manager.get_msg_cell(msg_id);
304 var cell = this.widget_manager.get_msg_cell(msg_id);
305 if (cell != null) {
305 if (cell != null) {
306 return cell;
306 return cell;
307 }
307 }
308
308
309 // Second, check to see if a get_cell callback was defined
309 // Second, check to see if a get_cell callback was defined
310 // for the message. get_cell callbacks are registered for
310 // for the message. get_cell callbacks are registered for
311 // 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
312 // message was triggered by a widget.
312 // message was triggered by a widget.
313 var kernel = this.comm_manager.kernel;
313 var kernel = this.comm_manager.kernel;
314 var callbacks = kernel.get_callbacks_for_msg(msg_id);
314 var callbacks = kernel.get_callbacks_for_msg(msg_id);
315 if (callbacks != undefined &&
315 if (callbacks != undefined &&
316 callbacks.iopub != undefined &&
316 callbacks.iopub != undefined &&
317 callbacks.iopub.get_cell != undefined) {
317 callbacks.iopub.get_cell != undefined) {
318
318
319 return callbacks.iopub.get_cell();
319 return callbacks.iopub.get_cell();
320 }
320 }
321
321
322 // Not triggered by a cell or widget (no get_cell callback
322 // Not triggered by a cell or widget (no get_cell callback
323 // exists).
323 // exists).
324 return null;
324 return null;
325 },
325 },
326
326
327 });
327 });
328
328
329
329
330 //--------------------------------------------------------------------
330 //--------------------------------------------------------------------
331 // WidgetView class
331 // WidgetView class
332 //--------------------------------------------------------------------
332 //--------------------------------------------------------------------
333 var WidgetView = Backbone.View.extend({
333 var WidgetView = Backbone.View.extend({
334
334
335 initialize: function() {
335 initialize: function() {
336 this.visible = true;
336 this.visible = true;
337 this.model.on('change',this.update,this);
337 this.model.on('change',this.update,this);
338 this._add_class_calls = this.model.get('_add_class')[0];
338 this._add_class_calls = this.model.get('_add_class')[0];
339 this._remove_class_calls = this.model.get('_remove_class')[0];
339 this._remove_class_calls = this.model.get('_remove_class')[0];
340 },
340 },
341
341
342 update: function() {
342 update: function() {
343 if (this.model.get('visible') != undefined) {
343 if (this.model.get('visible') != undefined) {
344 if (this.visible != this.model.get('visible')) {
344 if (this.visible != this.model.get('visible')) {
345 this.visible = this.model.get('visible');
345 this.visible = this.model.get('visible');
346 if (this.visible) {
346 if (this.visible) {
347 this.$el.show();
347 this.$el.show();
348 } else {
348 } else {
349 this.$el.hide();
349 this.$el.hide();
350 }
350 }
351 }
351 }
352 }
352 }
353
353
354 if (this.model.css != undefined) {
354 if (this.model.css != undefined) {
355 for (var selector in this.model.css) {
355 for (var selector in this.model.css) {
356 if (this.model.css.hasOwnProperty(selector)) {
356 if (this.model.css.hasOwnProperty(selector)) {
357
357
358 // Apply the css traits to all elements that match the selector.
358 // Apply the css traits to all elements that match the selector.
359 var elements = this._get_selector_element(selector);
359 var elements = this._get_selector_element(selector);
360 if (elements.length > 0) {
360 if (elements.length > 0) {
361 var css_traits = this.model.css[selector];
361 var css_traits = this.model.css[selector];
362 for (var css_key in css_traits) {
362 for (var css_key in css_traits) {
363 if (css_traits.hasOwnProperty(css_key)) {
363 if (css_traits.hasOwnProperty(css_key)) {
364 elements.css(css_key, css_traits[css_key]);
364 elements.css(css_key, css_traits[css_key]);
365 }
365 }
366 }
366 }
367 }
367 }
368 }
368 }
369 }
369 }
370 }
370 }
371
371
372 var add_class = this.model.get('_add_class');
372 var add_class = this.model.get('_add_class');
373 if (add_class != undefined){
373 if (add_class != undefined){
374 var add_class_calls = add_class[0];
374 var add_class_calls = add_class[0];
375 if (add_class_calls > this._add_class_calls) {
375 if (add_class_calls > this._add_class_calls) {
376 this._add_class_calls = add_class_calls;
376 this._add_class_calls = add_class_calls;
377 var elements = this._get_selector_element(add_class[1]);
377 var elements = this._get_selector_element(add_class[1]);
378 if (elements.length > 0) {
378 if (elements.length > 0) {
379 elements.addClass(add_class[2]);
379 elements.addClass(add_class[2]);
380 }
380 }
381 }
381 }
382 }
382 }
383
383
384 var remove_class = this.model.get('_remove_class');
384 var remove_class = this.model.get('_remove_class');
385 if (remove_class != undefined){
385 if (remove_class != undefined){
386 var remove_class_calls = remove_class[0];
386 var remove_class_calls = remove_class[0];
387 if (remove_class_calls > this._remove_class_calls) {
387 if (remove_class_calls > this._remove_class_calls) {
388 this._remove_class_calls = remove_class_calls;
388 this._remove_class_calls = remove_class_calls;
389 var elements = this._get_selector_element(remove_class[1]);
389 var elements = this._get_selector_element(remove_class[1]);
390 if (elements.length > 0) {
390 if (elements.length > 0) {
391 elements.removeClass(remove_class[2]);
391 elements.removeClass(remove_class[2]);
392 }
392 }
393 }
393 }
394 }
394 }
395 },
395 },
396
396
397 _get_selector_element: function(selector) {
397 _get_selector_element: function(selector) {
398 // Get the elements via the css selector. If the selector is
398 // Get the elements via the css selector. If the selector is
399 // blank, apply the style to the $el_to_style element. If
399 // blank, apply the style to the $el_to_style element. If
400 // the $el_to_style element is not defined, use apply the
400 // the $el_to_style element is not defined, use apply the
401 // style to the view's element.
401 // style to the view's element.
402 var elements = this.$el.find(selector);
402 var elements = this.$el.find(selector);
403 if (selector=='') {
403 if (selector=='') {
404 if (this.$el_to_style == undefined) {
404 if (this.$el_to_style == undefined) {
405 elements = this.$el;
405 elements = this.$el;
406 } else {
406 } else {
407 elements = this.$el_to_style;
407 elements = this.$el_to_style;
408 }
408 }
409 }
409 }
410 return elements;
410 return elements;
411 },
411 },
412 });
412 });
413
413
414
414
415 //--------------------------------------------------------------------
415 //--------------------------------------------------------------------
416 // WidgetManager class
416 // WidgetManager class
417 //--------------------------------------------------------------------
417 //--------------------------------------------------------------------
418 var WidgetManager = function(){
418 var WidgetManager = function(){
419 this.comm_manager = null;
419 this.comm_manager = null;
420 this.widget_model_types = {};
420 this.widget_model_types = {};
421 this.widget_view_types = {};
421 this.widget_view_types = {};
422
422
423 var that = this;
423 var that = this;
424 Backbone.sync = function(method, model, options, error) {
424 Backbone.sync = function(method, model, options, error) {
425 var result = model._handle_sync(method, options);
425 var result = model._handle_sync(method, options);
426 if (options.success) {
426 if (options.success) {
427 options.success(result);
427 options.success(result);
428 }
428 }
429 };
429 };
430 }
430 }
431
431
432
432
433 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
433 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
434 this.comm_manager = comm_manager;
434 this.comm_manager = comm_manager;
435
435
436 // Register already register widget model types with the comm manager.
436 // Register already register widget model types with the comm manager.
437 for (var widget_model_name in this.widget_model_types) {
437 for (var widget_model_name in this.widget_model_types) {
438 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
438 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
439 }
439 }
440 }
440 }
441
441
442
442
443 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
443 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
444 // Register the widget with the comm manager. Make sure to pass this object's context
444 // Register the widget with the comm manager. Make sure to pass this object's context
445 // in so `this` works in the call back.
445 // in so `this` works in the call back.
446 if (this.comm_manager!=null) {
446 if (this.comm_manager!=null) {
447 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
447 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
448 }
448 }
449 this.widget_model_types[widget_model_name] = widget_model_type;
449 this.widget_model_types[widget_model_name] = widget_model_type;
450 }
450 }
451
451
452
452
453 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
453 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
454 this.widget_view_types[widget_view_name] = widget_view_type;
454 this.widget_view_types[widget_view_name] = widget_view_type;
455 }
455 }
456
456
457
457
458 WidgetManager.prototype.get_msg_cell = function (msg_id) {
458 WidgetManager.prototype.get_msg_cell = function (msg_id) {
459 if (IPython.notebook != undefined && IPython.notebook != null) {
459 if (IPython.notebook != undefined && IPython.notebook != null) {
460 return IPython.notebook.get_msg_cell(msg_id);
460 return IPython.notebook.get_msg_cell(msg_id);
461 }
461 }
462 }
462 }
463
464
465 WidgetManager.prototype.on_create_widget = function (callback) {
466 this._create_widget_callback = callback;
467 }
468
469
470 WidgetManager.prototype._handle_create_widget = function (widget_model) {
471 if (this._create_widget_callback) {
472 try {
473 this._create_widget_callback(widget_model);
474 } catch (e) {
475 console.log("Exception in WidgetManager callback", e, widget_model);
476 }
477 }
478 }
463
479
464
480
465 WidgetManager.prototype._handle_com_open = function (comm, msg) {
481 WidgetManager.prototype._handle_com_open = function (comm, msg) {
466 var widget_type_name = msg.content.target_name;
482 var widget_type_name = msg.content.target_name;
467 var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this);
483 var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this);
484 this._handle_create_widget(widget_model);
468 }
485 }
469
486
470
487
471 //--------------------------------------------------------------------
488 //--------------------------------------------------------------------
472 // Init code
489 // Init code
473 //--------------------------------------------------------------------
490 //--------------------------------------------------------------------
474 IPython.WidgetManager = WidgetManager;
491 IPython.WidgetManager = WidgetManager;
475 IPython.WidgetModel = WidgetModel;
492 IPython.WidgetModel = WidgetModel;
476 IPython.WidgetView = WidgetView;
493 IPython.WidgetView = WidgetView;
477
494
478 if (IPython.widget_manager==undefined || IPython.widget_manager==null) {
495 if (IPython.widget_manager==undefined || IPython.widget_manager==null) {
479 IPython.widget_manager = new WidgetManager();
496 IPython.widget_manager = new WidgetManager();
480 }
497 }
481
498
482 return IPython.widget_manager;
499 return IPython.widget_manager;
483 });
500 });
General Comments 0
You need to be logged in to leave comments. Login now