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