Show More
@@ -1,436 +1,436 | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | "underscore", |
|
6 | 6 | "backbone", |
|
7 | 7 | "jquery", |
|
8 | 8 | "base/js/utils", |
|
9 | 9 | "base/js/namespace", |
|
10 | 10 | "services/kernels/comm" |
|
11 | 11 | ], function (_, Backbone, $, utils, IPython, comm) { |
|
12 | 12 | "use strict"; |
|
13 | 13 | //-------------------------------------------------------------------- |
|
14 | 14 | // WidgetManager class |
|
15 | 15 | //-------------------------------------------------------------------- |
|
16 | 16 | var WidgetManager = function (comm_manager, notebook) { |
|
17 | 17 | /** |
|
18 | 18 | * Public constructor |
|
19 | 19 | */ |
|
20 | 20 | WidgetManager._managers.push(this); |
|
21 | 21 | |
|
22 | 22 | // Attach a comm manager to the |
|
23 | 23 | this.keyboard_manager = notebook.keyboard_manager; |
|
24 | 24 | this.notebook = notebook; |
|
25 | 25 | this.comm_manager = comm_manager; |
|
26 | 26 | this.comm_target_name = 'ipython.widget'; |
|
27 | 27 | this._models = {}; /* Dictionary of model ids and model instance promises */ |
|
28 | 28 | |
|
29 | 29 | // Register with the comm manager. |
|
30 | 30 | this.comm_manager.register_target(this.comm_target_name, $.proxy(this._handle_comm_open, this)); |
|
31 | 31 | |
|
32 | 32 | // Load the initial state of the widget manager if a load callback was |
|
33 | 33 | // registered. |
|
34 | 34 | if (WidgetManager._load_callback) { |
|
35 | 35 | this.set_state(WidgetManager._load_callback.call(this)); |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | // Setup state saving code. |
|
39 | 39 | var that = this; |
|
40 | 40 | this.notebook.events.on('notebook_saved.Notebook', function() { |
|
41 | 41 | var save_callback = WidgetManager._save_callback; |
|
42 | 42 | var options = WidgetManager._get_state_options; |
|
43 | 43 | if (save_callback) { |
|
44 | 44 | that.get_state(options).then(function(state) { |
|
45 | 45 | save_callback.call(that, state); |
|
46 | }); | |
|
46 | }).catch(utils.reject('Could not call widget save state callback.', true)); | |
|
47 | 47 | } |
|
48 | 48 | }); |
|
49 | 49 | }; |
|
50 | 50 | |
|
51 | 51 | //-------------------------------------------------------------------- |
|
52 | 52 | // Class level |
|
53 | 53 | //-------------------------------------------------------------------- |
|
54 | 54 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ |
|
55 | 55 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ |
|
56 | 56 | WidgetManager._managers = []; /* List of widget managers */ |
|
57 | 57 | WidgetManager._load_callback = null; |
|
58 | 58 | WidgetManager._save_callback = null; |
|
59 | 59 | |
|
60 | 60 | WidgetManager.register_widget_model = function (model_name, model_type) { |
|
61 | 61 | // Registers a widget model by name. |
|
62 | 62 | WidgetManager._model_types[model_name] = model_type; |
|
63 | 63 | }; |
|
64 | 64 | |
|
65 | 65 | WidgetManager.register_widget_view = function (view_name, view_type) { |
|
66 | 66 | // Registers a widget view by name. |
|
67 | 67 | WidgetManager._view_types[view_name] = view_type; |
|
68 | 68 | }; |
|
69 | 69 | |
|
70 | 70 | WidgetManager.set_state_callbacks = function (load_callback, save_callback, options) { |
|
71 | 71 | // Registers callbacks for widget state persistence. |
|
72 | 72 | WidgetManager._load_callback = load_callback; |
|
73 | 73 | WidgetManager._save_callback = save_callback; |
|
74 | 74 | WidgetManager._get_state_options = options; |
|
75 | 75 | |
|
76 | 76 | // Use the load callback to immediately load widget states. |
|
77 | 77 | WidgetManager._managers.forEach(function(manager) { |
|
78 | 78 | if (load_callback) { |
|
79 | 79 | manager.set_state(load_callback.call(manager)); |
|
80 | 80 | } |
|
81 | 81 | }); |
|
82 | 82 | }; |
|
83 | 83 | |
|
84 | 84 | //-------------------------------------------------------------------- |
|
85 | 85 | // Instance level |
|
86 | 86 | //-------------------------------------------------------------------- |
|
87 | 87 | WidgetManager.prototype.display_view = function(msg, model) { |
|
88 | 88 | /** |
|
89 | 89 | * Displays a view for a particular model. |
|
90 | 90 | */ |
|
91 | 91 | var that = this; |
|
92 | 92 | return new Promise(function(resolve, reject) { |
|
93 | 93 | var cell = that.get_msg_cell(msg.parent_header.msg_id); |
|
94 | 94 | if (cell === null) { |
|
95 | 95 | reject(new Error("Could not determine where the display" + |
|
96 | 96 | " message was from. Widget will not be displayed")); |
|
97 | 97 | } else { |
|
98 | 98 | return that.display_view_in_cell(cell, model) |
|
99 | 99 | .catch(function(error) { |
|
100 | 100 | reject(new utils.WrappedError('View could not be displayed.', error)); |
|
101 | 101 | }); |
|
102 | 102 | } |
|
103 | 103 | }); |
|
104 | 104 | }; |
|
105 | 105 | |
|
106 | 106 | WidgetManager.prototype.display_view_in_cell = function(cell, model) { |
|
107 | 107 | // Displays a view in a cell. |
|
108 | 108 | var that = this; |
|
109 | 109 | return new Promise(function(resolve, reject) { |
|
110 | 110 | if (cell.display_widget_view) { |
|
111 | 111 | cell.display_widget_view(that.create_view(model, {cell: cell})) |
|
112 | 112 | .then(function(view) { |
|
113 | 113 | that._handle_display_view(view); |
|
114 | 114 | view.trigger('displayed'); |
|
115 | 115 | resolve(view); |
|
116 | 116 | }, function(error) { |
|
117 | 117 | reject(new utils.WrappedError('Could not create or display view', error)); |
|
118 | 118 | }); |
|
119 | 119 | } else { |
|
120 | 120 | reject(new Error('Cell does not have a `display_widget_view` method')); |
|
121 | 121 | } |
|
122 | 122 | }); |
|
123 | 123 | }; |
|
124 | 124 | |
|
125 | 125 | WidgetManager.prototype._handle_display_view = function (view) { |
|
126 | 126 | /** |
|
127 | 127 | * Have the IPython keyboard manager disable its event |
|
128 | 128 | * handling so the widget can capture keyboard input. |
|
129 | 129 | * Note, this is only done on the outer most widgets. |
|
130 | 130 | */ |
|
131 | 131 | if (this.keyboard_manager) { |
|
132 | 132 | this.keyboard_manager.register_events(view.$el); |
|
133 | 133 | |
|
134 | 134 | if (view.additional_elements) { |
|
135 | 135 | for (var i = 0; i < view.additional_elements.length; i++) { |
|
136 | 136 | this.keyboard_manager.register_events(view.additional_elements[i]); |
|
137 | 137 | } |
|
138 | 138 | } |
|
139 | 139 | } |
|
140 | 140 | }; |
|
141 | 141 | |
|
142 | 142 | WidgetManager.prototype.create_view = function(model, options) { |
|
143 | 143 | /** |
|
144 | 144 | * Creates a promise for a view of a given model |
|
145 | 145 | * |
|
146 | 146 | * Make sure the view creation is not out of order with |
|
147 | 147 | * any state updates. |
|
148 | 148 | */ |
|
149 | 149 | model.state_change = model.state_change.then(function() { |
|
150 | 150 | |
|
151 | 151 | return utils.load_class(model.get('_view_name'), model.get('_view_module'), |
|
152 | 152 | WidgetManager._view_types).then(function(ViewType) { |
|
153 | 153 | |
|
154 | 154 | // If a view is passed into the method, use that view's cell as |
|
155 | 155 | // the cell for the view that is created. |
|
156 | 156 | options = options || {}; |
|
157 | 157 | if (options.parent !== undefined) { |
|
158 | 158 | options.cell = options.parent.options.cell; |
|
159 | 159 | } |
|
160 | 160 | // Create and render the view... |
|
161 | 161 | var parameters = {model: model, options: options}; |
|
162 | 162 | var view = new ViewType(parameters); |
|
163 | 163 | view.listenTo(model, 'destroy', view.remove); |
|
164 | 164 | return Promise.resolve(view.render()).then(function() {return view;}); |
|
165 | 165 | }).catch(utils.reject("Couldn't create a view for model id '" + String(model.id) + "'", true)); |
|
166 | 166 | }); |
|
167 | 167 | model.views[utils.uuid()] = model.state_change; |
|
168 | 168 | return model.state_change; |
|
169 | 169 | }; |
|
170 | 170 | |
|
171 | 171 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
172 | 172 | var cell = null; |
|
173 | 173 | // First, check to see if the msg was triggered by cell execution. |
|
174 | 174 | if (this.notebook) { |
|
175 | 175 | cell = this.notebook.get_msg_cell(msg_id); |
|
176 | 176 | } |
|
177 | 177 | if (cell !== null) { |
|
178 | 178 | return cell; |
|
179 | 179 | } |
|
180 | 180 | // Second, check to see if a get_cell callback was defined |
|
181 | 181 | // for the message. get_cell callbacks are registered for |
|
182 | 182 | // widget messages, so this block is actually checking to see if the |
|
183 | 183 | // message was triggered by a widget. |
|
184 | 184 | var kernel = this.comm_manager.kernel; |
|
185 | 185 | if (kernel) { |
|
186 | 186 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
187 | 187 | if (callbacks && callbacks.iopub && |
|
188 | 188 | callbacks.iopub.get_cell !== undefined) { |
|
189 | 189 | return callbacks.iopub.get_cell(); |
|
190 | 190 | } |
|
191 | 191 | } |
|
192 | 192 | |
|
193 | 193 | // Not triggered by a cell or widget (no get_cell callback |
|
194 | 194 | // exists). |
|
195 | 195 | return null; |
|
196 | 196 | }; |
|
197 | 197 | |
|
198 | 198 | WidgetManager.prototype.callbacks = function (view) { |
|
199 | 199 | /** |
|
200 | 200 | * callback handlers specific a view |
|
201 | 201 | */ |
|
202 | 202 | var callbacks = {}; |
|
203 | 203 | if (view && view.options.cell) { |
|
204 | 204 | |
|
205 | 205 | // Try to get output handlers |
|
206 | 206 | var cell = view.options.cell; |
|
207 | 207 | var handle_output = null; |
|
208 | 208 | var handle_clear_output = null; |
|
209 | 209 | if (cell.output_area) { |
|
210 | 210 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); |
|
211 | 211 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); |
|
212 | 212 | } |
|
213 | 213 | |
|
214 | 214 | // Create callback dictionary using what is known |
|
215 | 215 | var that = this; |
|
216 | 216 | callbacks = { |
|
217 | 217 | iopub : { |
|
218 | 218 | output : handle_output, |
|
219 | 219 | clear_output : handle_clear_output, |
|
220 | 220 | |
|
221 | 221 | // Special function only registered by widget messages. |
|
222 | 222 | // Allows us to get the cell for a message so we know |
|
223 | 223 | // where to add widgets if the code requires it. |
|
224 | 224 | get_cell : function () { |
|
225 | 225 | return cell; |
|
226 | 226 | }, |
|
227 | 227 | }, |
|
228 | 228 | }; |
|
229 | 229 | } |
|
230 | 230 | return callbacks; |
|
231 | 231 | }; |
|
232 | 232 | |
|
233 | 233 | WidgetManager.prototype.get_model = function (model_id) { |
|
234 | 234 | /** |
|
235 | 235 | * Get a promise for a model by model id. |
|
236 | 236 | */ |
|
237 | 237 | return this._models[model_id]; |
|
238 | 238 | }; |
|
239 | 239 | |
|
240 | 240 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
241 | 241 | /** |
|
242 | 242 | * Handle when a comm is opened. |
|
243 | 243 | */ |
|
244 | 244 | return this.create_model({ |
|
245 | 245 | model_name: msg.content.data.model_name, |
|
246 | 246 | model_module: msg.content.data.model_module, |
|
247 | 247 | comm: comm}).catch(utils.reject("Couldn't create a model.", true)); |
|
248 | 248 | }; |
|
249 | 249 | |
|
250 | 250 | WidgetManager.prototype.create_model = function (options) { |
|
251 | 251 | /** |
|
252 | 252 | * Create and return a promise for a new widget model |
|
253 | 253 | * |
|
254 | 254 | * Minimally, one must provide the model_name and widget_class |
|
255 | 255 | * parameters to create a model from Javascript. |
|
256 | 256 | * |
|
257 | 257 | * Example |
|
258 | 258 | * -------- |
|
259 | 259 | * JS: |
|
260 | 260 | * IPython.notebook.kernel.widget_manager.create_model({ |
|
261 | 261 | * model_name: 'WidgetModel', |
|
262 | 262 | * widget_class: 'IPython.html.widgets.widget_int.IntSlider'}) |
|
263 | 263 | * .then(function(model) { console.log('Create success!', model); }, |
|
264 | 264 | * $.proxy(console.error, console)); |
|
265 | 265 | * |
|
266 | 266 | * Parameters |
|
267 | 267 | * ---------- |
|
268 | 268 | * options: dictionary |
|
269 | 269 | * Dictionary of options with the following contents: |
|
270 | 270 | * model_name: string |
|
271 | 271 | * Target name of the widget model to create. |
|
272 | 272 | * model_module: (optional) string |
|
273 | 273 | * Module name of the widget model to create. |
|
274 | 274 | * widget_class: (optional) string |
|
275 | 275 | * Target name of the widget in the back-end. |
|
276 | 276 | * comm: (optional) Comm |
|
277 | 277 | * |
|
278 | 278 | * Create a comm if it wasn't provided. |
|
279 | 279 | */ |
|
280 | 280 | var comm = options.comm; |
|
281 | 281 | if (!comm) { |
|
282 | 282 | comm = this.comm_manager.new_comm('ipython.widget', {'widget_class': options.widget_class}); |
|
283 | 283 | } |
|
284 | 284 | |
|
285 | 285 | var that = this; |
|
286 | 286 | var model_id = comm.comm_id; |
|
287 | 287 | var model_promise = utils.load_class(options.model_name, options.model_module, WidgetManager._model_types) |
|
288 | 288 | .then(function(ModelType) { |
|
289 | 289 | var widget_model = new ModelType(that, model_id, comm); |
|
290 | 290 | widget_model.once('comm:close', function () { |
|
291 | 291 | delete that._models[model_id]; |
|
292 | 292 | }); |
|
293 | 293 | widget_model.name = options.model_name; |
|
294 | 294 | widget_model.module = options.model_module; |
|
295 | 295 | return widget_model; |
|
296 | 296 | |
|
297 | 297 | }, function(error) { |
|
298 | 298 | delete that._models[model_id]; |
|
299 | 299 | var wrapped_error = new utils.WrappedError("Couldn't create model", error); |
|
300 | 300 | return Promise.reject(wrapped_error); |
|
301 | 301 | }); |
|
302 | 302 | this._models[model_id] = model_promise; |
|
303 | 303 | return model_promise; |
|
304 | 304 | }; |
|
305 | 305 | |
|
306 | 306 | WidgetManager.prototype.get_state = function(options) { |
|
307 | 307 | // Asynchronously get the state of the widget manager. |
|
308 | 308 | // |
|
309 | 309 | // This includes all of the widget models and the cells that they are |
|
310 | 310 | // displayed in. |
|
311 | 311 | // |
|
312 | 312 | // Parameters |
|
313 | 313 | // ---------- |
|
314 | 314 | // options: dictionary |
|
315 | 315 | // Dictionary of options with the following contents: |
|
316 | 316 | // only_displayed: (optional) boolean=false |
|
317 | 317 | // Only return models with one or more displayed views. |
|
318 | 318 | // not_alive: (optional) boolean=false |
|
319 | 319 | // Include models that have comms with severed connections. |
|
320 | 320 | // |
|
321 | 321 | // Returns |
|
322 | 322 | // ------- |
|
323 | 323 | // Promise for a state dictionary |
|
324 | 324 | var that = this; |
|
325 | 325 | return utils.resolve_promises_dict(this._models).then(function(models) { |
|
326 | 326 | var state = {}; |
|
327 | 327 | for (var model_id in models) { |
|
328 | 328 | if (models.hasOwnProperty(model_id)) { |
|
329 | 329 | var model = models[model_id]; |
|
330 | 330 | |
|
331 | 331 | // If the model has one or more views defined for it, |
|
332 | 332 | // consider it displayed. |
|
333 | 333 | var displayed_flag = !(options && options.only_displayed) || Object.keys(model.views).length > 0; |
|
334 | 334 | var alive_flag = (options && options.not_alive) || model.comm_alive; |
|
335 | 335 | if (displayed_flag && alive_flag) { |
|
336 | 336 | state[model_id] = { |
|
337 | 337 | model_name: model.name, |
|
338 | 338 | model_module: model.module, |
|
339 | 339 | state: model.get_state(), |
|
340 | 340 | views: [], |
|
341 | 341 | }; |
|
342 | 342 | |
|
343 | 343 | // Get the views that are displayed *now*. |
|
344 | 344 | for (var id in model.views) { |
|
345 | 345 | if (model.views.hasOwnProperty(id)) { |
|
346 | 346 | var view = model.views[id]; |
|
347 | 347 | var cell_index = that.notebook.find_cell_index(view.options.cell); |
|
348 | 348 | state[model_id].views.push(cell_index); |
|
349 | 349 | } |
|
350 | 350 | } |
|
351 | 351 | } |
|
352 | 352 | } |
|
353 | 353 | } |
|
354 | 354 | return state; |
|
355 | 355 | }); |
|
356 | 356 | }; |
|
357 | 357 | |
|
358 | 358 | WidgetManager.prototype.set_state = function(state) { |
|
359 | 359 | // Set the notebook's state. |
|
360 | 360 | // |
|
361 | 361 | // Reconstructs all of the widget models and attempts to redisplay the |
|
362 | 362 | // widgets in the appropriate cells by cell index. |
|
363 | 363 | |
|
364 | 364 | // Get the kernel when it's available. |
|
365 | 365 | var that = this; |
|
366 | 366 | return this._get_connected_kernel().then(function(kernel) { |
|
367 | 367 | |
|
368 | 368 | // Recreate all the widget models for the given state and |
|
369 | 369 | // display the views. |
|
370 | 370 | that.all_views = []; |
|
371 | 371 | var model_ids = Object.keys(state); |
|
372 | 372 | for (var i = 0; i < model_ids.length; i++) { |
|
373 | 373 | var model_id = model_ids[i]; |
|
374 | 374 | |
|
375 | 375 | // Recreate a comm using the widget's model id (model_id == comm_id). |
|
376 | 376 | var new_comm = new comm.Comm(kernel.widget_manager.comm_target_name, model_id); |
|
377 | 377 | kernel.comm_manager.register_comm(new_comm); |
|
378 | 378 | |
|
379 | 379 | // Create the model using the recreated comm. When the model is |
|
380 | 380 | // created we don't know yet if the comm is valid so set_comm_alive |
|
381 | 381 | // false. Once we receive the first state push from the back-end |
|
382 | 382 | // we know the comm is alive. |
|
383 | 383 | var views = kernel.widget_manager.create_model({ |
|
384 | 384 | comm: new_comm, |
|
385 | 385 | model_name: state[model_id].model_name, |
|
386 | 386 | model_module: state[model_id].model_module}) |
|
387 | 387 | .then(function(model) { |
|
388 | 388 | |
|
389 | 389 | model.set_comm_alive(false); |
|
390 | 390 | var view_promise = Promise.resolve().then(function() { |
|
391 | 391 | return model.set_state(state[model.id].state); |
|
392 | 392 | }).then(function() { |
|
393 | 393 | model.request_state().then(function() { |
|
394 | 394 | model.set_comm_alive(true); |
|
395 | 395 | }); |
|
396 | 396 | |
|
397 | 397 | // Display the views of the model. |
|
398 | 398 | var views = []; |
|
399 | 399 | var model_views = state[model.id].views; |
|
400 | 400 | for (var j=0; j<model_views.length; j++) { |
|
401 | 401 | var cell_index = model_views[j]; |
|
402 | 402 | var cell = that.notebook.get_cell(cell_index); |
|
403 | 403 | views.push(that.display_view_in_cell(cell, model)); |
|
404 | 404 | } |
|
405 | 405 | return Promise.all(views); |
|
406 | 406 | }); |
|
407 | 407 | return view_promise; |
|
408 | 408 | }); |
|
409 | 409 | that.all_views.push(views); |
|
410 | 410 | } |
|
411 | 411 | return Promise.all(that.all_views); |
|
412 | 412 | }).catch(utils.reject('Could not set widget manager state.', true)); |
|
413 | 413 | }; |
|
414 | 414 | |
|
415 | 415 | WidgetManager.prototype._get_connected_kernel = function() { |
|
416 | 416 | // Gets a promise for a connected kernel. |
|
417 | 417 | var that = this; |
|
418 | 418 | return new Promise(function(resolve, reject) { |
|
419 | 419 | if (that.comm_manager && |
|
420 | 420 | that.comm_manager.kernel && |
|
421 | 421 | that.comm_manager.kernel.is_connected()) { |
|
422 | 422 | |
|
423 | 423 | resolve(that.comm_manager.kernel); |
|
424 | 424 | } else { |
|
425 | 425 | that.notebook.events.on('kernel_connected.Kernel', function(event, data) { |
|
426 | 426 | resolve(data.kernel); |
|
427 | 427 | }); |
|
428 | 428 | } |
|
429 | 429 | }); |
|
430 | 430 | }; |
|
431 | 431 | |
|
432 | 432 | // Backwards compatibility. |
|
433 | 433 | IPython.WidgetManager = WidgetManager; |
|
434 | 434 | |
|
435 | 435 | return {'WidgetManager': WidgetManager}; |
|
436 | 436 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now