##// END OF EJS Templates
Merge pull request #5896 from ellisonbg/widget-fixes...
Min RK -
r16833:3c6f9298 merge
parent child Browse files
Show More
@@ -1,210 +1,213 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 (_, Backbone) {
27 27
28 28 //--------------------------------------------------------------------
29 29 // WidgetManager class
30 30 //--------------------------------------------------------------------
31 31 var WidgetManager = function (comm_manager) {
32 32 // Public constructor
33 33 WidgetManager._managers.push(this);
34 34
35 35 // Attach a comm manager to the
36 36 this.comm_manager = comm_manager;
37 37 this._models = {}; /* Dictionary of model ids and model instances */
38 38
39 39 // Register already-registered widget model types with the comm manager.
40 40 var that = this;
41 41 _.each(WidgetManager._model_types, function(model_type, model_name) {
42 42 that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that));
43 43 });
44 44 };
45 45
46 46 //--------------------------------------------------------------------
47 47 // Class level
48 48 //--------------------------------------------------------------------
49 49 WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */
50 50 WidgetManager._view_types = {}; /* Dictionary of view names and view types. */
51 51 WidgetManager._managers = []; /* List of widget managers */
52 52
53 53 WidgetManager.register_widget_model = function (model_name, model_type) {
54 54 // Registers a widget model by name.
55 55 WidgetManager._model_types[model_name] = model_type;
56 56
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 _.each(WidgetManager._managers, function(instance, i) {
60 60 if (instance.comm_manager !== null) {
61 61 instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance));
62 62 }
63 63 });
64 64 };
65 65
66 66 WidgetManager.register_widget_view = function (view_name, view_type) {
67 67 // Registers a widget view by name.
68 68 WidgetManager._view_types[view_name] = view_type;
69 69 };
70 70
71 71 //--------------------------------------------------------------------
72 72 // Instance level
73 73 //--------------------------------------------------------------------
74 74 WidgetManager.prototype.display_view = function(msg, model) {
75 75 // Displays a view for a particular model.
76 76 var cell = this.get_msg_cell(msg.parent_header.msg_id);
77 77 if (cell === null) {
78 78 console.log("Could not determine where the display" +
79 79 " message was from. Widget will not be displayed");
80 80 } else {
81 81 var view = this.create_view(model, {cell: cell});
82 82 if (view === null) {
83 83 console.error("View creation failed", model);
84 84 }
85 85 if (cell.widget_subarea) {
86 86 cell.widget_area.show();
87 87 this._handle_display_view(view);
88 88 cell.widget_subarea.append(view.$el);
89 89 }
90 90 }
91 91 };
92 92
93 93 WidgetManager.prototype._handle_display_view = function (view) {
94 94 // Have the IPython keyboard manager disable its event
95 95 // handling so the widget can capture keyboard input.
96 96 // Note, this is only done on the outer most widgets.
97 97 IPython.keyboard_manager.register_events(view.$el);
98 98
99 99 if (view.additional_elements) {
100 100 for (var i = 0; i < view.additional_elements.length; i++) {
101 101 IPython.keyboard_manager.register_events(view.additional_elements[i]);
102 102 }
103 103 }
104 104 };
105 105
106 106 WidgetManager.prototype.create_view = function(model, options, view) {
107 107 // Creates a view for a particular model.
108 108 var view_name = model.get('_view_name');
109 109 var ViewType = WidgetManager._view_types[view_name];
110 110 if (ViewType) {
111 111
112 112 // If a view is passed into the method, use that view's cell as
113 113 // the cell for the view that is created.
114 114 options = options || {};
115 115 if (view !== undefined) {
116 116 options.cell = view.options.cell;
117 117 }
118 118
119 119 // Create and render the view...
120 120 var parameters = {model: model, options: options};
121 121 view = new ViewType(parameters);
122 122 view.render();
123 model.views.push(view);
124 123 model.on('destroy', view.remove, view);
125 124 return view;
126 125 }
127 126 return null;
128 127 };
129 128
130 129 WidgetManager.prototype.get_msg_cell = function (msg_id) {
131 130 var cell = null;
132 131 // First, check to see if the msg was triggered by cell execution.
133 132 if (IPython.notebook) {
134 133 cell = IPython.notebook.get_msg_cell(msg_id);
135 134 }
136 135 if (cell !== null) {
137 136 return cell;
138 137 }
139 138 // Second, check to see if a get_cell callback was defined
140 139 // for the message. get_cell callbacks are registered for
141 140 // widget messages, so this block is actually checking to see if the
142 141 // message was triggered by a widget.
143 142 var kernel = this.comm_manager.kernel;
144 143 if (kernel) {
145 144 var callbacks = kernel.get_callbacks_for_msg(msg_id);
146 145 if (callbacks && callbacks.iopub &&
147 146 callbacks.iopub.get_cell !== undefined) {
148 147 return callbacks.iopub.get_cell();
149 148 }
150 149 }
151 150
152 151 // Not triggered by a cell or widget (no get_cell callback
153 152 // exists).
154 153 return null;
155 154 };
156 155
157 156 WidgetManager.prototype.callbacks = function (view) {
158 157 // callback handlers specific a view
159 158 var callbacks = {};
160 159 if (view && view.options.cell) {
161 160
162 161 // Try to get output handlers
163 162 var cell = view.options.cell;
164 163 var handle_output = null;
165 164 var handle_clear_output = null;
166 165 if (cell.output_area) {
167 166 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
168 167 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
169 168 }
170 169
171 170 // Create callback dict using what is known
172 171 var that = this;
173 172 callbacks = {
174 173 iopub : {
175 174 output : handle_output,
176 175 clear_output : handle_clear_output,
177 176
178 177 // Special function only registered by widget messages.
179 178 // Allows us to get the cell for a message so we know
180 179 // where to add widgets if the code requires it.
181 180 get_cell : function () {
182 181 return cell;
183 182 },
184 183 },
185 184 };
186 185 }
187 186 return callbacks;
188 187 };
189 188
190 189 WidgetManager.prototype.get_model = function (model_id) {
191 190 // Look-up a model instance by its id.
192 191 var model = this._models[model_id];
193 192 if (model !== undefined && model.id == model_id) {
194 193 return model;
195 194 }
196 195 return null;
197 196 };
198 197
199 198 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
200 199 // Handle when a comm is opened.
200 var that = this;
201 201 var model_id = comm.comm_id;
202 202 var widget_type_name = msg.content.target_name;
203 203 var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
204 widget_model.on('comm:close', function () {
205 delete that._models[model_id];
206 });
204 207 this._models[model_id] = widget_model;
205 208 };
206 209
207 210 IPython.WidgetManager = WidgetManager;
208 211 return IPython.WidgetManager;
209 212 });
210 213 }());
@@ -1,451 +1,452 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 // 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_views = [];
287 287 this.model.views.push(this);
288 288 },
289 289
290 290 update: function(){
291 291 // Triggered on model change.
292 292 //
293 293 // Update view to be consistent with this.model
294 294 },
295 295
296 296 create_child_view: function(child_model, options) {
297 297 // Create and return a child view.
298 298 //
299 299 // -given a model and (optionally) a view name if the view name is
300 300 // not given, it defaults to the model's default view attribute.
301 301
302 302 // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior
303 303 // it would be great to have the widget manager add the cell metadata
304 304 // to the subview without having to add it here.
305 305 var child_view = this.model.widget_manager.create_view(child_model, options || {}, this);
306 306 this.child_views[child_model.id] = child_view;
307 307 return child_view;
308 308 },
309 309
310 310 delete_child_view: function(child_model, options) {
311 311 // Delete a child view that was previously created using create_child_view.
312 312 var view = this.child_views[child_model.id];
313 313 if (view !== undefined) {
314 314 delete this.child_views[child_model.id];
315 315 view.remove();
316 child_model.views.pop(view);
316 317 }
317 318 },
318 319
319 320 do_diff: function(old_list, new_list, removed_callback, added_callback) {
320 321 // Difference a changed list and call remove and add callbacks for
321 322 // each removed and added item in the new list.
322 323 //
323 324 // Parameters
324 325 // ----------
325 326 // old_list : array
326 327 // new_list : array
327 328 // removed_callback : Callback(item)
328 329 // Callback that is called for each item removed.
329 330 // added_callback : Callback(item)
330 331 // Callback that is called for each item added.
331 332
332 333
333 334 // removed items
334 335 _.each(_.difference(old_list, new_list), function(item, index, list) {
335 336 removed_callback(item);
336 337 }, this);
337 338
338 339 // added items
339 340 _.each(_.difference(new_list, old_list), function(item, index, list) {
340 341 added_callback(item);
341 342 }, this);
342 343 },
343 344
344 345 callbacks: function(){
345 346 // Create msg callbacks for a comm msg.
346 347 return this.model.callbacks(this);
347 348 },
348 349
349 350 render: function(){
350 351 // Render the view.
351 352 //
352 353 // By default, this is only called the first time the view is created
353 354 },
354 355
355 356 send: function (content) {
356 357 // Send a custom msg associated with this view.
357 358 this.model.send(content, this.callbacks());
358 359 },
359 360
360 361 touch: function () {
361 362 this.model.save_changes(this.callbacks());
362 363 },
363 364 });
364 365
365 366
366 367 var DOMWidgetView = WidgetView.extend({
367 368 initialize: function (options) {
368 369 // Public constructor
369 370
370 371 // In the future we may want to make changes more granular
371 372 // (e.g., trigger on visible:change).
372 373 this.model.on('change', this.update, this);
373 374 this.model.on('msg:custom', this.on_msg, this);
374 375 DOMWidgetView.__super__.initialize.apply(this, arguments);
375 376 },
376 377
377 378 on_msg: function(msg) {
378 379 // Handle DOM specific msgs.
379 380 switch(msg.msg_type) {
380 381 case 'add_class':
381 382 this.add_class(msg.selector, msg.class_list);
382 383 break;
383 384 case 'remove_class':
384 385 this.remove_class(msg.selector, msg.class_list);
385 386 break;
386 387 }
387 388 },
388 389
389 390 add_class: function (selector, class_list) {
390 391 // Add a DOM class to an element.
391 392 this._get_selector_element(selector).addClass(class_list);
392 393 },
393 394
394 395 remove_class: function (selector, class_list) {
395 396 // Remove a DOM class from an element.
396 397 this._get_selector_element(selector).removeClass(class_list);
397 398 },
398 399
399 400 update: function () {
400 401 // Update the contents of this view
401 402 //
402 403 // Called when the model is changed. The model may have been
403 404 // changed by another view or by a state update from the back-end.
404 405 // The very first update seems to happen before the element is
405 406 // finished rendering so we use setTimeout to give the element time
406 407 // to render
407 408 var e = this.$el;
408 409 var visible = this.model.get('visible');
409 410 setTimeout(function() {e.toggle(visible);},0);
410 411
411 412 var css = this.model.get('_css');
412 413 if (css === undefined) {return;}
413 414 var that = this;
414 415 _.each(css, function(css_traits, selector){
415 416 // Apply the css traits to all elements that match the selector.
416 417 var elements = that._get_selector_element(selector);
417 418 if (elements.length > 0) {
418 419 _.each(css_traits, function(css_value, css_key){
419 420 elements.css(css_key, css_value);
420 421 });
421 422 }
422 423 });
423 424 },
424 425
425 426 _get_selector_element: function (selector) {
426 427 // Get the elements via the css selector.
427 428
428 429 // If the selector is blank, apply the style to the $el_to_style
429 430 // element. If the $el_to_style element is not defined, use apply
430 431 // the style to the view's element.
431 432 var elements;
432 433 if (!selector) {
433 434 if (this.$el_to_style === undefined) {
434 435 elements = this.$el;
435 436 } else {
436 437 elements = this.$el_to_style;
437 438 }
438 439 } else {
439 440 elements = this.$el.find(selector);
440 441 }
441 442 return elements;
442 443 },
443 444 });
444 445
445 446 IPython.WidgetModel = WidgetModel;
446 447 IPython.WidgetView = WidgetView;
447 448 IPython.DOMWidgetView = DOMWidgetView;
448 449
449 450 // Pass through WidgetManager namespace.
450 451 return WidgetManager;
451 452 });
@@ -1,282 +1,315 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 // 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 25 this.update_children([], this.model.get('_children'));
26 26 this.model.on('change:_children', function(model, value, options) {
27 27 this.update_children(model.previous('_children'), value);
28 28 }, this);
29 29 this.update();
30
31 // Trigger model displayed events for any models that are child to
32 // this model when this model is displayed.
33 var that = this;
34 this.model.on('displayed', function(){
35 that.is_displayed = true;
36 for (var property in that.child_views) {
37 if (that.child_views.hasOwnProperty(property)) {
38 that.child_views[property].model.trigger('displayed');
39 }
40 }
41 });
30 42 },
31 43
32 44 update_children: function(old_list, new_list) {
33 45 // Called when the children list changes.
34 46 this.do_diff(old_list,
35 47 new_list,
36 48 $.proxy(this.remove_child_model, this),
37 49 $.proxy(this.add_child_model, this));
38 50 },
39 51
40 52 remove_child_model: function(model) {
41 53 // Called when a model is removed from the children list.
42 54 this.child_views[model.id].remove();
43 55 this.delete_child_view(model);
44 56 },
45 57
46 58 add_child_model: function(model) {
47 59 // Called when a model is added to the children list.
48 60 var view = this.create_child_view(model);
49 61 this.$el.append(view.$el);
62
63 // Trigger the displayed event if this model is displayed.
64 if (this.is_displayed) {
65 model.trigger('displayed');
66 }
50 67 },
51 68
52 69 update: function(){
53 70 // Update the contents of this view
54 71 //
55 72 // Called when the model is changed. The model may have been
56 73 // changed by another view or by a state update from the back-end.
57 74 return ContainerView.__super__.update.apply(this);
58 75 },
59 76 });
60 77
61 78 WidgetManager.register_widget_view('ContainerView', ContainerView);
62 79
63 80 var PopupView = IPython.DOMWidgetView.extend({
64 81 render: function(){
65 82 // Called when view is rendered.
66 83 var that = this;
67 84 this.children={};
68 85
69 86 this.$el.on("remove", function(){
70 87 that.$window.remove();
71 88 });
72 89 this.$window = $('<div />')
73 90 .addClass('modal widget-modal')
74 91 .appendTo($('#notebook-container'))
75 92 .mousedown(function(){
76 93 that.bring_to_front();
77 94 });
78 95
79 96 // Set the elements array since the this.$window element is not child
80 97 // of this.$el and the parent widget manager or other widgets may
81 98 // need to know about all of the top-level widgets. The IPython
82 99 // widget manager uses this to register the elements with the
83 100 // keyboard manager.
84 this.additional_elements = [this.$window]
101 this.additional_elements = [this.$window];
85 102
86 103 this.$title_bar = $('<div />')
87 104 .addClass('popover-title')
88 105 .appendTo(this.$window)
89 106 .mousedown(function(){
90 107 that.bring_to_front();
91 108 });
92 109 this.$close = $('<button />')
93 110 .addClass('close icon-remove')
94 111 .css('margin-left', '5px')
95 112 .appendTo(this.$title_bar)
96 113 .click(function(){
97 114 that.hide();
98 115 event.stopPropagation();
99 116 });
100 117 this.$minimize = $('<button />')
101 118 .addClass('close icon-arrow-down')
102 119 .appendTo(this.$title_bar)
103 120 .click(function(){
104 121 that.popped_out = !that.popped_out;
105 122 if (!that.popped_out) {
106 123 that.$minimize
107 124 .removeClass('icon-arrow-down')
108 125 .addClass('icon-arrow-up');
109 126
110 127 that.$window
111 128 .draggable('destroy')
112 129 .resizable('destroy')
113 130 .removeClass('widget-modal modal')
114 131 .addClass('docked-widget-modal')
115 132 .detach()
116 133 .insertBefore(that.$show_button);
117 134 that.$show_button.hide();
118 135 that.$close.hide();
119 136 } else {
120 137 that.$minimize
121 138 .addClass('icon-arrow-down')
122 139 .removeClass('icon-arrow-up');
123 140
124 141 that.$window
125 142 .removeClass('docked-widget-modal')
126 143 .addClass('widget-modal modal')
127 144 .detach()
128 145 .appendTo($('#notebook-container'))
129 146 .draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'})
130 147 .resizable()
131 148 .children('.ui-resizable-handle').show();
132 149 that.show();
133 150 that.$show_button.show();
134 151 that.$close.show();
135 152 }
136 153 event.stopPropagation();
137 154 });
138 155 this.$title = $('<div />')
139 156 .addClass('widget-modal-title')
140 157 .html("&nbsp;")
141 158 .appendTo(this.$title_bar);
142 159 this.$body = $('<div />')
143 160 .addClass('modal-body')
144 161 .addClass('widget-modal-body')
145 162 .addClass('widget-container')
146 163 .addClass('vbox')
147 164 .appendTo(this.$window);
148 165
149 166 this.$show_button = $('<button />')
150 167 .html("&nbsp;")
151 168 .addClass('btn btn-info widget-modal-show')
152 169 .appendTo(this.$el)
153 170 .click(function(){
154 171 that.show();
155 172 });
156 173
157 174 this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'});
158 175 this.$window.resizable();
159 176 this.$window.on('resize', function(){
160 177 that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight());
161 178 });
162 179
163 180 this.$el_to_style = this.$body;
164 181 this._shown_once = false;
165 182 this.popped_out = true;
166 183
167 184 this.update_children([], this.model.get('_children'));
168 185 this.model.on('change:_children', function(model, value, options) {
169 186 this.update_children(model.previous('_children'), value);
170 187 }, this);
171 188 this.update();
189
190 // Trigger model displayed events for any models that are child to
191 // this model when this model is displayed.
192 this.model.on('displayed', function(){
193 that.is_displayed = true;
194 for (var property in that.child_views) {
195 if (that.child_views.hasOwnProperty(property)) {
196 that.child_views[property].model.trigger('displayed');
197 }
198 }
199 });
172 200 },
173 201
174 202 hide: function() {
175 203 // Called when the modal hide button is clicked.
176 204 this.$window.hide();
177 205 this.$show_button.removeClass('btn-info');
178 206 },
179 207
180 208 show: function() {
181 209 // Called when the modal show button is clicked.
182 210 this.$show_button.addClass('btn-info');
183 211 this.$window.show();
184 212 if (this.popped_out) {
185 213 this.$window.css("positon", "absolute");
186 214 this.$window.css("top", "0px");
187 215 this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) +
188 216 $(window).scrollLeft()) + "px");
189 217 this.bring_to_front();
190 218 }
191 219 },
192 220
193 221 bring_to_front: function() {
194 222 // Make the modal top-most, z-ordered about the other modals.
195 223 var $widget_modals = $(".widget-modal");
196 224 var max_zindex = 0;
197 225 $widget_modals.each(function (index, el){
198 226 max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index')));
199 227 });
200 228
201 229 // Start z-index of widget modals at 2000
202 230 max_zindex = Math.max(max_zindex, 2000);
203 231
204 232 $widget_modals.each(function (index, el){
205 233 $el = $(el);
206 234 if (max_zindex == parseInt($el.css('z-index'))) {
207 235 $el.css('z-index', max_zindex - 1);
208 236 }
209 237 });
210 238 this.$window.css('z-index', max_zindex);
211 239 },
212 240
213 241 update_children: function(old_list, new_list) {
214 242 // Called when the children list is modified.
215 243 this.do_diff(old_list,
216 244 new_list,
217 245 $.proxy(this.remove_child_model, this),
218 246 $.proxy(this.add_child_model, this));
219 247 },
220 248
221 249 remove_child_model: function(model) {
222 250 // Called when a child is removed from children list.
223 251 this.child_views[model.id].remove();
224 252 this.delete_child_view(model);
225 253 },
226 254
227 255 add_child_model: function(model) {
228 256 // Called when a child is added to children list.
229 257 var view = this.create_child_view(model);
230 258 this.$body.append(view.$el);
259
260 // Trigger the displayed event if this model is displayed.
261 if (this.is_displayed) {
262 model.trigger('displayed');
263 }
231 264 },
232 265
233 266 update: function(){
234 267 // Update the contents of this view
235 268 //
236 269 // Called when the model is changed. The model may have been
237 270 // changed by another view or by a state update from the back-end.
238 271 var description = this.model.get('description');
239 272 if (description.trim().length === 0) {
240 273 this.$title.html("&nbsp;"); // Preserve title height
241 274 } else {
242 275 this.$title.text(description);
243 276 }
244 277
245 278 var button_text = this.model.get('button_text');
246 279 if (button_text.trim().length === 0) {
247 280 this.$show_button.html("&nbsp;"); // Preserve button height
248 281 } else {
249 282 this.$show_button.text(button_text);
250 283 }
251 284
252 285 if (!this._shown_once) {
253 286 this._shown_once = true;
254 287 this.show();
255 288 }
256 289
257 290 return PopupView.__super__.update.apply(this);
258 291 },
259 292
260 293 _get_selector_element: function(selector) {
261 294 // Get an element view a 'special' jquery selector. (see widget.js)
262 295 //
263 296 // Since the modal actually isn't within the $el in the DOM, we need to extend
264 297 // the selector logic to allow the user to set css on the modal if need be.
265 298 // The convention used is:
266 299 // "modal" - select the modal div
267 300 // "modal [selector]" - select element(s) within the modal div.
268 301 // "[selector]" - select elements within $el
269 302 // "" - select the $el_to_style
270 303 if (selector.substring(0, 5) == 'modal') {
271 304 if (selector == 'modal') {
272 305 return this.$window;
273 306 } else {
274 307 return this.$window.find(selector.substring(6));
275 308 }
276 309 } else {
277 310 return PopupView.__super__._get_selector_element.apply(this, [selector]);
278 311 }
279 312 },
280 313 });
281 314 WidgetManager.register_widget_view('PopupView', PopupView);
282 315 });
@@ -1,243 +1,273 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 // 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 = 'accordion' + IPython.utils.uuid();
23 23 this.$el
24 24 .attr('id', guid)
25 25 .addClass('accordion');
26 26 this.containers = [];
27 27 this.model_containers = {};
28 28 this.update_children([], this.model.get('_children'));
29 29 this.model.on('change:_children', function(model, value, options) {
30 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 var that = this;
38 39 this.model.on('displayed', function() {
39 40 this.update_titles();
41 // Trigger model displayed events for any models that are child to
42 // this model when this model is displayed.
43 that.is_displayed = true;
44 for (var property in that.child_views) {
45 if (that.child_views.hasOwnProperty(property)) {
46 that.child_views[property].model.trigger('displayed');
47 }
48 }
40 49 }, this);
41 50 },
42 51
43 52 update_titles: function(titles) {
44 53 // Set tab titles
45 54 if (!titles) {
46 55 titles = this.model.get('_titles');
47 56 }
48 57
49 58 var that = this;
50 59 _.each(titles, function(title, page_index) {
51 60 var accordian = that.containers[page_index];
52 61 if (accordian !== undefined) {
53 62 accordian
54 63 .find('.accordion-heading')
55 64 .find('.accordion-toggle')
56 65 .text(title);
57 66 }
58 67 });
59 68 },
60 69
61 70 update_selected_index: function(old_index, new_index, options) {
62 71 // Only update the selection if the selection wasn't triggered
63 72 // by the front-end. It must be triggered by the back-end.
64 73 if (options === undefined || options.updated_view != this) {
65 74 this.containers[old_index].find('.accordion-body').collapse('hide');
66 75 if (0 <= new_index && new_index < this.containers.length) {
67 76 this.containers[new_index].find('.accordion-body').collapse('show');
68 77 }
69 78 }
70 79 },
71 80
72 81 update_children: function(old_list, new_list) {
73 82 // Called when the children list is modified.
74 83 this.do_diff(old_list,
75 84 new_list,
76 85 $.proxy(this.remove_child_model, this),
77 86 $.proxy(this.add_child_model, this));
78 87 },
79 88
80 89 remove_child_model: function(model) {
81 90 // Called when a child is removed from children list.
82 91 var accordion_group = this.model_containers[model.id];
83 92 this.containers.splice(accordion_group.container_index, 1);
84 93 delete this.model_containers[model.id];
85 94 accordion_group.remove();
86 95 this.delete_child_view(model);
87 96 },
88 97
89 98 add_child_model: function(model) {
90 99 // Called when a child is added to children list.
91 100 var view = this.create_child_view(model);
92 101 var index = this.containers.length;
93 102 var uuid = IPython.utils.uuid();
94 103 var accordion_group = $('<div />')
95 104 .addClass('accordion-group')
96 105 .appendTo(this.$el);
97 106 var accordion_heading = $('<div />')
98 107 .addClass('accordion-heading')
99 108 .appendTo(accordion_group);
100 109 var that = this;
101 110 var accordion_toggle = $('<a />')
102 111 .addClass('accordion-toggle')
103 112 .attr('data-toggle', 'collapse')
104 113 .attr('data-parent', '#' + this.$el.attr('id'))
105 114 .attr('href', '#' + uuid)
106 115 .click(function(evt){
107 116
108 117 // Calling model.set will trigger all of the other views of the
109 118 // model to update.
110 119 that.model.set("selected_index", index, {updated_view: that});
111 120 that.touch();
112 121 })
113 122 .text('Page ' + index)
114 123 .appendTo(accordion_heading);
115 124 var accordion_body = $('<div />', {id: uuid})
116 125 .addClass('accordion-body collapse')
117 126 .appendTo(accordion_group);
118 127 var accordion_inner = $('<div />')
119 128 .addClass('accordion-inner')
120 129 .appendTo(accordion_body);
121 130 var container_index = this.containers.push(accordion_group) - 1;
122 131 accordion_group.container_index = container_index;
123 132 this.model_containers[model.id] = accordion_group;
124 133 accordion_inner.append(view.$el);
125 134
126 135 this.update();
127 136 this.update_titles();
137
138 // Trigger the displayed event if this model is displayed.
139 if (this.is_displayed) {
140 model.trigger('displayed');
141 }
128 142 },
129 143 });
130 144 WidgetManager.register_widget_view('AccordionView', AccordionView);
131 145
132 146
133 147 var TabView = IPython.DOMWidgetView.extend({
134 148 initialize: function() {
135 149 // Public constructor.
136 150 this.containers = [];
137 151 TabView.__super__.initialize.apply(this, arguments);
138 152 },
139 153
140 154 render: function(){
141 155 // Called when view is rendered.
142 156 var uuid = 'tabs'+IPython.utils.uuid();
143 157 var that = this;
144 158 this.$tabs = $('<div />', {id: uuid})
145 159 .addClass('nav')
146 160 .addClass('nav-tabs')
147 161 .appendTo(this.$el);
148 162 this.$tab_contents = $('<div />', {id: uuid + 'Content'})
149 163 .addClass('tab-content')
150 164 .appendTo(this.$el);
151 165 this.containers = [];
152 166 this.update_children([], this.model.get('_children'));
153 167 this.model.on('change:_children', function(model, value, options) {
154 168 this.update_children(model.previous('_children'), value);
155 169 }, this);
170
171 // Trigger model displayed events for any models that are child to
172 // this model when this model is displayed.
173 this.model.on('displayed', function(){
174 that.is_displayed = true;
175 for (var property in that.child_views) {
176 if (that.child_views.hasOwnProperty(property)) {
177 that.child_views[property].model.trigger('displayed');
178 }
179 }
180 });
156 181 },
157 182
158 183 update_children: function(old_list, new_list) {
159 184 // Called when the children list is modified.
160 185 this.do_diff(old_list,
161 186 new_list,
162 187 $.proxy(this.remove_child_model, this),
163 188 $.proxy(this.add_child_model, this));
164 189 },
165 190
166 191 remove_child_model: function(model) {
167 192 // Called when a child is removed from children list.
168 193 var view = this.child_views[model.id];
169 194 this.containers.splice(view.parent_tab.tab_text_index, 1);
170 195 view.parent_tab.remove();
171 196 view.parent_container.remove();
172 197 view.remove();
173 198 this.delete_child_view(model);
174 199 },
175 200
176 201 add_child_model: function(model) {
177 202 // Called when a child is added to children list.
178 203 var view = this.create_child_view(model);
179 204 var index = this.containers.length;
180 205 var uuid = IPython.utils.uuid();
181 206
182 207 var that = this;
183 208 var tab = $('<li />')
184 209 .css('list-style-type', 'none')
185 210 .appendTo(this.$tabs);
186 211 view.parent_tab = tab;
187 212
188 213 var tab_text = $('<a />')
189 214 .attr('href', '#' + uuid)
190 215 .attr('data-toggle', 'tab')
191 216 .text('Page ' + index)
192 217 .appendTo(tab)
193 218 .click(function (e) {
194 219
195 220 // Calling model.set will trigger all of the other views of the
196 221 // model to update.
197 222 that.model.set("selected_index", index, {updated_view: this});
198 223 that.touch();
199 224 that.select_page(index);
200 225 });
201 226 tab.tab_text_index = this.containers.push(tab_text) - 1;
202 227
203 228 var contents_div = $('<div />', {id: uuid})
204 229 .addClass('tab-pane')
205 230 .addClass('fade')
206 231 .append(view.$el)
207 232 .appendTo(this.$tab_contents);
208 233 view.parent_container = contents_div;
234
235 // Trigger the displayed event if this model is displayed.
236 if (this.is_displayed) {
237 model.trigger('displayed');
238 }
209 239 },
210 240
211 241 update: function(options) {
212 242 // Update the contents of this view
213 243 //
214 244 // Called when the model is changed. The model may have been
215 245 // changed by another view or by a state update from the back-end.
216 246 if (options === undefined || options.updated_view != this) {
217 247 // Set tab titles
218 248 var titles = this.model.get('_titles');
219 249 var that = this;
220 250 _.each(titles, function(title, page_index) {
221 251 var tab_text = that.containers[page_index];
222 252 if (tab_text !== undefined) {
223 253 tab_text.text(title);
224 254 }
225 255 });
226 256
227 257 var selected_index = this.model.get('selected_index');
228 258 if (0 <= selected_index && selected_index < this.containers.length) {
229 259 this.select_page(selected_index);
230 260 }
231 261 }
232 262 return TabView.__super__.update.apply(this);
233 263 },
234 264
235 265 select_page: function(index) {
236 266 // Select a page.
237 267 this.$tabs.find('li')
238 268 .removeClass('active');
239 269 this.containers[index].tab('show');
240 270 },
241 271 });
242 272 WidgetManager.register_widget_view('TabView', TabView);
243 273 });
General Comments 0
You need to be logged in to leave comments. Login now