##// END OF EJS Templates
Add support for parsing pairs of numbers for range sliders
Gordon Ball -
Show More
@@ -9,9 +9,9 define([
9 9 var IntTextView = int_widgets.IntTextView;
10 10
11 11 var FloatSliderView = IntSliderView.extend({
12 _validate_text_input: function(x) {
13 return parseFloat(x);
14 },
12 _parse_text_input: parseFloat,
13
14 _range_regex: /^\s*([+-]?\d*\.?\d+)\s*[-:]\s*([+-]?\d*\.?\d+)/,
15 15
16 16 _validate_slide_value: function(x) {
17 17 // Validate the value of the slider before sending it to the back-end
@@ -170,34 +170,70 define([
170 170 },
171 171
172 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 183 var text = this.$readout.text();
174 var value = this._validate_text_input(text);
175 if (isNaN(value)) {
176 this.$readout.text(this.model.get('value'));
184 var vmin = this.model.get('min');
185 var vmax = this.model.get('max');
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 215 } else {
178 //check for outside range
179 if (value > this.model.get('max')) value = this.model.get('max');
180 if (value < this.model.get('min')) value = this.model.get('min');
181
182 //update the readout unconditionally
183 //this covers eg, entering a float value which rounds to the
184 //existing int value, which will not trigger an update since the model
185 //doesn't change, but we should update the text to reflect that
186 //a float value isn't being used
187 this.$readout.text(value);
188
189 //note that the step size currently isn't enforced, so if an
190 //off-step value is input it will be retained
191
192 //update the model
193 this.model.set('value', value, {updated_view: this});
194 this.touch();
216 // single value case
217 var value = this._parse_text_input(text);
218 if (isNaN(value)) {
219 this.$readout.text(this.model.get('value'));
220 } else {
221 value = Math.max(Math.min(value, vmax), vmin);
222
223 if (value != this.model.get('value')) {
224 this.$readout.text(value);
225 this.model.set('value', value, {updated_view: this});
226 this.touch();
227 } else {
228 this.$readout.text(this.model.get('value'));
229 }
230 }
195 231 }
196 232 },
197 233
198 _validate_text_input: function(x) {
199 return parseInt(x);
200 },
234 _parse_text_input: parseInt,
235
236 _range_regex: /^\s*([+-]?\d+)\s*[-:]\s*([+-]?\d+)/,
201 237
202 238 handleSliderChange: function(e, ui) {
203 239 // Called when the slider value is changed.
General Comments 0
You need to be logged in to leave comments. Login now