##// END OF EJS Templates
Missing view argument when recursively calling widgetmanager.callbacks(view)
Jonathan Frederic -
Show More
@@ -1,279 +1,279 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 (function () {
18 (function () {
19 "use strict";
19 "use strict";
20
20
21 // Use require.js 'define' method so that require.js is intelligent enough to
21 // Use require.js 'define' method so that require.js is intelligent enough to
22 // syncronously load everything within this file when it is being 'required'
22 // syncronously load everything within this file when it is being 'required'
23 // elsewhere.
23 // elsewhere.
24 define(["underscore",
24 define(["underscore",
25 "backbone",
25 "backbone",
26 ], function (underscore, backbone) {
26 ], function (underscore, backbone) {
27
27
28 //--------------------------------------------------------------------
28 //--------------------------------------------------------------------
29 // WidgetManager class
29 // WidgetManager class
30 //--------------------------------------------------------------------
30 //--------------------------------------------------------------------
31 var WidgetManager = function () {
31 var WidgetManager = function () {
32 this.comm_manager = null;
32 this.comm_manager = null;
33 this.widget_model_types = {};
33 this.widget_model_types = {};
34 this.widget_view_types = {};
34 this.widget_view_types = {};
35 this._model_instances = {};
35 this._model_instances = {};
36
36
37 Backbone.sync = function (method, model, options, error) {
37 Backbone.sync = function (method, model, options, error) {
38 var result = model._handle_sync(method, options);
38 var result = model._handle_sync(method, options);
39 if (options.success) {
39 if (options.success) {
40 options.success(result);
40 options.success(result);
41 }
41 }
42 };
42 };
43 };
43 };
44
44
45
45
46 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
46 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
47 this.comm_manager = comm_manager;
47 this.comm_manager = comm_manager;
48
48
49 // Register already-registered widget model types with the comm manager.
49 // Register already-registered widget model types with the comm manager.
50 for (var widget_model_name in this.widget_model_types) {
50 for (var widget_model_name in this.widget_model_types) {
51 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
51 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
52 }
52 }
53 };
53 };
54
54
55
55
56 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
56 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
57 // Register the widget with the comm manager. Make sure to pass this object's context
57 // Register the widget with the comm manager. Make sure to pass this object's context
58 // in so `this` works in the call back.
58 // in so `this` works in the call back.
59 if (this.comm_manager !== null) {
59 if (this.comm_manager !== null) {
60 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
60 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
61 }
61 }
62 this.widget_model_types[widget_model_name] = widget_model_type;
62 this.widget_model_types[widget_model_name] = widget_model_type;
63 };
63 };
64
64
65
65
66 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
66 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
67 this.widget_view_types[widget_view_name] = widget_view_type;
67 this.widget_view_types[widget_view_name] = widget_view_type;
68 };
68 };
69
69
70
70
71 WidgetManager.prototype.handle_msg = function(msg, model) {
71 WidgetManager.prototype.handle_msg = function(msg, model) {
72 var method = msg.content.data.method;
72 var method = msg.content.data.method;
73 switch (method) {
73 switch (method) {
74 case 'display':
74 case 'display':
75 var cell = this.get_msg_cell(msg.parent_header.msg_id);
75 var cell = this.get_msg_cell(msg.parent_header.msg_id);
76 if (cell === null) {
76 if (cell === null) {
77 console.log("Could not determine where the display" +
77 console.log("Could not determine where the display" +
78 " message was from. Widget will not be displayed");
78 " message was from. Widget will not be displayed");
79 } else {
79 } else {
80 var view = this.create_view(model, msg.content.data.view_name);
80 var view = this.create_view(model, msg.content.data.view_name);
81 if (view !== undefined
81 if (view !== undefined
82 && cell.widget_subarea !== undefined
82 && cell.widget_subarea !== undefined
83 && cell.widget_subarea !== null) {
83 && cell.widget_subarea !== null) {
84
84
85 view.cell = cell;
85 view.cell = cell;
86 cell.widget_area.show();
86 cell.widget_area.show();
87 cell.widget_subarea.append(view.$el);
87 cell.widget_subarea.append(view.$el);
88 }
88 }
89 }
89 }
90 break;
90 break;
91 }
91 }
92 }
92 }
93
93
94 <<<<<<< HEAD
94 <<<<<<< HEAD
95 <<<<<<< HEAD
95 <<<<<<< HEAD
96 WidgetManager.prototype.create_view = function(model, view_name, cell) {
96 WidgetManager.prototype.create_view = function(model, view_name, cell) {
97 =======
97 =======
98 WidgetManager.prototype.create_view = function(model, view_name, options) {
98 WidgetManager.prototype.create_view = function(model, view_name, options) {
99 >>>>>>> Completely remove cell from model and view.
99 >>>>>>> Completely remove cell from model and view.
100 view_name = view_name || model.get('default_view_name');
100 view_name = view_name || model.get('default_view_name');
101 =======
101 =======
102 WidgetManager.prototype.create_view = function(model, view_name, cell, options) {
102 WidgetManager.prototype.create_view = function(model, view_name, cell, options) {
103 view_name = view_name || model.get('default_view_name');
103 view_name = view_name || model.get('default_view_name');
104 >>>>>>> Add widget view options in creating child views
104 >>>>>>> Add widget view options in creating child views
105 var ViewType = this.widget_view_types[view_name];
105 var ViewType = this.widget_view_types[view_name];
106 if (ViewType !== undefined && ViewType !== null) {
106 if (ViewType !== undefined && ViewType !== null) {
107 var view = new ViewType({model: model, widget_manager: this, options: options});
107 var view = new ViewType({model: model, widget_manager: this, options: options});
108 view.render();
108 view.render();
109 model.views.push(view);
109 model.views.push(view);
110 model.on('destroy', view.remove, view);
110 model.on('destroy', view.remove, view);
111 <<<<<<< HEAD
111 <<<<<<< HEAD
112 /*
112 /*
113 // TODO: handle view deletion. Don't forget to delete child views
113 // TODO: handle view deletion. Don't forget to delete child views
114 var that = this;
114 var that = this;
115 view.$el.on("remove", function () {
115 view.$el.on("remove", function () {
116 var index = that.views.indexOf(view);
116 var index = that.views.indexOf(view);
117 if (index > -1) {
117 if (index > -1) {
118 that.views.splice(index, 1);
118 that.views.splice(index, 1);
119 =======
119 =======
120 /*
120 /*
121 // TODO: handle view deletion. Don't forget to delete child views
121 // TODO: handle view deletion. Don't forget to delete child views
122 var that = this;
122 var that = this;
123 view.$el.on("remove", function () {
123 view.$el.on("remove", function () {
124 var index = that.views.indexOf(view);
124 var index = that.views.indexOf(view);
125 if (index > -1) {
125 if (index > -1) {
126 that.views.splice(index, 1);
126 that.views.splice(index, 1);
127 }
127 }
128 view.remove(); // Clean-up view
128 view.remove(); // Clean-up view
129
129
130 // Close the comm if there are no views left.
130 // Close the comm if there are no views left.
131 if (that.views.length() === 0) {
131 if (that.views.length() === 0) {
132 //trigger comm close event?
132 //trigger comm close event?
133 }
133 }
134
134
135
135
136 if (that.comm !== undefined) {
136 if (that.comm !== undefined) {
137 that.comm.close();
137 that.comm.close();
138 delete that.comm.model; // Delete ref so GC will collect widget model.
138 delete that.comm.model; // Delete ref so GC will collect widget model.
139 delete that.comm;
139 delete that.comm;
140 >>>>>>> Add widget view options in creating child views
140 >>>>>>> Add widget view options in creating child views
141 }
141 }
142 view.remove(); // Clean-up view
142 view.remove(); // Clean-up view
143
143
144 // Close the comm if there are no views left.
144 // Close the comm if there are no views left.
145 if (that.views.length() === 0) {
145 if (that.views.length() === 0) {
146 //trigger comm close event?
146 //trigger comm close event?
147 }
147 }
148
148
149
149
150 if (that.comm !== undefined) {
150 if (that.comm !== undefined) {
151 that.comm.close();
151 that.comm.close();
152 delete that.comm.model; // Delete ref so GC will collect widget model.
152 delete that.comm.model; // Delete ref so GC will collect widget model.
153 delete that.comm;
153 delete that.comm;
154 }
154 }
155 delete that.model_id; // Delete id from model so widget manager cleans up.
155 delete that.model_id; // Delete id from model so widget manager cleans up.
156 });
156 });
157 */
157 */
158 return view;
158 return view;
159 }
159 }
160 },
160 },
161
161
162 WidgetManager.prototype.get_msg_cell = function (msg_id) {
162 WidgetManager.prototype.get_msg_cell = function (msg_id) {
163 var cell = null;
163 var cell = null;
164 // First, check to see if the msg was triggered by cell execution.
164 // First, check to see if the msg was triggered by cell execution.
165 if (IPython.notebook !== undefined && IPython.notebook !== null) {
165 if (IPython.notebook !== undefined && IPython.notebook !== null) {
166 cell = IPython.notebook.get_msg_cell(msg_id);
166 cell = IPython.notebook.get_msg_cell(msg_id);
167 }
167 }
168 if (cell !== null) {
168 if (cell !== null) {
169 return cell
169 return cell
170 }
170 }
171 // Second, check to see if a get_cell callback was defined
171 // Second, check to see if a get_cell callback was defined
172 // for the message. get_cell callbacks are registered for
172 // for the message. get_cell callbacks are registered for
173 // widget messages, so this block is actually checking to see if the
173 // widget messages, so this block is actually checking to see if the
174 // message was triggered by a widget.
174 // message was triggered by a widget.
175 var kernel = this.get_kernel();
175 var kernel = this.get_kernel();
176 if (kernel !== undefined && kernel !== null) {
176 if (kernel !== undefined && kernel !== null) {
177 var callbacks = kernel.get_callbacks_for_msg(msg_id);
177 var callbacks = kernel.get_callbacks_for_msg(msg_id);
178 if (callbacks !== undefined &&
178 if (callbacks !== undefined &&
179 callbacks.iopub !== undefined &&
179 callbacks.iopub !== undefined &&
180 callbacks.iopub.get_cell !== undefined) {
180 callbacks.iopub.get_cell !== undefined) {
181
181
182 return callbacks.iopub.get_cell();
182 return callbacks.iopub.get_cell();
183 }
183 }
184 }
184 }
185
185
186 // Not triggered by a cell or widget (no get_cell callback
186 // Not triggered by a cell or widget (no get_cell callback
187 // exists).
187 // exists).
188 return null;
188 return null;
189 };
189 };
190
190
191 WidgetManager.prototype.callbacks = function (view) {
191 WidgetManager.prototype.callbacks = function (view) {
192 // callback handlers specific a view
192 // callback handlers specific a view
193 var callbacks = {};
193 var callbacks = {};
194 var cell = view.cell;
194 var cell = view.cell;
195 if (cell !== null) {
195 if (cell !== null) {
196 // Try to get output handlers
196 // Try to get output handlers
197 var handle_output = null;
197 var handle_output = null;
198 var handle_clear_output = null;
198 var handle_clear_output = null;
199 if (cell.output_area !== undefined && cell.output_area !== null) {
199 if (cell.output_area !== undefined && cell.output_area !== null) {
200 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
200 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
201 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
201 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
202 }
202 }
203
203
204 // Create callback dict using what is known
204 // Create callback dict using what is known
205 var that = this;
205 var that = this;
206 callbacks = {
206 callbacks = {
207 iopub : {
207 iopub : {
208 output : handle_output,
208 output : handle_output,
209 clear_output : handle_clear_output,
209 clear_output : handle_clear_output,
210
210
211 status : function (msg) {
211 status : function (msg) {
212 view.model._handle_status(msg, that.callbacks());
212 view.model._handle_status(msg, that.callbacks(view));
213 },
213 },
214
214
215 // Special function only registered by widget messages.
215 // Special function only registered by widget messages.
216 // Allows us to get the cell for a message so we know
216 // Allows us to get the cell for a message so we know
217 // where to add widgets if the code requires it.
217 // where to add widgets if the code requires it.
218 get_cell : function () {
218 get_cell : function () {
219 return cell;
219 return cell;
220 },
220 },
221 },
221 },
222 };
222 };
223 }
223 }
224 return callbacks;
224 return callbacks;
225 };
225 };
226
226
227
227
228 WidgetManager.prototype.get_model = function (model_id) {
228 WidgetManager.prototype.get_model = function (model_id) {
229 var model = this._model_instances[model_id];
229 var model = this._model_instances[model_id];
230 if (model !== undefined && model.id == model_id) {
230 if (model !== undefined && model.id == model_id) {
231 return model;
231 return model;
232 }
232 }
233 return null;
233 return null;
234 };
234 };
235
235
236
236
237 WidgetManager.prototype.get_kernel = function () {
237 WidgetManager.prototype.get_kernel = function () {
238 if (this.comm_manager === null) {
238 if (this.comm_manager === null) {
239 return null;
239 return null;
240 } else {
240 } else {
241 return this.comm_manager.kernel;
241 return this.comm_manager.kernel;
242 }
242 }
243 };
243 };
244
244
245
245
246 WidgetManager.prototype.on_create_widget = function (callback) {
246 WidgetManager.prototype.on_create_widget = function (callback) {
247 this._create_widget_callback = callback;
247 this._create_widget_callback = callback;
248 };
248 };
249
249
250
250
251 WidgetManager.prototype._handle_create_widget = function (widget_model) {
251 WidgetManager.prototype._handle_create_widget = function (widget_model) {
252 if (this._create_widget_callback) {
252 if (this._create_widget_callback) {
253 try {
253 try {
254 this._create_widget_callback(widget_model);
254 this._create_widget_callback(widget_model);
255 } catch (e) {
255 } catch (e) {
256 console.log("Exception in WidgetManager callback", e, widget_model);
256 console.log("Exception in WidgetManager callback", e, widget_model);
257 }
257 }
258 }
258 }
259 };
259 };
260
260
261
261
262 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
262 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
263 var widget_type_name = msg.content.target_name;
263 var widget_type_name = msg.content.target_name;
264 var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm);
264 var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm);
265 this._model_instances[comm.comm_id] = widget_model; // comm_id == model_id
265 this._model_instances[comm.comm_id] = widget_model; // comm_id == model_id
266 this._handle_create_widget(widget_model);
266 this._handle_create_widget(widget_model);
267 };
267 };
268
268
269 //--------------------------------------------------------------------
269 //--------------------------------------------------------------------
270 // Init code
270 // Init code
271 //--------------------------------------------------------------------
271 //--------------------------------------------------------------------
272 IPython.WidgetManager = WidgetManager;
272 IPython.WidgetManager = WidgetManager;
273 if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
273 if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
274 IPython.widget_manager = new WidgetManager();
274 IPython.widget_manager = new WidgetManager();
275 }
275 }
276
276
277 return IPython.widget_manager;
277 return IPython.widget_manager;
278 });
278 });
279 }());
279 }());
General Comments 0
You need to be logged in to leave comments. Login now