##// END OF EJS Templates
Dont err if view name isn't registered.
Jonathan Frederic -
Show More
@@ -1,500 +1,508 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 if (view != null) {
216 parent_view.display_child(view);
216 new_views.push(view);
217 displayed = true;
217 parent_view.display_child(view);
218 displayed = true;
219 }
218 }
220 }
219 }
221 }
220 }
222 }
221
223
222 if (!displayed) {
224 if (!displayed) {
223 // No parent view is defined or exists. Add the view's
225 // No parent view is defined or exists. Add the view's
224 // element to cell's widget div.
226 // element to cell's widget div.
225 var view = this._create_view(view_name, cell);
227 var view = this._create_view(view_name, cell);
226 new_views.push(view);
228 if (view != null) {
229 new_views.push(view);
227
230
228 if (cell.widget_subarea != undefined && cell.widget_subarea != null) {
231 if (cell.widget_subarea != undefined && cell.widget_subarea != null) {
229 cell.widget_area.show();
232 cell.widget_area.show();
230 cell.widget_subarea.append(view.$el);
233 cell.widget_subarea.append(view.$el);
234 }
231 }
235 }
232 }
236 }
233
237
234 for (var view_index in new_views) {
238 for (var view_index in new_views) {
235 var view = new_views[view_index];
239 var view = new_views[view_index];
236 view.update();
240 view.update();
237 }
241 }
238 },
242 },
239
243
240
244
241 // Create a view
245 // Create a view
242 _create_view: function (view_name, cell) {
246 _create_view: function (view_name, cell) {
243 var view = new this.widget_manager.widget_view_types[view_name]({model: this});
247 var view_type = this.widget_manager.widget_view_types[view_name];
244 view.render();
248 if (view_type != undefined && view_type != null) {
245 if (this.views[cell]==undefined) {
249 var view = new view_type({model: this});
246 this.views[cell] = []
250 view.render();
247 }
251 if (this.views[cell]==undefined) {
248 this.views[cell].push(view);
252 this.views[cell] = []
249 view.cell = cell;
250
251 // Handle when the view element is remove from the page.
252 var that = this;
253 view.$el.on("remove", function(){
254 var index = that.views[cell].indexOf(view);
255 if (index > -1) {
256 that.views[cell].splice(index, 1);
257 }
258 view.remove(); // Clean-up view
259 if (that.views[cell].length()==0) {
260 delete that.views[cell];
261 }
253 }
254 this.views[cell].push(view);
255 view.cell = cell;
262
256
263 // Close the comm if there are no views left.
257 // Handle when the view element is remove from the page.
264 if (that.views.length()==0) {
258 var that = this;
265 that.comm.close();
259 view.$el.on("remove", function(){
266 }
260 var index = that.views[cell].indexOf(view);
267 });
261 if (index > -1) {
268 return view;
262 that.views[cell].splice(index, 1);
263 }
264 view.remove(); // Clean-up view
265 if (that.views[cell].length()==0) {
266 delete that.views[cell];
267 }
268
269 // Close the comm if there are no views left.
270 if (that.views.length()==0) {
271 that.comm.close();
272 }
273 });
274 return view;
275 }
276 return null;
269 },
277 },
270
278
271
279
272 // Build a callback dict.
280 // Build a callback dict.
273 _make_callbacks: function (cell) {
281 _make_callbacks: function (cell) {
274 var callbacks = {};
282 var callbacks = {};
275 if (cell != null && cell.output_area != undefined && cell.output_area != null) {
283 if (cell != null && cell.output_area != undefined && cell.output_area != null) {
276 var that = this;
284 var that = this;
277 callbacks = {
285 callbacks = {
278 iopub : {
286 iopub : {
279 output : $.proxy(cell.output_area.handle_output, cell.output_area),
287 output : $.proxy(cell.output_area.handle_output, cell.output_area),
280 clear_output : $.proxy(cell.output_area.handle_clear_output, cell.output_area),
288 clear_output : $.proxy(cell.output_area.handle_clear_output, cell.output_area),
281 status : function(msg){
289 status : function(msg){
282 that._handle_status(cell, msg);
290 that._handle_status(cell, msg);
283 },
291 },
284 get_cell : function() {
292 get_cell : function() {
285 if (that.last_modified_view != undefined &&
293 if (that.last_modified_view != undefined &&
286 that.last_modified_view.cell != undefined) {
294 that.last_modified_view.cell != undefined) {
287 return that.last_modified_view.cell;
295 return that.last_modified_view.cell;
288 } else {
296 } else {
289 return null
297 return null
290 }
298 }
291 },
299 },
292 },
300 },
293 };
301 };
294 }
302 }
295 return callbacks;
303 return callbacks;
296 },
304 },
297
305
298
306
299 // Get the output area corresponding to the msg_id.
307 // Get the output area corresponding to the msg_id.
300 // cell is an instance of IPython.Cell
308 // cell is an instance of IPython.Cell
301 _get_msg_cell: function (msg_id) {
309 _get_msg_cell: function (msg_id) {
302
310
303 // First, check to see if the msg was triggered by cell execution.
311 // First, check to see if the msg was triggered by cell execution.
304 var cell = this.widget_manager.get_msg_cell(msg_id);
312 var cell = this.widget_manager.get_msg_cell(msg_id);
305 if (cell != null) {
313 if (cell != null) {
306 return cell;
314 return cell;
307 }
315 }
308
316
309 // Second, check to see if a get_cell callback was defined
317 // Second, check to see if a get_cell callback was defined
310 // for the message. get_cell callbacks are registered for
318 // for the message. get_cell callbacks are registered for
311 // widget messages, so this block is actually checking to see if the
319 // widget messages, so this block is actually checking to see if the
312 // message was triggered by a widget.
320 // message was triggered by a widget.
313 var kernel = this.comm_manager.kernel;
321 var kernel = this.comm_manager.kernel;
314 var callbacks = kernel.get_callbacks_for_msg(msg_id);
322 var callbacks = kernel.get_callbacks_for_msg(msg_id);
315 if (callbacks != undefined &&
323 if (callbacks != undefined &&
316 callbacks.iopub != undefined &&
324 callbacks.iopub != undefined &&
317 callbacks.iopub.get_cell != undefined) {
325 callbacks.iopub.get_cell != undefined) {
318
326
319 return callbacks.iopub.get_cell();
327 return callbacks.iopub.get_cell();
320 }
328 }
321
329
322 // Not triggered by a cell or widget (no get_cell callback
330 // Not triggered by a cell or widget (no get_cell callback
323 // exists).
331 // exists).
324 return null;
332 return null;
325 },
333 },
326
334
327 });
335 });
328
336
329
337
330 //--------------------------------------------------------------------
338 //--------------------------------------------------------------------
331 // WidgetView class
339 // WidgetView class
332 //--------------------------------------------------------------------
340 //--------------------------------------------------------------------
333 var WidgetView = Backbone.View.extend({
341 var WidgetView = Backbone.View.extend({
334
342
335 initialize: function() {
343 initialize: function() {
336 this.visible = true;
344 this.visible = true;
337 this.model.on('change',this.update,this);
345 this.model.on('change',this.update,this);
338 this._add_class_calls = this.model.get('_add_class')[0];
346 this._add_class_calls = this.model.get('_add_class')[0];
339 this._remove_class_calls = this.model.get('_remove_class')[0];
347 this._remove_class_calls = this.model.get('_remove_class')[0];
340 },
348 },
341
349
342 update: function() {
350 update: function() {
343 if (this.model.get('visible') != undefined) {
351 if (this.model.get('visible') != undefined) {
344 if (this.visible != this.model.get('visible')) {
352 if (this.visible != this.model.get('visible')) {
345 this.visible = this.model.get('visible');
353 this.visible = this.model.get('visible');
346 if (this.visible) {
354 if (this.visible) {
347 this.$el.show();
355 this.$el.show();
348 } else {
356 } else {
349 this.$el.hide();
357 this.$el.hide();
350 }
358 }
351 }
359 }
352 }
360 }
353
361
354 if (this.model.css != undefined) {
362 if (this.model.css != undefined) {
355 for (var selector in this.model.css) {
363 for (var selector in this.model.css) {
356 if (this.model.css.hasOwnProperty(selector)) {
364 if (this.model.css.hasOwnProperty(selector)) {
357
365
358 // Apply the css traits to all elements that match the selector.
366 // Apply the css traits to all elements that match the selector.
359 var elements = this._get_selector_element(selector);
367 var elements = this._get_selector_element(selector);
360 if (elements.length > 0) {
368 if (elements.length > 0) {
361 var css_traits = this.model.css[selector];
369 var css_traits = this.model.css[selector];
362 for (var css_key in css_traits) {
370 for (var css_key in css_traits) {
363 if (css_traits.hasOwnProperty(css_key)) {
371 if (css_traits.hasOwnProperty(css_key)) {
364 elements.css(css_key, css_traits[css_key]);
372 elements.css(css_key, css_traits[css_key]);
365 }
373 }
366 }
374 }
367 }
375 }
368 }
376 }
369 }
377 }
370 }
378 }
371
379
372 var add_class = this.model.get('_add_class');
380 var add_class = this.model.get('_add_class');
373 if (add_class != undefined){
381 if (add_class != undefined){
374 var add_class_calls = add_class[0];
382 var add_class_calls = add_class[0];
375 if (add_class_calls > this._add_class_calls) {
383 if (add_class_calls > this._add_class_calls) {
376 this._add_class_calls = add_class_calls;
384 this._add_class_calls = add_class_calls;
377 var elements = this._get_selector_element(add_class[1]);
385 var elements = this._get_selector_element(add_class[1]);
378 if (elements.length > 0) {
386 if (elements.length > 0) {
379 elements.addClass(add_class[2]);
387 elements.addClass(add_class[2]);
380 }
388 }
381 }
389 }
382 }
390 }
383
391
384 var remove_class = this.model.get('_remove_class');
392 var remove_class = this.model.get('_remove_class');
385 if (remove_class != undefined){
393 if (remove_class != undefined){
386 var remove_class_calls = remove_class[0];
394 var remove_class_calls = remove_class[0];
387 if (remove_class_calls > this._remove_class_calls) {
395 if (remove_class_calls > this._remove_class_calls) {
388 this._remove_class_calls = remove_class_calls;
396 this._remove_class_calls = remove_class_calls;
389 var elements = this._get_selector_element(remove_class[1]);
397 var elements = this._get_selector_element(remove_class[1]);
390 if (elements.length > 0) {
398 if (elements.length > 0) {
391 elements.removeClass(remove_class[2]);
399 elements.removeClass(remove_class[2]);
392 }
400 }
393 }
401 }
394 }
402 }
395 },
403 },
396
404
397 _get_selector_element: function(selector) {
405 _get_selector_element: function(selector) {
398 // Get the elements via the css selector. If the selector is
406 // Get the elements via the css selector. If the selector is
399 // blank, apply the style to the $el_to_style element. If
407 // blank, apply the style to the $el_to_style element. If
400 // the $el_to_style element is not defined, use apply the
408 // the $el_to_style element is not defined, use apply the
401 // style to the view's element.
409 // style to the view's element.
402 var elements = this.$el.find(selector);
410 var elements = this.$el.find(selector);
403 if (selector=='') {
411 if (selector=='') {
404 if (this.$el_to_style == undefined) {
412 if (this.$el_to_style == undefined) {
405 elements = this.$el;
413 elements = this.$el;
406 } else {
414 } else {
407 elements = this.$el_to_style;
415 elements = this.$el_to_style;
408 }
416 }
409 }
417 }
410 return elements;
418 return elements;
411 },
419 },
412 });
420 });
413
421
414
422
415 //--------------------------------------------------------------------
423 //--------------------------------------------------------------------
416 // WidgetManager class
424 // WidgetManager class
417 //--------------------------------------------------------------------
425 //--------------------------------------------------------------------
418 var WidgetManager = function(){
426 var WidgetManager = function(){
419 this.comm_manager = null;
427 this.comm_manager = null;
420 this.widget_model_types = {};
428 this.widget_model_types = {};
421 this.widget_view_types = {};
429 this.widget_view_types = {};
422
430
423 var that = this;
431 var that = this;
424 Backbone.sync = function(method, model, options, error) {
432 Backbone.sync = function(method, model, options, error) {
425 var result = model._handle_sync(method, options);
433 var result = model._handle_sync(method, options);
426 if (options.success) {
434 if (options.success) {
427 options.success(result);
435 options.success(result);
428 }
436 }
429 };
437 };
430 }
438 }
431
439
432
440
433 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
441 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
434 this.comm_manager = comm_manager;
442 this.comm_manager = comm_manager;
435
443
436 // Register already register widget model types with the comm manager.
444 // Register already register widget model types with the comm manager.
437 for (var widget_model_name in this.widget_model_types) {
445 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));
446 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
439 }
447 }
440 }
448 }
441
449
442
450
443 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
451 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
452 // Register the widget with the comm manager. Make sure to pass this object's context
445 // in so `this` works in the call back.
453 // in so `this` works in the call back.
446 if (this.comm_manager!=null) {
454 if (this.comm_manager!=null) {
447 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
455 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
448 }
456 }
449 this.widget_model_types[widget_model_name] = widget_model_type;
457 this.widget_model_types[widget_model_name] = widget_model_type;
450 }
458 }
451
459
452
460
453 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
461 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
454 this.widget_view_types[widget_view_name] = widget_view_type;
462 this.widget_view_types[widget_view_name] = widget_view_type;
455 }
463 }
456
464
457
465
458 WidgetManager.prototype.get_msg_cell = function (msg_id) {
466 WidgetManager.prototype.get_msg_cell = function (msg_id) {
459 if (IPython.notebook != undefined && IPython.notebook != null) {
467 if (IPython.notebook != undefined && IPython.notebook != null) {
460 return IPython.notebook.get_msg_cell(msg_id);
468 return IPython.notebook.get_msg_cell(msg_id);
461 }
469 }
462 }
470 }
463
471
464
472
465 WidgetManager.prototype.on_create_widget = function (callback) {
473 WidgetManager.prototype.on_create_widget = function (callback) {
466 this._create_widget_callback = callback;
474 this._create_widget_callback = callback;
467 }
475 }
468
476
469
477
470 WidgetManager.prototype._handle_create_widget = function (widget_model) {
478 WidgetManager.prototype._handle_create_widget = function (widget_model) {
471 if (this._create_widget_callback) {
479 if (this._create_widget_callback) {
472 try {
480 try {
473 this._create_widget_callback(widget_model);
481 this._create_widget_callback(widget_model);
474 } catch (e) {
482 } catch (e) {
475 console.log("Exception in WidgetManager callback", e, widget_model);
483 console.log("Exception in WidgetManager callback", e, widget_model);
476 }
484 }
477 }
485 }
478 }
486 }
479
487
480
488
481 WidgetManager.prototype._handle_com_open = function (comm, msg) {
489 WidgetManager.prototype._handle_com_open = function (comm, msg) {
482 var widget_type_name = msg.content.target_name;
490 var widget_type_name = msg.content.target_name;
483 var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this);
491 var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this);
484 this._handle_create_widget(widget_model);
492 this._handle_create_widget(widget_model);
485 }
493 }
486
494
487
495
488 //--------------------------------------------------------------------
496 //--------------------------------------------------------------------
489 // Init code
497 // Init code
490 //--------------------------------------------------------------------
498 //--------------------------------------------------------------------
491 IPython.WidgetManager = WidgetManager;
499 IPython.WidgetManager = WidgetManager;
492 IPython.WidgetModel = WidgetModel;
500 IPython.WidgetModel = WidgetModel;
493 IPython.WidgetView = WidgetView;
501 IPython.WidgetView = WidgetView;
494
502
495 if (IPython.widget_manager==undefined || IPython.widget_manager==null) {
503 if (IPython.widget_manager==undefined || IPython.widget_manager==null) {
496 IPython.widget_manager = new WidgetManager();
504 IPython.widget_manager = new WidgetManager();
497 }
505 }
498
506
499 return IPython.widget_manager;
507 return IPython.widget_manager;
500 });
508 });
General Comments 0
You need to be logged in to leave comments. Login now