##// END OF EJS Templates
document _keys function and rename to jquery_slider_keys
Jonathan Frederic -
Show More
@@ -1,271 +1,269 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2013 The IPython Development Team
2 // Copyright (C) 2013 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // FloatRangeWidget
9 // FloatRangeWidget
10 //============================================================================
10 //============================================================================
11
11
12 /**
12 /**
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager){
17 define(["notebook/js/widgets/widget"], function(widget_manager){
18 var FloatRangeWidgetModel = IPython.WidgetModel.extend({});
18 var FloatRangeWidgetModel = IPython.WidgetModel.extend({});
19 widget_manager.register_widget_model('FloatRangeWidgetModel', FloatRangeWidgetModel);
19 widget_manager.register_widget_model('FloatRangeWidgetModel', FloatRangeWidgetModel);
20
20
21 var FloatSliderView = IPython.DOMWidgetView.extend({
21 var FloatSliderView = IPython.DOMWidgetView.extend({
22
22
23 // Called when view is rendered.
23 // Called when view is rendered.
24 render : function(){
24 render : function(){
25 this.$el
25 this.$el
26 .addClass('widget-hbox-single')
26 .addClass('widget-hbox-single');
27 .html('');
28 this.$label = $('<div />')
27 this.$label = $('<div />')
29 .appendTo(this.$el)
28 .appendTo(this.$el)
30 .addClass('widget-hlabel')
29 .addClass('widget-hlabel')
31 .hide();
30 .hide();
32 this.$slider = $('<div />')
31 this.$slider = $('<div />')
33 .slider({})
32 .slider({})
34 .addClass('slider');
33 .addClass('slider');
35
34
36 // Put the slider in a container
35 // Put the slider in a container
37 this.$slider_container = $('<div />')
36 this.$slider_container = $('<div />')
38 .addClass('widget-hslider')
37 .addClass('widget-hslider')
39 .append(this.$slider);
38 .append(this.$slider);
40 this.$el_to_style = this.$slider_container; // Set default element to style
39 this.$el_to_style = this.$slider_container; // Set default element to style
41 this.$el.append(this.$slider_container);
40 this.$el.append(this.$slider_container);
42
41
43 // Set defaults.
42 // Set defaults.
44 this.update();
43 this.update();
45 },
44 },
46
45
47 update : function(options){
46 update : function(options){
48 // Update the contents of this view
47 // Update the contents of this view
49 //
48 //
50 // Called when the model is changed. The model may have been
49 // Called when the model is changed. The model may have been
51 // changed by another view or by a state update from the back-end.
50 // changed by another view or by a state update from the back-end.
52
51
53 if (options === undefined || options.updated_view != this) {
52 if (options === undefined || options.updated_view != this) {
54 // Slider related keys.
53 // JQuery slider option keys. These keys happen to have a
55 var _keys = ['step', 'max', 'min', 'disabled'];
54 // one-to-one mapping with the corrosponding keys of the model.
56 for (var index in _keys) {
55 var jquery_slider_keys = ['step', 'max', 'min', 'disabled'];
57 var key = _keys[index];
56 for (var index in jquery_slider_keys) {
57 var key = jquery_slider_keys[index];
58 if (this.model.get(key) !== undefined) {
58 if (this.model.get(key) !== undefined) {
59 this.$slider.slider("option", key, this.model.get(key));
59 this.$slider.slider("option", key, this.model.get(key));
60 }
60 }
61 }
61 }
62
62
63 // WORKAROUND FOR JQUERY SLIDER BUG.
63 // WORKAROUND FOR JQUERY SLIDER BUG.
64 // The horizontal position of the slider handle
64 // The horizontal position of the slider handle
65 // depends on the value of the slider at the time
65 // depends on the value of the slider at the time
66 // of orientation change. Before applying the new
66 // of orientation change. Before applying the new
67 // workaround, we set the value to the minimum to
67 // workaround, we set the value to the minimum to
68 // make sure that the horizontal placement of the
68 // make sure that the horizontal placement of the
69 // handle in the vertical slider is always
69 // handle in the vertical slider is always
70 // consistent.
70 // consistent.
71 var orientation = this.model.get('orientation');
71 var orientation = this.model.get('orientation');
72 var value = this.model.get('min');
72 var value = this.model.get('min');
73 this.$slider.slider('option', 'value', value);
73 this.$slider.slider('option', 'value', value);
74 this.$slider.slider('option', 'orientation', orientation);
74 this.$slider.slider('option', 'orientation', orientation);
75 value = this.model.get('value');
75 value = this.model.get('value');
76 this.$slider.slider('option', 'value', value);
76 this.$slider.slider('option', 'value', value);
77
77
78 // Use the right CSS classes for vertical & horizontal sliders
78 // Use the right CSS classes for vertical & horizontal sliders
79 if (orientation=='vertical') {
79 if (orientation=='vertical') {
80 this.$slider_container
80 this.$slider_container
81 .removeClass('widget-hslider')
81 .removeClass('widget-hslider')
82 .addClass('widget-vslider');
82 .addClass('widget-vslider');
83 this.$el
83 this.$el
84 .removeClass('widget-hbox-single')
84 .removeClass('widget-hbox-single')
85 .addClass('widget-vbox-single');
85 .addClass('widget-vbox-single');
86 this.$label
86 this.$label
87 .removeClass('widget-hlabel')
87 .removeClass('widget-hlabel')
88 .addClass('widget-vlabel');
88 .addClass('widget-vlabel');
89
89
90 } else {
90 } else {
91 this.$slider_container
91 this.$slider_container
92 .removeClass('widget-vslider')
92 .removeClass('widget-vslider')
93 .addClass('widget-hslider');
93 .addClass('widget-hslider');
94 this.$el
94 this.$el
95 .removeClass('widget-vbox-single')
95 .removeClass('widget-vbox-single')
96 .addClass('widget-hbox-single');
96 .addClass('widget-hbox-single');
97 this.$label
97 this.$label
98 .removeClass('widget-vlabel')
98 .removeClass('widget-vlabel')
99 .addClass('widget-hlabel');
99 .addClass('widget-hlabel');
100 }
100 }
101
101
102 var description = this.model.get('description');
102 var description = this.model.get('description');
103 if (description.length === 0) {
103 if (description.length === 0) {
104 this.$label.hide();
104 this.$label.hide();
105 } else {
105 } else {
106 this.$label.html(description);
106 this.$label.html(description);
107 this.$label.show();
107 this.$label.show();
108 }
108 }
109 }
109 }
110 return IPython.DOMWidgetView.prototype.update.call(this);
110 return IPython.DOMWidgetView.prototype.update.call(this);
111 },
111 },
112
112
113 // Handles: User input
113 // Handles: User input
114 events: { "slide" : "handleSliderChange" },
114 events: { "slide" : "handleSliderChange" },
115 handleSliderChange: function(e, ui) {
115 handleSliderChange: function(e, ui) {
116
116
117 // 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
118 // model to update.
118 // model to update.
119 this.model.set('value', ui.value, {updated_view: this});
119 this.model.set('value', ui.value, {updated_view: this});
120 this.touch();
120 this.touch();
121 },
121 },
122 });
122 });
123
123
124 widget_manager.register_widget_view('FloatSliderView', FloatSliderView);
124 widget_manager.register_widget_view('FloatSliderView', FloatSliderView);
125
125
126
126
127 var FloatTextView = IPython.DOMWidgetView.extend({
127 var FloatTextView = IPython.DOMWidgetView.extend({
128
128
129 // Called when view is rendered.
129 // Called when view is rendered.
130 render : function(){
130 render : function(){
131 this.$el
131 this.$el
132 .addClass('widget-hbox-single')
132 .addClass('widget-hbox-single');
133 .html('');
134 this.$label = $('<div />')
133 this.$label = $('<div />')
135 .appendTo(this.$el)
134 .appendTo(this.$el)
136 .addClass('widget-hlabel')
135 .addClass('widget-hlabel')
137 .hide();
136 .hide();
138 this.$textbox = $('<input type="text" />')
137 this.$textbox = $('<input type="text" />')
139 .addClass('input')
138 .addClass('input')
140 .addClass('widget-numeric-text')
139 .addClass('widget-numeric-text')
141 .appendTo(this.$el);
140 .appendTo(this.$el);
142 this.$el_to_style = this.$textbox; // Set default element to style
141 this.$el_to_style = this.$textbox; // Set default element to style
143 this.update(); // Set defaults.
142 this.update(); // Set defaults.
144 },
143 },
145
144
146 update : function(options){
145 update : function(options){
147 // Update the contents of this view
146 // Update the contents of this view
148 //
147 //
149 // Called when the model is changed. The model may have been
148 // Called when the model is changed. The model may have been
150 // changed by another view or by a state update from the back-end.
149 // changed by another view or by a state update from the back-end.
151
150
152 if (options === undefined || options.updated_view != this) {
151 if (options === undefined || options.updated_view != this) {
153 var value = this.model.get('value');
152 var value = this.model.get('value');
154 if (parseFloat(this.$textbox.val()) != value) {
153 if (parseFloat(this.$textbox.val()) != value) {
155 this.$textbox.val(value);
154 this.$textbox.val(value);
156 }
155 }
157
156
158 if (this.model.get('disabled')) {
157 if (this.model.get('disabled')) {
159 this.$textbox.attr('disabled','disabled');
158 this.$textbox.attr('disabled','disabled');
160 } else {
159 } else {
161 this.$textbox.removeAttr('disabled');
160 this.$textbox.removeAttr('disabled');
162 }
161 }
163
162
164 var description = this.model.get('description');
163 var description = this.model.get('description');
165 if (description.length === 0) {
164 if (description.length === 0) {
166 this.$label.hide();
165 this.$label.hide();
167 } else {
166 } else {
168 this.$label.html(description);
167 this.$label.html(description);
169 this.$label.show();
168 this.$label.show();
170 }
169 }
171 }
170 }
172 return IPython.DOMWidgetView.prototype.update.call(this);
171 return IPython.DOMWidgetView.prototype.update.call(this);
173 },
172 },
174
173
175
174
176 events: {"keyup input" : "handleChanging",
175 events: {"keyup input" : "handleChanging",
177 "paste input" : "handleChanging",
176 "paste input" : "handleChanging",
178 "cut input" : "handleChanging",
177 "cut input" : "handleChanging",
179 "change input" : "handleChanged"}, // Fires only when control is validated or looses focus.
178 "change input" : "handleChanged"}, // Fires only when control is validated or looses focus.
180
179
181 // Handles and validates user input.
180 // Handles and validates user input.
182 handleChanging: function(e) {
181 handleChanging: function(e) {
183
182
184 // Try to parse value as a float.
183 // Try to parse value as a float.
185 var numericalValue = 0.0;
184 var numericalValue = 0.0;
186 if (e.target.value !== '') {
185 if (e.target.value !== '') {
187 numericalValue = parseFloat(e.target.value);
186 numericalValue = parseFloat(e.target.value);
188 }
187 }
189
188
190 // If parse failed, reset value to value stored in model.
189 // If parse failed, reset value to value stored in model.
191 if (isNaN(numericalValue)) {
190 if (isNaN(numericalValue)) {
192 e.target.value = this.model.get('value');
191 e.target.value = this.model.get('value');
193 } else if (!isNaN(numericalValue)) {
192 } else if (!isNaN(numericalValue)) {
194 if (this.model.get('max') !== undefined) {
193 if (this.model.get('max') !== undefined) {
195 numericalValue = Math.min(this.model.get('max'), numericalValue);
194 numericalValue = Math.min(this.model.get('max'), numericalValue);
196 }
195 }
197 if (this.model.get('min') !== undefined) {
196 if (this.model.get('min') !== undefined) {
198 numericalValue = Math.max(this.model.get('min'), numericalValue);
197 numericalValue = Math.max(this.model.get('min'), numericalValue);
199 }
198 }
200
199
201 // Apply the value if it has changed.
200 // Apply the value if it has changed.
202 if (numericalValue != this.model.get('value')) {
201 if (numericalValue != this.model.get('value')) {
203
202
204 // Calling model.set will trigger all of the other views of the
203 // Calling model.set will trigger all of the other views of the
205 // model to update.
204 // model to update.
206 this.model.set('value', numericalValue, {updated_view: this});
205 this.model.set('value', numericalValue, {updated_view: this});
207 this.touch();
206 this.touch();
208 }
207 }
209 }
208 }
210 },
209 },
211
210
212 // Applies validated input.
211 // Applies validated input.
213 handleChanged: function(e) {
212 handleChanged: function(e) {
214 // Update the textbox
213 // Update the textbox
215 if (this.model.get('value') != e.target.value) {
214 if (this.model.get('value') != e.target.value) {
216 e.target.value = this.model.get('value');
215 e.target.value = this.model.get('value');
217 }
216 }
218 }
217 }
219 });
218 });
220
219
221 widget_manager.register_widget_view('FloatTextView', FloatTextView);
220 widget_manager.register_widget_view('FloatTextView', FloatTextView);
222
221
223
222
224 var ProgressView = IPython.DOMWidgetView.extend({
223 var ProgressView = IPython.DOMWidgetView.extend({
225
224
226 // Called when view is rendered.
225 // Called when view is rendered.
227 render : function(){
226 render : function(){
228 this.$el
227 this.$el
229 .addClass('widget-hbox-single')
228 .addClass('widget-hbox-single');
230 .html('');
231 this.$label = $('<div />')
229 this.$label = $('<div />')
232 .appendTo(this.$el)
230 .appendTo(this.$el)
233 .addClass('widget-hlabel')
231 .addClass('widget-hlabel')
234 .hide();
232 .hide();
235 this.$progress = $('<div />')
233 this.$progress = $('<div />')
236 .addClass('progress')
234 .addClass('progress')
237 .addClass('widget-progress')
235 .addClass('widget-progress')
238 .appendTo(this.$el);
236 .appendTo(this.$el);
239 this.$el_to_style = this.$progress; // Set default element to style
237 this.$el_to_style = this.$progress; // Set default element to style
240 this.$bar = $('<div />')
238 this.$bar = $('<div />')
241 .addClass('bar')
239 .addClass('bar')
242 .css('width', '50%')
240 .css('width', '50%')
243 .appendTo(this.$progress);
241 .appendTo(this.$progress);
244 this.update(); // Set defaults.
242 this.update(); // Set defaults.
245 },
243 },
246
244
247 update : function(){
245 update : function(){
248 // Update the contents of this view
246 // Update the contents of this view
249 //
247 //
250 // Called when the model is changed. The model may have been
248 // Called when the model is changed. The model may have been
251 // changed by another view or by a state update from the back-end.
249 // changed by another view or by a state update from the back-end.
252 var value = this.model.get('value');
250 var value = this.model.get('value');
253 var max = this.model.get('max');
251 var max = this.model.get('max');
254 var min = this.model.get('min');
252 var min = this.model.get('min');
255 var percent = 100.0 * (value - min) / (max - min);
253 var percent = 100.0 * (value - min) / (max - min);
256 this.$bar.css('width', percent + '%');
254 this.$bar.css('width', percent + '%');
257
255
258 var description = this.model.get('description');
256 var description = this.model.get('description');
259 if (description.length === 0) {
257 if (description.length === 0) {
260 this.$label.hide();
258 this.$label.hide();
261 } else {
259 } else {
262 this.$label.html(description);
260 this.$label.html(description);
263 this.$label.show();
261 this.$label.show();
264 }
262 }
265 return IPython.DOMWidgetView.prototype.update.call(this);
263 return IPython.DOMWidgetView.prototype.update.call(this);
266 },
264 },
267
265
268 });
266 });
269
267
270 widget_manager.register_widget_view('ProgressView', ProgressView);
268 widget_manager.register_widget_view('ProgressView', ProgressView);
271 });
269 });
@@ -1,219 +1,218 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2013 The IPython Development Team
2 // Copyright (C) 2013 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // IntRangeWidget
9 // IntRangeWidget
10 //============================================================================
10 //============================================================================
11
11
12 /**
12 /**
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager){
17 define(["notebook/js/widgets/widget"], function(widget_manager){
18 var IntRangeWidgetModel = IPython.WidgetModel.extend({});
18 var IntRangeWidgetModel = IPython.WidgetModel.extend({});
19 widget_manager.register_widget_model('IntRangeWidgetModel', IntRangeWidgetModel);
19 widget_manager.register_widget_model('IntRangeWidgetModel', IntRangeWidgetModel);
20
20
21 var IntSliderView = IPython.DOMWidgetView.extend({
21 var IntSliderView = IPython.DOMWidgetView.extend({
22
22
23 // Called when view is rendered.
23 // Called when view is rendered.
24 render : function(){
24 render : function(){
25 this.$el
25 this.$el
26 .addClass('widget-hbox-single')
26 .addClass('widget-hbox-single');
27 .html('');
28 this.$label = $('<div />')
27 this.$label = $('<div />')
29 .appendTo(this.$el)
28 .appendTo(this.$el)
30 .addClass('widget-hlabel')
29 .addClass('widget-hlabel')
31 .hide();
30 .hide();
32 this.$slider = $('<div />')
31 this.$slider = $('<div />')
33 .slider({})
32 .slider({})
34 .addClass('slider');
33 .addClass('slider');
35
34
36 // Put the slider in a container
35 // Put the slider in a container
37 this.$slider_container = $('<div />')
36 this.$slider_container = $('<div />')
38 .addClass('widget-hslider')
37 .addClass('widget-hslider')
39 .append(this.$slider);
38 .append(this.$slider);
40 this.$el_to_style = this.$slider_container; // Set default element to style
39 this.$el_to_style = this.$slider_container; // Set default element to style
41 this.$el.append(this.$slider_container);
40 this.$el.append(this.$slider_container);
42
41
43 // Set defaults.
42 // Set defaults.
44 this.update();
43 this.update();
45 },
44 },
46
45
47 update : function(options){
46 update : function(options){
48 // Update the contents of this view
47 // Update the contents of this view
49 //
48 //
50 // Called when the model is changed. The model may have been
49 // Called when the model is changed. The model may have been
51 // changed by another view or by a state update from the back-end.
50 // changed by another view or by a state update from the back-end.
52 if (options === undefined || options.updated_view != this) {
51 if (options === undefined || options.updated_view != this) {
53 // Slider related keys.
52 // JQuery slider option keys. These keys happen to have a
54 var _keys = ['step', 'max', 'min', 'disabled'];
53 // one-to-one mapping with the corrosponding keys of the model.
55 for (var index in _keys) {
54 var jquery_slider_keys = ['step', 'max', 'min', 'disabled'];
56 var key = _keys[index];
55 for (var index in jquery_slider_keys) {
56 var key = jquery_slider_keys[index];
57 if (this.model.get(key) !== undefined) {
57 if (this.model.get(key) !== undefined) {
58 this.$slider.slider("option", key, this.model.get(key));
58 this.$slider.slider("option", key, this.model.get(key));
59 }
59 }
60 }
60 }
61
61
62 // WORKAROUND FOR JQUERY SLIDER BUG.
62 // WORKAROUND FOR JQUERY SLIDER BUG.
63 // The horizontal position of the slider handle
63 // The horizontal position of the slider handle
64 // depends on the value of the slider at the time
64 // depends on the value of the slider at the time
65 // of orientation change. Before applying the new
65 // of orientation change. Before applying the new
66 // workaround, we set the value to the minimum to
66 // workaround, we set the value to the minimum to
67 // make sure that the horizontal placement of the
67 // make sure that the horizontal placement of the
68 // handle in the vertical slider is always
68 // handle in the vertical slider is always
69 // consistent.
69 // consistent.
70 var orientation = this.model.get('orientation');
70 var orientation = this.model.get('orientation');
71 var value = this.model.get('min');
71 var value = this.model.get('min');
72 this.$slider.slider('option', 'value', value);
72 this.$slider.slider('option', 'value', value);
73 this.$slider.slider('option', 'orientation', orientation);
73 this.$slider.slider('option', 'orientation', orientation);
74 value = this.model.get('value');
74 value = this.model.get('value');
75 this.$slider.slider('option', 'value', value);
75 this.$slider.slider('option', 'value', value);
76
76
77 // Use the right CSS classes for vertical & horizontal sliders
77 // Use the right CSS classes for vertical & horizontal sliders
78 if (orientation=='vertical') {
78 if (orientation=='vertical') {
79 this.$slider_container
79 this.$slider_container
80 .removeClass('widget-hslider')
80 .removeClass('widget-hslider')
81 .addClass('widget-vslider');
81 .addClass('widget-vslider');
82 this.$el
82 this.$el
83 .removeClass('widget-hbox-single')
83 .removeClass('widget-hbox-single')
84 .addClass('widget-vbox-single');
84 .addClass('widget-vbox-single');
85 this.$label
85 this.$label
86 .removeClass('widget-hlabel')
86 .removeClass('widget-hlabel')
87 .addClass('widget-vlabel');
87 .addClass('widget-vlabel');
88
88
89 } else {
89 } else {
90 this.$slider_container
90 this.$slider_container
91 .removeClass('widget-vslider')
91 .removeClass('widget-vslider')
92 .addClass('widget-hslider');
92 .addClass('widget-hslider');
93 this.$el
93 this.$el
94 .removeClass('widget-vbox-single')
94 .removeClass('widget-vbox-single')
95 .addClass('widget-hbox-single');
95 .addClass('widget-hbox-single');
96 this.$label
96 this.$label
97 .removeClass('widget-vlabel')
97 .removeClass('widget-vlabel')
98 .addClass('widget-hlabel');
98 .addClass('widget-hlabel');
99 }
99 }
100
100
101 var description = this.model.get('description');
101 var description = this.model.get('description');
102 if (description.length === 0) {
102 if (description.length === 0) {
103 this.$label.hide();
103 this.$label.hide();
104 } else {
104 } else {
105 this.$label.html(description);
105 this.$label.html(description);
106 this.$label.show();
106 this.$label.show();
107 }
107 }
108 }
108 }
109 return IPython.DOMWidgetView.prototype.update.call(this);
109 return IPython.DOMWidgetView.prototype.update.call(this);
110 },
110 },
111
111
112 // Handles: User input
112 // Handles: User input
113 events: { "slide" : "handleSliderChange" },
113 events: { "slide" : "handleSliderChange" },
114 handleSliderChange: function(e, ui) {
114 handleSliderChange: function(e, ui) {
115
115
116 // Calling model.set will trigger all of the other views of the
116 // Calling model.set will trigger all of the other views of the
117 // model to update.
117 // model to update.
118 this.model.set('value', ~~ui.value, {updated_view: this}); // Double bit-wise not to truncate decimel
118 this.model.set('value', ~~ui.value, {updated_view: this}); // Double bit-wise not to truncate decimel
119 this.touch();
119 this.touch();
120 },
120 },
121 });
121 });
122
122
123 widget_manager.register_widget_view('IntSliderView', IntSliderView);
123 widget_manager.register_widget_view('IntSliderView', IntSliderView);
124
124
125 var IntTextView = IPython.DOMWidgetView.extend({
125 var IntTextView = IPython.DOMWidgetView.extend({
126
126
127 // Called when view is rendered.
127 // Called when view is rendered.
128 render : function(){
128 render : function(){
129 this.$el
129 this.$el
130 .addClass('widget-hbox-single')
130 .addClass('widget-hbox-single');
131 .html('');
132 this.$label = $('<div />')
131 this.$label = $('<div />')
133 .appendTo(this.$el)
132 .appendTo(this.$el)
134 .addClass('widget-hlabel')
133 .addClass('widget-hlabel')
135 .hide();
134 .hide();
136 this.$textbox = $('<input type="text" />')
135 this.$textbox = $('<input type="text" />')
137 .addClass('input')
136 .addClass('input')
138 .addClass('widget-numeric-text')
137 .addClass('widget-numeric-text')
139 .appendTo(this.$el);
138 .appendTo(this.$el);
140 this.$el_to_style = this.$textbox; // Set default element to style
139 this.$el_to_style = this.$textbox; // Set default element to style
141 this.update(); // Set defaults.
140 this.update(); // Set defaults.
142 },
141 },
143
142
144 update : function(options){
143 update : function(options){
145 // Update the contents of this view
144 // Update the contents of this view
146 //
145 //
147 // Called when the model is changed. The model may have been
146 // Called when the model is changed. The model may have been
148 // changed by another view or by a state update from the back-end.
147 // changed by another view or by a state update from the back-end.
149 if (options === undefined || options.updated_view != this) {
148 if (options === undefined || options.updated_view != this) {
150 var value = this.model.get('value');
149 var value = this.model.get('value');
151 if (parseInt(this.$textbox.val()) != value) {
150 if (parseInt(this.$textbox.val()) != value) {
152 this.$textbox.val(value);
151 this.$textbox.val(value);
153 }
152 }
154
153
155 if (this.model.get('disabled')) {
154 if (this.model.get('disabled')) {
156 this.$textbox.attr('disabled','disabled');
155 this.$textbox.attr('disabled','disabled');
157 } else {
156 } else {
158 this.$textbox.removeAttr('disabled');
157 this.$textbox.removeAttr('disabled');
159 }
158 }
160
159
161 var description = this.model.get('description');
160 var description = this.model.get('description');
162 if (description.length === 0) {
161 if (description.length === 0) {
163 this.$label.hide();
162 this.$label.hide();
164 } else {
163 } else {
165 this.$label.html(description);
164 this.$label.html(description);
166 this.$label.show();
165 this.$label.show();
167 }
166 }
168 }
167 }
169 return IPython.DOMWidgetView.prototype.update.call(this);
168 return IPython.DOMWidgetView.prototype.update.call(this);
170 },
169 },
171
170
172
171
173 events: {"keyup input" : "handleChanging",
172 events: {"keyup input" : "handleChanging",
174 "paste input" : "handleChanging",
173 "paste input" : "handleChanging",
175 "cut input" : "handleChanging",
174 "cut input" : "handleChanging",
176 "change input" : "handleChanged"}, // Fires only when control is validated or looses focus.
175 "change input" : "handleChanged"}, // Fires only when control is validated or looses focus.
177
176
178 // Handles and validates user input.
177 // Handles and validates user input.
179 handleChanging: function(e) {
178 handleChanging: function(e) {
180
179
181 // Try to parse value as a float.
180 // Try to parse value as a float.
182 var numericalValue = 0;
181 var numericalValue = 0;
183 if (e.target.value !== '') {
182 if (e.target.value !== '') {
184 numericalValue = parseInt(e.target.value);
183 numericalValue = parseInt(e.target.value);
185 }
184 }
186
185
187 // If parse failed, reset value to value stored in model.
186 // If parse failed, reset value to value stored in model.
188 if (isNaN(numericalValue)) {
187 if (isNaN(numericalValue)) {
189 e.target.value = this.model.get('value');
188 e.target.value = this.model.get('value');
190 } else if (!isNaN(numericalValue)) {
189 } else if (!isNaN(numericalValue)) {
191 if (this.model.get('max') !== undefined) {
190 if (this.model.get('max') !== undefined) {
192 numericalValue = Math.min(this.model.get('max'), numericalValue);
191 numericalValue = Math.min(this.model.get('max'), numericalValue);
193 }
192 }
194 if (this.model.get('min') !== undefined) {
193 if (this.model.get('min') !== undefined) {
195 numericalValue = Math.max(this.model.get('min'), numericalValue);
194 numericalValue = Math.max(this.model.get('min'), numericalValue);
196 }
195 }
197
196
198 // Apply the value if it has changed.
197 // Apply the value if it has changed.
199 if (numericalValue != this.model.get('value')) {
198 if (numericalValue != this.model.get('value')) {
200
199
201 // Calling model.set will trigger all of the other views of the
200 // Calling model.set will trigger all of the other views of the
202 // model to update.
201 // model to update.
203 this.model.set('value', numericalValue, {updated_view: this});
202 this.model.set('value', numericalValue, {updated_view: this});
204 this.touch();
203 this.touch();
205 }
204 }
206 }
205 }
207 },
206 },
208
207
209 // Applies validated input.
208 // Applies validated input.
210 handleChanged: function(e) {
209 handleChanged: function(e) {
211 // Update the textbox
210 // Update the textbox
212 if (this.model.get('value') != e.target.value) {
211 if (this.model.get('value') != e.target.value) {
213 e.target.value = this.model.get('value');
212 e.target.value = this.model.get('value');
214 }
213 }
215 }
214 }
216 });
215 });
217
216
218 widget_manager.register_widget_view('IntTextView', IntTextView);
217 widget_manager.register_widget_view('IntTextView', IntTextView);
219 });
218 });
General Comments 0
You need to be logged in to leave comments. Login now