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