##// END OF EJS Templates
Add support for parsing pairs of numbers for range sliders
Gordon Ball -
Show More
@@ -9,9 +9,9 define([
9 var IntTextView = int_widgets.IntTextView;
9 var IntTextView = int_widgets.IntTextView;
10
10
11 var FloatSliderView = IntSliderView.extend({
11 var FloatSliderView = IntSliderView.extend({
12 _validate_text_input: function(x) {
12 _parse_text_input: parseFloat,
13 return parseFloat(x);
13
14 },
14 _range_regex: /^\s*([+-]?\d*\.?\d+)\s*[-:]\s*([+-]?\d*\.?\d+)/,
15
15
16 _validate_slide_value: function(x) {
16 _validate_slide_value: function(x) {
17 // Validate the value of the slider before sending it to the back-end
17 // Validate the value of the slider before sending it to the back-end
@@ -170,34 +170,70 define([
170 },
170 },
171
171
172 handleTextChange: function() {
172 handleTextChange: function() {
173 // this handles the entry of text into the contentEditable label
174 // first, the value is checked if it contains a parseable number
175 // (or pair of numbers, for the _range case)
176 // then it is clamped within the min-max range of the slider
177 // finally, the model is updated if the value is to be changed
178 //
179 // if any of these conditions are not met, the text is reset
180 //
181 // the step size is not enforced
182
173 var text = this.$readout.text();
183 var text = this.$readout.text();
174 var value = this._validate_text_input(text);
184 var vmin = this.model.get('min');
175 if (isNaN(value)) {
185 var vmax = this.model.get('max');
176 this.$readout.text(this.model.get('value'));
186 if (this.model.get("_range")) {
187 // range case
188 // ranges can be expressed either "val-val" or "val:val" (+spaces)
189 var match = this._range_regex.exec(text);
190 if (match) {
191 var values = [this._parse_text_input(match[1]),
192 this._parse_text_input(match[2])];
193 // reject input where NaN or lower > upper
194 if (isNaN(values[0]) ||
195 isNaN(values[1]) ||
196 (values[0] > values[1])) {
197 this.$readout.text(this.model.get('value').join('-'));
198 } else {
199 // clamp to range
200 values = [Math.max(Math.min(values[0], vmax), vmin),
201 Math.max(Math.min(values[1], vmax), vmin)];
202
203 if ((values[0] != this.model.get('value')[0]) ||
204 (values[1] != this.model.get('value')[1])) {
205 this.$readout.text(values.join('-'));
206 this.model.set('value', values, {updated_view: this});
207 this.touch();
208 } else {
209 this.$readout.text(this.model.get('value').join('-'));
210 }
211 }
212 } else {
213 this.$readout.text(this.model.get('value').join('-'));
214 }
177 } else {
215 } else {
178 //check for outside range
216 // single value case
179 if (value > this.model.get('max')) value = this.model.get('max');
217 var value = this._parse_text_input(text);
180 if (value < this.model.get('min')) value = this.model.get('min');
218 if (isNaN(value)) {
181
219 this.$readout.text(this.model.get('value'));
182 //update the readout unconditionally
220 } else {
183 //this covers eg, entering a float value which rounds to the
221 value = Math.max(Math.min(value, vmax), vmin);
184 //existing int value, which will not trigger an update since the model
222
185 //doesn't change, but we should update the text to reflect that
223 if (value != this.model.get('value')) {
186 //a float value isn't being used
224 this.$readout.text(value);
187 this.$readout.text(value);
225 this.model.set('value', value, {updated_view: this});
188
226 this.touch();
189 //note that the step size currently isn't enforced, so if an
227 } else {
190 //off-step value is input it will be retained
228 this.$readout.text(this.model.get('value'));
191
229 }
192 //update the model
230 }
193 this.model.set('value', value, {updated_view: this});
194 this.touch();
195 }
231 }
196 },
232 },
197
233
198 _validate_text_input: function(x) {
234 _parse_text_input: parseInt,
199 return parseInt(x);
235
200 },
236 _range_regex: /^\s*([+-]?\d+)\s*[-:]\s*([+-]?\d+)/,
201
237
202 handleSliderChange: function(e, ui) {
238 handleSliderChange: function(e, ui) {
203 // Called when the slider value is changed.
239 // Called when the slider value is changed.
General Comments 0
You need to be logged in to leave comments. Login now