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