Show More
@@ -1,393 +1,392 | |||
|
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 | // Base Widget Model and View classes |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | 12 | /** |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | **/ |
|
16 | 16 | |
|
17 | 17 | define(["notebook/js/widgetmanager", |
|
18 | 18 | "underscore", |
|
19 | 19 | "backbone"], |
|
20 | 20 | function(widget_manager, underscore, backbone){ |
|
21 | 21 | |
|
22 | 22 | var WidgetModel = Backbone.Model.extend({ |
|
23 | 23 | constructor: function (widget_manager, model_id, comm) { |
|
24 | 24 | // Construcctor |
|
25 | 25 | // |
|
26 | 26 | // Creates a WidgetModel instance. |
|
27 | 27 | // |
|
28 | 28 | // Parameters |
|
29 | 29 | // ---------- |
|
30 | 30 | // widget_manager : WidgetManager instance |
|
31 | 31 | // model_id : string |
|
32 | 32 | // An ID unique to this model. |
|
33 | 33 | // comm : Comm instance (optional) |
|
34 | 34 | this.widget_manager = widget_manager; |
|
35 | 35 | this.pending_msgs = 0; |
|
36 | 36 | this.msg_throttle = 2; |
|
37 | 37 | this.msg_buffer = null; |
|
38 | 38 | this.key_value_lock = null; |
|
39 | 39 | this.id = model_id; |
|
40 | 40 | this.views = []; |
|
41 | 41 | |
|
42 | 42 | if (comm !== undefined) { |
|
43 | 43 | // Remember comm associated with the model. |
|
44 | 44 | this.comm = comm; |
|
45 | 45 | comm.model = this; |
|
46 | 46 | |
|
47 | 47 | // Hook comm messages up to model. |
|
48 | 48 | comm.on_close($.proxy(this._handle_comm_closed, this)); |
|
49 | 49 | comm.on_msg($.proxy(this._handle_comm_msg, this)); |
|
50 | 50 | } |
|
51 | 51 | return Backbone.Model.apply(this); |
|
52 | 52 | }, |
|
53 | 53 | |
|
54 | 54 | send: function (content, callbacks) { |
|
55 | 55 | // Send a custom msg over the comm. |
|
56 | 56 | if (this.comm !== undefined) { |
|
57 | 57 | var data = {method: 'custom', custom_content: content}; |
|
58 | 58 | this.comm.send(data, callbacks); |
|
59 | 59 | } |
|
60 | 60 | }, |
|
61 | 61 | |
|
62 | 62 | _handle_comm_closed: function (msg) { |
|
63 | 63 | // Handle when a widget is closed. |
|
64 | 64 | this.trigger('comm:close'); |
|
65 | 65 | delete this.comm.model; // Delete ref so GC will collect widget model. |
|
66 | 66 | delete this.comm; |
|
67 | 67 | delete this.model_id; // Delete id from model so widget manager cleans up. |
|
68 | 68 | // TODO: Handle deletion, like this.destroy(), and delete views, etc. |
|
69 | 69 | }, |
|
70 | 70 | |
|
71 | 71 | _handle_comm_msg: function (msg) { |
|
72 | 72 | // Handle incoming comm msg. |
|
73 | 73 | var method = msg.content.data.method; |
|
74 | 74 | switch (method) { |
|
75 | 75 | case 'update': |
|
76 | 76 | this.apply_update(msg.content.data.state); |
|
77 | 77 | break; |
|
78 | 78 | case 'custom': |
|
79 | 79 | this.trigger('msg:custom', msg.content.data.custom_content); |
|
80 | 80 | break; |
|
81 | 81 | case 'display': |
|
82 | 82 | this.widget_manager.display_view(msg, this); |
|
83 | 83 | break; |
|
84 | 84 | } |
|
85 | 85 | }, |
|
86 | 86 | |
|
87 | 87 | apply_update: function (state) { |
|
88 | 88 | // Handle when a widget is updated via the python side. |
|
89 | 89 | for (var key in state) { |
|
90 | 90 | if (state.hasOwnProperty(key)) { |
|
91 | 91 | var value = state[key]; |
|
92 | 92 | this.key_value_lock = [key, value]; |
|
93 | 93 | try { |
|
94 | 94 | this.set(key, this._unpack_models(value)); |
|
95 | 95 | } finally { |
|
96 | 96 | this.key_value_lock = null; |
|
97 | 97 | } |
|
98 | 98 | } |
|
99 | 99 | } |
|
100 | //TODO: are there callbacks that make sense in this case? If so, attach them here as an option | |
|
101 | this.save(); | |
|
100 | //TODO: are there callbacks that make sense in this case? If so, attach them here as an option | |
|
102 | 101 | }, |
|
103 | 102 | |
|
104 | 103 | _handle_status: function (msg, callbacks) { |
|
105 | 104 | // Handle status msgs. |
|
106 | 105 | |
|
107 | 106 | // execution_state : ('busy', 'idle', 'starting') |
|
108 | 107 | if (this.comm !== undefined) { |
|
109 | 108 | if (msg.content.execution_state ==='idle') { |
|
110 | 109 | // Send buffer if this message caused another message to be |
|
111 | 110 | // throttled. |
|
112 | 111 | if (this.msg_buffer !== null && |
|
113 | 112 | this.msg_throttle === this.pending_msgs) { |
|
114 | 113 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; |
|
115 | 114 | this.comm.send(data, callbacks); |
|
116 | 115 | this.msg_buffer = null; |
|
117 | 116 | } else { |
|
118 | 117 | --this.pending_msgs; |
|
119 | 118 | } |
|
120 | 119 | } |
|
121 | 120 | } |
|
122 | 121 | }, |
|
123 | 122 | |
|
124 | 123 | _handle_sync: function (method, options) { |
|
125 | 124 | // Custom syncronization logic. |
|
126 | 125 | var model_json = this.toJSON(); |
|
127 | 126 | var attr; |
|
128 | 127 | |
|
129 | 128 | // Only send updated state if the state hasn't been changed |
|
130 | 129 | // during an update. |
|
131 | 130 | if (this.comm !== undefined) { |
|
132 | 131 | if (this.pending_msgs >= this.msg_throttle) { |
|
133 | 132 | // The throttle has been exceeded, buffer the current msg so |
|
134 | 133 | // it can be sent once the kernel has finished processing |
|
135 | 134 | // some of the existing messages. |
|
136 | 135 | if (this.msg_buffer === null) { |
|
137 | 136 | this.msg_buffer = $.extend({}, model_json); // Copy |
|
138 | 137 | } |
|
139 | 138 | for (attr in options.attrs) { |
|
140 | 139 | var value = this._pack_models(options.attrs[attr]); |
|
141 | 140 | if (this.key_value_lock === null || attr !== this.key_value_lock[0] || value !== this.key_value_lock[1]) { |
|
142 | 141 | this.msg_buffer[attr] = value; |
|
143 | 142 | } |
|
144 | 143 | } |
|
145 | 144 | |
|
146 | 145 | } else { |
|
147 | 146 | // We haven't exceeded the throttle, send the message like |
|
148 | 147 | // normal. If this is a patch operation, just send the |
|
149 | 148 | // changes. |
|
150 | 149 | var send_json = model_json; |
|
151 | 150 | send_json = {}; |
|
152 | 151 | for (attr in options.attrs) { |
|
153 | 152 | var value = this._pack_models(options.attrs[attr]); |
|
154 | 153 | if (this.key_value_lock === null || attr !== this.key_value_lock[0] || value !== this.key_value_lock[1]) { |
|
155 | 154 | send_json[attr] = value; |
|
156 | 155 | } |
|
157 | 156 | } |
|
158 | 157 | |
|
159 | 158 | var is_empty = true; |
|
160 | 159 | for (var prop in send_json) if (send_json.hasOwnProperty(prop)) is_empty = false; |
|
161 | 160 | if (!is_empty) { |
|
162 | 161 | ++this.pending_msgs; |
|
163 | 162 | var data = {method: 'backbone', sync_data: send_json}; |
|
164 | 163 | this.comm.send(data, options.callbacks); |
|
165 | 164 | } |
|
166 | 165 | } |
|
167 | 166 | } |
|
168 | 167 | |
|
169 | 168 | // Since the comm is a one-way communication, assume the message |
|
170 | 169 | // arrived. |
|
171 | 170 | return model_json; |
|
172 | 171 | }, |
|
173 | 172 | |
|
174 | 173 | push: function(callbacks) { |
|
175 | 174 | // Push this model's state to the back-end |
|
176 | 175 | // |
|
177 | 176 | // This invokes a Backbone.Sync. |
|
178 | 177 | this.save(this.changedAttributes(), {patch: true, callbacks: callbacks}); |
|
179 | 178 | }, |
|
180 | 179 | |
|
181 | 180 | _pack_models: function(value) { |
|
182 | 181 | // Replace models with model ids recursively. |
|
183 | 182 | if (value instanceof Backbone.Model) { |
|
184 | 183 | return value.id; |
|
185 | 184 | } else if (value instanceof Object) { |
|
186 | 185 | var packed = {}; |
|
187 | 186 | for (var key in value) { |
|
188 | 187 | packed[key] = this._pack_models(value[key]); |
|
189 | 188 | } |
|
190 | 189 | return packed; |
|
191 | 190 | } else { |
|
192 | 191 | return value; |
|
193 | 192 | } |
|
194 | 193 | }, |
|
195 | 194 | |
|
196 | 195 | _unpack_models: function(value) { |
|
197 | 196 | // Replace model ids with models recursively. |
|
198 | 197 | if (value instanceof Object) { |
|
199 | 198 | var unpacked = {}; |
|
200 | 199 | for (var key in value) { |
|
201 | 200 | unpacked[key] = this._unpack_models(value[key]); |
|
202 | 201 | } |
|
203 | 202 | return unpacked; |
|
204 | 203 | } else { |
|
205 | 204 | var model = this.widget_manager.get_model(value); |
|
206 | 205 | if (model !== null) { |
|
207 | 206 | return model; |
|
208 | 207 | } else { |
|
209 | 208 | return value; |
|
210 | 209 | } |
|
211 | 210 | } |
|
212 | 211 | }, |
|
213 | 212 | |
|
214 | 213 | }); |
|
215 | 214 | widget_manager.register_widget_model('WidgetModel', WidgetModel); |
|
216 | 215 | |
|
217 | 216 | |
|
218 | 217 | var WidgetView = Backbone.View.extend({ |
|
219 | 218 | initialize: function(parameters) { |
|
220 | 219 | // Public constructor. |
|
221 | 220 | this.model.on('change',this.update,this); |
|
222 | 221 | this.options = parameters.options; |
|
223 | 222 | this.child_views = []; |
|
224 | 223 | this.model.views.push(this); |
|
225 | 224 | }, |
|
226 | 225 | |
|
227 | 226 | update: function(){ |
|
228 | 227 | // Triggered on model change. |
|
229 | 228 | // |
|
230 | 229 | // Update view to be consistent with this.model |
|
231 | 230 | }, |
|
232 | 231 | |
|
233 | 232 | create_child_view: function(child_model, options) { |
|
234 | 233 | // Create and return a child view. |
|
235 | 234 | // |
|
236 | 235 | // -given a model and (optionally) a view name if the view name is |
|
237 | 236 | // not given, it defaults to the model's default view attribute. |
|
238 | 237 | |
|
239 | 238 | // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior |
|
240 | 239 | // it would be great to have the widget manager add the cell metadata |
|
241 | 240 | // to the subview without having to add it here. |
|
242 | 241 | options = options || {}; |
|
243 | 242 | options.cell = this.options.cell; |
|
244 | 243 | var child_view = this.model.widget_manager.create_view(child_model, options); |
|
245 | 244 | this.child_views[child_model.id] = child_view; |
|
246 | 245 | return child_view; |
|
247 | 246 | }, |
|
248 | 247 | |
|
249 | 248 | delete_child_view: function(child_model, options) { |
|
250 | 249 | // Delete a child view that was previously created using create_child_view. |
|
251 | 250 | var view = this.child_views[child_model.id]; |
|
252 | 251 | delete this.child_views[child_model.id]; |
|
253 | 252 | view.remove(); |
|
254 | 253 | }, |
|
255 | 254 | |
|
256 | 255 | do_diff: function(old_list, new_list, removed_callback, added_callback) { |
|
257 | 256 | // Difference a changed list and call remove and add callbacks for |
|
258 | 257 | // each removed and added item in the new list. |
|
259 | 258 | // |
|
260 | 259 | // Parameters |
|
261 | 260 | // ---------- |
|
262 | 261 | // old_list : array |
|
263 | 262 | // new_list : array |
|
264 | 263 | // removed_callback : Callback(item) |
|
265 | 264 | // Callback that is called for each item removed. |
|
266 | 265 | // added_callback : Callback(item) |
|
267 | 266 | // Callback that is called for each item added. |
|
268 | 267 | |
|
269 | 268 | |
|
270 | 269 | // removed items |
|
271 | 270 | _.each(_.difference(old_list, new_list), function(item, index, list) { |
|
272 | 271 | removed_callback(item); |
|
273 | 272 | }, this); |
|
274 | 273 | |
|
275 | 274 | // added items |
|
276 | 275 | _.each(_.difference(new_list, old_list), function(item, index, list) { |
|
277 | 276 | added_callback(item); |
|
278 | 277 | }, this); |
|
279 | 278 | }, |
|
280 | 279 | |
|
281 | 280 | callbacks: function(){ |
|
282 | 281 | // Create msg callbacks for a comm msg. |
|
283 | 282 | return this.model.widget_manager.callbacks(this); |
|
284 | 283 | }, |
|
285 | 284 | |
|
286 | 285 | render: function(){ |
|
287 | 286 | // Render the view. |
|
288 | 287 | // |
|
289 | 288 | // By default, this is only called the first time the view is created |
|
290 | 289 | }, |
|
291 | 290 | |
|
292 | 291 | send: function (content) { |
|
293 | 292 | // Send a custom msg associated with this view. |
|
294 | 293 | this.model.send(content, this.callbacks()); |
|
295 | 294 | }, |
|
296 | 295 | |
|
297 | 296 | touch: function () { |
|
298 | 297 | this.model.push(this.callbacks()); |
|
299 | 298 | }, |
|
300 | 299 | |
|
301 | 300 | }); |
|
302 | 301 | |
|
303 | 302 | |
|
304 | 303 | var DOMWidgetView = WidgetView.extend({ |
|
305 | 304 | initialize: function (options) { |
|
306 | 305 | // Public constructor |
|
307 | 306 | |
|
308 | 307 | // In the future we may want to make changes more granular |
|
309 | 308 | // (e.g., trigger on visible:change). |
|
310 | 309 | this.model.on('change', this.update, this); |
|
311 | 310 | this.model.on('msg:custom', this.on_msg, this); |
|
312 | 311 | DOMWidgetView.__super__.initialize.apply(this, arguments); |
|
313 | 312 | }, |
|
314 | 313 | |
|
315 | 314 | on_msg: function(msg) { |
|
316 | 315 | // Handle DOM specific msgs. |
|
317 | 316 | switch(msg.msg_type) { |
|
318 | 317 | case 'add_class': |
|
319 | 318 | this.add_class(msg.selector, msg.class_list); |
|
320 | 319 | break; |
|
321 | 320 | case 'remove_class': |
|
322 | 321 | this.remove_class(msg.selector, msg.class_list); |
|
323 | 322 | break; |
|
324 | 323 | } |
|
325 | 324 | }, |
|
326 | 325 | |
|
327 | 326 | add_class: function (selector, class_list) { |
|
328 | 327 | // Add a DOM class to an element. |
|
329 | 328 | this._get_selector_element(selector).addClass(class_list); |
|
330 | 329 | }, |
|
331 | 330 | |
|
332 | 331 | remove_class: function (selector, class_list) { |
|
333 | 332 | // Remove a DOM class from an element. |
|
334 | 333 | this._get_selector_element(selector).removeClass(class_list); |
|
335 | 334 | }, |
|
336 | 335 | |
|
337 | 336 | update: function () { |
|
338 | 337 | // Update the contents of this view |
|
339 | 338 | // |
|
340 | 339 | // Called when the model is changed. The model may have been |
|
341 | 340 | // changed by another view or by a state update from the back-end. |
|
342 | 341 | // The very first update seems to happen before the element is |
|
343 | 342 | // finished rendering so we use setTimeout to give the element time |
|
344 | 343 | // to render |
|
345 | 344 | var e = this.$el; |
|
346 | 345 | var visible = this.model.get('visible'); |
|
347 | 346 | setTimeout(function() {e.toggle(visible)},0); |
|
348 | 347 | |
|
349 | 348 | var css = this.model.get('_css'); |
|
350 | 349 | if (css === undefined) {return;} |
|
351 | 350 | for (var selector in css) { |
|
352 | 351 | if (css.hasOwnProperty(selector)) { |
|
353 | 352 | // Apply the css traits to all elements that match the selector. |
|
354 | 353 | var elements = this._get_selector_element(selector); |
|
355 | 354 | if (elements.length > 0) { |
|
356 | 355 | var css_traits = css[selector]; |
|
357 | 356 | for (var css_key in css_traits) { |
|
358 | 357 | if (css_traits.hasOwnProperty(css_key)) { |
|
359 | 358 | elements.css(css_key, css_traits[css_key]); |
|
360 | 359 | } |
|
361 | 360 | } |
|
362 | 361 | } |
|
363 | 362 | } |
|
364 | 363 | } |
|
365 | 364 | }, |
|
366 | 365 | |
|
367 | 366 | _get_selector_element: function (selector) { |
|
368 | 367 | // Get the elements via the css selector. |
|
369 | 368 | |
|
370 | 369 | // If the selector is blank, apply the style to the $el_to_style |
|
371 | 370 | // element. If the $el_to_style element is not defined, use apply |
|
372 | 371 | // the style to the view's element. |
|
373 | 372 | var elements; |
|
374 | 373 | if (selector === undefined || selector === null || selector === '') { |
|
375 | 374 | if (this.$el_to_style === undefined) { |
|
376 | 375 | elements = this.$el; |
|
377 | 376 | } else { |
|
378 | 377 | elements = this.$el_to_style; |
|
379 | 378 | } |
|
380 | 379 | } else { |
|
381 | 380 | elements = this.$el.find(selector); |
|
382 | 381 | } |
|
383 | 382 | return elements; |
|
384 | 383 | }, |
|
385 | 384 | }); |
|
386 | 385 | |
|
387 | 386 | IPython.WidgetModel = WidgetModel; |
|
388 | 387 | IPython.WidgetView = WidgetView; |
|
389 | 388 | IPython.DOMWidgetView = DOMWidgetView; |
|
390 | 389 | |
|
391 | 390 | // Pass through widget_manager instance (probably not a good practice). |
|
392 | 391 | return widget_manager; |
|
393 | 392 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now