From c498ce58484f4acee3ddc5d991a68289daea1f6c Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Herv=C3=A9=20Cauwelier?= <herve@itaapy.com>
Date: Tue, 2 Sep 2008 22:32:55 +0200
Subject: [PATCH] slider: support floating values, set number of decimals
---
ui/ui.slider.js | 66 ++++++++++++++++++++++++++++++++----------------------
1 files changed, 39 insertions(+), 27 deletions(-)
diff --git a/ui/ui.slider.js b/ui/ui.slider.js
index ad33290..64e131b 100644
|
a
|
b
|
$.widget("ui.slider", { |
| 24 | 24 | return { |
| 25 | 25 | options: this.options, |
| 26 | 26 | handle: this.currentHandle, |
| 27 | | value: this.options.axis != "both" || !this.options.axis ? Math.round(this.value(null,this.options.axis == "vertical" ? "y" : "x")) : { |
| 28 | | x: Math.round(this.value(null,"x")), |
| 29 | | y: Math.round(this.value(null,"y")) |
| | 27 | value: this.options.axis != "both" || !this.options.axis ? this.round(this.value(null,this.options.axis == "vertical" ? "y" : "x")) : { |
| | 28 | x: this.round(this.value(null,"x")), |
| | 29 | y: this.round(this.value(null,"y")) |
| 30 | 30 | }, |
| 31 | 31 | range: this._getRange() |
| 32 | 32 | }; |
| … |
… |
$.widget("ui.slider", { |
| 137 | 137 | |
| 138 | 138 | var element = this.element[0], o = this.options; |
| 139 | 139 | this.actualSize = { width: this.element.outerWidth() , height: this.element.outerHeight() }; |
| 140 | | |
| | 140 | |
| | 141 | //Prepare precision for math functions |
| | 142 | this.precision = o.precision && Math.pow(10, parseInt(precision)) || 0; |
| | 143 | |
| 141 | 144 | $.extend(o, { |
| 142 | 145 | axis: o.axis || (element.offsetWidth < element.offsetHeight ? 'vertical' : 'horizontal'), |
| 143 | | max: !isNaN(parseInt(o.max,10)) ? { x: parseInt(o.max, 10), y: parseInt(o.max, 10) } : ({ x: o.max && o.max.x || 100, y: o.max && o.max.y || 100 }), |
| 144 | | min: !isNaN(parseInt(o.min,10)) ? { x: parseInt(o.min, 10), y: parseInt(o.min, 10) } : ({ x: o.min && o.min.x || 0, y: o.min && o.min.y || 0 }) |
| | 146 | max: !isNaN(this.parse(o.max)) ? { x: this.parse(o.max), y: this.parse(o.max) } : ({ x: o.max && o.max.x || 100, y: o.max && o.max.y || 100 }), |
| | 147 | min: !isNaN(this.parse(o.min)) ? { x: this.parse(o.min), y: this.parse(o.min) } : ({ x: o.min && o.min.x || 0, y: o.min && o.min.y || 0 }) |
| 145 | 148 | }); |
| 146 | 149 | //Prepare the real maxValue |
| 147 | 150 | o.realMax = { |
| … |
… |
$.widget("ui.slider", { |
| 150 | 153 | }; |
| 151 | 154 | //Calculate stepping based on steps |
| 152 | 155 | o.stepping = { |
| 153 | | x: o.stepping && o.stepping.x || parseInt(o.stepping, 10) || (o.steps ? o.realMax.x/(o.steps.x || parseInt(o.steps, 10) || o.realMax.x) : 0), |
| 154 | | y: o.stepping && o.stepping.y || parseInt(o.stepping, 10) || (o.steps ? o.realMax.y/(o.steps.y || parseInt(o.steps, 10) || o.realMax.y) : 0) |
| | 156 | x: o.stepping && o.stepping.x || this.parse(o.stepping) || (o.steps ? o.realMax.x/(o.steps.x || this.parse(o.steps) || o.realMax.x) : 0), |
| | 157 | y: o.stepping && o.stepping.y || this.parse(o.stepping) || (o.steps ? o.realMax.y/(o.steps.y || this.parse(o.steps) || o.realMax.y) : 0) |
| 155 | 158 | }; |
| 156 | 159 | }, |
| 157 | 160 | |
| … |
… |
$.widget("ui.slider", { |
| 230 | 233 | _updateRange: function() { |
| 231 | 234 | var prop = this.options.axis == "vertical" ? "top" : "left"; |
| 232 | 235 | var size = this.options.axis == "vertical" ? "height" : "width"; |
| 233 | | this.rangeElement.css(prop, (parseInt($(this.handle[0]).css(prop),10) || 0) + this._handleSize(0, this.options.axis == "vertical" ? "y" : "x")/2); |
| 234 | | this.rangeElement.css(size, (parseInt($(this.handle[1]).css(prop),10) || 0) - (parseInt($(this.handle[0]).css(prop),10) || 0)); |
| | 236 | this.rangeElement.css(prop, (this.parse($(this.handle[0]).css(prop)) || 0) + this._handleSize(0, this.options.axis == "vertical" ? "y" : "x")/2); |
| | 237 | this.rangeElement.css(size, (this.parse($(this.handle[1]).css(prop)) || 0) - (this.parse($(this.handle[0]).css(prop)) || 0)); |
| 235 | 238 | }, |
| 236 | 239 | _getRange: function() { |
| 237 | | return this.rangeElement ? this._convertValue(parseInt(this.rangeElement.css(this.options.axis == "vertical" ? "height" : "width"),10), this.options.axis == "vertical" ? "y" : "x") : null; |
| | 240 | return this.rangeElement ? this._convertValue(this.parse(this.rangeElement.css(this.options.axis == "vertical" ? "height" : "width")), this.options.axis == "vertical" ? "y" : "x") : null; |
| 238 | 241 | }, |
| 239 | 242 | |
| 240 | 243 | _handleIndex: function() { |
| … |
… |
$.widget("ui.slider", { |
| 247 | 250 | var curHandle = $(handle != undefined && handle !== null ? this.handle[handle] || handle : this.currentHandle); |
| 248 | 251 | |
| 249 | 252 | if(curHandle.data("mouse").sliderValue) { |
| 250 | | return parseInt(curHandle.data("mouse").sliderValue[axis],10); |
| | 253 | return this.parse(curHandle.data("mouse").sliderValue[axis]); |
| 251 | 254 | } else { |
| 252 | | return parseInt(((parseInt(curHandle.css(axis == "x" ? "left" : "top"),10) / (this.actualSize[axis == "x" ? "width" : "height"] - this._handleSize(handle,axis))) * this.options.realMax[axis]) + this.options.min[axis],10); |
| | 255 | return this.parse(((this.parse(curHandle.css(axis == "x" ? "left" : "top")) / (this.actualSize[axis == "x" ? "width" : "height"] - this._handleSize(handle,axis))) * this.options.realMax[axis]) + this.options.min[axis]); |
| 253 | 256 | } |
| 254 | 257 | |
| 255 | 258 | }, |
| … |
… |
$.widget("ui.slider", { |
| 338 | 341 | |
| 339 | 342 | if (o.stepping.x) { |
| 340 | 343 | var value = this._convertValue(position.left, "x"); |
| 341 | | value = Math.round(value / o.stepping.x) * o.stepping.x; |
| | 344 | value = this.round(value / o.stepping.x) * o.stepping.x; |
| 342 | 345 | position.left = this._translateValue(value, "x"); |
| 343 | 346 | } |
| 344 | 347 | if (o.stepping.y) { |
| 345 | 348 | var value = this._convertValue(position.top, "y"); |
| 346 | | value = Math.round(value / o.stepping.y) * o.stepping.y; |
| | 349 | value = this.round(value / o.stepping.y) * o.stepping.y; |
| 347 | 350 | position.top = this._translateValue(value, "y"); |
| 348 | 351 | } |
| 349 | 352 | |
| … |
… |
$.widget("ui.slider", { |
| 355 | 358 | |
| 356 | 359 | //Store the slider's value |
| 357 | 360 | this.currentHandle.data("mouse").sliderValue = { |
| 358 | | x: Math.round(this._convertValue(position.left, "x")) || 0, |
| 359 | | y: Math.round(this._convertValue(position.top, "y")) || 0 |
| | 361 | x: this.round(this._convertValue(position.left, "x")) || 0, |
| | 362 | y: this.round(this._convertValue(position.top, "y")) || 0 |
| 360 | 363 | }; |
| 361 | 364 | |
| 362 | 365 | if (this.rangeElement) |
| … |
… |
$.widget("ui.slider", { |
| 393 | 396 | if(x !== undefined && x.constructor != Number) { |
| 394 | 397 | var me = /^\-\=/.test(x), pe = /^\+\=/.test(x); |
| 395 | 398 | if(me || pe) { |
| 396 | | x = this.value(null, "x") + parseInt(x.replace(me ? '=' : '+=', ''), 10); |
| | 399 | x = this.value(null, "x") + this.parse(x.replace(me ? '=' : '+=', '')); |
| 397 | 400 | } else { |
| 398 | | x = isNaN(parseInt(x, 10)) ? undefined : parseInt(x, 10); |
| | 401 | x = isNaN(this.parse(x)) ? undefined : this.parse(x); |
| 399 | 402 | } |
| 400 | 403 | } |
| 401 | 404 | |
| 402 | 405 | if(y !== undefined && y.constructor != Number) { |
| 403 | 406 | var me = /^\-\=/.test(y), pe = /^\+\=/.test(y); |
| 404 | 407 | if(me || pe) { |
| 405 | | y = this.value(null, "y") + parseInt(y.replace(me ? '=' : '+=', ''), 10); |
| | 408 | y = this.value(null, "y") + this.parse(y.replace(me ? '=' : '+=', '')); |
| 406 | 409 | } else { |
| 407 | | y = isNaN(parseInt(y, 10)) ? undefined : parseInt(y, 10); |
| | 410 | y = isNaN(this.parse(y)) ? undefined : this.parse(y); |
| 408 | 411 | } |
| 409 | 412 | } |
| 410 | 413 | |
| 411 | 414 | if(o.axis != "vertical" && x !== undefined) { |
| 412 | | if(o.stepping.x) x = Math.round(x / o.stepping.x) * o.stepping.x; |
| | 415 | if(o.stepping.x) x = this.round(x / o.stepping.x) * o.stepping.x; |
| 413 | 416 | x = this._translateValue(x, "x"); |
| 414 | 417 | x = this._translateLimits(x, "x"); |
| 415 | 418 | x = this._translateRange(x, "x"); |
| 416 | 419 | |
| 417 | | o.animate ? this.currentHandle.stop().animate({ left: x }, (Math.abs(parseInt(this.currentHandle.css("left")) - x)) * (!isNaN(parseInt(o.animate)) ? o.animate : 5)) : this.currentHandle.css({ left: x }); |
| | 420 | o.animate ? this.currentHandle.stop().animate({ left: x }, (Math.abs(this.parse(this.currentHandle.css("left")) - x)) * (!isNaN(this.parse(o.animate)) ? o.animate : 5)) : this.currentHandle.css({ left: x }); |
| 418 | 421 | } |
| 419 | 422 | |
| 420 | 423 | if(o.axis != "horizontal" && y !== undefined) { |
| 421 | | if(o.stepping.y) y = Math.round(y / o.stepping.y) * o.stepping.y; |
| | 424 | if(o.stepping.y) y = this.round(y / o.stepping.y) * o.stepping.y; |
| 422 | 425 | y = this._translateValue(y, "y"); |
| 423 | 426 | y = this._translateLimits(y, "y"); |
| 424 | 427 | y = this._translateRange(y, "y"); |
| 425 | | o.animate ? this.currentHandle.stop().animate({ top: y }, (Math.abs(parseInt(this.currentHandle.css("top")) - y)) * (!isNaN(parseInt(o.animate)) ? o.animate : 5)) : this.currentHandle.css({ top: y }); |
| | 428 | o.animate ? this.currentHandle.stop().animate({ top: y }, (Math.abs(this.parse(this.currentHandle.css("top")) - y)) * (!isNaN(this.parse(o.animate)) ? o.animate : 5)) : this.currentHandle.css({ top: y }); |
| 426 | 429 | } |
| 427 | 430 | |
| 428 | 431 | if (this.rangeElement) |
| … |
… |
$.widget("ui.slider", { |
| 430 | 433 | |
| 431 | 434 | //Store the slider's value |
| 432 | 435 | this.currentHandle.data("mouse").sliderValue = { |
| 433 | | x: Math.round(this._convertValue(x, "x")) || 0, |
| 434 | | y: Math.round(this._convertValue(y, "y")) || 0 |
| | 436 | x: this.round(this._convertValue(x, "x")) || 0, |
| | 437 | y: this.round(this._convertValue(y, "y")) || 0 |
| 435 | 438 | }; |
| 436 | 439 | |
| 437 | 440 | if (!noPropagation) { |
| … |
… |
$.widget("ui.slider", { |
| 440 | 443 | this._propagate('change', null); |
| 441 | 444 | this._propagate("slide", null); |
| 442 | 445 | } |
| | 446 | }, |
| | 447 | round: function(value) { |
| | 448 | var precision = this.precision; |
| | 449 | return precision ? Math.round(value * precision) / precision : Math.round(value); |
| | 450 | }, |
| | 451 | parse: function(value) { |
| | 452 | if (this.precision == 0) |
| | 453 | return parseInt(value); |
| | 454 | return parseFloat(value); |
| 443 | 455 | } |
| 444 | 456 | }); |
| 445 | 457 | |