Show More
@@ -1,279 +1,279 b'' | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2013 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // WidgetModel, WidgetView, and WidgetManager |
|
10 | 10 | //============================================================================ |
|
11 | 11 | /** |
|
12 | 12 | * Base Widget classes |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | * @submodule widget |
|
16 | 16 | */ |
|
17 | 17 | |
|
18 | 18 | (function () { |
|
19 | 19 | "use strict"; |
|
20 | 20 | |
|
21 | 21 | // Use require.js 'define' method so that require.js is intelligent enough to |
|
22 | 22 | // syncronously load everything within this file when it is being 'required' |
|
23 | 23 | // elsewhere. |
|
24 | 24 | define(["underscore", |
|
25 | 25 | "backbone", |
|
26 | 26 | ], function (underscore, backbone) { |
|
27 | 27 | |
|
28 | 28 | //-------------------------------------------------------------------- |
|
29 | 29 | // WidgetManager class |
|
30 | 30 | //-------------------------------------------------------------------- |
|
31 | 31 | var WidgetManager = function () { |
|
32 | 32 | this.comm_manager = null; |
|
33 | 33 | this.widget_model_types = {}; |
|
34 | 34 | this.widget_view_types = {}; |
|
35 | 35 | this._model_instances = {}; |
|
36 | 36 | |
|
37 | 37 | Backbone.sync = function (method, model, options, error) { |
|
38 | 38 | var result = model._handle_sync(method, options); |
|
39 | 39 | if (options.success) { |
|
40 | 40 | options.success(result); |
|
41 | 41 | } |
|
42 | 42 | }; |
|
43 | 43 | }; |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | WidgetManager.prototype.attach_comm_manager = function (comm_manager) { |
|
47 | 47 | this.comm_manager = comm_manager; |
|
48 | 48 | |
|
49 | 49 | // Register already-registered widget model types with the comm manager. |
|
50 | 50 | for (var widget_model_name in this.widget_model_types) { |
|
51 | 51 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); |
|
52 | 52 | } |
|
53 | 53 | }; |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { |
|
57 | 57 | // Register the widget with the comm manager. Make sure to pass this object's context |
|
58 | 58 | // in so `this` works in the call back. |
|
59 | 59 | if (this.comm_manager !== null) { |
|
60 | 60 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); |
|
61 | 61 | } |
|
62 | 62 | this.widget_model_types[widget_model_name] = widget_model_type; |
|
63 | 63 | }; |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { |
|
67 | 67 | this.widget_view_types[widget_view_name] = widget_view_type; |
|
68 | 68 | }; |
|
69 | 69 | |
|
70 | 70 | |
|
71 | 71 | WidgetManager.prototype.handle_msg = function(msg, model) { |
|
72 | 72 | var method = msg.content.data.method; |
|
73 | 73 | switch (method) { |
|
74 | 74 | case 'display': |
|
75 | 75 | var cell = this.get_msg_cell(msg.parent_header.msg_id); |
|
76 | 76 | if (cell === null) { |
|
77 | 77 | console.log("Could not determine where the display" + |
|
78 | 78 | " message was from. Widget will not be displayed"); |
|
79 | 79 | } else { |
|
80 | 80 | var view = this.create_view(model, msg.content.data.view_name); |
|
81 | 81 | if (view !== undefined |
|
82 | 82 | && cell.widget_subarea !== undefined |
|
83 | 83 | && cell.widget_subarea !== null) { |
|
84 | 84 | |
|
85 | 85 | view.cell = cell; |
|
86 | 86 | cell.widget_area.show(); |
|
87 | 87 | cell.widget_subarea.append(view.$el); |
|
88 | 88 | } |
|
89 | 89 | } |
|
90 | 90 | break; |
|
91 | 91 | } |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | <<<<<<< HEAD |
|
95 | 95 | <<<<<<< HEAD |
|
96 | 96 | WidgetManager.prototype.create_view = function(model, view_name, cell) { |
|
97 | 97 | ======= |
|
98 | 98 | WidgetManager.prototype.create_view = function(model, view_name, options) { |
|
99 | 99 | >>>>>>> Completely remove cell from model and view. |
|
100 | 100 | view_name = view_name || model.get('default_view_name'); |
|
101 | 101 | ======= |
|
102 | 102 | WidgetManager.prototype.create_view = function(model, view_name, cell, options) { |
|
103 | 103 | view_name = view_name || model.get('default_view_name'); |
|
104 | 104 | >>>>>>> Add widget view options in creating child views |
|
105 | 105 | var ViewType = this.widget_view_types[view_name]; |
|
106 | 106 | if (ViewType !== undefined && ViewType !== null) { |
|
107 | 107 | var view = new ViewType({model: model, widget_manager: this, options: options}); |
|
108 | 108 | view.render(); |
|
109 | 109 | model.views.push(view); |
|
110 | 110 | model.on('destroy', view.remove, view); |
|
111 | 111 | <<<<<<< HEAD |
|
112 | 112 | /* |
|
113 | 113 | // TODO: handle view deletion. Don't forget to delete child views |
|
114 | 114 | var that = this; |
|
115 | 115 | view.$el.on("remove", function () { |
|
116 | 116 | var index = that.views.indexOf(view); |
|
117 | 117 | if (index > -1) { |
|
118 | 118 | that.views.splice(index, 1); |
|
119 | 119 | ======= |
|
120 | 120 | /* |
|
121 | 121 | // TODO: handle view deletion. Don't forget to delete child views |
|
122 | 122 | var that = this; |
|
123 | 123 | view.$el.on("remove", function () { |
|
124 | 124 | var index = that.views.indexOf(view); |
|
125 | 125 | if (index > -1) { |
|
126 | 126 | that.views.splice(index, 1); |
|
127 | 127 | } |
|
128 | 128 | view.remove(); // Clean-up view |
|
129 | 129 | |
|
130 | 130 | // Close the comm if there are no views left. |
|
131 | 131 | if (that.views.length() === 0) { |
|
132 | 132 | //trigger comm close event? |
|
133 | 133 | } |
|
134 | 134 | |
|
135 | 135 | |
|
136 | 136 | if (that.comm !== undefined) { |
|
137 | 137 | that.comm.close(); |
|
138 | 138 | delete that.comm.model; // Delete ref so GC will collect widget model. |
|
139 | 139 | delete that.comm; |
|
140 | 140 | >>>>>>> Add widget view options in creating child views |
|
141 | 141 | } |
|
142 | 142 | view.remove(); // Clean-up view |
|
143 | 143 | |
|
144 | 144 | // Close the comm if there are no views left. |
|
145 | 145 | if (that.views.length() === 0) { |
|
146 | 146 | //trigger comm close event? |
|
147 | 147 | } |
|
148 | 148 | |
|
149 | 149 | |
|
150 | 150 | if (that.comm !== undefined) { |
|
151 | 151 | that.comm.close(); |
|
152 | 152 | delete that.comm.model; // Delete ref so GC will collect widget model. |
|
153 | 153 | delete that.comm; |
|
154 | 154 | } |
|
155 | 155 | delete that.model_id; // Delete id from model so widget manager cleans up. |
|
156 | 156 | }); |
|
157 | 157 | */ |
|
158 | 158 | return view; |
|
159 | 159 | } |
|
160 | 160 | }, |
|
161 | 161 | |
|
162 | 162 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
163 | 163 | var cell = null; |
|
164 | 164 | // First, check to see if the msg was triggered by cell execution. |
|
165 | 165 | if (IPython.notebook !== undefined && IPython.notebook !== null) { |
|
166 | 166 | cell = IPython.notebook.get_msg_cell(msg_id); |
|
167 | 167 | } |
|
168 | 168 | if (cell !== null) { |
|
169 | 169 | return cell |
|
170 | 170 | } |
|
171 | 171 | // Second, check to see if a get_cell callback was defined |
|
172 | 172 | // for the message. get_cell callbacks are registered for |
|
173 | 173 | // widget messages, so this block is actually checking to see if the |
|
174 | 174 | // message was triggered by a widget. |
|
175 | 175 | var kernel = this.get_kernel(); |
|
176 | 176 | if (kernel !== undefined && kernel !== null) { |
|
177 | 177 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
178 | 178 | if (callbacks !== undefined && |
|
179 | 179 | callbacks.iopub !== undefined && |
|
180 | 180 | callbacks.iopub.get_cell !== undefined) { |
|
181 | 181 | |
|
182 | 182 | return callbacks.iopub.get_cell(); |
|
183 | 183 | } |
|
184 | 184 | } |
|
185 | 185 | |
|
186 | 186 | // Not triggered by a cell or widget (no get_cell callback |
|
187 | 187 | // exists). |
|
188 | 188 | return null; |
|
189 | 189 | }; |
|
190 | 190 | |
|
191 | 191 | WidgetManager.prototype.callbacks = function (view) { |
|
192 | 192 | // callback handlers specific a view |
|
193 | 193 | var callbacks = {}; |
|
194 | 194 | var cell = view.cell; |
|
195 | 195 | if (cell !== null) { |
|
196 | 196 | // Try to get output handlers |
|
197 | 197 | var handle_output = null; |
|
198 | 198 | var handle_clear_output = null; |
|
199 | 199 | if (cell.output_area !== undefined && cell.output_area !== null) { |
|
200 | 200 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); |
|
201 | 201 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); |
|
202 | 202 | } |
|
203 | 203 | |
|
204 | 204 | // Create callback dict using what is known |
|
205 | 205 | var that = this; |
|
206 | 206 | callbacks = { |
|
207 | 207 | iopub : { |
|
208 | 208 | output : handle_output, |
|
209 | 209 | clear_output : handle_clear_output, |
|
210 | 210 | |
|
211 | 211 | status : function (msg) { |
|
212 | view.model._handle_status(msg, that.callbacks()); | |
|
212 | view.model._handle_status(msg, that.callbacks(view)); | |
|
213 | 213 | }, |
|
214 | 214 | |
|
215 | 215 | // Special function only registered by widget messages. |
|
216 | 216 | // Allows us to get the cell for a message so we know |
|
217 | 217 | // where to add widgets if the code requires it. |
|
218 | 218 | get_cell : function () { |
|
219 | 219 | return cell; |
|
220 | 220 | }, |
|
221 | 221 | }, |
|
222 | 222 | }; |
|
223 | 223 | } |
|
224 | 224 | return callbacks; |
|
225 | 225 | }; |
|
226 | 226 | |
|
227 | 227 | |
|
228 | 228 | WidgetManager.prototype.get_model = function (model_id) { |
|
229 | 229 | var model = this._model_instances[model_id]; |
|
230 | 230 | if (model !== undefined && model.id == model_id) { |
|
231 | 231 | return model; |
|
232 | 232 | } |
|
233 | 233 | return null; |
|
234 | 234 | }; |
|
235 | 235 | |
|
236 | 236 | |
|
237 | 237 | WidgetManager.prototype.get_kernel = function () { |
|
238 | 238 | if (this.comm_manager === null) { |
|
239 | 239 | return null; |
|
240 | 240 | } else { |
|
241 | 241 | return this.comm_manager.kernel; |
|
242 | 242 | } |
|
243 | 243 | }; |
|
244 | 244 | |
|
245 | 245 | |
|
246 | 246 | WidgetManager.prototype.on_create_widget = function (callback) { |
|
247 | 247 | this._create_widget_callback = callback; |
|
248 | 248 | }; |
|
249 | 249 | |
|
250 | 250 | |
|
251 | 251 | WidgetManager.prototype._handle_create_widget = function (widget_model) { |
|
252 | 252 | if (this._create_widget_callback) { |
|
253 | 253 | try { |
|
254 | 254 | this._create_widget_callback(widget_model); |
|
255 | 255 | } catch (e) { |
|
256 | 256 | console.log("Exception in WidgetManager callback", e, widget_model); |
|
257 | 257 | } |
|
258 | 258 | } |
|
259 | 259 | }; |
|
260 | 260 | |
|
261 | 261 | |
|
262 | 262 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
263 | 263 | var widget_type_name = msg.content.target_name; |
|
264 | 264 | var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm); |
|
265 | 265 | this._model_instances[comm.comm_id] = widget_model; // comm_id == model_id |
|
266 | 266 | this._handle_create_widget(widget_model); |
|
267 | 267 | }; |
|
268 | 268 | |
|
269 | 269 | //-------------------------------------------------------------------- |
|
270 | 270 | // Init code |
|
271 | 271 | //-------------------------------------------------------------------- |
|
272 | 272 | IPython.WidgetManager = WidgetManager; |
|
273 | 273 | if (IPython.widget_manager === undefined || IPython.widget_manager === null) { |
|
274 | 274 | IPython.widget_manager = new WidgetManager(); |
|
275 | 275 | } |
|
276 | 276 | |
|
277 | 277 | return IPython.widget_manager; |
|
278 | 278 | }); |
|
279 | 279 | }()); |
General Comments 0
You need to be logged in to leave comments.
Login now