##// END OF EJS Templates
Added missing parameter to get_msg_cell call
Jonathan Frederic -
Show More
@@ -1,485 +1,485 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(){
25 ], function(){
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_view_types) {
32 constructor: function(comm_manager, comm, widget_view_types) {
33 this.comm_manager = comm_manager;
33 this.comm_manager = comm_manager;
34 this.widget_view_types = widget_view_types;
34 this.widget_view_types = widget_view_types;
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 output_area in this.views) {
56 for (var output_area in this.views) {
57 var views = this.views[output_area];
57 var views = this.views[output_area];
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_status: function (output_area, msg) {
68 handle_status: function (output_area, 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
72 // Send buffer if this message caused another message to be
72 // Send buffer if this message caused another message to be
73 // throttled.
73 // throttled.
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 output_area = this._get_output_area(msg.parent_header.msg_id);
78 var callbacks = this._make_callbacks(output_area);
78 var callbacks = this._make_callbacks(output_area);
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;
82 } else {
82 } else {
83
83
84 // Only decrease the pending message count if the buffer
84 // Only decrease the pending message count if the buffer
85 // doesn't get flushed (sent).
85 // doesn't get flushed (sent).
86 --this.pending_msgs;
86 --this.pending_msgs;
87 }
87 }
88 }
88 }
89 },
89 },
90
90
91
91
92 // Custom syncronization logic.
92 // Custom syncronization logic.
93 handle_sync: function (method, options) {
93 handle_sync: function (method, options) {
94 var model_json = this.toJSON();
94 var model_json = this.toJSON();
95
95
96 // Only send updated state if the state hasn't been changed
96 // Only send updated state if the state hasn't been changed
97 // during an update.
97 // during an update.
98 if (!this.updating) {
98 if (!this.updating) {
99 if (this.pending_msgs >= this.msg_throttle) {
99 if (this.pending_msgs >= this.msg_throttle) {
100 // The throttle has been exceeded, buffer the current msg so
100 // The throttle has been exceeded, buffer the current msg so
101 // it can be sent once the kernel has finished processing
101 // it can be sent once the kernel has finished processing
102 // some of the existing messages.
102 // some of the existing messages.
103 if (method=='patch') {
103 if (method=='patch') {
104 if (this.msg_buffer == null) {
104 if (this.msg_buffer == null) {
105 this.msg_buffer = $.extend({}, model_json); // Copy
105 this.msg_buffer = $.extend({}, model_json); // Copy
106 }
106 }
107 for (var attr in options.attrs) {
107 for (var attr in options.attrs) {
108 this.msg_buffer[attr] = options.attrs[attr];
108 this.msg_buffer[attr] = options.attrs[attr];
109 }
109 }
110 } else {
110 } else {
111 this.msg_buffer = $.extend({}, model_json); // Copy
111 this.msg_buffer = $.extend({}, model_json); // Copy
112 }
112 }
113
113
114 } else {
114 } else {
115 // We haven't exceeded the throttle, send the message like
115 // We haven't exceeded the throttle, send the message like
116 // normal. If this is a patch operation, just send the
116 // normal. If this is a patch operation, just send the
117 // changes.
117 // changes.
118 var send_json = model_json;
118 var send_json = model_json;
119 if (method=='patch') {
119 if (method=='patch') {
120 send_json = {};
120 send_json = {};
121 for (var attr in options.attrs) {
121 for (var attr in options.attrs) {
122 send_json[attr] = options.attrs[attr];
122 send_json[attr] = options.attrs[attr];
123 }
123 }
124 }
124 }
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 output_area = 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 output_area = this.last_modified_view.output_area;
131 }
131 }
132
132
133 var callbacks = this._make_callbacks(output_area);
133 var callbacks = this._make_callbacks(output_area);
134 this.comm.send(data, callbacks);
134 this.comm.send(data, callbacks);
135 this.pending_msgs++;
135 this.pending_msgs++;
136 }
136 }
137 }
137 }
138
138
139 // Since the comm is a one-way communication, assume the message
139 // Since the comm is a one-way communication, assume the message
140 // arrived.
140 // arrived.
141 return model_json;
141 return model_json;
142 },
142 },
143
143
144
144
145 // Handle incomming comm msg.
145 // Handle incomming comm msg.
146 handle_comm_msg: function (msg) {
146 handle_comm_msg: function (msg) {
147 var method = msg.content.data.method;
147 var method = msg.content.data.method;
148 switch (method){
148 switch (method){
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 output_area = this._get_output_area(msg.parent_header.msg_id);
153 if (output_area == null) {
153 if (output_area == 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 output_area);
160 }
160 }
161 break;
161 break;
162 case 'update':
162 case 'update':
163 this.handle_update(msg.content.data.state);
163 this.handle_update(msg.content.data.state);
164 break;
164 break;
165 }
165 }
166 },
166 },
167
167
168
168
169 // Handle when a widget is updated via the python side.
169 // Handle when a widget is updated via the python side.
170 handle_update: function (state) {
170 handle_update: function (state) {
171 this.updating = true;
171 this.updating = true;
172 try {
172 try {
173 for (var key in state) {
173 for (var key in state) {
174 if (state.hasOwnProperty(key)) {
174 if (state.hasOwnProperty(key)) {
175 if (key == "_css"){
175 if (key == "_css"){
176 this.css = state[key];
176 this.css = state[key];
177 } else {
177 } else {
178 this.set(key, state[key]);
178 this.set(key, state[key]);
179 }
179 }
180 }
180 }
181 }
181 }
182 this.id = this.comm.comm_id;
182 this.id = this.comm.comm_id;
183 this.save();
183 this.save();
184 } finally {
184 } finally {
185 this.updating = false;
185 this.updating = false;
186 }
186 }
187 },
187 },
188
188
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 output_area in this.views) {
193 var views = this.views[output_area];
193 var views = this.views[output_area];
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();
197 }
197 }
198 }
198 }
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, output_area) {
203 display_view: function (view_name, parent_comm_id, output_area) {
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[output_area];
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, output_area);
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, output_area);
225 var view = this._create_view(view_name, output_area);
226 new_views.push(view);
226 new_views.push(view);
227 this._get_widget_area_element(output_area, true)
227 this._get_widget_area_element(output_area, true)
228 .append(view.$el);
228 .append(view.$el);
229
229
230 }
230 }
231
231
232 for (var view_index in new_views) {
232 for (var view_index in new_views) {
233 var view = new_views[view_index];
233 var view = new_views[view_index];
234 view.update();
234 view.update();
235 }
235 }
236 },
236 },
237
237
238
238
239 // Create a view
239 // Create a view
240 _create_view: function (view_name, output_area) {
240 _create_view: function (view_name, output_area) {
241 var view = new this.widget_view_types[view_name]({model: this});
241 var view = new this.widget_view_types[view_name]({model: this});
242 view.render();
242 view.render();
243 if (this.views[output_area]==undefined) {
243 if (this.views[output_area]==undefined) {
244 this.views[output_area] = []
244 this.views[output_area] = []
245 }
245 }
246 this.views[output_area].push(view);
246 this.views[output_area].push(view);
247 view.output_area = output_area;
247 view.output_area = output_area;
248
248
249 // Handle when the view element is remove from the page.
249 // Handle when the view element is remove from the page.
250 var that = this;
250 var that = this;
251 view.$el.on("remove", function(){
251 view.$el.on("remove", function(){
252 var index = that.views[output_area].indexOf(view);
252 var index = that.views[output_area].indexOf(view);
253 if (index > -1) {
253 if (index > -1) {
254 that.views[output_area].splice(index, 1);
254 that.views[output_area].splice(index, 1);
255 }
255 }
256 view.remove(); // Clean-up view
256 view.remove(); // Clean-up view
257 if (that.views[output_area].length()==0) {
257 if (that.views[output_area].length()==0) {
258 delete that.views[output_area];
258 delete that.views[output_area];
259 }
259 }
260
260
261 // Close the comm if there are no views left.
261 // Close the comm if there are no views left.
262 if (that.views.length()==0) {
262 if (that.views.length()==0) {
263 that.comm.close();
263 that.comm.close();
264 }
264 }
265 });
265 });
266 return view;
266 return view;
267 },
267 },
268
268
269
269
270 // Build a callback dict.
270 // Build a callback dict.
271 _make_callbacks: function (output_area) {
271 _make_callbacks: function (output_area) {
272 var callbacks = {};
272 var callbacks = {};
273 if (output_area != null) {
273 if (output_area != null) {
274 var that = this;
274 var that = this;
275 callbacks = {
275 callbacks = {
276 iopub : {
276 iopub : {
277 output : $.proxy(output_area.handle_output, output_area),
277 output : $.proxy(output_area.handle_output, output_area),
278 clear_output : $.proxy(output_area.handle_clear_output, output_area),
278 clear_output : $.proxy(output_area.handle_clear_output, output_area),
279 status : function(msg){
279 status : function(msg){
280 that.handle_status(output_area, msg);
280 that.handle_status(output_area, msg);
281 },
281 },
282 get_output_area : function() {
282 get_output_area : function() {
283 if (that.last_modified_view != undefined &&
283 if (that.last_modified_view != undefined &&
284 that.last_modified_view.output_area != undefined) {
284 that.last_modified_view.output_area != undefined) {
285 return that.last_modified_view.output_area;
285 return that.last_modified_view.output_area;
286 } else {
286 } else {
287 return null
287 return null
288 }
288 }
289 },
289 },
290 },
290 },
291 };
291 };
292 }
292 }
293 return callbacks;
293 return callbacks;
294 },
294 },
295
295
296
296
297 // Get the output area corresponding to the msg_id.
297 // Get the output area corresponding to the msg_id.
298 // output_area is an instance of Ipython.OutputArea
298 // output_area is an instance of Ipython.OutputArea
299 _get_output_area: function (msg_id) {
299 _get_output_area: function (msg_id) {
300
300
301 // First, check to see if the msg was triggered by cell execution.
301 // First, check to see if the msg was triggered by cell execution.
302 var cell = IPython.notebook.get_msg_cell();
302 var cell = IPython.notebook.get_msg_cell(msg_id);
303 if (cell != null) {
303 if (cell != null) {
304 return cell.output_area;
304 return cell.output_area;
305 }
305 }
306
306
307 // Second, check to see if a get_output_area callback was defined
307 // Second, check to see if a get_output_area callback was defined
308 // for the message. get_output_area callbacks are registered for
308 // for the message. get_output_area callbacks are registered for
309 // widget messages, so this block is actually checking to see if the
309 // widget messages, so this block is actually checking to see if the
310 // message was triggered by a widget.
310 // message was triggered by a widget.
311 var kernel = this.comm_manager.kernel;
311 var kernel = this.comm_manager.kernel;
312 var callbacks = kernel.get_callbacks_for_msg(msg_id);
312 var callbacks = kernel.get_callbacks_for_msg(msg_id);
313 if (callbacks != undefined &&
313 if (callbacks != undefined &&
314 callbacks.iopub != undefined &&
314 callbacks.iopub != undefined &&
315 callbacks.iopub.get_output_area != undefined) {
315 callbacks.iopub.get_output_area != undefined) {
316
316
317 var output_area = callbacks.iopub.get_output_area();
317 var output_area = callbacks.iopub.get_output_area();
318 if (output_area != null) {
318 if (output_area != null) {
319 return output_area;
319 return output_area;
320 }
320 }
321 }
321 }
322
322
323 // Not triggered by a cell or widget (no get_output_area callback
323 // Not triggered by a cell or widget (no get_output_area callback
324 // exists).
324 // exists).
325 return null;
325 return null;
326 },
326 },
327
327
328 // Gets widget output area (as a JQuery element) from the
328 // Gets widget output area (as a JQuery element) from the
329 // output_area (Ipython.OutputArea instance)
329 // output_area (Ipython.OutputArea instance)
330 _get_widget_area_element: function (output_area, show) {
330 _get_widget_area_element: function (output_area, show) {
331 var widget_area = output_area.element
331 var widget_area = output_area.element
332 .parent() // output_wrapper
332 .parent() // output_wrapper
333 .parent() // cell
333 .parent() // cell
334 .find('.widget-area');
334 .find('.widget-area');
335 if (show) { widget_area.show(); }
335 if (show) { widget_area.show(); }
336 return widget_area.find('.widget-subarea');
336 return widget_area.find('.widget-subarea');
337 },
337 },
338
338
339 });
339 });
340
340
341
341
342 //--------------------------------------------------------------------
342 //--------------------------------------------------------------------
343 // WidgetView class
343 // WidgetView class
344 //--------------------------------------------------------------------
344 //--------------------------------------------------------------------
345 var WidgetView = Backbone.View.extend({
345 var WidgetView = Backbone.View.extend({
346
346
347 initialize: function() {
347 initialize: function() {
348 this.visible = true;
348 this.visible = true;
349 this.model.on('change',this.update,this);
349 this.model.on('change',this.update,this);
350 this._add_class_calls = this.model.get('_add_class')[0];
350 this._add_class_calls = this.model.get('_add_class')[0];
351 this._remove_class_calls = this.model.get('_remove_class')[0];
351 this._remove_class_calls = this.model.get('_remove_class')[0];
352 },
352 },
353
353
354 update: function() {
354 update: function() {
355 if (this.model.get('visible') != undefined) {
355 if (this.model.get('visible') != undefined) {
356 if (this.visible != this.model.get('visible')) {
356 if (this.visible != this.model.get('visible')) {
357 this.visible = this.model.get('visible');
357 this.visible = this.model.get('visible');
358 if (this.visible) {
358 if (this.visible) {
359 this.$el.show();
359 this.$el.show();
360 } else {
360 } else {
361 this.$el.hide();
361 this.$el.hide();
362 }
362 }
363 }
363 }
364 }
364 }
365
365
366 if (this.model.css != undefined) {
366 if (this.model.css != undefined) {
367 for (var selector in this.model.css) {
367 for (var selector in this.model.css) {
368 if (this.model.css.hasOwnProperty(selector)) {
368 if (this.model.css.hasOwnProperty(selector)) {
369
369
370 // Apply the css traits to all elements that match the selector.
370 // Apply the css traits to all elements that match the selector.
371 var elements = this.get_selector_element(selector);
371 var elements = this.get_selector_element(selector);
372 if (elements.length > 0) {
372 if (elements.length > 0) {
373 var css_traits = this.model.css[selector];
373 var css_traits = this.model.css[selector];
374 for (var css_key in css_traits) {
374 for (var css_key in css_traits) {
375 if (css_traits.hasOwnProperty(css_key)) {
375 if (css_traits.hasOwnProperty(css_key)) {
376 elements.css(css_key, css_traits[css_key]);
376 elements.css(css_key, css_traits[css_key]);
377 }
377 }
378 }
378 }
379 }
379 }
380 }
380 }
381 }
381 }
382 }
382 }
383
383
384 var add_class = this.model.get('_add_class');
384 var add_class = this.model.get('_add_class');
385 if (add_class != undefined){
385 if (add_class != undefined){
386 var add_class_calls = add_class[0];
386 var add_class_calls = add_class[0];
387 if (add_class_calls > this._add_class_calls) {
387 if (add_class_calls > this._add_class_calls) {
388 this._add_class_calls = add_class_calls;
388 this._add_class_calls = add_class_calls;
389 var elements = this.get_selector_element(add_class[1]);
389 var elements = this.get_selector_element(add_class[1]);
390 if (elements.length > 0) {
390 if (elements.length > 0) {
391 elements.addClass(add_class[2]);
391 elements.addClass(add_class[2]);
392 }
392 }
393 }
393 }
394 }
394 }
395
395
396 var remove_class = this.model.get('_remove_class');
396 var remove_class = this.model.get('_remove_class');
397 if (remove_class != undefined){
397 if (remove_class != undefined){
398 var remove_class_calls = remove_class[0];
398 var remove_class_calls = remove_class[0];
399 if (remove_class_calls > this._remove_class_calls) {
399 if (remove_class_calls > this._remove_class_calls) {
400 this._remove_class_calls = remove_class_calls;
400 this._remove_class_calls = remove_class_calls;
401 var elements = this.get_selector_element(remove_class[1]);
401 var elements = this.get_selector_element(remove_class[1]);
402 if (elements.length > 0) {
402 if (elements.length > 0) {
403 elements.removeClass(remove_class[2]);
403 elements.removeClass(remove_class[2]);
404 }
404 }
405 }
405 }
406 }
406 }
407 },
407 },
408
408
409 get_selector_element: function(selector) {
409 get_selector_element: function(selector) {
410 // Get the elements via the css selector. If the selector is
410 // Get the elements via the css selector. If the selector is
411 // blank, apply the style to the $el_to_style element. If
411 // blank, apply the style to the $el_to_style element. If
412 // the $el_to_style element is not defined, use apply the
412 // the $el_to_style element is not defined, use apply the
413 // style to the view's element.
413 // style to the view's element.
414 var elements = this.$el.find(selector);
414 var elements = this.$el.find(selector);
415 if (selector=='') {
415 if (selector=='') {
416 if (this.$el_to_style == undefined) {
416 if (this.$el_to_style == undefined) {
417 elements = this.$el;
417 elements = this.$el;
418 } else {
418 } else {
419 elements = this.$el_to_style;
419 elements = this.$el_to_style;
420 }
420 }
421 }
421 }
422 return elements;
422 return elements;
423 },
423 },
424 });
424 });
425
425
426
426
427 //--------------------------------------------------------------------
427 //--------------------------------------------------------------------
428 // WidgetManager class
428 // WidgetManager class
429 //--------------------------------------------------------------------
429 //--------------------------------------------------------------------
430 var WidgetManager = function(){
430 var WidgetManager = function(){
431 this.comm_manager = null;
431 this.comm_manager = null;
432 this.widget_model_types = {};
432 this.widget_model_types = {};
433 this.widget_view_types = {};
433 this.widget_view_types = {};
434
434
435 var that = this;
435 var that = this;
436 Backbone.sync = function(method, model, options, error) {
436 Backbone.sync = function(method, model, options, error) {
437 var result = model.handle_sync(method, options);
437 var result = model.handle_sync(method, options);
438 if (options.success) {
438 if (options.success) {
439 options.success(result);
439 options.success(result);
440 }
440 }
441 };
441 };
442 }
442 }
443
443
444
444
445 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
445 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
446 this.comm_manager = comm_manager;
446 this.comm_manager = comm_manager;
447
447
448 // Register already register widget model types with the comm manager.
448 // Register already register widget model types with the comm manager.
449 for (var widget_model_name in this.widget_model_types) {
449 for (var widget_model_name in this.widget_model_types) {
450 this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this));
450 this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this));
451 }
451 }
452 }
452 }
453
453
454
454
455 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
455 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
456 // Register the widget with the comm manager. Make sure to pass this object's context
456 // Register the widget with the comm manager. Make sure to pass this object's context
457 // in so `this` works in the call back.
457 // in so `this` works in the call back.
458 if (this.comm_manager!=null) {
458 if (this.comm_manager!=null) {
459 this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this));
459 this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this));
460 }
460 }
461 this.widget_model_types[widget_model_name] = widget_model_type;
461 this.widget_model_types[widget_model_name] = widget_model_type;
462 }
462 }
463
463
464
464
465 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
465 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
466 this.widget_view_types[widget_view_name] = widget_view_type;
466 this.widget_view_types[widget_view_name] = widget_view_type;
467 }
467 }
468
468
469
469
470 WidgetManager.prototype.handle_com_open = function (comm, msg) {
470 WidgetManager.prototype.handle_com_open = function (comm, msg) {
471 var widget_type_name = msg.content.target_name;
471 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);
472 var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this.widget_view_types);
473 }
473 }
474
474
475
475
476 //--------------------------------------------------------------------
476 //--------------------------------------------------------------------
477 // Init code
477 // Init code
478 //--------------------------------------------------------------------
478 //--------------------------------------------------------------------
479 IPython.WidgetManager = WidgetManager;
479 IPython.WidgetManager = WidgetManager;
480 IPython.WidgetModel = WidgetModel;
480 IPython.WidgetModel = WidgetModel;
481 IPython.WidgetView = WidgetView;
481 IPython.WidgetView = WidgetView;
482
482
483 IPython.widget_manager = new WidgetManager();
483 IPython.widget_manager = new WidgetManager();
484
484
485 });
485 });
General Comments 0
You need to be logged in to leave comments. Login now