##// END OF EJS Templates
Fixed bug in throttling code.
Jonathan Frederic -
Show More
@@ -1,212 +1,216 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2013 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // WidgetModel, WidgetView, and WidgetManager
10 10 //============================================================================
11 11 /**
12 12 * Base Widget classes
13 13 * @module IPython
14 14 * @namespace IPython
15 15 * @submodule widget
16 16 */
17 17
18 18 (function () {
19 19 "use strict";
20 20
21 21 // Use require.js 'define' method so that require.js is intelligent enough to
22 22 // syncronously load everything within this file when it is being 'required'
23 23 // elsewhere.
24 24 define(["underscore",
25 25 "backbone",
26 26 ], function (Underscore, Backbone) {
27 27
28 28 //--------------------------------------------------------------------
29 29 // WidgetManager class
30 30 //--------------------------------------------------------------------
31 31 var WidgetManager = function (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 !== undefined
86 86 && cell.widget_subarea !== null) {
87 87
88 88 cell.widget_area.show();
89 89 cell.widget_subarea.append(view.$el);
90 90 }
91 91 }
92 92 };
93 93
94 94 WidgetManager.prototype.create_view = function(model, options, view) {
95 95 // Creates a view for a particular model.
96 96 var view_name = model.get('_view_name');
97 97 var ViewType = WidgetManager._view_types[view_name];
98 98 if (ViewType !== undefined && ViewType !== null) {
99 99
100 100 // If a view is passed into the method, use that view's cell as
101 101 // the cell for the view that is created.
102 102 options = options || {};
103 103 if (view !== undefined) {
104 104 options.cell = view.options.cell;
105 105 }
106 106
107 107 // Create and render the view...
108 108 var parameters = {model: model, options: options};
109 109 var view = new ViewType(parameters);
110 110 view.render();
111 111 model.views.push(view);
112 112 model.on('destroy', view.remove, view);
113 113
114 114 this._handle_new_view(view);
115 115 return view;
116 116 }
117 117 return null;
118 118 };
119 119
120 120 WidgetManager.prototype._handle_new_view = function (view) {
121 121 // Called when a view has been created and rendered.
122 122
123 123 // If the view has a well defined element, inform the keyboard
124 124 // manager about the view's element, so as the element can
125 125 // escape the dreaded command mode.
126 126 if (view.$el !== undefined && view.$el !== null) {
127 127 IPython.keyboard_manager.register_events(view.$el);
128 128 }
129 129 }
130 130
131 131 WidgetManager.prototype.get_msg_cell = function (msg_id) {
132 132 var cell = null;
133 133 // First, check to see if the msg was triggered by cell execution.
134 134 if (IPython.notebook !== undefined && IPython.notebook !== null) {
135 135 cell = IPython.notebook.get_msg_cell(msg_id);
136 136 }
137 137 if (cell !== null) {
138 138 return cell
139 139 }
140 140 // Second, check to see if a get_cell callback was defined
141 141 // for the message. get_cell callbacks are registered for
142 142 // widget messages, so this block is actually checking to see if the
143 143 // message was triggered by a widget.
144 144 var kernel = this.comm_manager.kernel;
145 145 if (kernel !== undefined && kernel !== null) {
146 146 var callbacks = kernel.get_callbacks_for_msg(msg_id);
147 147 if (callbacks !== undefined &&
148 148 callbacks.iopub !== undefined &&
149 149 callbacks.iopub.get_cell !== undefined) {
150 150
151 151 return callbacks.iopub.get_cell();
152 152 }
153 153 }
154 154
155 155 // Not triggered by a cell or widget (no get_cell callback
156 156 // exists).
157 157 return null;
158 158 };
159 159
160 160 WidgetManager.prototype.callbacks = function (view) {
161 161 // callback handlers specific a view
162 162 var callbacks = {};
163 var cell = view.options.cell;
164 if (cell !== null) {
163 if (view !== undefined &&
164 view !== null &&
165 view.options.cell !== undefined &&
166 view.options.cell !== null) {
167
165 168 // Try to get output handlers
169 var cell = view.options.cell;
166 170 var handle_output = null;
167 171 var handle_clear_output = null;
168 172 if (cell.output_area !== undefined && cell.output_area !== null) {
169 173 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
170 174 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
171 175 }
172 176
173 177 // Create callback dict using what is known
174 178 var that = this;
175 179 callbacks = {
176 180 iopub : {
177 181 output : handle_output,
178 182 clear_output : handle_clear_output,
179 183
180 184 // Special function only registered by widget messages.
181 185 // Allows us to get the cell for a message so we know
182 186 // where to add widgets if the code requires it.
183 187 get_cell : function () {
184 188 return cell;
185 189 },
186 190 },
187 191 };
188 192 }
189 193 return callbacks;
190 194 };
191 195
192 196 WidgetManager.prototype.get_model = function (model_id) {
193 197 // Look-up a model instance by its id.
194 198 var model = this._models[model_id];
195 199 if (model !== undefined && model.id == model_id) {
196 200 return model;
197 201 }
198 202 return null;
199 203 };
200 204
201 205 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
202 206 // Handle when a comm is opened.
203 207 var model_id = comm.comm_id;
204 208 var widget_type_name = msg.content.target_name;
205 209 var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
206 210 this._models[model_id] = widget_model;
207 211 };
208 212
209 213 IPython.WidgetManager = WidgetManager;
210 214 return IPython.WidgetManager;
211 215 });
212 216 }());
@@ -1,413 +1,419 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(["notebook/js/widgetmanager",
18 18 "underscore",
19 19 "backbone"],
20 20 function(WidgetManager, Underscore, Backbone){
21 21
22 22 var WidgetModel = Backbone.Model.extend({
23 23 constructor: function (widget_manager, model_id, comm) {
24 24 // Construcctor
25 25 //
26 26 // Creates a WidgetModel instance.
27 27 //
28 28 // Parameters
29 29 // ----------
30 30 // widget_manager : WidgetManager instance
31 31 // model_id : string
32 32 // An ID unique to this model.
33 33 // comm : Comm instance (optional)
34 34 this.widget_manager = widget_manager;
35 35 this.pending_msgs = 0;
36 36 this.msg_throttle = 3;
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 this.pending_msgs++;
59 60 }
60 61 },
61 62
62 63 _handle_comm_closed: function (msg) {
63 64 // Handle when a widget is closed.
64 65 this.trigger('comm:close');
65 66 delete this.comm.model; // Delete ref so GC will collect widget model.
66 67 delete this.comm;
67 68 delete this.model_id; // Delete id from model so widget manager cleans up.
68 69 _.each(this.views, function(view, i) {
69 70 view.remove();
70 71 });
71 72 },
72 73
73 74 _handle_comm_msg: function (msg) {
74 75 // Handle incoming comm msg.
75 76 var method = msg.content.data.method;
76 77 switch (method) {
77 78 case 'update':
78 79 this.apply_update(msg.content.data.state);
79 80 break;
80 81 case 'custom':
81 82 this.trigger('msg:custom', msg.content.data.content);
82 83 break;
83 84 case 'display':
84 85 this.widget_manager.display_view(msg, this);
85 86 break;
86 87 }
87 88 },
88 89
89 90 apply_update: function (state) {
90 91 // Handle when a widget is updated via the python side.
91 92 var that = this;
92 93 _.each(state, function(value, key) {
93 94 that.key_value_lock = [key, value];
94 95 try {
95 96 that.set(key, that._unpack_models(value));
96 97 } finally {
97 98 that.key_value_lock = null;
98 99 }
99 100 });
100 101 },
101 102
102 103 _handle_status: function (msg, callbacks) {
103 104 // Handle status msgs.
104 105
105 106 // execution_state : ('busy', 'idle', 'starting')
106 107 if (this.comm !== undefined) {
107 108 if (msg.content.execution_state ==='idle') {
108 109 // Send buffer if this message caused another message to be
109 110 // throttled.
110 111 if (this.msg_buffer !== null &&
111 112 this.msg_throttle === this.pending_msgs) {
112 113 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
113 114 this.comm.send(data, callbacks);
114 115 this.msg_buffer = null;
115 116 } else {
116 117 --this.pending_msgs;
117 118 }
118 119 }
119 120 }
120 121 },
121 122
122 123 callbacks: function(view) {
123 124 // Create msg callbacks for a comm msg.
124 125 var callbacks = this.widget_manager.callbacks(view);
125 126
126 127 if (callbacks.iopub === undefined) {
127 128 callbacks.iopub = {};
128 129 }
129 130
130 131 var that = this;
131 132 callbacks.iopub.status = function (msg) {
132 133 that._handle_status(msg, callbacks);
133 134 }
134 135 return callbacks;
135 136 },
136 137
137 138 sync: function (method, model, options) {
138 139 // Handle sync to the back-end. Called when a model.save() is called.
139 140
140 141 // Make sure a comm exists.
141 142 var error = options.error || function() {
142 143 console.error('Backbone sync error:', arguments);
143 144 }
144 145 if (this.comm === undefined) {
145 146 error();
146 147 return false;
147 148 }
148 149
149 150 // Delete any key value pairs that the back-end already knows about.
150 151 var attrs = (method === 'patch') ? options.attrs : model.toJSON(options);
151 152 if (this.key_value_lock !== null) {
152 153 var key = this.key_value_lock[0];
153 154 var value = this.key_value_lock[1];
154 155 if (attrs[key] === value) {
155 156 delete attrs[key];
156 157 }
157 158 }
158 159
159 160 // Only sync if there are attributes to send to the back-end.
160 161 if (_.size(attrs) > 0) {
161 var callbacks = options.callbacks || {};
162
163 // If this message was sent via backbone itself, it will not
164 // have any callbacks. It's important that we create callbacks
165 // so we can listen for status messages, etc...
166 var callbacks = options.callbacks || this.callbacks();
167
168 // Check throttle.
162 169 if (this.pending_msgs >= this.msg_throttle) {
163 170 // The throttle has been exceeded, buffer the current msg so
164 171 // it can be sent once the kernel has finished processing
165 172 // some of the existing messages.
166 173
167 174 // Combine updates if it is a 'patch' sync, otherwise replace updates
168 175 switch (method) {
169 176 case 'patch':
170 177 this.msg_buffer = $.extend(this.msg_buffer || {}, attrs);
171 178 break;
172 179 case 'update':
173 180 case 'create':
174 181 this.msg_buffer = attrs;
175 182 break;
176 183 default:
177 184 error();
178 185 return false;
179 186 }
180 187 this.msg_buffer_callbacks = callbacks;
181 188
182 189 } else {
183 190 // We haven't exceeded the throttle, send the message like
184 // normal. If this is a patch operation, just send the
185 // changes.
191 // normal.
186 192 var data = {method: 'backbone', sync_data: attrs};
187 193 this.comm.send(data, callbacks);
188 194 this.pending_msgs++;
189 195 }
190 196 }
191 197 // Since the comm is a one-way communication, assume the message
192 198 // arrived. Don't call success since we don't have a model back from the server
193 199 // this means we miss out on the 'sync' event.
194 200 },
195 201
196 202 save_changes: function(callbacks) {
197 203 // Push this model's state to the back-end
198 204 //
199 205 // This invokes a Backbone.Sync.
200 206 this.save(this.changedAttributes(), {patch: true, callbacks: callbacks});
201 207 },
202 208
203 209 _pack_models: function(value) {
204 210 // Replace models with model ids recursively.
205 211 if (value instanceof Backbone.Model) {
206 212 return value.id;
207 213 } else if (value instanceof Object) {
208 214 var packed = {};
209 215 var that = this;
210 216 _.each(value, function(sub_value, key) {
211 217 packed[key] = that._pack_models(sub_value);
212 218 });
213 219 return packed;
214 220 } else {
215 221 return value;
216 222 }
217 223 },
218 224
219 225 _unpack_models: function(value) {
220 226 // Replace model ids with models recursively.
221 227 if (value instanceof Object) {
222 228 var unpacked = {};
223 229 var that = this;
224 230 _.each(value, function(sub_value, key) {
225 231 unpacked[key] = that._unpack_models(sub_value);
226 232 });
227 233 return unpacked;
228 234 } else {
229 235 var model = this.widget_manager.get_model(value);
230 236 if (model !== null) {
231 237 return model;
232 238 } else {
233 239 return value;
234 240 }
235 241 }
236 242 },
237 243
238 244 });
239 245 WidgetManager.register_widget_model('WidgetModel', WidgetModel);
240 246
241 247
242 248 var WidgetView = Backbone.View.extend({
243 249 initialize: function(parameters) {
244 250 // Public constructor.
245 251 this.model.on('change',this.update,this);
246 252 this.options = parameters.options;
247 253 this.child_views = [];
248 254 this.model.views.push(this);
249 255 },
250 256
251 257 update: function(){
252 258 // Triggered on model change.
253 259 //
254 260 // Update view to be consistent with this.model
255 261 },
256 262
257 263 create_child_view: function(child_model, options) {
258 264 // Create and return a child view.
259 265 //
260 266 // -given a model and (optionally) a view name if the view name is
261 267 // not given, it defaults to the model's default view attribute.
262 268
263 269 // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior
264 270 // it would be great to have the widget manager add the cell metadata
265 271 // to the subview without having to add it here.
266 272 var child_view = this.model.widget_manager.create_view(child_model, options || {}, this);
267 273 this.child_views[child_model.id] = child_view;
268 274 return child_view;
269 275 },
270 276
271 277 delete_child_view: function(child_model, options) {
272 278 // Delete a child view that was previously created using create_child_view.
273 279 var view = this.child_views[child_model.id];
274 280 if (view !== undefined) {
275 281 delete this.child_views[child_model.id];
276 282 view.remove();
277 283 }
278 284 },
279 285
280 286 do_diff: function(old_list, new_list, removed_callback, added_callback) {
281 287 // Difference a changed list and call remove and add callbacks for
282 288 // each removed and added item in the new list.
283 289 //
284 290 // Parameters
285 291 // ----------
286 292 // old_list : array
287 293 // new_list : array
288 294 // removed_callback : Callback(item)
289 295 // Callback that is called for each item removed.
290 296 // added_callback : Callback(item)
291 297 // Callback that is called for each item added.
292 298
293 299
294 300 // removed items
295 301 _.each(_.difference(old_list, new_list), function(item, index, list) {
296 302 removed_callback(item);
297 303 }, this);
298 304
299 305 // added items
300 306 _.each(_.difference(new_list, old_list), function(item, index, list) {
301 307 added_callback(item);
302 308 }, this);
303 309 },
304 310
305 311 callbacks: function(){
306 312 // Create msg callbacks for a comm msg.
307 313 return this.model.callbacks(this);
308 314 },
309 315
310 316 render: function(){
311 317 // Render the view.
312 318 //
313 319 // By default, this is only called the first time the view is created
314 320 },
315 321
316 322 send: function (content) {
317 323 // Send a custom msg associated with this view.
318 324 this.model.send(content, this.callbacks());
319 325 },
320 326
321 327 touch: function () {
322 328 this.model.save_changes(this.callbacks());
323 329 },
324 330 });
325 331
326 332
327 333 var DOMWidgetView = WidgetView.extend({
328 334 initialize: function (options) {
329 335 // Public constructor
330 336
331 337 // In the future we may want to make changes more granular
332 338 // (e.g., trigger on visible:change).
333 339 this.model.on('change', this.update, this);
334 340 this.model.on('msg:custom', this.on_msg, this);
335 341 DOMWidgetView.__super__.initialize.apply(this, arguments);
336 342 },
337 343
338 344 on_msg: function(msg) {
339 345 // Handle DOM specific msgs.
340 346 switch(msg.msg_type) {
341 347 case 'add_class':
342 348 this.add_class(msg.selector, msg.class_list);
343 349 break;
344 350 case 'remove_class':
345 351 this.remove_class(msg.selector, msg.class_list);
346 352 break;
347 353 }
348 354 },
349 355
350 356 add_class: function (selector, class_list) {
351 357 // Add a DOM class to an element.
352 358 this._get_selector_element(selector).addClass(class_list);
353 359 },
354 360
355 361 remove_class: function (selector, class_list) {
356 362 // Remove a DOM class from an element.
357 363 this._get_selector_element(selector).removeClass(class_list);
358 364 },
359 365
360 366 update: function () {
361 367 // Update the contents of this view
362 368 //
363 369 // Called when the model is changed. The model may have been
364 370 // changed by another view or by a state update from the back-end.
365 371 // The very first update seems to happen before the element is
366 372 // finished rendering so we use setTimeout to give the element time
367 373 // to render
368 374 var e = this.$el;
369 375 var visible = this.model.get('visible');
370 376 setTimeout(function() {e.toggle(visible)},0);
371 377
372 378 var css = this.model.get('_css');
373 379 if (css === undefined) {return;}
374 380 var that = this;
375 381 _.each(css, function(css_traits, selector){
376 382 // Apply the css traits to all elements that match the selector.
377 383 var elements = that._get_selector_element(selector);
378 384 if (elements.length > 0) {
379 385 _.each(css_traits, function(css_value, css_key){
380 386 elements.css(css_key, css_value);
381 387 });
382 388 }
383 389 });
384 390
385 391 },
386 392
387 393 _get_selector_element: function (selector) {
388 394 // Get the elements via the css selector.
389 395
390 396 // If the selector is blank, apply the style to the $el_to_style
391 397 // element. If the $el_to_style element is not defined, use apply
392 398 // the style to the view's element.
393 399 var elements;
394 400 if (selector === undefined || selector === null || selector === '') {
395 401 if (this.$el_to_style === undefined) {
396 402 elements = this.$el;
397 403 } else {
398 404 elements = this.$el_to_style;
399 405 }
400 406 } else {
401 407 elements = this.$el.find(selector);
402 408 }
403 409 return elements;
404 410 },
405 411 });
406 412
407 413 IPython.WidgetModel = WidgetModel;
408 414 IPython.WidgetView = WidgetView;
409 415 IPython.DOMWidgetView = DOMWidgetView;
410 416
411 417 // Pass through WidgetManager namespace.
412 418 return WidgetManager;
413 419 });
@@ -1,195 +1,195 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 // StringWidget
10 10 //============================================================================
11 11
12 12 /**
13 13 * @module IPython
14 14 * @namespace IPython
15 15 **/
16 16
17 17 define(["notebook/js/widgets/widget"], function(WidgetManager){
18 18
19 19 var HTMLView = IPython.DOMWidgetView.extend({
20 20 render : function(){
21 21 // Called when view is rendered.
22 22 this.update(); // Set defaults.
23 23 },
24 24
25 25 update : function(){
26 26 // Update the contents of this view
27 27 //
28 28 // Called when the model is changed. The model may have been
29 29 // changed by another view or by a state update from the back-end.
30 30 this.$el.html(this.model.get('value')); // CAUTION! .html(...) CALL MANDITORY!!!
31 31 return HTMLView.__super__.update.apply(this);
32 32 },
33 33 });
34 34 WidgetManager.register_widget_view('HTMLView', HTMLView);
35 35
36 36
37 37 var LatexView = IPython.DOMWidgetView.extend({
38 38 render : function(){
39 39 // Called when view is rendered.
40 40 this.update(); // Set defaults.
41 41 },
42 42
43 43 update : function(){
44 44 // Update the contents of this view
45 45 //
46 46 // Called when the model is changed. The model may have been
47 47 // changed by another view or by a state update from the back-end.
48 48 this.$el.text(this.model.get('value'));
49 49 MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]);
50 50
51 51 return LatexView.__super__.update.apply(this);
52 52 },
53 53 });
54 54 WidgetManager.register_widget_view('LatexView', LatexView);
55 55
56 56
57 57 var TextAreaView = IPython.DOMWidgetView.extend({
58 58 render: function(){
59 59 // Called when view is rendered.
60 60 this.$el
61 61 .addClass('widget-hbox');
62 62 this.$label = $('<div />')
63 63 .appendTo(this.$el)
64 64 .addClass('widget-hlabel')
65 65 .hide();
66 66 this.$textbox = $('<textarea />')
67 67 .attr('rows', 5)
68 68 .addClass('widget-text')
69 69 .appendTo(this.$el);
70 70 this.$el_to_style = this.$textbox; // Set default element to style
71 71 this.update(); // Set defaults.
72 72
73 73 this.model.on('msg:custom', $.proxy(this._handle_textarea_msg, this));
74 74 },
75 75
76 76 _handle_textarea_msg: function (content){
77 77 // Handle when a custom msg is recieved from the back-end.
78 78 if (content.method == "scroll_to_bottom") {
79 79 this.scroll_to_bottom();
80 80 }
81 81 },
82 82
83 83 scroll_to_bottom: function (){
84 84 // Scroll the text-area view to the bottom.
85 85 this.$textbox.scrollTop(this.$textbox[0].scrollHeight);
86 86 },
87 87
88 88 update: function(options){
89 89 // Update the contents of this view
90 90 //
91 91 // Called when the model is changed. The model may have been
92 92 // changed by another view or by a state update from the back-end.
93 93 if (options === undefined || options.updated_view != this) {
94 94 this.$textbox.val(this.model.get('value'));
95 95
96 96 var disabled = this.model.get('disabled');
97 97 this.$textbox.prop('disabled', disabled);
98 98
99 99 var description = this.model.get('description');
100 100 if (description.length === 0) {
101 101 this.$label.hide();
102 102 } else {
103 103 this.$label.text(description);
104 104 this.$label.show();
105 105 }
106 106 }
107 107 return TextAreaView.__super__.update.apply(this);
108 108 },
109 109
110 110 events: {
111 111 // Dictionary of events and their handlers.
112 112 "keyup textarea" : "handleChanging",
113 113 "paste textarea" : "handleChanging",
114 114 "cut textarea" : "handleChanging"
115 115 },
116 116
117 117 handleChanging: function(e) {
118 118 // Handles and validates user input.
119 119
120 120 // Calling model.set will trigger all of the other views of the
121 121 // model to update.
122 122 this.model.set('value', e.target.value, {updated_view: this});
123 123 this.touch();
124 124 },
125 125 });
126 126 WidgetManager.register_widget_view('TextAreaView', TextAreaView);
127 127
128 128
129 129 var TextBoxView = IPython.DOMWidgetView.extend({
130 130 render: function(){
131 131 // Called when view is rendered.
132 132 this.$el
133 133 .addClass('widget-hbox-single');
134 134 this.$label = $('<div />')
135 135 .addClass('widget-hlabel')
136 136 .appendTo(this.$el)
137 137 .hide();
138 138 this.$textbox = $('<input type="text" />')
139 139 .addClass('input')
140 140 .addClass('widget-text')
141 141 .appendTo(this.$el);
142 142 this.$el_to_style = this.$textbox; // Set default element to style
143 143 this.update(); // Set defaults.
144 144 },
145 145
146 146 update: function(options){
147 147 // Update the contents of this view
148 148 //
149 149 // Called when the model is changed. The model may have been
150 150 // changed by another view or by a state update from the back-end.
151 151 if (options === undefined || options.updated_view != this) {
152 152 if (this.$textbox.val() != this.model.get('value')) {
153 153 this.$textbox.val(this.model.get('value'));
154 154 }
155 155
156 156 var disabled = this.model.get('disabled');
157 157 this.$textbox.prop('disabled', disabled);
158 158
159 159 var description = this.model.get('description');
160 160 if (description.length === 0) {
161 161 this.$label.hide();
162 162 } else {
163 163 this.$label.text(description);
164 164 this.$label.show();
165 165 }
166 166 }
167 167 return TextBoxView.__super__.update.apply(this);
168 168 },
169 169
170 170 events: {
171 171 // Dictionary of events and their handlers.
172 172 "keyup input" : "handleChanging",
173 173 "paste input" : "handleChanging",
174 174 "cut input" : "handleChanging",
175 175 "keypress input" : "handleKeypress"
176 176 },
177 177
178 178 handleChanging: function(e) {
179 // Handles and validates user input.
180
179 // Handles user input.
180
181 181 // Calling model.set will trigger all of the other views of the
182 182 // model to update.
183 183 this.model.set('value', e.target.value, {updated_view: this});
184 184 this.touch();
185 185 },
186 186
187 187 handleKeypress: function(e) {
188 188 // Handles text submition
189 189 if (e.keyCode == 13) { // Return key
190 190 this.send({event: 'submit'});
191 191 }
192 192 },
193 193 });
194 194 WidgetManager.register_widget_view('TextBoxView', TextBoxView);
195 195 });
General Comments 0
You need to be logged in to leave comments. Login now