Show More
@@ -1,492 +1,494 | |||
|
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(["widgets/js/manager", |
|
18 | 18 | "underscore", |
|
19 | 19 | "backbone"], |
|
20 | 20 | function(WidgetManager, _, Backbone){ |
|
21 | 21 | |
|
22 | 22 | var WidgetModel = Backbone.Model.extend({ |
|
23 | 23 | constructor: function (widget_manager, model_id, comm) { |
|
24 | 24 | // Constructor |
|
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._buffered_state_diff = {}; |
|
36 | 36 | this.pending_msgs = 0; |
|
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', content: content}; |
|
58 | 58 | this.comm.send(data, callbacks); |
|
59 | 59 | this.pending_msgs++; |
|
60 | 60 | } |
|
61 | 61 | }, |
|
62 | 62 | |
|
63 | 63 | _handle_comm_closed: function (msg) { |
|
64 | 64 | // Handle when a widget is closed. |
|
65 | 65 | this.trigger('comm:close'); |
|
66 | 66 | delete this.comm.model; // Delete ref so GC will collect widget model. |
|
67 | 67 | delete this.comm; |
|
68 | 68 | delete this.model_id; // Delete id from model so widget manager cleans up. |
|
69 | 69 | _.each(this.views, function(view, i) { |
|
70 | 70 | view.remove(); |
|
71 | 71 | }); |
|
72 | 72 | }, |
|
73 | 73 | |
|
74 | 74 | _handle_comm_msg: function (msg) { |
|
75 | 75 | // Handle incoming comm msg. |
|
76 | 76 | var method = msg.content.data.method; |
|
77 | 77 | switch (method) { |
|
78 | 78 | case 'update': |
|
79 | 79 | this.apply_update(msg.content.data.state); |
|
80 | 80 | break; |
|
81 | 81 | case 'custom': |
|
82 | 82 | this.trigger('msg:custom', msg.content.data.content); |
|
83 | 83 | break; |
|
84 | 84 | case 'display': |
|
85 | 85 | this.widget_manager.display_view(msg, this); |
|
86 | 86 | this.trigger('displayed'); |
|
87 | 87 | break; |
|
88 | 88 | } |
|
89 | 89 | }, |
|
90 | 90 | |
|
91 | 91 | apply_update: function (state) { |
|
92 | 92 | // Handle when a widget is updated via the python side. |
|
93 | 93 | var that = this; |
|
94 | 94 | _.each(state, function(value, key) { |
|
95 | 95 | that.key_value_lock = [key, value]; |
|
96 | 96 | try { |
|
97 | 97 | WidgetModel.__super__.set.apply(that, [key, that._unpack_models(value)]); |
|
98 | 98 | } finally { |
|
99 | 99 | that.key_value_lock = null; |
|
100 | 100 | } |
|
101 | 101 | }); |
|
102 | 102 | }, |
|
103 | 103 | |
|
104 | 104 | _handle_status: function (msg, callbacks) { |
|
105 | 105 | // Handle status msgs. |
|
106 | 106 | |
|
107 | 107 | // execution_state : ('busy', 'idle', 'starting') |
|
108 | 108 | if (this.comm !== undefined) { |
|
109 | 109 | if (msg.content.execution_state ==='idle') { |
|
110 | 110 | // Send buffer if this message caused another message to be |
|
111 | 111 | // throttled. |
|
112 | 112 | if (this.msg_buffer !== null && |
|
113 | 113 | (this.get('msg_throttle') || 3) === this.pending_msgs) { |
|
114 | 114 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; |
|
115 | 115 | this.comm.send(data, callbacks); |
|
116 | 116 | this.msg_buffer = null; |
|
117 | 117 | } else { |
|
118 | 118 | --this.pending_msgs; |
|
119 | 119 | } |
|
120 | 120 | } |
|
121 | 121 | } |
|
122 | 122 | }, |
|
123 | 123 | |
|
124 | 124 | callbacks: function(view) { |
|
125 | 125 | // Create msg callbacks for a comm msg. |
|
126 | 126 | var callbacks = this.widget_manager.callbacks(view); |
|
127 | 127 | |
|
128 | 128 | if (callbacks.iopub === undefined) { |
|
129 | 129 | callbacks.iopub = {}; |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 132 | var that = this; |
|
133 | 133 | callbacks.iopub.status = function (msg) { |
|
134 | 134 | that._handle_status(msg, callbacks); |
|
135 | 135 | }; |
|
136 | 136 | return callbacks; |
|
137 | 137 | }, |
|
138 | 138 | |
|
139 | 139 | set: function(key, val, options) { |
|
140 | 140 | // Set a value. |
|
141 | 141 | var return_value = WidgetModel.__super__.set.apply(this, arguments); |
|
142 | 142 | |
|
143 | 143 | // Backbone only remembers the diff of the most recent set() |
|
144 | 144 | // operation. Calling set multiple times in a row results in a |
|
145 | 145 | // loss of diff information. Here we keep our own running diff. |
|
146 | 146 | this._buffered_state_diff = $.extend(this._buffered_state_diff, this.changedAttributes() || {}); |
|
147 | 147 | return return_value; |
|
148 | 148 | }, |
|
149 | 149 | |
|
150 | 150 | sync: function (method, model, options) { |
|
151 | 151 | // Handle sync to the back-end. Called when a model.save() is called. |
|
152 | 152 | |
|
153 | 153 | // Make sure a comm exists. |
|
154 | 154 | var error = options.error || function() { |
|
155 | 155 | console.error('Backbone sync error:', arguments); |
|
156 | 156 | }; |
|
157 | 157 | if (this.comm === undefined) { |
|
158 | 158 | error(); |
|
159 | 159 | return false; |
|
160 | 160 | } |
|
161 | 161 | |
|
162 | 162 | // Delete any key value pairs that the back-end already knows about. |
|
163 | 163 | var attrs = (method === 'patch') ? options.attrs : model.toJSON(options); |
|
164 | 164 | if (this.key_value_lock !== null) { |
|
165 | 165 | var key = this.key_value_lock[0]; |
|
166 | 166 | var value = this.key_value_lock[1]; |
|
167 | 167 | if (attrs[key] === value) { |
|
168 | 168 | delete attrs[key]; |
|
169 | 169 | } |
|
170 | 170 | } |
|
171 | 171 | |
|
172 | 172 | // Only sync if there are attributes to send to the back-end. |
|
173 | 173 | attrs = this._pack_models(attrs); |
|
174 | 174 | if (_.size(attrs) > 0) { |
|
175 | 175 | |
|
176 | 176 | // If this message was sent via backbone itself, it will not |
|
177 | 177 | // have any callbacks. It's important that we create callbacks |
|
178 | 178 | // so we can listen for status messages, etc... |
|
179 | 179 | var callbacks = options.callbacks || this.callbacks(); |
|
180 | 180 | |
|
181 | 181 | // Check throttle. |
|
182 | 182 | if (this.pending_msgs >= (this.get('msg_throttle') || 3)) { |
|
183 | 183 | // The throttle has been exceeded, buffer the current msg so |
|
184 | 184 | // it can be sent once the kernel has finished processing |
|
185 | 185 | // some of the existing messages. |
|
186 | 186 | |
|
187 | 187 | // Combine updates if it is a 'patch' sync, otherwise replace updates |
|
188 | 188 | switch (method) { |
|
189 | 189 | case 'patch': |
|
190 | 190 | this.msg_buffer = $.extend(this.msg_buffer || {}, attrs); |
|
191 | 191 | break; |
|
192 | 192 | case 'update': |
|
193 | 193 | case 'create': |
|
194 | 194 | this.msg_buffer = attrs; |
|
195 | 195 | break; |
|
196 | 196 | default: |
|
197 | 197 | error(); |
|
198 | 198 | return false; |
|
199 | 199 | } |
|
200 | 200 | this.msg_buffer_callbacks = callbacks; |
|
201 | 201 | |
|
202 | 202 | } else { |
|
203 | 203 | // We haven't exceeded the throttle, send the message like |
|
204 | 204 | // normal. |
|
205 | 205 | var data = {method: 'backbone', sync_data: attrs}; |
|
206 | 206 | this.comm.send(data, callbacks); |
|
207 | 207 | this.pending_msgs++; |
|
208 | 208 | } |
|
209 | 209 | } |
|
210 | 210 | // Since the comm is a one-way communication, assume the message |
|
211 | 211 | // arrived. Don't call success since we don't have a model back from the server |
|
212 | 212 | // this means we miss out on the 'sync' event. |
|
213 | 213 | this._buffered_state_diff = {}; |
|
214 | 214 | }, |
|
215 | 215 | |
|
216 | 216 | save_changes: function(callbacks) { |
|
217 | 217 | // Push this model's state to the back-end |
|
218 | 218 | // |
|
219 | 219 | // This invokes a Backbone.Sync. |
|
220 | 220 | this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks}); |
|
221 | 221 | }, |
|
222 | 222 | |
|
223 | 223 | _pack_models: function(value) { |
|
224 | 224 | // Replace models with model ids recursively. |
|
225 | 225 | if (value instanceof Backbone.Model) { |
|
226 | 226 | return value.id; |
|
227 | 227 | |
|
228 | 228 | } else if ($.isArray(value)) { |
|
229 | 229 | var packed = []; |
|
230 | 230 | var that = this; |
|
231 | 231 | _.each(value, function(sub_value, key) { |
|
232 | 232 | packed.push(that._pack_models(sub_value)); |
|
233 | 233 | }); |
|
234 | 234 | return packed; |
|
235 | 235 | |
|
236 | 236 | } else if (value instanceof Object) { |
|
237 | 237 | var packed = {}; |
|
238 | 238 | var that = this; |
|
239 | 239 | _.each(value, function(sub_value, key) { |
|
240 | 240 | packed[key] = that._pack_models(sub_value); |
|
241 | 241 | }); |
|
242 | 242 | return packed; |
|
243 | 243 | |
|
244 | 244 | } else { |
|
245 | 245 | return value; |
|
246 | 246 | } |
|
247 | 247 | }, |
|
248 | 248 | |
|
249 | 249 | _unpack_models: function(value) { |
|
250 | 250 | // Replace model ids with models recursively. |
|
251 | 251 | if ($.isArray(value)) { |
|
252 | 252 | var unpacked = []; |
|
253 | 253 | var that = this; |
|
254 | 254 | _.each(value, function(sub_value, key) { |
|
255 | 255 | unpacked.push(that._unpack_models(sub_value)); |
|
256 | 256 | }); |
|
257 | 257 | return unpacked; |
|
258 | 258 | |
|
259 | 259 | } else if (value instanceof Object) { |
|
260 | 260 | var unpacked = {}; |
|
261 | 261 | var that = this; |
|
262 | 262 | _.each(value, function(sub_value, key) { |
|
263 | 263 | unpacked[key] = that._unpack_models(sub_value); |
|
264 | 264 | }); |
|
265 | 265 | return unpacked; |
|
266 | 266 | |
|
267 | 267 | } else { |
|
268 | 268 | var model = this.widget_manager.get_model(value); |
|
269 | 269 | if (model) { |
|
270 | 270 | return model; |
|
271 | 271 | } else { |
|
272 | 272 | return value; |
|
273 | 273 | } |
|
274 | 274 | } |
|
275 | 275 | }, |
|
276 | 276 | |
|
277 | 277 | }); |
|
278 | 278 | WidgetManager.register_widget_model('WidgetModel', WidgetModel); |
|
279 | 279 | |
|
280 | 280 | |
|
281 | 281 | var WidgetView = Backbone.View.extend({ |
|
282 | 282 | initialize: function(parameters) { |
|
283 | 283 | // Public constructor. |
|
284 | 284 | this.model.on('change',this.update,this); |
|
285 | 285 | this.options = parameters.options; |
|
286 | 286 | this.child_model_views = {}; |
|
287 | 287 | this.child_views = {}; |
|
288 | 288 | this.model.views.push(this); |
|
289 | 289 | }, |
|
290 | 290 | |
|
291 | 291 | update: function(){ |
|
292 | 292 | // Triggered on model change. |
|
293 | 293 | // |
|
294 | 294 | // Update view to be consistent with this.model |
|
295 | 295 | }, |
|
296 | 296 | |
|
297 | 297 | create_child_view: function(child_model, options) { |
|
298 | 298 | // Create and return a child view. |
|
299 | 299 | // |
|
300 | 300 | // -given a model and (optionally) a view name if the view name is |
|
301 | 301 | // not given, it defaults to the model's default view attribute. |
|
302 | 302 | |
|
303 | 303 | // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior |
|
304 | 304 | // it would be great to have the widget manager add the cell metadata |
|
305 | 305 | // to the subview without having to add it here. |
|
306 | 306 | var child_view = this.model.widget_manager.create_view(child_model, options || {}, this); |
|
307 | 307 | |
|
308 | 308 | // Associate the view id with the model id. |
|
309 | 309 | if (this.child_model_views[child_model.id] === undefined) { |
|
310 | 310 | this.child_model_views[child_model.id] = []; |
|
311 | 311 | } |
|
312 | 312 | this.child_model_views[child_model.id].push(child_view.id); |
|
313 | 313 | |
|
314 | 314 | // Remember the view by id. |
|
315 | 315 | this.child_views[child_view.id] = child_view; |
|
316 | 316 | return child_view; |
|
317 | 317 | }, |
|
318 | 318 | |
|
319 | 319 | delete_child_view: function(child_model, options) { |
|
320 | 320 | // Delete a child view that was previously created using create_child_view. |
|
321 | 321 | var view_ids = this.child_model_views[child_model.id]; |
|
322 | 322 | if (view_ids !== undefined) { |
|
323 | 323 | |
|
324 |
// |
|
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
|
328 |
|
|
|
329 |
|
|
|
330 | child_model.views.pop(view); | |
|
324 | // Only delete the first view in the list. | |
|
325 | var view_id = view_ids[0]; | |
|
326 | var view = this.child_views[view_id]; | |
|
327 | delete this.child_views[view_id]; | |
|
328 | delete view_ids[0]; | |
|
329 | child_model.views.pop(view); | |
|
330 | ||
|
331 | // Remove the view list specific to this model if it is empty. | |
|
332 | if (view_ids.length === 0) { | |
|
333 | delete this.child_model_views[child_model.id]; | |
|
331 | 334 | } |
|
332 | ||
|
333 | // Remove the view list specific to this model. | |
|
334 | delete this.child_model_views[child_model.id]; | |
|
335 | return view; | |
|
335 | 336 | } |
|
337 | return null; | |
|
336 | 338 | }, |
|
337 | 339 | |
|
338 | 340 | do_diff: function(old_list, new_list, removed_callback, added_callback) { |
|
339 | 341 | // Difference a changed list and call remove and add callbacks for |
|
340 | 342 | // each removed and added item in the new list. |
|
341 | 343 | // |
|
342 | 344 | // Parameters |
|
343 | 345 | // ---------- |
|
344 | 346 | // old_list : array |
|
345 | 347 | // new_list : array |
|
346 | 348 | // removed_callback : Callback(item) |
|
347 | 349 | // Callback that is called for each item removed. |
|
348 | 350 | // added_callback : Callback(item) |
|
349 | 351 | // Callback that is called for each item added. |
|
350 | 352 | |
|
351 | 353 | |
|
352 | 354 | // removed items |
|
353 | 355 | _.each(this.difference(old_list, new_list), function(item, index, list) { |
|
354 | 356 | removed_callback(item); |
|
355 | 357 | }, this); |
|
356 | 358 | |
|
357 | 359 | // added items |
|
358 | 360 | _.each(this.difference(new_list, old_list), function(item, index, list) { |
|
359 | 361 | added_callback(item); |
|
360 | 362 | }, this); |
|
361 | 363 | }, |
|
362 | 364 | |
|
363 | 365 | difference: function(a, b) { |
|
364 | 366 | // Calculate the difference of two lists by contents. |
|
365 | 367 | // |
|
366 | 368 | // This function is like the underscore difference function |
|
367 | 369 | // except it will not fail when given a list with duplicates. |
|
368 | 370 | // i.e.: |
|
369 | 371 | // diff([1, 2, 2, 3], [3, 2]) |
|
370 | 372 | // Underscores results: |
|
371 | 373 | // [1] |
|
372 | 374 | // This method: |
|
373 | 375 | // [1, 2] |
|
374 | 376 | var contents = a.slice(0); |
|
375 | 377 | var found_index; |
|
376 | 378 | for (var i = 0; i < b.length; i++) { |
|
377 | 379 | found_index = _.indexOf(contents, b[i]); |
|
378 | 380 | if (found_index >= 0) { |
|
379 | 381 | contents.splice(found_index, 1); |
|
380 | 382 | } |
|
381 | 383 | } |
|
382 | 384 | return contents; |
|
383 | 385 | }, |
|
384 | 386 | |
|
385 | 387 | callbacks: function(){ |
|
386 | 388 | // Create msg callbacks for a comm msg. |
|
387 | 389 | return this.model.callbacks(this); |
|
388 | 390 | }, |
|
389 | 391 | |
|
390 | 392 | render: function(){ |
|
391 | 393 | // Render the view. |
|
392 | 394 | // |
|
393 | 395 | // By default, this is only called the first time the view is created |
|
394 | 396 | }, |
|
395 | 397 | |
|
396 | 398 | send: function (content) { |
|
397 | 399 | // Send a custom msg associated with this view. |
|
398 | 400 | this.model.send(content, this.callbacks()); |
|
399 | 401 | }, |
|
400 | 402 | |
|
401 | 403 | touch: function () { |
|
402 | 404 | this.model.save_changes(this.callbacks()); |
|
403 | 405 | }, |
|
404 | 406 | }); |
|
405 | 407 | |
|
406 | 408 | |
|
407 | 409 | var DOMWidgetView = WidgetView.extend({ |
|
408 | 410 | initialize: function (options) { |
|
409 | 411 | // Public constructor |
|
410 | 412 | |
|
411 | 413 | // In the future we may want to make changes more granular |
|
412 | 414 | // (e.g., trigger on visible:change). |
|
413 | 415 | this.model.on('change', this.update, this); |
|
414 | 416 | this.model.on('msg:custom', this.on_msg, this); |
|
415 | 417 | DOMWidgetView.__super__.initialize.apply(this, arguments); |
|
416 | 418 | }, |
|
417 | 419 | |
|
418 | 420 | on_msg: function(msg) { |
|
419 | 421 | // Handle DOM specific msgs. |
|
420 | 422 | switch(msg.msg_type) { |
|
421 | 423 | case 'add_class': |
|
422 | 424 | this.add_class(msg.selector, msg.class_list); |
|
423 | 425 | break; |
|
424 | 426 | case 'remove_class': |
|
425 | 427 | this.remove_class(msg.selector, msg.class_list); |
|
426 | 428 | break; |
|
427 | 429 | } |
|
428 | 430 | }, |
|
429 | 431 | |
|
430 | 432 | add_class: function (selector, class_list) { |
|
431 | 433 | // Add a DOM class to an element. |
|
432 | 434 | this._get_selector_element(selector).addClass(class_list); |
|
433 | 435 | }, |
|
434 | 436 | |
|
435 | 437 | remove_class: function (selector, class_list) { |
|
436 | 438 | // Remove a DOM class from an element. |
|
437 | 439 | this._get_selector_element(selector).removeClass(class_list); |
|
438 | 440 | }, |
|
439 | 441 | |
|
440 | 442 | update: function () { |
|
441 | 443 | // Update the contents of this view |
|
442 | 444 | // |
|
443 | 445 | // Called when the model is changed. The model may have been |
|
444 | 446 | // changed by another view or by a state update from the back-end. |
|
445 | 447 | // The very first update seems to happen before the element is |
|
446 | 448 | // finished rendering so we use setTimeout to give the element time |
|
447 | 449 | // to render |
|
448 | 450 | var e = this.$el; |
|
449 | 451 | var visible = this.model.get('visible'); |
|
450 | 452 | setTimeout(function() {e.toggle(visible);},0); |
|
451 | 453 | |
|
452 | 454 | var css = this.model.get('_css'); |
|
453 | 455 | if (css === undefined) {return;} |
|
454 | 456 | var that = this; |
|
455 | 457 | _.each(css, function(css_traits, selector){ |
|
456 | 458 | // Apply the css traits to all elements that match the selector. |
|
457 | 459 | var elements = that._get_selector_element(selector); |
|
458 | 460 | if (elements.length > 0) { |
|
459 | 461 | _.each(css_traits, function(css_value, css_key){ |
|
460 | 462 | elements.css(css_key, css_value); |
|
461 | 463 | }); |
|
462 | 464 | } |
|
463 | 465 | }); |
|
464 | 466 | }, |
|
465 | 467 | |
|
466 | 468 | _get_selector_element: function (selector) { |
|
467 | 469 | // Get the elements via the css selector. |
|
468 | 470 | |
|
469 | 471 | // If the selector is blank, apply the style to the $el_to_style |
|
470 | 472 | // element. If the $el_to_style element is not defined, use apply |
|
471 | 473 | // the style to the view's element. |
|
472 | 474 | var elements; |
|
473 | 475 | if (!selector) { |
|
474 | 476 | if (this.$el_to_style === undefined) { |
|
475 | 477 | elements = this.$el; |
|
476 | 478 | } else { |
|
477 | 479 | elements = this.$el_to_style; |
|
478 | 480 | } |
|
479 | 481 | } else { |
|
480 | 482 | elements = this.$el.find(selector); |
|
481 | 483 | } |
|
482 | 484 | return elements; |
|
483 | 485 | }, |
|
484 | 486 | }); |
|
485 | 487 | |
|
486 | 488 | IPython.WidgetModel = WidgetModel; |
|
487 | 489 | IPython.WidgetView = WidgetView; |
|
488 | 490 | IPython.DOMWidgetView = DOMWidgetView; |
|
489 | 491 | |
|
490 | 492 | // Pass through WidgetManager namespace. |
|
491 | 493 | return WidgetManager; |
|
492 | 494 | }); |
@@ -1,325 +1,323 | |||
|
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 | // ContainerWidget |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | 12 | /** |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | **/ |
|
16 | 16 | |
|
17 | 17 | define(["widgets/js/widget"], function(WidgetManager) { |
|
18 | 18 | |
|
19 | 19 | var ContainerView = IPython.DOMWidgetView.extend({ |
|
20 | 20 | render: function(){ |
|
21 | 21 | // Called when view is rendered. |
|
22 | 22 | this.$el.addClass('widget-container') |
|
23 | 23 | .addClass('vbox'); |
|
24 | 24 | this.children={}; |
|
25 |
this.update_children([], this.model.get(' |
|
|
26 |
this.model.on('change: |
|
|
27 |
this.update_children(model.previous(' |
|
|
25 | this.update_children([], this.model.get('children')); | |
|
26 | this.model.on('change:children', function(model, value, options) { | |
|
27 | this.update_children(model.previous('children'), value); | |
|
28 | 28 | }, this); |
|
29 | 29 | this.update(); |
|
30 | 30 | |
|
31 | 31 | // Trigger model displayed events for any models that are child to |
|
32 | 32 | // this model when this model is displayed. |
|
33 | 33 | var that = this; |
|
34 | 34 | this.model.on('displayed', function(){ |
|
35 | 35 | that.is_displayed = true; |
|
36 | 36 | for (var property in that.child_views) { |
|
37 | 37 | if (that.child_views.hasOwnProperty(property)) { |
|
38 | 38 | that.child_views[property].model.trigger('displayed'); |
|
39 | 39 | } |
|
40 | 40 | } |
|
41 | 41 | }); |
|
42 | 42 | }, |
|
43 | 43 | |
|
44 | 44 | update_children: function(old_list, new_list) { |
|
45 | 45 | // Called when the children list changes. |
|
46 | 46 | this.do_diff(old_list, |
|
47 | 47 | new_list, |
|
48 | 48 | $.proxy(this.remove_child_model, this), |
|
49 | 49 | $.proxy(this.add_child_model, this)); |
|
50 | 50 | }, |
|
51 | 51 | |
|
52 | 52 | remove_child_model: function(model) { |
|
53 | 53 | // Called when a model is removed from the children list. |
|
54 |
this.child_view |
|
|
55 | this.delete_child_view(model); | |
|
54 | this.delete_child_view(model).remove(); | |
|
56 | 55 | }, |
|
57 | 56 | |
|
58 | 57 | add_child_model: function(model) { |
|
59 | 58 | // Called when a model is added to the children list. |
|
60 | 59 | var view = this.create_child_view(model); |
|
61 | 60 | this.$el.append(view.$el); |
|
62 | 61 | |
|
63 | 62 | // Trigger the displayed event if this model is displayed. |
|
64 | 63 | if (this.is_displayed) { |
|
65 | 64 | model.trigger('displayed'); |
|
66 | 65 | } |
|
67 | 66 | }, |
|
68 | 67 | |
|
69 | 68 | update: function(){ |
|
70 | 69 | // Update the contents of this view |
|
71 | 70 | // |
|
72 | 71 | // Called when the model is changed. The model may have been |
|
73 | 72 | // changed by another view or by a state update from the back-end. |
|
74 | 73 | return ContainerView.__super__.update.apply(this); |
|
75 | 74 | }, |
|
76 | 75 | }); |
|
77 | 76 | |
|
78 | 77 | WidgetManager.register_widget_view('ContainerView', ContainerView); |
|
79 | 78 | |
|
80 | 79 | var PopupView = IPython.DOMWidgetView.extend({ |
|
81 | 80 | render: function(){ |
|
82 | 81 | // Called when view is rendered. |
|
83 | 82 | var that = this; |
|
84 | 83 | this.children={}; |
|
85 | 84 | |
|
86 | 85 | this.$el.on("remove", function(){ |
|
87 | 86 | that.$backdrop.remove(); |
|
88 | 87 | }); |
|
89 | 88 | this.$backdrop = $('<div />') |
|
90 | 89 | .appendTo($('#notebook-container')) |
|
91 | 90 | .addClass('modal-dialog') |
|
92 | 91 | .css('position', 'absolute') |
|
93 | 92 | .css('left', '0px') |
|
94 | 93 | .css('top', '0px'); |
|
95 | 94 | this.$window = $('<div />') |
|
96 | 95 | .appendTo(this.$backdrop) |
|
97 | 96 | .addClass('modal-content widget-modal') |
|
98 | 97 | .mousedown(function(){ |
|
99 | 98 | that.bring_to_front(); |
|
100 | 99 | }); |
|
101 | 100 | |
|
102 | 101 | // Set the elements array since the this.$window element is not child |
|
103 | 102 | // of this.$el and the parent widget manager or other widgets may |
|
104 | 103 | // need to know about all of the top-level widgets. The IPython |
|
105 | 104 | // widget manager uses this to register the elements with the |
|
106 | 105 | // keyboard manager. |
|
107 | 106 | this.additional_elements = [this.$window]; |
|
108 | 107 | |
|
109 | 108 | this.$title_bar = $('<div />') |
|
110 | 109 | .addClass('popover-title') |
|
111 | 110 | .appendTo(this.$window) |
|
112 | 111 | .mousedown(function(){ |
|
113 | 112 | that.bring_to_front(); |
|
114 | 113 | }); |
|
115 | 114 | this.$close = $('<button />') |
|
116 | 115 | .addClass('close icon-remove') |
|
117 | 116 | .css('margin-left', '5px') |
|
118 | 117 | .appendTo(this.$title_bar) |
|
119 | 118 | .click(function(){ |
|
120 | 119 | that.hide(); |
|
121 | 120 | event.stopPropagation(); |
|
122 | 121 | }); |
|
123 | 122 | this.$minimize = $('<button />') |
|
124 | 123 | .addClass('close icon-arrow-down') |
|
125 | 124 | .appendTo(this.$title_bar) |
|
126 | 125 | .click(function(){ |
|
127 | 126 | that.popped_out = !that.popped_out; |
|
128 | 127 | if (!that.popped_out) { |
|
129 | 128 | that.$minimize |
|
130 | 129 | .removeClass('icon-arrow-down') |
|
131 | 130 | .addClass('icon-arrow-up'); |
|
132 | 131 | |
|
133 | 132 | that.$window |
|
134 | 133 | .draggable('destroy') |
|
135 | 134 | .resizable('destroy') |
|
136 | 135 | .removeClass('widget-modal modal-content') |
|
137 | 136 | .addClass('docked-widget-modal') |
|
138 | 137 | .detach() |
|
139 | 138 | .insertBefore(that.$show_button); |
|
140 | 139 | that.$show_button.hide(); |
|
141 | 140 | that.$close.hide(); |
|
142 | 141 | } else { |
|
143 | 142 | that.$minimize |
|
144 | 143 | .addClass('icon-arrow-down') |
|
145 | 144 | .removeClass('icon-arrow-up'); |
|
146 | 145 | |
|
147 | 146 | that.$window |
|
148 | 147 | .removeClass('docked-widget-modal') |
|
149 | 148 | .addClass('widget-modal modal-content') |
|
150 | 149 | .detach() |
|
151 | 150 | .appendTo(that.$backdrop) |
|
152 | 151 | .draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'}) |
|
153 | 152 | .resizable() |
|
154 | 153 | .children('.ui-resizable-handle').show(); |
|
155 | 154 | that.show(); |
|
156 | 155 | that.$show_button.show(); |
|
157 | 156 | that.$close.show(); |
|
158 | 157 | } |
|
159 | 158 | event.stopPropagation(); |
|
160 | 159 | }); |
|
161 | 160 | this.$title = $('<div />') |
|
162 | 161 | .addClass('widget-modal-title') |
|
163 | 162 | .html(" ") |
|
164 | 163 | .appendTo(this.$title_bar); |
|
165 | 164 | this.$body = $('<div />') |
|
166 | 165 | .addClass('modal-body') |
|
167 | 166 | .addClass('widget-modal-body') |
|
168 | 167 | .addClass('widget-container') |
|
169 | 168 | .addClass('vbox') |
|
170 | 169 | .appendTo(this.$window); |
|
171 | 170 | |
|
172 | 171 | this.$show_button = $('<button />') |
|
173 | 172 | .html(" ") |
|
174 | 173 | .addClass('btn btn-info widget-modal-show') |
|
175 | 174 | .appendTo(this.$el) |
|
176 | 175 | .click(function(){ |
|
177 | 176 | that.show(); |
|
178 | 177 | }); |
|
179 | 178 | |
|
180 | 179 | this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'}); |
|
181 | 180 | this.$window.resizable(); |
|
182 | 181 | this.$window.on('resize', function(){ |
|
183 | 182 | that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight()); |
|
184 | 183 | }); |
|
185 | 184 | |
|
186 | 185 | this.$el_to_style = this.$body; |
|
187 | 186 | this._shown_once = false; |
|
188 | 187 | this.popped_out = true; |
|
189 | 188 | |
|
190 |
this.update_children([], this.model.get(' |
|
|
191 |
this.model.on('change: |
|
|
192 |
this.update_children(model.previous(' |
|
|
189 | this.update_children([], this.model.get('children')); | |
|
190 | this.model.on('change:children', function(model, value, options) { | |
|
191 | this.update_children(model.previous('children'), value); | |
|
193 | 192 | }, this); |
|
194 | 193 | this.update(); |
|
195 | 194 | |
|
196 | 195 | // Trigger model displayed events for any models that are child to |
|
197 | 196 | // this model when this model is displayed. |
|
198 | 197 | this.model.on('displayed', function(){ |
|
199 | 198 | that.is_displayed = true; |
|
200 | 199 | for (var property in that.child_views) { |
|
201 | 200 | if (that.child_views.hasOwnProperty(property)) { |
|
202 | 201 | that.child_views[property].model.trigger('displayed'); |
|
203 | 202 | } |
|
204 | 203 | } |
|
205 | 204 | }); |
|
206 | 205 | }, |
|
207 | 206 | |
|
208 | 207 | hide: function() { |
|
209 | 208 | // Called when the modal hide button is clicked. |
|
210 | 209 | this.$window.hide(); |
|
211 | 210 | this.$show_button.removeClass('btn-info'); |
|
212 | 211 | }, |
|
213 | 212 | |
|
214 | 213 | show: function() { |
|
215 | 214 | // Called when the modal show button is clicked. |
|
216 | 215 | this.$show_button.addClass('btn-info'); |
|
217 | 216 | this.$window.show(); |
|
218 | 217 | if (this.popped_out) { |
|
219 | 218 | this.$window.css("positon", "absolute"); |
|
220 | 219 | this.$window.css("top", "0px"); |
|
221 | 220 | this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) + |
|
222 | 221 | $(window).scrollLeft()) + "px"); |
|
223 | 222 | this.bring_to_front(); |
|
224 | 223 | } |
|
225 | 224 | }, |
|
226 | 225 | |
|
227 | 226 | bring_to_front: function() { |
|
228 | 227 | // Make the modal top-most, z-ordered about the other modals. |
|
229 | 228 | var $widget_modals = $(".widget-modal"); |
|
230 | 229 | var max_zindex = 0; |
|
231 | 230 | $widget_modals.each(function (index, el){ |
|
232 | 231 | var zindex = parseInt($(el).css('z-index')); |
|
233 | 232 | if (!isNaN(zindex)) { |
|
234 | 233 | max_zindex = Math.max(max_zindex, zindex); |
|
235 | 234 | } |
|
236 | 235 | }); |
|
237 | 236 | |
|
238 | 237 | // Start z-index of widget modals at 2000 |
|
239 | 238 | max_zindex = Math.max(max_zindex, 2000); |
|
240 | 239 | |
|
241 | 240 | $widget_modals.each(function (index, el){ |
|
242 | 241 | $el = $(el); |
|
243 | 242 | if (max_zindex == parseInt($el.css('z-index'))) { |
|
244 | 243 | $el.css('z-index', max_zindex - 1); |
|
245 | 244 | } |
|
246 | 245 | }); |
|
247 | 246 | this.$window.css('z-index', max_zindex); |
|
248 | 247 | }, |
|
249 | 248 | |
|
250 | 249 | update_children: function(old_list, new_list) { |
|
251 | 250 | // Called when the children list is modified. |
|
252 | 251 | this.do_diff(old_list, |
|
253 | 252 | new_list, |
|
254 | 253 | $.proxy(this.remove_child_model, this), |
|
255 | 254 | $.proxy(this.add_child_model, this)); |
|
256 | 255 | }, |
|
257 | 256 | |
|
258 | 257 | remove_child_model: function(model) { |
|
259 | 258 | // Called when a child is removed from children list. |
|
260 |
this.child_view |
|
|
261 | this.delete_child_view(model); | |
|
259 | this.delete_child_view(model).remove(); | |
|
262 | 260 | }, |
|
263 | 261 | |
|
264 | 262 | add_child_model: function(model) { |
|
265 | 263 | // Called when a child is added to children list. |
|
266 | 264 | var view = this.create_child_view(model); |
|
267 | 265 | this.$body.append(view.$el); |
|
268 | 266 | |
|
269 | 267 | // Trigger the displayed event if this model is displayed. |
|
270 | 268 | if (this.is_displayed) { |
|
271 | 269 | model.trigger('displayed'); |
|
272 | 270 | } |
|
273 | 271 | }, |
|
274 | 272 | |
|
275 | 273 | update: function(){ |
|
276 | 274 | // Update the contents of this view |
|
277 | 275 | // |
|
278 | 276 | // Called when the model is changed. The model may have been |
|
279 | 277 | // changed by another view or by a state update from the back-end. |
|
280 | 278 | var description = this.model.get('description'); |
|
281 | 279 | if (description.trim().length === 0) { |
|
282 | 280 | this.$title.html(" "); // Preserve title height |
|
283 | 281 | } else { |
|
284 | 282 | this.$title.text(description); |
|
285 | 283 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$title.get(0)]); |
|
286 | 284 | } |
|
287 | 285 | |
|
288 | 286 | var button_text = this.model.get('button_text'); |
|
289 | 287 | if (button_text.trim().length === 0) { |
|
290 | 288 | this.$show_button.html(" "); // Preserve button height |
|
291 | 289 | } else { |
|
292 | 290 | this.$show_button.text(button_text); |
|
293 | 291 | } |
|
294 | 292 | |
|
295 | 293 | if (!this._shown_once) { |
|
296 | 294 | this._shown_once = true; |
|
297 | 295 | this.show(); |
|
298 | 296 | } |
|
299 | 297 | |
|
300 | 298 | return PopupView.__super__.update.apply(this); |
|
301 | 299 | }, |
|
302 | 300 | |
|
303 | 301 | _get_selector_element: function(selector) { |
|
304 | 302 | // Get an element view a 'special' jquery selector. (see widget.js) |
|
305 | 303 | // |
|
306 | 304 | // Since the modal actually isn't within the $el in the DOM, we need to extend |
|
307 | 305 | // the selector logic to allow the user to set css on the modal if need be. |
|
308 | 306 | // The convention used is: |
|
309 | 307 | // "modal" - select the modal div |
|
310 | 308 | // "modal [selector]" - select element(s) within the modal div. |
|
311 | 309 | // "[selector]" - select elements within $el |
|
312 | 310 | // "" - select the $el_to_style |
|
313 | 311 | if (selector.substring(0, 5) == 'modal') { |
|
314 | 312 | if (selector == 'modal') { |
|
315 | 313 | return this.$window; |
|
316 | 314 | } else { |
|
317 | 315 | return this.$window.find(selector.substring(6)); |
|
318 | 316 | } |
|
319 | 317 | } else { |
|
320 | 318 | return PopupView.__super__._get_selector_element.apply(this, [selector]); |
|
321 | 319 | } |
|
322 | 320 | }, |
|
323 | 321 | }); |
|
324 | 322 | WidgetManager.register_widget_view('PopupView', PopupView); |
|
325 | 323 | }); |
@@ -1,273 +1,272 | |||
|
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 | // SelectionContainerWidget |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | 12 | /** |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | **/ |
|
16 | 16 | |
|
17 | 17 | define(["widgets/js/widget"], function(WidgetManager){ |
|
18 | 18 | |
|
19 | 19 | var AccordionView = IPython.DOMWidgetView.extend({ |
|
20 | 20 | render: function(){ |
|
21 | 21 | // Called when view is rendered. |
|
22 | 22 | var guid = 'panel-group' + IPython.utils.uuid(); |
|
23 | 23 | this.$el |
|
24 | 24 | .attr('id', guid) |
|
25 | 25 | .addClass('panel-group'); |
|
26 | 26 | this.containers = []; |
|
27 | 27 | this.model_containers = {}; |
|
28 |
this.update_children([], this.model.get(' |
|
|
29 |
this.model.on('change: |
|
|
30 |
this.update_children(model.previous(' |
|
|
28 | this.update_children([], this.model.get('children')); | |
|
29 | this.model.on('change:children', function(model, value, options) { | |
|
30 | this.update_children(model.previous('children'), value); | |
|
31 | 31 | }, this); |
|
32 | 32 | this.model.on('change:selected_index', function(model, value, options) { |
|
33 | 33 | this.update_selected_index(model.previous('selected_index'), value, options); |
|
34 | 34 | }, this); |
|
35 | 35 | this.model.on('change:_titles', function(model, value, options) { |
|
36 | 36 | this.update_titles(value); |
|
37 | 37 | }, this); |
|
38 | 38 | var that = this; |
|
39 | 39 | this.model.on('displayed', function() { |
|
40 | 40 | this.update_titles(); |
|
41 | 41 | // Trigger model displayed events for any models that are child to |
|
42 | 42 | // this model when this model is displayed. |
|
43 | 43 | that.is_displayed = true; |
|
44 | 44 | for (var property in that.child_views) { |
|
45 | 45 | if (that.child_views.hasOwnProperty(property)) { |
|
46 | 46 | that.child_views[property].model.trigger('displayed'); |
|
47 | 47 | } |
|
48 | 48 | } |
|
49 | 49 | }, this); |
|
50 | 50 | }, |
|
51 | 51 | |
|
52 | 52 | update_titles: function(titles) { |
|
53 | 53 | // Set tab titles |
|
54 | 54 | if (!titles) { |
|
55 | 55 | titles = this.model.get('_titles'); |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | var that = this; |
|
59 | 59 | _.each(titles, function(title, page_index) { |
|
60 | 60 | var accordian = that.containers[page_index]; |
|
61 | 61 | if (accordian !== undefined) { |
|
62 | 62 | accordian |
|
63 | 63 | .find('.panel-heading') |
|
64 | 64 | .find('.accordion-toggle') |
|
65 | 65 | .text(title); |
|
66 | 66 | } |
|
67 | 67 | }); |
|
68 | 68 | }, |
|
69 | 69 | |
|
70 | 70 | update_selected_index: function(old_index, new_index, options) { |
|
71 | 71 | // Only update the selection if the selection wasn't triggered |
|
72 | 72 | // by the front-end. It must be triggered by the back-end. |
|
73 | 73 | if (options === undefined || options.updated_view != this) { |
|
74 | 74 | this.containers[old_index].find('.panel-collapse').collapse('hide'); |
|
75 | 75 | if (0 <= new_index && new_index < this.containers.length) { |
|
76 | 76 | this.containers[new_index].find('.panel-collapse').collapse('show'); |
|
77 | 77 | } |
|
78 | 78 | } |
|
79 | 79 | }, |
|
80 | 80 | |
|
81 | 81 | update_children: function(old_list, new_list) { |
|
82 | 82 | // Called when the children list is modified. |
|
83 | 83 | this.do_diff(old_list, |
|
84 | 84 | new_list, |
|
85 | 85 | $.proxy(this.remove_child_model, this), |
|
86 | 86 | $.proxy(this.add_child_model, this)); |
|
87 | 87 | }, |
|
88 | 88 | |
|
89 | 89 | remove_child_model: function(model) { |
|
90 | 90 | // Called when a child is removed from children list. |
|
91 | 91 | var accordion_group = this.model_containers[model.id]; |
|
92 | 92 | this.containers.splice(accordion_group.container_index, 1); |
|
93 | 93 | delete this.model_containers[model.id]; |
|
94 | 94 | accordion_group.remove(); |
|
95 | 95 | this.delete_child_view(model); |
|
96 | 96 | }, |
|
97 | 97 | |
|
98 | 98 | add_child_model: function(model) { |
|
99 | 99 | // Called when a child is added to children list. |
|
100 | 100 | var view = this.create_child_view(model); |
|
101 | 101 | var index = this.containers.length; |
|
102 | 102 | var uuid = IPython.utils.uuid(); |
|
103 | 103 | var accordion_group = $('<div />') |
|
104 | 104 | .addClass('panel panel-default') |
|
105 | 105 | .appendTo(this.$el); |
|
106 | 106 | var accordion_heading = $('<div />') |
|
107 | 107 | .addClass('panel-heading') |
|
108 | 108 | .appendTo(accordion_group); |
|
109 | 109 | var that = this; |
|
110 | 110 | var accordion_toggle = $('<a />') |
|
111 | 111 | .addClass('accordion-toggle') |
|
112 | 112 | .attr('data-toggle', 'collapse') |
|
113 | 113 | .attr('data-parent', '#' + this.$el.attr('id')) |
|
114 | 114 | .attr('href', '#' + uuid) |
|
115 | 115 | .click(function(evt){ |
|
116 | 116 | |
|
117 | 117 | // Calling model.set will trigger all of the other views of the |
|
118 | 118 | // model to update. |
|
119 | 119 | that.model.set("selected_index", index, {updated_view: that}); |
|
120 | 120 | that.touch(); |
|
121 | 121 | }) |
|
122 | 122 | .text('Page ' + index) |
|
123 | 123 | .appendTo(accordion_heading); |
|
124 | 124 | var accordion_body = $('<div />', {id: uuid}) |
|
125 | 125 | .addClass('panel-collapse collapse') |
|
126 | 126 | .appendTo(accordion_group); |
|
127 | 127 | var accordion_inner = $('<div />') |
|
128 | 128 | .addClass('panel-body') |
|
129 | 129 | .appendTo(accordion_body); |
|
130 | 130 | var container_index = this.containers.push(accordion_group) - 1; |
|
131 | 131 | accordion_group.container_index = container_index; |
|
132 | 132 | this.model_containers[model.id] = accordion_group; |
|
133 | 133 | accordion_inner.append(view.$el); |
|
134 | 134 | |
|
135 | 135 | this.update(); |
|
136 | 136 | this.update_titles(); |
|
137 | 137 | |
|
138 | 138 | // Trigger the displayed event if this model is displayed. |
|
139 | 139 | if (this.is_displayed) { |
|
140 | 140 | model.trigger('displayed'); |
|
141 | 141 | } |
|
142 | 142 | }, |
|
143 | 143 | }); |
|
144 | 144 | WidgetManager.register_widget_view('AccordionView', AccordionView); |
|
145 | 145 | |
|
146 | 146 | |
|
147 | 147 | var TabView = IPython.DOMWidgetView.extend({ |
|
148 | 148 | initialize: function() { |
|
149 | 149 | // Public constructor. |
|
150 | 150 | this.containers = []; |
|
151 | 151 | TabView.__super__.initialize.apply(this, arguments); |
|
152 | 152 | }, |
|
153 | 153 | |
|
154 | 154 | render: function(){ |
|
155 | 155 | // Called when view is rendered. |
|
156 | 156 | var uuid = 'tabs'+IPython.utils.uuid(); |
|
157 | 157 | var that = this; |
|
158 | 158 | this.$tabs = $('<div />', {id: uuid}) |
|
159 | 159 | .addClass('nav') |
|
160 | 160 | .addClass('nav-tabs') |
|
161 | 161 | .appendTo(this.$el); |
|
162 | 162 | this.$tab_contents = $('<div />', {id: uuid + 'Content'}) |
|
163 | 163 | .addClass('tab-content') |
|
164 | 164 | .appendTo(this.$el); |
|
165 | 165 | this.containers = []; |
|
166 |
this.update_children([], this.model.get(' |
|
|
167 |
this.model.on('change: |
|
|
168 |
this.update_children(model.previous(' |
|
|
166 | this.update_children([], this.model.get('children')); | |
|
167 | this.model.on('change:children', function(model, value, options) { | |
|
168 | this.update_children(model.previous('children'), value); | |
|
169 | 169 | }, this); |
|
170 | 170 | |
|
171 | 171 | // Trigger model displayed events for any models that are child to |
|
172 | 172 | // this model when this model is displayed. |
|
173 | 173 | this.model.on('displayed', function(){ |
|
174 | 174 | that.is_displayed = true; |
|
175 | 175 | for (var property in that.child_views) { |
|
176 | 176 | if (that.child_views.hasOwnProperty(property)) { |
|
177 | 177 | that.child_views[property].model.trigger('displayed'); |
|
178 | 178 | } |
|
179 | 179 | } |
|
180 | 180 | }); |
|
181 | 181 | }, |
|
182 | 182 | |
|
183 | 183 | update_children: function(old_list, new_list) { |
|
184 | 184 | // Called when the children list is modified. |
|
185 | 185 | this.do_diff(old_list, |
|
186 | 186 | new_list, |
|
187 | 187 | $.proxy(this.remove_child_model, this), |
|
188 | 188 | $.proxy(this.add_child_model, this)); |
|
189 | 189 | }, |
|
190 | 190 | |
|
191 | 191 | remove_child_model: function(model) { |
|
192 | 192 | // Called when a child is removed from children list. |
|
193 |
var view = this.child_view |
|
|
193 | var view = this.delete_child_view(model); | |
|
194 | 194 | this.containers.splice(view.parent_tab.tab_text_index, 1); |
|
195 | 195 | view.parent_tab.remove(); |
|
196 | 196 | view.parent_container.remove(); |
|
197 | 197 | view.remove(); |
|
198 | this.delete_child_view(model); | |
|
199 | 198 | }, |
|
200 | 199 | |
|
201 | 200 | add_child_model: function(model) { |
|
202 | 201 | // Called when a child is added to children list. |
|
203 | 202 | var view = this.create_child_view(model); |
|
204 | 203 | var index = this.containers.length; |
|
205 | 204 | var uuid = IPython.utils.uuid(); |
|
206 | 205 | |
|
207 | 206 | var that = this; |
|
208 | 207 | var tab = $('<li />') |
|
209 | 208 | .css('list-style-type', 'none') |
|
210 | 209 | .appendTo(this.$tabs); |
|
211 | 210 | view.parent_tab = tab; |
|
212 | 211 | |
|
213 | 212 | var tab_text = $('<a />') |
|
214 | 213 | .attr('href', '#' + uuid) |
|
215 | 214 | .attr('data-toggle', 'tab') |
|
216 | 215 | .text('Page ' + index) |
|
217 | 216 | .appendTo(tab) |
|
218 | 217 | .click(function (e) { |
|
219 | 218 | |
|
220 | 219 | // Calling model.set will trigger all of the other views of the |
|
221 | 220 | // model to update. |
|
222 | 221 | that.model.set("selected_index", index, {updated_view: this}); |
|
223 | 222 | that.touch(); |
|
224 | 223 | that.select_page(index); |
|
225 | 224 | }); |
|
226 | 225 | tab.tab_text_index = this.containers.push(tab_text) - 1; |
|
227 | 226 | |
|
228 | 227 | var contents_div = $('<div />', {id: uuid}) |
|
229 | 228 | .addClass('tab-pane') |
|
230 | 229 | .addClass('fade') |
|
231 | 230 | .append(view.$el) |
|
232 | 231 | .appendTo(this.$tab_contents); |
|
233 | 232 | view.parent_container = contents_div; |
|
234 | 233 | |
|
235 | 234 | // Trigger the displayed event if this model is displayed. |
|
236 | 235 | if (this.is_displayed) { |
|
237 | 236 | model.trigger('displayed'); |
|
238 | 237 | } |
|
239 | 238 | }, |
|
240 | 239 | |
|
241 | 240 | update: function(options) { |
|
242 | 241 | // Update the contents of this view |
|
243 | 242 | // |
|
244 | 243 | // Called when the model is changed. The model may have been |
|
245 | 244 | // changed by another view or by a state update from the back-end. |
|
246 | 245 | if (options === undefined || options.updated_view != this) { |
|
247 | 246 | // Set tab titles |
|
248 | 247 | var titles = this.model.get('_titles'); |
|
249 | 248 | var that = this; |
|
250 | 249 | _.each(titles, function(title, page_index) { |
|
251 | 250 | var tab_text = that.containers[page_index]; |
|
252 | 251 | if (tab_text !== undefined) { |
|
253 | 252 | tab_text.text(title); |
|
254 | 253 | } |
|
255 | 254 | }); |
|
256 | 255 | |
|
257 | 256 | var selected_index = this.model.get('selected_index'); |
|
258 | 257 | if (0 <= selected_index && selected_index < this.containers.length) { |
|
259 | 258 | this.select_page(selected_index); |
|
260 | 259 | } |
|
261 | 260 | } |
|
262 | 261 | return TabView.__super__.update.apply(this); |
|
263 | 262 | }, |
|
264 | 263 | |
|
265 | 264 | select_page: function(index) { |
|
266 | 265 | // Select a page. |
|
267 | 266 | this.$tabs.find('li') |
|
268 | 267 | .removeClass('active'); |
|
269 | 268 | this.containers[index].tab('show'); |
|
270 | 269 | }, |
|
271 | 270 | }); |
|
272 | 271 | WidgetManager.register_widget_view('TabView', TabView); |
|
273 | 272 | }); |
@@ -1,35 +1,33 | |||
|
1 | 1 | """ContainerWidget class. |
|
2 | 2 | |
|
3 | 3 | Represents a container that can be used to group other widgets. |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | # Copyright (c) IPython Development Team. |
|
7 | 7 | # Distributed under the terms of the Modified BSD License. |
|
8 | 8 | |
|
9 | 9 | from .widget import DOMWidget |
|
10 | 10 | from IPython.utils.traitlets import Unicode, Tuple, TraitError |
|
11 | 11 | |
|
12 | 12 | class ContainerWidget(DOMWidget): |
|
13 | 13 | _view_name = Unicode('ContainerView', sync=True) |
|
14 | 14 | |
|
15 | 15 | # Child widgets in the container. |
|
16 | 16 | # Using a tuple here to force reassignment to update the list. |
|
17 | 17 | # When a proper notifying-list trait exists, that is what should be used here. |
|
18 | children = Tuple() | |
|
19 | _children = Tuple(sync=True) | |
|
20 | ||
|
18 | children = Tuple(sync=True) | |
|
21 | 19 | |
|
22 | 20 | def __init__(self, **kwargs): |
|
23 | 21 | super(ContainerWidget, self).__init__(**kwargs) |
|
24 | 22 | self.on_displayed(ContainerWidget._fire_children_displayed) |
|
25 | 23 | |
|
26 | 24 | def _fire_children_displayed(self): |
|
27 |
for child in self. |
|
|
25 | for child in self.children: | |
|
28 | 26 | child._handle_displayed() |
|
29 | 27 | |
|
30 | 28 | |
|
31 | 29 | class PopupWidget(ContainerWidget): |
|
32 | 30 | _view_name = Unicode('PopupView', sync=True) |
|
33 | 31 | |
|
34 | 32 | description = Unicode(sync=True) |
|
35 | 33 | button_text = Unicode(sync=True) |
General Comments 0
You need to be logged in to leave comments.
Login now