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