Show More
@@ -1,268 +1,268 b'' | |||||
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/namespace" |
|
8 | "base/js/namespace" | |
9 | ], function (_, Backbone, $, IPython) { |
|
9 | ], function (_, Backbone, $, IPython) { | |
10 | "use strict"; |
|
10 | "use strict"; | |
11 | //-------------------------------------------------------------------- |
|
11 | //-------------------------------------------------------------------- | |
12 | // WidgetManager class |
|
12 | // WidgetManager class | |
13 | //-------------------------------------------------------------------- |
|
13 | //-------------------------------------------------------------------- | |
14 | var WidgetManager = function (comm_manager, notebook) { |
|
14 | var WidgetManager = function (comm_manager, notebook) { | |
15 | // Public constructor |
|
15 | // Public constructor | |
16 | WidgetManager._managers.push(this); |
|
16 | WidgetManager._managers.push(this); | |
17 |
|
17 | |||
18 | // Attach a comm manager to the |
|
18 | // Attach a comm manager to the | |
19 | this.keyboard_manager = notebook.keyboard_manager; |
|
19 | this.keyboard_manager = notebook.keyboard_manager; | |
20 | this.notebook = notebook; |
|
20 | this.notebook = notebook; | |
21 | this.comm_manager = comm_manager; |
|
21 | this.comm_manager = comm_manager; | |
22 | this._models = {}; /* Dictionary of model ids and model instances */ |
|
22 | this._models = {}; /* Dictionary of model ids and model instances */ | |
23 |
|
23 | |||
24 | // Register with the comm manager. |
|
24 | // Register with the comm manager. | |
25 | this.comm_manager.register_target('ipython.widget', $.proxy(this._handle_comm_open, this)); |
|
25 | this.comm_manager.register_target('ipython.widget', $.proxy(this._handle_comm_open, this)); | |
26 | }; |
|
26 | }; | |
27 |
|
27 | |||
28 | //-------------------------------------------------------------------- |
|
28 | //-------------------------------------------------------------------- | |
29 | // Class level |
|
29 | // Class level | |
30 | //-------------------------------------------------------------------- |
|
30 | //-------------------------------------------------------------------- | |
31 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ |
|
31 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ | |
32 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ |
|
32 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ | |
33 | WidgetManager._managers = []; /* List of widget managers */ |
|
33 | WidgetManager._managers = []; /* List of widget managers */ | |
34 |
|
34 | |||
35 | WidgetManager.register_widget_model = function (model_name, model_type) { |
|
35 | WidgetManager.register_widget_model = function (model_name, model_type) { | |
36 | // Registers a widget model by name. |
|
36 | // Registers a widget model by name. | |
37 | WidgetManager._model_types[model_name] = model_type; |
|
37 | WidgetManager._model_types[model_name] = model_type; | |
38 | }; |
|
38 | }; | |
39 |
|
39 | |||
40 | WidgetManager.register_widget_view = function (view_name, view_type) { |
|
40 | WidgetManager.register_widget_view = function (view_name, view_type) { | |
41 | // Registers a widget view by name. |
|
41 | // Registers a widget view by name. | |
42 | WidgetManager._view_types[view_name] = view_type; |
|
42 | WidgetManager._view_types[view_name] = view_type; | |
43 | }; |
|
43 | }; | |
44 |
|
44 | |||
45 | //-------------------------------------------------------------------- |
|
45 | //-------------------------------------------------------------------- | |
46 | // Instance level |
|
46 | // Instance level | |
47 | //-------------------------------------------------------------------- |
|
47 | //-------------------------------------------------------------------- | |
48 | WidgetManager.prototype.display_view = function(msg, model) { |
|
48 | WidgetManager.prototype.display_view = function(msg, model) { | |
49 | // Displays a view for a particular model. |
|
49 | // Displays a view for a particular model. | |
50 | var cell = this.get_msg_cell(msg.parent_header.msg_id); |
|
50 | var cell = this.get_msg_cell(msg.parent_header.msg_id); | |
51 | if (cell === null) { |
|
51 | if (cell === null) { | |
52 | console.log("Could not determine where the display" + |
|
52 | console.log("Could not determine where the display" + | |
53 | " message was from. Widget will not be displayed"); |
|
53 | " message was from. Widget will not be displayed"); | |
54 | } else { |
|
54 | } else { | |
55 | var that = this; |
|
55 | var that = this; | |
56 | this.create_view(model, {cell: cell, callback: function(view) { |
|
56 | this.create_view(model, {cell: cell, callback: function(view) { | |
57 | that._handle_display_view(view); |
|
57 | that._handle_display_view(view); | |
58 | if (cell.widget_subarea) { |
|
58 | if (cell.widget_subarea) { | |
59 | cell.widget_subarea.append(view.$el); |
|
59 | cell.widget_subarea.append(view.$el); | |
60 | } |
|
60 | } | |
61 | view.trigger('displayed'); |
|
61 | view.trigger('displayed'); | |
62 | }}); |
|
62 | }}); | |
63 | } |
|
63 | } | |
64 | }; |
|
64 | }; | |
65 |
|
65 | |||
66 | WidgetManager.prototype._handle_display_view = function (view) { |
|
66 | WidgetManager.prototype._handle_display_view = function (view) { | |
67 | // Have the IPython keyboard manager disable its event |
|
67 | // Have the IPython keyboard manager disable its event | |
68 | // handling so the widget can capture keyboard input. |
|
68 | // handling so the widget can capture keyboard input. | |
69 | // Note, this is only done on the outer most widgets. |
|
69 | // Note, this is only done on the outer most widgets. | |
70 | if (this.keyboard_manager) { |
|
70 | if (this.keyboard_manager) { | |
71 | this.keyboard_manager.register_events(view.$el); |
|
71 | this.keyboard_manager.register_events(view.$el); | |
72 |
|
72 | |||
73 | if (view.additional_elements) { |
|
73 | if (view.additional_elements) { | |
74 | for (var i = 0; i < view.additional_elements.length; i++) { |
|
74 | for (var i = 0; i < view.additional_elements.length; i++) { | |
75 | this.keyboard_manager.register_events(view.additional_elements[i]); |
|
75 | this.keyboard_manager.register_events(view.additional_elements[i]); | |
76 | } |
|
76 | } | |
77 | } |
|
77 | } | |
78 | } |
|
78 | } | |
79 | }; |
|
79 | }; | |
80 |
|
80 | |||
81 |
|
81 | |||
82 | WidgetManager.prototype.create_view = function(model, options) { |
|
82 | WidgetManager.prototype.create_view = function(model, options) { | |
83 | // Creates a view for a particular model. |
|
83 | // Creates a view for a particular model. | |
84 |
|
84 | |||
85 | var view_name = model.get('_view_name'); |
|
85 | var view_name = model.get('_view_name'); | |
86 | var view_mod = model.get('_view_module'); |
|
86 | var view_mod = model.get('_view_module'); | |
87 | var errback = options.errback || function(err) {console.log(err);}; |
|
87 | var errback = options.errback || function(err) {console.log(err);}; | |
88 |
|
88 | |||
89 | var instantiate_view = function(ViewType) { |
|
89 | var instantiate_view = function(ViewType) { | |
90 | if (ViewType) { |
|
90 | if (ViewType) { | |
91 | // If a view is passed into the method, use that view's cell as |
|
91 | // If a view is passed into the method, use that view's cell as | |
92 | // the cell for the view that is created. |
|
92 | // the cell for the view that is created. | |
93 | options = options || {}; |
|
93 | options = options || {}; | |
94 | if (options.parent !== undefined) { |
|
94 | if (options.parent !== undefined) { | |
95 | options.cell = options.parent.options.cell; |
|
95 | options.cell = options.parent.options.cell; | |
96 | } |
|
96 | } | |
97 |
|
97 | |||
98 | // Create and render the view... |
|
98 | // Create and render the view... | |
99 | var parameters = {model: model, options: options}; |
|
99 | var parameters = {model: model, options: options}; | |
100 | var view = new ViewType(parameters); |
|
100 | var view = new ViewType(parameters); | |
101 | view.render(); |
|
101 | view.render(); | |
102 | model.on('destroy', view.remove, view); |
|
102 | model.on('destroy', view.remove, view); | |
103 | options.callback(view); |
|
103 | options.callback(view); | |
104 | } else { |
|
104 | } else { | |
105 | errback({unknown_view: true, view_name: view_name, |
|
105 | errback({unknown_view: true, view_name: view_name, | |
106 | view_module: view_mod}); |
|
106 | view_module: view_mod}); | |
107 | } |
|
107 | } | |
108 | }; |
|
108 | }; | |
109 |
|
109 | |||
110 | if (view_mod) { |
|
110 | if (view_mod) { | |
111 | require([view_mod], function(module) { |
|
111 | require([view_mod], function(module) { | |
112 | instantiate_view(module[view_name]); |
|
112 | instantiate_view(module[view_name]); | |
113 | }, errback); |
|
113 | }, errback); | |
114 | } else { |
|
114 | } else { | |
115 | instantiate_view(WidgetManager._view_types[view_name]); |
|
115 | instantiate_view(WidgetManager._view_types[view_name]); | |
116 | } |
|
116 | } | |
117 | }; |
|
117 | }; | |
118 |
|
118 | |||
119 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
119 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
120 | var cell = null; |
|
120 | var cell = null; | |
121 | // First, check to see if the msg was triggered by cell execution. |
|
121 | // First, check to see if the msg was triggered by cell execution. | |
122 | if (this.notebook) { |
|
122 | if (this.notebook) { | |
123 | cell = this.notebook.get_msg_cell(msg_id); |
|
123 | cell = this.notebook.get_msg_cell(msg_id); | |
124 | } |
|
124 | } | |
125 | if (cell !== null) { |
|
125 | if (cell !== null) { | |
126 | return cell; |
|
126 | return cell; | |
127 | } |
|
127 | } | |
128 | // Second, check to see if a get_cell callback was defined |
|
128 | // Second, check to see if a get_cell callback was defined | |
129 | // for the message. get_cell callbacks are registered for |
|
129 | // for the message. get_cell callbacks are registered for | |
130 | // widget messages, so this block is actually checking to see if the |
|
130 | // widget messages, so this block is actually checking to see if the | |
131 | // message was triggered by a widget. |
|
131 | // message was triggered by a widget. | |
132 | var kernel = this.comm_manager.kernel; |
|
132 | var kernel = this.comm_manager.kernel; | |
133 | if (kernel) { |
|
133 | if (kernel) { | |
134 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
134 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
135 | if (callbacks && callbacks.iopub && |
|
135 | if (callbacks && callbacks.iopub && | |
136 | callbacks.iopub.get_cell !== undefined) { |
|
136 | callbacks.iopub.get_cell !== undefined) { | |
137 | return callbacks.iopub.get_cell(); |
|
137 | return callbacks.iopub.get_cell(); | |
138 | } |
|
138 | } | |
139 | } |
|
139 | } | |
140 |
|
140 | |||
141 | // Not triggered by a cell or widget (no get_cell callback |
|
141 | // Not triggered by a cell or widget (no get_cell callback | |
142 | // exists). |
|
142 | // exists). | |
143 | return null; |
|
143 | return null; | |
144 | }; |
|
144 | }; | |
145 |
|
145 | |||
146 | WidgetManager.prototype.callbacks = function (view) { |
|
146 | WidgetManager.prototype.callbacks = function (view) { | |
147 | // callback handlers specific a view |
|
147 | // callback handlers specific a view | |
148 | var callbacks = {}; |
|
148 | var callbacks = {}; | |
149 | if (view && view.options.cell) { |
|
149 | if (view && view.options.cell) { | |
150 |
|
150 | |||
151 | // Try to get output handlers |
|
151 | // Try to get output handlers | |
152 | var cell = view.options.cell; |
|
152 | var cell = view.options.cell; | |
153 | var handle_output = null; |
|
153 | var handle_output = null; | |
154 | var handle_clear_output = null; |
|
154 | var handle_clear_output = null; | |
155 | if (cell.output_area) { |
|
155 | if (cell.output_area) { | |
156 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); |
|
156 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); | |
157 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); |
|
157 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); | |
158 | } |
|
158 | } | |
159 |
|
159 | |||
160 | // Create callback dict using what is known |
|
160 | // Create callback dict using what is known | |
161 | var that = this; |
|
161 | var that = this; | |
162 | callbacks = { |
|
162 | callbacks = { | |
163 | iopub : { |
|
163 | iopub : { | |
164 | output : handle_output, |
|
164 | output : handle_output, | |
165 | clear_output : handle_clear_output, |
|
165 | clear_output : handle_clear_output, | |
166 |
|
166 | |||
167 | // Special function only registered by widget messages. |
|
167 | // Special function only registered by widget messages. | |
168 | // Allows us to get the cell for a message so we know |
|
168 | // Allows us to get the cell for a message so we know | |
169 | // where to add widgets if the code requires it. |
|
169 | // where to add widgets if the code requires it. | |
170 | get_cell : function () { |
|
170 | get_cell : function () { | |
171 | return cell; |
|
171 | return cell; | |
172 | }, |
|
172 | }, | |
173 | }, |
|
173 | }, | |
174 | }; |
|
174 | }; | |
175 | } |
|
175 | } | |
176 | return callbacks; |
|
176 | return callbacks; | |
177 | }; |
|
177 | }; | |
178 |
|
178 | |||
179 | WidgetManager.prototype.get_model = function (model_id) { |
|
179 | WidgetManager.prototype.get_model = function (model_id) { | |
180 | // Look-up a model instance by its id. |
|
180 | // Look-up a model instance by its id. | |
181 | var model = this._models[model_id]; |
|
181 | var model = this._models[model_id]; | |
182 | if (model !== undefined && model.id == model_id) { |
|
182 | if (model !== undefined && model.id == model_id) { | |
183 | return model; |
|
183 | return model; | |
184 | } |
|
184 | } | |
185 | return null; |
|
185 | return null; | |
186 | }; |
|
186 | }; | |
187 |
|
187 | |||
188 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
188 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { | |
189 | // Handle when a comm is opened. |
|
189 | // Handle when a comm is opened. | |
190 | return this.create_model({model_name: msg.content.data.target_name, comm: comm}); |
|
190 | return this._create_model({model_name: msg.content.data.target_name, comm: comm}); | |
191 | }; |
|
191 | }; | |
192 |
|
192 | |||
193 | WidgetManager.prototype.create_model = function (model_name, target_name, init_state_callback) { |
|
193 | WidgetManager.prototype.create_model = function (model_name, target_name, init_state_callback) { | |
194 | // Create and return a new widget model. |
|
194 | // Create and return a new widget model. | |
195 | // |
|
195 | // | |
196 | // Parameters |
|
196 | // Parameters | |
197 | // ---------- |
|
197 | // ---------- | |
198 | // model_name: string |
|
198 | // model_name: string | |
199 | // Target name of the widget model to create. |
|
199 | // Target name of the widget model to create. | |
200 | // target_name: string |
|
200 | // target_name: string | |
201 | // Target name of the widget in the back-end. |
|
201 | // Target name of the widget in the back-end. | |
202 | // init_state_callback: (optional) callback |
|
202 | // init_state_callback: (optional) callback | |
203 | // Called when the first state push from the back-end is |
|
203 | // Called when the first state push from the back-end is | |
204 | // recieved. |
|
204 | // recieved. | |
205 | return this._create_model({ |
|
205 | return this._create_model({ | |
206 | model_name: model_name, |
|
206 | model_name: model_name, | |
207 | target_name: target_name, |
|
207 | target_name: target_name, | |
208 | init_state_callback: init_state_callback}); |
|
208 | init_state_callback: init_state_callback}); | |
209 | }; |
|
209 | }; | |
210 |
|
210 | |||
211 | WidgetManager.prototype._create_model = function (options) { |
|
211 | WidgetManager.prototype._create_model = function (options) { | |
212 | // Create and return a new widget model. |
|
212 | // Create and return a new widget model. | |
213 | // |
|
213 | // | |
214 | // Parameters |
|
214 | // Parameters | |
215 | // ---------- |
|
215 | // ---------- | |
216 | // options: dictionary |
|
216 | // options: dictionary | |
217 | // Dictionary of options with the following contents: |
|
217 | // Dictionary of options with the following contents: | |
218 | // model_name: string |
|
218 | // model_name: string | |
219 | // Target name of the widget model to create. |
|
219 | // Target name of the widget model to create. | |
220 | // target_name: (optional) string |
|
220 | // target_name: (optional) string | |
221 | // Target name of the widget in the back-end. |
|
221 | // Target name of the widget in the back-end. | |
222 | // comm: (optional) Comm |
|
222 | // comm: (optional) Comm | |
223 | // init_state_callback: (optional) callback |
|
223 | // init_state_callback: (optional) callback | |
224 | // Called when the first state push from the back-end is |
|
224 | // Called when the first state push from the back-end is | |
225 | // recieved. |
|
225 | // recieved. | |
226 |
|
226 | |||
227 | // Create a comm if it wasn't provided. |
|
227 | // Create a comm if it wasn't provided. | |
228 | var comm = options.comm; |
|
228 | var comm = options.comm; | |
229 | if (!comm) { |
|
229 | if (!comm) { | |
230 | comm = this.comm_manager.new_comm('ipython.widget', {'target_name': options.target_name}); |
|
230 | comm = this.comm_manager.new_comm('ipython.widget', {'target_name': options.target_name}); | |
231 | } |
|
231 | } | |
232 |
|
232 | |||
233 | // Create and return a new model that is connected to the comm. |
|
233 | // Create and return a new model that is connected to the comm. | |
234 | var that = this; |
|
234 | var that = this; | |
235 |
|
235 | |||
236 | var instantiate_model = function(ModelType) { |
|
236 | var instantiate_model = function(ModelType) { | |
237 | var model_id = comm.comm_id; |
|
237 | var model_id = comm.comm_id; | |
238 | var widget_model = new ModelType(that, model_id, comm, options.init_state_callback); |
|
238 | var widget_model = new ModelType(that, model_id, comm, options.init_state_callback); | |
239 | widget_model.on('comm:close', function () { |
|
239 | widget_model.on('comm:close', function () { | |
240 | delete that._models[model_id]; |
|
240 | delete that._models[model_id]; | |
241 | }); |
|
241 | }); | |
242 | that._models[model_id] = widget_model; |
|
242 | that._models[model_id] = widget_model; | |
243 | }; |
|
243 | }; | |
244 |
|
244 | |||
245 | var widget_type_name = msg.content.data.model_name; |
|
245 | var widget_type_name = msg.content.data.model_name; | |
246 | var widget_module = msg.content.data.model_module; |
|
246 | var widget_module = msg.content.data.model_module; | |
247 |
|
247 | |||
248 | if (widget_module) { |
|
248 | if (widget_module) { | |
249 | // Load the module containing the widget model |
|
249 | // Load the module containing the widget model | |
250 | require([widget_module], function(mod) { |
|
250 | require([widget_module], function(mod) { | |
251 | if (mod[widget_type_name]) { |
|
251 | if (mod[widget_type_name]) { | |
252 | instantiate_model(mod[widget_type_name]); |
|
252 | instantiate_model(mod[widget_type_name]); | |
253 | } else { |
|
253 | } else { | |
254 | console.log("Error creating widget model: " + widget_type_name |
|
254 | console.log("Error creating widget model: " + widget_type_name | |
255 | + " not found in " + widget_module); |
|
255 | + " not found in " + widget_module); | |
256 | } |
|
256 | } | |
257 | }, function(err) { console.log(err); }); |
|
257 | }, function(err) { console.log(err); }); | |
258 | } else { |
|
258 | } else { | |
259 | // No module specified, load from the global models registry |
|
259 | // No module specified, load from the global models registry | |
260 | instantiate_model(WidgetManager._model_types[widget_type_name]); |
|
260 | instantiate_model(WidgetManager._model_types[widget_type_name]); | |
261 | } |
|
261 | } | |
262 | }; |
|
262 | }; | |
263 |
|
263 | |||
264 | // Backwards compatability. |
|
264 | // Backwards compatability. | |
265 | IPython.WidgetManager = WidgetManager; |
|
265 | IPython.WidgetManager = WidgetManager; | |
266 |
|
266 | |||
267 | return {'WidgetManager': WidgetManager}; |
|
267 | return {'WidgetManager': WidgetManager}; | |
268 | }); |
|
268 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now