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