##// END OF EJS Templates
Added PEP8 style comments to all of the JS code.
Jonathan Frederic -
Show More
@@ -18,10 +18,7 b' define(["notebook/js/widgetmanager",'
18 "underscore",
18 "underscore",
19 "backbone"],
19 "backbone"],
20 function(widget_manager, underscore, backbone){
20 function(widget_manager, underscore, backbone){
21
21
22 //--------------------------------------------------------------------
23 // WidgetModel class
24 //--------------------------------------------------------------------
25 var WidgetModel = Backbone.Model.extend({
22 var WidgetModel = Backbone.Model.extend({
26 constructor: function (widget_manager, model_id, comm) {
23 constructor: function (widget_manager, model_id, comm) {
27 // Construcctor
24 // Construcctor
@@ -55,14 +52,15 b' function(widget_manager, underscore, backbone){'
55 },
52 },
56
53
57 send: function (content, callbacks) {
54 send: function (content, callbacks) {
55 // Send a custom msg over the comm.
58 if (this.comm !== undefined) {
56 if (this.comm !== undefined) {
59 var data = {method: 'custom', custom_content: content};
57 var data = {method: 'custom', custom_content: content};
60 this.comm.send(data, callbacks);
58 this.comm.send(data, callbacks);
61 }
59 }
62 },
60 },
63
61
64 // Handle when a widget is closed.
65 _handle_comm_closed: function (msg) {
62 _handle_comm_closed: function (msg) {
63 // Handle when a widget is closed.
66 this.trigger('comm:close');
64 this.trigger('comm:close');
67 delete this.comm.model; // Delete ref so GC will collect widget model.
65 delete this.comm.model; // Delete ref so GC will collect widget model.
68 delete this.comm;
66 delete this.comm;
@@ -70,9 +68,8 b' function(widget_manager, underscore, backbone){'
70 // TODO: Handle deletion, like this.destroy(), and delete views, etc.
68 // TODO: Handle deletion, like this.destroy(), and delete views, etc.
71 },
69 },
72
70
73
74 // Handle incoming comm msg.
75 _handle_comm_msg: function (msg) {
71 _handle_comm_msg: function (msg) {
72 // Handle incoming comm msg.
76 var method = msg.content.data.method;
73 var method = msg.content.data.method;
77 switch (method) {
74 switch (method) {
78 case 'update':
75 case 'update':
@@ -87,9 +84,8 b' function(widget_manager, underscore, backbone){'
87 }
84 }
88 },
85 },
89
86
90
91 // Handle when a widget is updated via the python side.
92 apply_update: function (state) {
87 apply_update: function (state) {
88 // Handle when a widget is updated via the python side.
93 for (var key in state) {
89 for (var key in state) {
94 if (state.hasOwnProperty(key)) {
90 if (state.hasOwnProperty(key)) {
95 var value = state[key];
91 var value = state[key];
@@ -105,9 +101,10 b' function(widget_manager, underscore, backbone){'
105 this.save();
101 this.save();
106 },
102 },
107
103
108
109 _handle_status: function (msg, callbacks) {
104 _handle_status: function (msg, callbacks) {
110 //execution_state : ('busy', 'idle', 'starting')
105 // Handle status msgs.
106
107 // execution_state : ('busy', 'idle', 'starting')
111 if (this.comm !== undefined) {
108 if (this.comm !== undefined) {
112 if (msg.content.execution_state ==='idle') {
109 if (msg.content.execution_state ==='idle') {
113 // Send buffer if this message caused another message to be
110 // Send buffer if this message caused another message to be
@@ -124,9 +121,8 b' function(widget_manager, underscore, backbone){'
124 }
121 }
125 },
122 },
126
123
127
128 // Custom syncronization logic.
129 _handle_sync: function (method, options) {
124 _handle_sync: function (method, options) {
125 // Custom syncronization logic.
130 var model_json = this.toJSON();
126 var model_json = this.toJSON();
131 var attr;
127 var attr;
132
128
@@ -176,6 +172,7 b' function(widget_manager, underscore, backbone){'
176 },
172 },
177
173
178 _pack_models: function(value) {
174 _pack_models: function(value) {
175 // Replace models with model ids recursively.
179 if (value instanceof Backbone.Model) {
176 if (value instanceof Backbone.Model) {
180 return value.id;
177 return value.id;
181 } else if (value instanceof Object) {
178 } else if (value instanceof Object) {
@@ -190,6 +187,7 b' function(widget_manager, underscore, backbone){'
190 },
187 },
191
188
192 _unpack_models: function(value) {
189 _unpack_models: function(value) {
190 // Replace model ids with models recursively.
193 if (value instanceof Object) {
191 if (value instanceof Object) {
194 var unpacked = {};
192 var unpacked = {};
195 for (var key in value) {
193 for (var key in value) {
@@ -210,11 +208,9 b' function(widget_manager, underscore, backbone){'
210 widget_manager.register_widget_model('WidgetModel', WidgetModel);
208 widget_manager.register_widget_model('WidgetModel', WidgetModel);
211
209
212
210
213 //--------------------------------------------------------------------
214 // WidgetView class
215 //--------------------------------------------------------------------
216 var WidgetView = Backbone.View.extend({
211 var WidgetView = Backbone.View.extend({
217 initialize: function(parameters) {
212 initialize: function(parameters) {
213 // Public constructor.
218 this.model.on('change',this.update,this);
214 this.model.on('change',this.update,this);
219 this.options = parameters.options;
215 this.options = parameters.options;
220 this.child_views = [];
216 this.child_views = [];
@@ -222,19 +218,23 b' function(widget_manager, underscore, backbone){'
222 },
218 },
223
219
224 update: function(){
220 update: function(){
225 // update view to be consistent with this.model
221 // Triggered on model change.
226 // triggered on model change
222 //
223 // Update view to be consistent with this.model
227 },
224 },
228
225
229 create_child_view: function(child_model, options) {
226 create_child_view: function(child_model, options) {
230 // create and return a child view, given a model and (optionally) a view name
227 // Create and return a child view.
231 // if the view name is not given, it defaults to the model's default view attribute
228 //
229 // - given a model and (optionally) a view name if the view name is
230 // not given, it defaults to the model's default view attribute.
232 var child_view = this.model.widget_manager.create_view(child_model, options);
231 var child_view = this.model.widget_manager.create_view(child_model, options);
233 this.child_views[child_model.id] = child_view;
232 this.child_views[child_model.id] = child_view;
234 return child_view;
233 return child_view;
235 },
234 },
236
235
237 delete_child_view: function(child_model, options) {
236 delete_child_view: function(child_model, options) {
237 // Delete a child view that was previously created using create_child_view.
238 var view = this.child_views[child_model.id];
238 var view = this.child_views[child_model.id];
239 delete this.child_views[child_model.id];
239 delete this.child_views[child_model.id];
240 view.remove();
240 view.remove();
@@ -266,31 +266,42 b' function(widget_manager, underscore, backbone){'
266 },
266 },
267
267
268 callbacks: function(){
268 callbacks: function(){
269 // Create msg callbacks for a comm msg.
269 return this.model.widget_manager.callbacks(this);
270 return this.model.widget_manager.callbacks(this);
270 },
271 },
271
272
272 render: function(){
273 render: function(){
273 // render the view. By default, this is only called the first time the view is created
274 // Render the view.
275 //
276 // By default, this is only called the first time the view is created
274 },
277 },
278
275 send: function (content) {
279 send: function (content) {
280 // Send a custom msg associated with this view.
276 this.model.send(content, this.callbacks());
281 this.model.send(content, this.callbacks());
277 },
282 },
278
283
279 touch: function () {
284 touch: function () {
285 // Associate recent model changes with this notebook.
280 this.model.save(this.model.changedAttributes(), {patch: true, callbacks: this.callbacks()});
286 this.model.save(this.model.changedAttributes(), {patch: true, callbacks: this.callbacks()});
281 },
287 },
282
288
283 });
289 });
284
290
291
285 var DOMWidgetView = WidgetView.extend({
292 var DOMWidgetView = WidgetView.extend({
286 initialize: function (options) {
293 initialize: function (options) {
287 // TODO: make changes more granular (e.g., trigger on visible:change)
294 // Public constructor
295
296 // In the future we may want to make changes more granular
297 // (e.g., trigger on visible:change).
288 this.model.on('change', this.update, this);
298 this.model.on('change', this.update, this);
289 this.model.on('msg:custom', this.on_msg, this);
299 this.model.on('msg:custom', this.on_msg, this);
290 DOMWidgetView.__super__.initialize.apply(this, arguments);
300 DOMWidgetView.__super__.initialize.apply(this, arguments);
291 },
301 },
292
302
293 on_msg: function(msg) {
303 on_msg: function(msg) {
304 // Handle DOM specific msgs.
294 switch(msg.msg_type) {
305 switch(msg.msg_type) {
295 case 'add_class':
306 case 'add_class':
296 this.add_class(msg.selector, msg.class_list);
307 this.add_class(msg.selector, msg.class_list);
@@ -302,10 +313,12 b' function(widget_manager, underscore, backbone){'
302 },
313 },
303
314
304 add_class: function (selector, class_list) {
315 add_class: function (selector, class_list) {
316 // Add a DOM class to an element.
305 this._get_selector_element(selector).addClass(class_list);
317 this._get_selector_element(selector).addClass(class_list);
306 },
318 },
307
319
308 remove_class: function (selector, class_list) {
320 remove_class: function (selector, class_list) {
321 // Remove a DOM class from an element.
309 this._get_selector_element(selector).removeClass(class_list);
322 this._get_selector_element(selector).removeClass(class_list);
310 },
323 },
311
324
@@ -340,10 +353,11 b' function(widget_manager, underscore, backbone){'
340 },
353 },
341
354
342 _get_selector_element: function (selector) {
355 _get_selector_element: function (selector) {
343 // Get the elements via the css selector. If the selector is
356 // Get the elements via the css selector.
344 // blank, apply the style to the $el_to_style element. If
357
345 // the $el_to_style element is not defined, use apply the
358 // If the selector is blank, apply the style to the $el_to_style
346 // style to the view's element.
359 // element. If the $el_to_style element is not defined, use apply
360 // the style to the view's element.
347 var elements;
361 var elements;
348 if (selector === undefined || selector === null || selector === '') {
362 if (selector === undefined || selector === null || selector === '') {
349 if (this.$el_to_style === undefined) {
363 if (this.$el_to_style === undefined) {
@@ -362,5 +376,6 b' function(widget_manager, underscore, backbone){'
362 IPython.WidgetView = WidgetView;
376 IPython.WidgetView = WidgetView;
363 IPython.DOMWidgetView = DOMWidgetView;
377 IPython.DOMWidgetView = DOMWidgetView;
364
378
379 // Pass through widget_manager instance (probably not a good practice).
365 return widget_manager;
380 return widget_manager;
366 });
381 });
@@ -15,10 +15,10 b''
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager){
17 define(["notebook/js/widgets/widget"], function(widget_manager){
18
18 var CheckBoxView = IPython.DOMWidgetView.extend({
19 var CheckBoxView = IPython.DOMWidgetView.extend({
19
20 // Called when view is rendered.
21 render : function(){
20 render : function(){
21 // Called when view is rendered.
22 this.$el
22 this.$el
23 .addClass('widget-hbox-single');
23 .addClass('widget-hbox-single');
24 this.$label = $('<div />')
24 this.$label = $('<div />')
@@ -35,6 +35,8 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
35 },
35 },
36
36
37 handle_click: function() {
37 handle_click: function() {
38 // Handles when the checkbox is clicked.
39
38 // Calling model.set will trigger all of the other views of the
40 // Calling model.set will trigger all of the other views of the
39 // model to update.
41 // model to update.
40 var value = this.model.get('value');
42 var value = this.model.get('value');
@@ -65,13 +67,12 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
65 },
67 },
66
68
67 });
69 });
68
69 widget_manager.register_widget_view('CheckBoxView', CheckBoxView);
70 widget_manager.register_widget_view('CheckBoxView', CheckBoxView);
70
71
72
71 var ToggleButtonView = IPython.DOMWidgetView.extend({
73 var ToggleButtonView = IPython.DOMWidgetView.extend({
72
73 // Called when view is rendered.
74 render : function() {
74 render : function() {
75 // Called when view is rendered.
75 var that = this;
76 var that = this;
76 this.setElement($('<button />')
77 this.setElement($('<button />')
77 .addClass('btn')
78 .addClass('btn')
@@ -110,8 +111,8 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
110 return ToggleButtonView.__super__.update.apply(this);
111 return ToggleButtonView.__super__.update.apply(this);
111 },
112 },
112
113
113 // Handles and validates user input.
114 handle_click: function(e) {
114 handle_click: function(e) {
115 // Handles and validates user input.
115
116
116 // Calling model.set will trigger all of the other views of the
117 // Calling model.set will trigger all of the other views of the
117 // model to update.
118 // model to update.
@@ -120,7 +121,5 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
120 this.touch();
121 this.touch();
121 },
122 },
122 });
123 });
123
124 widget_manager.register_widget_view('ToggleButtonView', ToggleButtonView);
124 widget_manager.register_widget_view('ToggleButtonView', ToggleButtonView);
125
126 });
125 });
@@ -15,10 +15,10 b''
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager){
17 define(["notebook/js/widgets/widget"], function(widget_manager){
18 var ButtonView = IPython.DOMWidgetView.extend({
18
19
19 var ButtonView = IPython.DOMWidgetView.extend({
20 // Called when view is rendered.
21 render : function(){
20 render : function(){
21 // Called when view is rendered.
22 this.setElement($("<button />")
22 this.setElement($("<button />")
23 .addClass('btn'));
23 .addClass('btn'));
24
24
@@ -49,14 +49,14 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
49 },
49 },
50
50
51 events: {
51 events: {
52 // Dictionary of events and their handlers.
52 'click': '_handle_click',
53 'click': '_handle_click',
53 },
54 },
54
55
55 _handle_click: function(){
56 _handle_click: function(){
57 // Handles when the button is clicked.
56 this.send({event: 'click'});
58 this.send({event: 'click'});
57 },
59 },
58 });
60 });
59
60 widget_manager.register_widget_view('ButtonView', ButtonView);
61 widget_manager.register_widget_view('ButtonView', ButtonView);
61
62 });
62 });
@@ -15,9 +15,10 b''
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager) {
17 define(["notebook/js/widgets/widget"], function(widget_manager) {
18 var ContainerView = IPython.DOMWidgetView.extend({
18
19
19 var ContainerView = IPython.DOMWidgetView.extend({
20 render: function(){
20 render: function(){
21 // Called when view is rendered.
21 this.$el
22 this.$el
22 .addClass('widget-container');
23 .addClass('widget-container');
23 this.children={};
24 this.children={};
@@ -29,6 +30,7 b' define(["notebook/js/widgets/widget"], function(widget_manager) {'
29 },
30 },
30
31
31 update_children: function(old_list, new_list) {
32 update_children: function(old_list, new_list) {
33 // Called when the children list changes.
32 this.do_diff(old_list,
34 this.do_diff(old_list,
33 new_list,
35 new_list,
34 $.proxy(this.remove_child_model, this),
36 $.proxy(this.remove_child_model, this),
@@ -36,11 +38,13 b' define(["notebook/js/widgets/widget"], function(widget_manager) {'
36 },
38 },
37
39
38 remove_child_model: function(model) {
40 remove_child_model: function(model) {
41 // Called when a model is removed from the children list.
39 this.child_views[model.id].remove();
42 this.child_views[model.id].remove();
40 this.delete_child_view(model);
43 this.delete_child_view(model);
41 },
44 },
42
45
43 add_child_model: function(model) {
46 add_child_model: function(model) {
47 // Called when a model is added to the children list.
44 var view = this.create_child_view(model);
48 var view = this.create_child_view(model);
45 this.$el.append(view.$el);
49 this.$el.append(view.$el);
46 },
50 },
@@ -53,13 +57,12 b' define(["notebook/js/widgets/widget"], function(widget_manager) {'
53 return ContainerView.__super__.update.apply(this);
57 return ContainerView.__super__.update.apply(this);
54 },
58 },
55 });
59 });
56
57 widget_manager.register_widget_view('ContainerView', ContainerView);
60 widget_manager.register_widget_view('ContainerView', ContainerView);
58
61
59
62
60 var ModalView = IPython.DOMWidgetView.extend({
63 var ModalView = IPython.DOMWidgetView.extend({
61
62 render: function(){
64 render: function(){
65 // Called when view is rendered.
63 var that = this;
66 var that = this;
64 this.children={};
67 this.children={};
65 this.update_children([], this.model.get('children'));
68 this.update_children([], this.model.get('children'));
@@ -160,13 +163,14 b' define(["notebook/js/widgets/widget"], function(widget_manager) {'
160 },
163 },
161
164
162 hide: function() {
165 hide: function() {
166 // Called when the modal hide button is clicked.
163 this.$window.hide();
167 this.$window.hide();
164 this.$show_button.removeClass('btn-info');
168 this.$show_button.removeClass('btn-info');
165 },
169 },
166
170
167 show: function() {
171 show: function() {
172 // Called when the modal show button is clicked.
168 this.$show_button.addClass('btn-info');
173 this.$show_button.addClass('btn-info');
169
170 this.$window.show();
174 this.$window.show();
171 if (this.popped_out) {
175 if (this.popped_out) {
172 this.$window.css("positon", "absolute");
176 this.$window.css("positon", "absolute");
@@ -178,6 +182,7 b' define(["notebook/js/widgets/widget"], function(widget_manager) {'
178 },
182 },
179
183
180 bring_to_front: function() {
184 bring_to_front: function() {
185 // Make the modal top-most, z-ordered about the other modals.
181 var $widget_modals = $(".widget-modal");
186 var $widget_modals = $(".widget-modal");
182 var max_zindex = 0;
187 var max_zindex = 0;
183 $widget_modals.each(function (index, el){
188 $widget_modals.each(function (index, el){
@@ -197,6 +202,7 b' define(["notebook/js/widgets/widget"], function(widget_manager) {'
197 },
202 },
198
203
199 update_children: function(old_list, new_list) {
204 update_children: function(old_list, new_list) {
205 // Called when the children list is modified.
200 this.do_diff(old_list,
206 this.do_diff(old_list,
201 new_list,
207 new_list,
202 $.proxy(this.remove_child_model, this),
208 $.proxy(this.remove_child_model, this),
@@ -204,11 +210,13 b' define(["notebook/js/widgets/widget"], function(widget_manager) {'
204 },
210 },
205
211
206 remove_child_model: function(model) {
212 remove_child_model: function(model) {
213 // Called when a child is removed from children list.
207 this.child_views[model.id].remove();
214 this.child_views[model.id].remove();
208 this.delete_child_view(model);
215 this.delete_child_view(model);
209 },
216 },
210
217
211 add_child_model: function(model) {
218 add_child_model: function(model) {
219 // Called when a child is added to children list.
212 var view = this.create_child_view(model);
220 var view = this.create_child_view(model);
213 this.$body.append(view.$el);
221 this.$body.append(view.$el);
214 },
222 },
@@ -245,7 +253,8 b' define(["notebook/js/widgets/widget"], function(widget_manager) {'
245 },
253 },
246
254
247 _get_selector_element: function(selector) {
255 _get_selector_element: function(selector) {
248
256 // Get an element view a 'special' jquery selector. (see widget.js)
257 //
249 // Since the modal actually isn't within the $el in the DOM, we need to extend
258 // Since the modal actually isn't within the $el in the DOM, we need to extend
250 // the selector logic to allow the user to set css on the modal if need be.
259 // the selector logic to allow the user to set css on the modal if need be.
251 // The convention used is:
260 // The convention used is:
@@ -263,8 +272,6 b' define(["notebook/js/widgets/widget"], function(widget_manager) {'
263 return ModalView.__super__._get_selector_element.apply(this, [selector]);
272 return ModalView.__super__._get_selector_element.apply(this, [selector]);
264 }
273 }
265 },
274 },
266
267 });
275 });
268
269 widget_manager.register_widget_view('ModalView', ModalView);
276 widget_manager.register_widget_view('ModalView', ModalView);
270 });
277 });
@@ -15,10 +15,10 b''
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager){
17 define(["notebook/js/widgets/widget"], function(widget_manager){
18
18 var FloatSliderView = IPython.DOMWidgetView.extend({
19 var FloatSliderView = IPython.DOMWidgetView.extend({
19
20 // Called when view is rendered.
21 render : function(){
20 render : function(){
21 // Called when view is rendered.
22 this.$el
22 this.$el
23 .addClass('widget-hbox-single');
23 .addClass('widget-hbox-single');
24 this.$label = $('<div />')
24 this.$label = $('<div />')
@@ -107,24 +107,26 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
107 return FloatSliderView.__super__.update.apply(this);
107 return FloatSliderView.__super__.update.apply(this);
108 },
108 },
109
109
110 // Handles: User input
110 events: {
111 events: { "slide" : "handleSliderChange" },
111 // Dictionary of events and their handlers.
112 "slide" : "handleSliderChange"
113 },
114
112 handleSliderChange: function(e, ui) {
115 handleSliderChange: function(e, ui) {
113
116 // Handle when the slider value is changed.
117
114 // Calling model.set will trigger all of the other views of the
118 // Calling model.set will trigger all of the other views of the
115 // model to update.
119 // model to update.
116 this.model.set('value', ui.value, {updated_view: this});
120 this.model.set('value', ui.value, {updated_view: this});
117 this.touch();
121 this.touch();
118 },
122 },
119 });
123 });
120
121 widget_manager.register_widget_view('FloatSliderView', FloatSliderView);
124 widget_manager.register_widget_view('FloatSliderView', FloatSliderView);
122
125
123
126
124 var FloatTextView = IPython.DOMWidgetView.extend({
127 var FloatTextView = IPython.DOMWidgetView.extend({
125
126 // Called when view is rendered.
127 render : function(){
128 render : function(){
129 // Called when view is rendered.
128 this.$el
130 this.$el
129 .addClass('widget-hbox-single');
131 .addClass('widget-hbox-single');
130 this.$label = $('<div />')
132 this.$label = $('<div />')
@@ -144,7 +146,6 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
144 //
146 //
145 // Called when the model is changed. The model may have been
147 // Called when the model is changed. The model may have been
146 // changed by another view or by a state update from the back-end.
148 // changed by another view or by a state update from the back-end.
147
148 if (options === undefined || options.updated_view != this) {
149 if (options === undefined || options.updated_view != this) {
149 var value = this.model.get('value');
150 var value = this.model.get('value');
150 if (parseFloat(this.$textbox.val()) != value) {
151 if (parseFloat(this.$textbox.val()) != value) {
@@ -168,15 +169,20 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
168 return FloatTextView.__super__.update.apply(this);
169 return FloatTextView.__super__.update.apply(this);
169 },
170 },
170
171
172 events: {
173 // Dictionary of events and their handlers.
174
175 "keyup input" : "handleChanging",
176 "paste input" : "handleChanging",
177 "cut input" : "handleChanging",
178
179 // Fires only when control is validated or looses focus.
180 "change input" : "handleChanged"
181 },
171
182
172 events: {"keyup input" : "handleChanging",
173 "paste input" : "handleChanging",
174 "cut input" : "handleChanging",
175 "change input" : "handleChanged"}, // Fires only when control is validated or looses focus.
176
177 // Handles and validates user input.
178 handleChanging: function(e) {
183 handleChanging: function(e) {
179
184 // Handles and validates user input.
185
180 // Try to parse value as a float.
186 // Try to parse value as a float.
181 var numericalValue = 0.0;
187 var numericalValue = 0.0;
182 if (e.target.value !== '') {
188 if (e.target.value !== '') {
@@ -205,22 +211,19 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
205 }
211 }
206 },
212 },
207
213
208 // Applies validated input.
209 handleChanged: function(e) {
214 handleChanged: function(e) {
210 // Update the textbox
215 // Applies validated input.
211 if (this.model.get('value') != e.target.value) {
216 if (this.model.get('value') != e.target.value) {
212 e.target.value = this.model.get('value');
217 e.target.value = this.model.get('value');
213 }
218 }
214 }
219 }
215 });
220 });
216
217 widget_manager.register_widget_view('FloatTextView', FloatTextView);
221 widget_manager.register_widget_view('FloatTextView', FloatTextView);
218
222
219
223
220 var ProgressView = IPython.DOMWidgetView.extend({
224 var ProgressView = IPython.DOMWidgetView.extend({
221
222 // Called when view is rendered.
223 render : function(){
225 render : function(){
226 // Called when view is rendered.
224 this.$el
227 this.$el
225 .addClass('widget-hbox-single');
228 .addClass('widget-hbox-single');
226 this.$label = $('<div />')
229 this.$label = $('<div />')
@@ -258,9 +261,7 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
258 this.$label.show();
261 this.$label.show();
259 }
262 }
260 return ProgressView.__super__.update.apply(this);
263 return ProgressView.__super__.update.apply(this);
261 },
264 },
262
263 });
265 });
264
265 widget_manager.register_widget_view('ProgressView', ProgressView);
266 widget_manager.register_widget_view('ProgressView', ProgressView);
266 });
267 });
@@ -15,10 +15,10 b''
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager){
17 define(["notebook/js/widgets/widget"], function(widget_manager){
18 var ImageView = IPython.DOMWidgetView.extend({
18
19
19 var ImageView = IPython.DOMWidgetView.extend({
20 // Called when view is rendered.
21 render : function(){
20 render : function(){
21 // Called when view is rendered.
22 this.setElement($("<img />"));
22 this.setElement($("<img />"));
23 this.update(); // Set defaults.
23 this.update(); // Set defaults.
24 },
24 },
@@ -46,9 +46,6 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
46 }
46 }
47 return ImageView.__super__.update.apply(this);
47 return ImageView.__super__.update.apply(this);
48 },
48 },
49
50 });
49 });
51
52 widget_manager.register_widget_view('ImageView', ImageView);
50 widget_manager.register_widget_view('ImageView', ImageView);
53
54 });
51 });
@@ -15,10 +15,10 b''
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager){
17 define(["notebook/js/widgets/widget"], function(widget_manager){
18 var IntSliderView = IPython.DOMWidgetView.extend({
18
19
19 var IntSliderView = IPython.DOMWidgetView.extend({
20 // Called when view is rendered.
21 render : function(){
20 render : function(){
21 // Called when view is rendered.
22 this.$el
22 this.$el
23 .addClass('widget-hbox-single');
23 .addClass('widget-hbox-single');
24 this.$label = $('<div />')
24 this.$label = $('<div />')
@@ -106,23 +106,26 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
106 return IntSliderView.__super__.update.apply(this);
106 return IntSliderView.__super__.update.apply(this);
107 },
107 },
108
108
109 // Handles: User input
109 events: {
110 events: { "slide" : "handleSliderChange" },
110 // Dictionary of events and their handlers.
111 "slide" : "handleSliderChange"
112 },
113
111 handleSliderChange: function(e, ui) {
114 handleSliderChange: function(e, ui) {
112
115 // Called when the slider value is changed.
116
113 // Calling model.set will trigger all of the other views of the
117 // Calling model.set will trigger all of the other views of the
114 // model to update.
118 // model to update.
115 this.model.set('value', ~~ui.value, {updated_view: this}); // Double bit-wise not to truncate decimel
119 this.model.set('value', ~~ui.value, {updated_view: this}); // Double bit-wise not to truncate decimel
116 this.touch();
120 this.touch();
117 },
121 },
118 });
122 });
119
120 widget_manager.register_widget_view('IntSliderView', IntSliderView);
123 widget_manager.register_widget_view('IntSliderView', IntSliderView);
121
124
122 var IntTextView = IPython.DOMWidgetView.extend({
125
123
126 var IntTextView = IPython.DOMWidgetView.extend({
124 // Called when view is rendered.
125 render : function(){
127 render : function(){
128 // Called when view is rendered.
126 this.$el
129 this.$el
127 .addClass('widget-hbox-single');
130 .addClass('widget-hbox-single');
128 this.$label = $('<div />')
131 this.$label = $('<div />')
@@ -164,15 +167,19 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
164 }
167 }
165 return IntTextView.__super__.update.apply(this);
168 return IntTextView.__super__.update.apply(this);
166 },
169 },
170
171 events: {
172 // Dictionary of events and their handlers.
173 "keyup input" : "handleChanging",
174 "paste input" : "handleChanging",
175 "cut input" : "handleChanging",
176
177 // Fires only when control is validated or looses focus.
178 "change input" : "handleChanged"
179 },
167
180
168
169 events: {"keyup input" : "handleChanging",
170 "paste input" : "handleChanging",
171 "cut input" : "handleChanging",
172 "change input" : "handleChanged"}, // Fires only when control is validated or looses focus.
173
174 // Handles and validates user input.
175 handleChanging: function(e) {
181 handleChanging: function(e) {
182 // Handles and validates user input.
176
183
177 // Try to parse value as a float.
184 // Try to parse value as a float.
178 var numericalValue = 0;
185 var numericalValue = 0;
@@ -202,14 +209,12 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
202 }
209 }
203 },
210 },
204
211
205 // Applies validated input.
206 handleChanged: function(e) {
212 handleChanged: function(e) {
207 // Update the textbox
213 // Applies validated input.
208 if (this.model.get('value') != e.target.value) {
214 if (this.model.get('value') != e.target.value) {
209 e.target.value = this.model.get('value');
215 e.target.value = this.model.get('value');
210 }
216 }
211 }
217 }
212 });
218 });
213
214 widget_manager.register_widget_view('IntTextView', IntTextView);
219 widget_manager.register_widget_view('IntTextView', IntTextView);
215 });
220 });
@@ -15,11 +15,10 b''
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager){
17 define(["notebook/js/widgets/widget"], function(widget_manager){
18
18 var DropdownView = IPython.DOMWidgetView.extend({
19 var DropdownView = IPython.DOMWidgetView.extend({
19
20 // Called when view is rendered.
21 render : function(){
20 render : function(){
22
21 // Called when view is rendered.
23 this.$el
22 this.$el
24 .addClass('widget-hbox-single')
23 .addClass('widget-hbox-single')
25 .html('');
24 .html('');
@@ -101,8 +100,8 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
101 return DropdownView.__super__.update.apply(this);
100 return DropdownView.__super__.update.apply(this);
102 },
101 },
103
102
104 // Handle when a value is clicked.
105 handle_click: function (e) {
103 handle_click: function (e) {
104 // Handle when a value is clicked.
106
105
107 // Calling model.set will trigger all of the other views of the
106 // Calling model.set will trigger all of the other views of the
108 // model to update.
107 // model to update.
@@ -111,13 +110,12 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
111 },
110 },
112
111
113 });
112 });
114
115 widget_manager.register_widget_view('DropdownView', DropdownView);
113 widget_manager.register_widget_view('DropdownView', DropdownView);
116
114
117 var RadioButtonsView = IPython.DOMWidgetView.extend({
115
118
116 var RadioButtonsView = IPython.DOMWidgetView.extend({
119 // Called when view is rendered.
120 render : function(){
117 render : function(){
118 // Called when view is rendered.
121 this.$el
119 this.$el
122 .addClass('widget-hbox');
120 .addClass('widget-hbox');
123 this.$label = $('<div />')
121 this.$label = $('<div />')
@@ -193,8 +191,8 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
193 return RadioButtonsView.__super__.update.apply(this);
191 return RadioButtonsView.__super__.update.apply(this);
194 },
192 },
195
193
196 // Handle when a value is clicked.
197 handle_click: function (e) {
194 handle_click: function (e) {
195 // Handle when a value is clicked.
198
196
199 // Calling model.set will trigger all of the other views of the
197 // Calling model.set will trigger all of the other views of the
200 // model to update.
198 // model to update.
@@ -202,14 +200,12 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
202 this.touch();
200 this.touch();
203 },
201 },
204 });
202 });
205
206 widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView);
203 widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView);
207
204
208
205
209 var ToggleButtonsView = IPython.DOMWidgetView.extend({
206 var ToggleButtonsView = IPython.DOMWidgetView.extend({
210
211 // Called when view is rendered.
212 render : function(){
207 render : function(){
208 // Called when view is rendered.
213 this.$el
209 this.$el
214 .addClass('widget-hbox-single');
210 .addClass('widget-hbox-single');
215 this.$label = $('<div />')
211 this.$label = $('<div />')
@@ -280,23 +276,21 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
280 return ToggleButtonsView.__super__.update.apply(this);
276 return ToggleButtonsView.__super__.update.apply(this);
281 },
277 },
282
278
283 // Handle when a value is clicked.
284 handle_click: function (e) {
279 handle_click: function (e) {
280 // Handle when a value is clicked.
285
281
286 // Calling model.set will trigger all of the other views of the
282 // Calling model.set will trigger all of the other views of the
287 // model to update.
283 // model to update.
288 this.model.set('value', $(e.target).html(), {updated_view: this});
284 this.model.set('value', $(e.target).html(), {updated_view: this});
289 this.touch();
285 this.touch();
290 },
286 },
291
292 });
287 });
293
294 widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView);
288 widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView);
295
289
296 var ListBoxView = IPython.DOMWidgetView.extend({
290
297
291 var ListBoxView = IPython.DOMWidgetView.extend({
298 // Called when view is rendered.
299 render : function(){
292 render : function(){
293 // Called when view is rendered.
300 this.$el
294 this.$el
301 .addClass('widget-hbox');
295 .addClass('widget-hbox');
302 this.$label = $('<div />')
296 this.$label = $('<div />')
@@ -364,16 +358,14 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
364 return ListBoxView.__super__.update.apply(this);
358 return ListBoxView.__super__.update.apply(this);
365 },
359 },
366
360
367 // Handle when a value is clicked.
368 handle_click: function (e) {
361 handle_click: function (e) {
362 // Handle when a value is clicked.
369
363
370 // Calling model.set will trigger all of the other views of the
364 // Calling model.set will trigger all of the other views of the
371 // model to update.
365 // model to update.
372 this.model.set('value', $(e.target).html(), {updated_view: this});
366 this.model.set('value', $(e.target).html(), {updated_view: this});
373 this.touch();
367 this.touch();
374 },
368 },
375
376 });
369 });
377
378 widget_manager.register_widget_view('ListBoxView', ListBoxView);
370 widget_manager.register_widget_view('ListBoxView', ListBoxView);
379 });
371 });
@@ -15,9 +15,10 b''
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager){
17 define(["notebook/js/widgets/widget"], function(widget_manager){
18
18 var AccordionView = IPython.DOMWidgetView.extend({
19 var AccordionView = IPython.DOMWidgetView.extend({
19
20 render: function(){
20 render: function(){
21 // Called when view is rendered.
21 var guid = 'accordion' + IPython.utils.uuid();
22 var guid = 'accordion' + IPython.utils.uuid();
22 this.$el
23 this.$el
23 .attr('id', guid)
24 .attr('id', guid)
@@ -35,7 +36,6 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
35 //
36 //
36 // Called when the model is changed. The model may have been
37 // Called when the model is changed. The model may have been
37 // changed by another view or by a state update from the back-end.
38 // changed by another view or by a state update from the back-end.
38
39 if (options === undefined || options.updated_view != this) {
39 if (options === undefined || options.updated_view != this) {
40 // Set tab titles
40 // Set tab titles
41 var titles = this.model.get('_titles');
41 var titles = this.model.get('_titles');
@@ -67,6 +67,7 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
67 },
67 },
68
68
69 update_children: function(old_list, new_list) {
69 update_children: function(old_list, new_list) {
70 // Called when the children list is modified.
70 this.do_diff(old_list,
71 this.do_diff(old_list,
71 new_list,
72 new_list,
72 $.proxy(this.remove_child_model, this),
73 $.proxy(this.remove_child_model, this),
@@ -74,6 +75,7 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
74 },
75 },
75
76
76 remove_child_model: function(model) {
77 remove_child_model: function(model) {
78 // Called when a child is removed from children list.
77 var accordion_group = this.model_containers[model.id];
79 var accordion_group = this.model_containers[model.id];
78 this.containers.splice(accordion_group.container_index, 1);
80 this.containers.splice(accordion_group.container_index, 1);
79 delete this.model_containers[model.id];
81 delete this.model_containers[model.id];
@@ -82,6 +84,7 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
82 },
84 },
83
85
84 add_child_model: function(model) {
86 add_child_model: function(model) {
87 // Called when a child is added to children list.
85 var view = this.create_child_view(model);
88 var view = this.create_child_view(model);
86 var index = this.containers.length;
89 var index = this.containers.length;
87 var uuid = IPython.utils.uuid();
90 var uuid = IPython.utils.uuid();
@@ -126,17 +129,18 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
126 setTimeout(function(){ that.update(); }, 500);
129 setTimeout(function(){ that.update(); }, 500);
127 },
130 },
128 });
131 });
129
130 widget_manager.register_widget_view('AccordionView', AccordionView);
132 widget_manager.register_widget_view('AccordionView', AccordionView);
131
133
132 var TabView = IPython.DOMWidgetView.extend({
134
133
135 var TabView = IPython.DOMWidgetView.extend({
134 initialize: function() {
136 initialize: function() {
137 // Public constructor.
135 this.containers = [];
138 this.containers = [];
136 TabView.__super__.initialize.apply(this, arguments);
139 TabView.__super__.initialize.apply(this, arguments);
137 },
140 },
138
141
139 render: function(){
142 render: function(){
143 // Called when view is rendered.
140 var uuid = 'tabs'+IPython.utils.uuid();
144 var uuid = 'tabs'+IPython.utils.uuid();
141 var that = this;
145 var that = this;
142 this.$tabs = $('<div />', {id: uuid})
146 this.$tabs = $('<div />', {id: uuid})
@@ -152,19 +156,9 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
152 this.update_children(model.previous('children'), value);
156 this.update_children(model.previous('children'), value);
153 }, this);
157 }, this);
154 },
158 },
155
159
156 update_children: function(old_list, new_list) {
157 _.each(this.containers, function(element, index, list) {
158 element.remove();
159 }, this);
160 this.containers = [];
161 this.update_child_views(old_list, new_list);
162 _.each(new_list, function(element, index, list) {
163 this.add_child_view(this.child_views[element]);
164 }, this)
165 },
166
167 update_children: function(old_list, new_list) {
160 update_children: function(old_list, new_list) {
161 // Called when the children list is modified.
168 this.do_diff(old_list,
162 this.do_diff(old_list,
169 new_list,
163 new_list,
170 $.proxy(this.remove_child_model, this),
164 $.proxy(this.remove_child_model, this),
@@ -172,6 +166,7 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
172 },
166 },
173
167
174 remove_child_model: function(model) {
168 remove_child_model: function(model) {
169 // Called when a child is removed from children list.
175 var view = this.child_views[model.id];
170 var view = this.child_views[model.id];
176 this.containers.splice(view.parent_tab.tab_text_index, 1);
171 this.containers.splice(view.parent_tab.tab_text_index, 1);
177 view.parent_tab.remove();
172 view.parent_tab.remove();
@@ -181,6 +176,7 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
181 },
176 },
182
177
183 add_child_model: function(model) {
178 add_child_model: function(model) {
179 // Called when a child is added to children list.
184 var view = this.create_child_view(model);
180 var view = this.create_child_view(model);
185 var index = this.containers.length;
181 var index = this.containers.length;
186 var uuid = IPython.utils.uuid();
182 var uuid = IPython.utils.uuid();
@@ -238,11 +234,11 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
238 },
234 },
239
235
240 select_page: function(index) {
236 select_page: function(index) {
237 // Select a page.
241 this.$tabs.find('li')
238 this.$tabs.find('li')
242 .removeClass('active');
239 .removeClass('active');
243 this.containers[index].tab('show');
240 this.containers[index].tab('show');
244 },
241 },
245 });
242 });
246
247 widget_manager.register_widget_view('TabView', TabView);
243 widget_manager.register_widget_view('TabView', TabView);
248 });
244 });
@@ -15,10 +15,10 b''
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager){
17 define(["notebook/js/widgets/widget"], function(widget_manager){
18 var HTMLView = IPython.DOMWidgetView.extend({
18
19
19 var HTMLView = IPython.DOMWidgetView.extend({
20 // Called when view is rendered.
21 render : function(){
20 render : function(){
21 // Called when view is rendered.
22 this.update(); // Set defaults.
22 this.update(); // Set defaults.
23 },
23 },
24
24
@@ -30,16 +30,13 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
30 this.$el.html(this.model.get('value'));
30 this.$el.html(this.model.get('value'));
31 return HTMLView.__super__.update.apply(this);
31 return HTMLView.__super__.update.apply(this);
32 },
32 },
33
34 });
33 });
35
36 widget_manager.register_widget_view('HTMLView', HTMLView);
34 widget_manager.register_widget_view('HTMLView', HTMLView);
37
35
38
36
39 var LatexView = IPython.DOMWidgetView.extend({
37 var LatexView = IPython.DOMWidgetView.extend({
40
41 // Called when view is rendered.
42 render : function(){
38 render : function(){
39 // Called when view is rendered.
43 this.update(); // Set defaults.
40 this.update(); // Set defaults.
44 },
41 },
45
42
@@ -52,16 +49,14 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
52 MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]);
49 MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]);
53
50
54 return LatexView.__super__.update.apply(this);
51 return LatexView.__super__.update.apply(this);
55 },
52 },
56
57 });
53 });
58
59 widget_manager.register_widget_view('LatexView', LatexView);
54 widget_manager.register_widget_view('LatexView', LatexView);
60
55
61 var TextAreaView = IPython.DOMWidgetView.extend({
56
62
57 var TextAreaView = IPython.DOMWidgetView.extend({
63 // Called when view is rendered.
64 render: function(){
58 render: function(){
59 // Called when view is rendered.
65 this.$el
60 this.$el
66 .addClass('widget-hbox')
61 .addClass('widget-hbox')
67 .html('');
62 .html('');
@@ -79,19 +74,18 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
79 this.model.on('msg:custom', $.proxy(this._handle_textarea_msg, this));
74 this.model.on('msg:custom', $.proxy(this._handle_textarea_msg, this));
80 },
75 },
81
76
82
83 _handle_textarea_msg: function (content){
77 _handle_textarea_msg: function (content){
78 // Handle when a custom msg is recieved from the back-end.
84 if (content.method == "scroll_to_bottom") {
79 if (content.method == "scroll_to_bottom") {
85 this.scroll_to_bottom();
80 this.scroll_to_bottom();
86 }
81 }
87 },
82 },
88
83
89
90 scroll_to_bottom: function (){
84 scroll_to_bottom: function (){
85 // Scroll the text-area view to the bottom.
91 this.$textbox.scrollTop(this.$textbox[0].scrollHeight);
86 this.$textbox.scrollTop(this.$textbox[0].scrollHeight);
92 },
87 },
93
88
94
95 update: function(options){
89 update: function(options){
96 // Update the contents of this view
90 // Update the contents of this view
97 //
91 //
@@ -114,12 +108,15 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
114 return TextAreaView.__super__.update.apply(this);
108 return TextAreaView.__super__.update.apply(this);
115 },
109 },
116
110
117 events: {"keyup textarea": "handleChanging",
111 events: {
118 "paste textarea": "handleChanging",
112 // Dictionary of events and their handlers.
119 "cut textarea": "handleChanging"},
113 "keyup textarea": "handleChanging",
114 "paste textarea": "handleChanging",
115 "cut textarea": "handleChanging"
116 },
120
117
121 // Handles and validates user input.
122 handleChanging: function(e) {
118 handleChanging: function(e) {
119 // Handles and validates user input.
123
120
124 // Calling model.set will trigger all of the other views of the
121 // Calling model.set will trigger all of the other views of the
125 // model to update.
122 // model to update.
@@ -127,13 +124,12 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
127 this.touch();
124 this.touch();
128 },
125 },
129 });
126 });
130
131 widget_manager.register_widget_view('TextAreaView', TextAreaView);
127 widget_manager.register_widget_view('TextAreaView', TextAreaView);
132
128
133 var TextBoxView = IPython.DOMWidgetView.extend({
129
134
130 var TextBoxView = IPython.DOMWidgetView.extend({
135 // Called when view is rendered.
136 render: function(){
131 render: function(){
132 // Called when view is rendered.
137 this.$el
133 this.$el
138 .addClass('widget-hbox-single')
134 .addClass('widget-hbox-single')
139 .html('');
135 .html('');
@@ -173,13 +169,16 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
173 return TextBoxView.__super__.update.apply(this);
169 return TextBoxView.__super__.update.apply(this);
174 },
170 },
175
171
176 events: {"keyup input": "handleChanging",
172 events: {
177 "paste input": "handleChanging",
173 // Dictionary of events and their handlers.
178 "cut input": "handleChanging",
174 "keyup input": "handleChanging",
179 "keypress input": "handleKeypress"},
175 "paste input": "handleChanging",
176 "cut input": "handleChanging",
177 "keypress input": "handleKeypress"
178 },
180
179
181 // Handles and validates user input.
182 handleChanging: function(e) {
180 handleChanging: function(e) {
181 // Handles and validates user input.
183
182
184 // Calling model.set will trigger all of the other views of the
183 // Calling model.set will trigger all of the other views of the
185 // model to update.
184 // model to update.
@@ -187,13 +186,12 b' define(["notebook/js/widgets/widget"], function(widget_manager){'
187 this.touch();
186 this.touch();
188 },
187 },
189
188
190 // Handles text submition
191 handleKeypress: function(e) {
189 handleKeypress: function(e) {
190 // Handles text submition
192 if (e.keyCode == 13) { // Return key
191 if (e.keyCode == 13) { // Return key
193 this.send({event: 'submit'});
192 this.send({event: 'submit'});
194 }
193 }
195 },
194 },
196 });
195 });
197
198 widget_manager.register_widget_view('TextBoxView', TextBoxView);
196 widget_manager.register_widget_view('TextBoxView', TextBoxView);
199 });
197 });
General Comments 0
You need to be logged in to leave comments. Login now