Skip to main content

Search and Top Navigation

Ticket #3282: 0001-slider-support-floating-values-set-number-of-decim.patch
File 0001-slider-support-floating-values-set-number-of-decim.patch, 8.9 KB (added by herve, September 03, 2008 07:49PM UTC)

The patch in git format

From c498ce58484f4acee3ddc5d991a68289daea1f6c Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Herv=C3=A9=20Cauwelier?= 
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/ui/ui.slider.js
+++ b/ui/ui.slider.js
@@ -24,9 +24,9 @@ $.widget("ui.slider", {
 		return {
 			options: this.options,
 			handle: this.currentHandle,
-			value: this.options.axis != "both" || !this.options.axis ? Math.round(this.value(null,this.options.axis == "vertical" ? "y" : "x")) : {
-				x: Math.round(this.value(null,"x")),
-				y: Math.round(this.value(null,"y"))
+			value: this.options.axis != "both" || !this.options.axis ? this.round(this.value(null,this.options.axis == "vertical" ? "y" : "x")) : {
+				x: this.round(this.value(null,"x")),
+				y: this.round(this.value(null,"y"))
 			},
 			range: this._getRange()
 		};
@@ -137,11 +137,14 @@ $.widget("ui.slider", {
 		
 		var element = this.element[0], o = this.options;
 		this.actualSize = { width: this.element.outerWidth() , height: this.element.outerHeight() };			
-		
+
+		//Prepare precision for math functions
+		this.precision = o.precision && Math.pow(10, parseInt(precision)) || 0;
+
 		$.extend(o, {
 			axis: o.axis || (element.offsetWidth < element.offsetHeight ? 'vertical' : 'horizontal'),
-			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 }),
-			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 })
+			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 }),
+			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 })
 		});
 		//Prepare the real maxValue
 		o.realMax = {
@@ -150,8 +153,8 @@ $.widget("ui.slider", {
 		};
 		//Calculate stepping based on steps
 		o.stepping = {
-			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),
-			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)
+			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),
+			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)
 		};
 	},
 
@@ -230,11 +233,11 @@ $.widget("ui.slider", {
 	_updateRange: function() {
 		var prop = this.options.axis == "vertical" ? "top" : "left";
 		var size = this.options.axis == "vertical" ? "height" : "width";
-		this.rangeElement.css(prop, (parseInt($(this.handle[0]).css(prop),10) || 0) + this._handleSize(0, this.options.axis == "vertical" ? "y" : "x")/2);
-		this.rangeElement.css(size, (parseInt($(this.handle[1]).css(prop),10) || 0) - (parseInt($(this.handle[0]).css(prop),10) || 0));
+		this.rangeElement.css(prop, (this.parse($(this.handle[0]).css(prop)) || 0) + this._handleSize(0, this.options.axis == "vertical" ? "y" : "x")/2);
+		this.rangeElement.css(size, (this.parse($(this.handle[1]).css(prop)) || 0) - (this.parse($(this.handle[0]).css(prop)) || 0));
 	},
 	_getRange: function() {
-		return this.rangeElement ? this._convertValue(parseInt(this.rangeElement.css(this.options.axis == "vertical" ? "height" : "width"),10), this.options.axis == "vertical" ? "y" : "x") : null;
+		return this.rangeElement ? this._convertValue(this.parse(this.rangeElement.css(this.options.axis == "vertical" ? "height" : "width")), this.options.axis == "vertical" ? "y" : "x") : null;
 	},
 
 	_handleIndex: function() {
@@ -247,9 +250,9 @@ $.widget("ui.slider", {
 		var curHandle = $(handle != undefined && handle !== null ? this.handle[handle] || handle : this.currentHandle);
 		
 		if(curHandle.data("mouse").sliderValue) {
-			return parseInt(curHandle.data("mouse").sliderValue[axis],10);
+			return this.parse(curHandle.data("mouse").sliderValue[axis]);
 		} else {
-			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);
+			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]);
 		}
 
 	},
@@ -338,12 +341,12 @@ $.widget("ui.slider", {
 		
 		if (o.stepping.x) {
 			var value = this._convertValue(position.left, "x");
-			value = Math.round(value / o.stepping.x) * o.stepping.x;
+			value = this.round(value / o.stepping.x) * o.stepping.x;
 			position.left = this._translateValue(value, "x");	
 		}
 		if (o.stepping.y) {
 			var value = this._convertValue(position.top, "y");
-			value = Math.round(value / o.stepping.y) * o.stepping.y;
+			value = this.round(value / o.stepping.y) * o.stepping.y;
 			position.top = this._translateValue(value, "y");	
 		}
 		
@@ -355,8 +358,8 @@ $.widget("ui.slider", {
 		
 		//Store the slider's value
 		this.currentHandle.data("mouse").sliderValue = {
-			x: Math.round(this._convertValue(position.left, "x")) || 0,
-			y: Math.round(this._convertValue(position.top, "y")) || 0
+			x: this.round(this._convertValue(position.left, "x")) || 0,
+			y: this.round(this._convertValue(position.top, "y")) || 0
 		};
 		
 		if (this.rangeElement)
@@ -393,36 +396,36 @@ $.widget("ui.slider", {
 		if(x !== undefined && x.constructor != Number) {
 			var me = /^\-\=/.test(x), pe = /^\+\=/.test(x);
 			if(me || pe) {
-				x = this.value(null, "x") + parseInt(x.replace(me ? '=' : '+=', ''), 10);
+				x = this.value(null, "x") + this.parse(x.replace(me ? '=' : '+=', ''));
 			} else {
-				x = isNaN(parseInt(x, 10)) ? undefined : parseInt(x, 10);
+				x = isNaN(this.parse(x)) ? undefined : this.parse(x);
 			}
 		}
 		
 		if(y !== undefined && y.constructor != Number) {
 			var me = /^\-\=/.test(y), pe = /^\+\=/.test(y);
 			if(me || pe) {
-				y = this.value(null, "y") + parseInt(y.replace(me ? '=' : '+=', ''), 10);
+				y = this.value(null, "y") + this.parse(y.replace(me ? '=' : '+=', ''));
 			} else {
-				y = isNaN(parseInt(y, 10)) ? undefined : parseInt(y, 10);
+				y = isNaN(this.parse(y)) ? undefined : this.parse(y);
 			}
 		}
 
 		if(o.axis != "vertical" && x !== undefined) {
-			if(o.stepping.x) x = Math.round(x / o.stepping.x) * o.stepping.x;
+			if(o.stepping.x) x = this.round(x / o.stepping.x) * o.stepping.x;
 			x = this._translateValue(x, "x");
 			x = this._translateLimits(x, "x");
 			x = this._translateRange(x, "x");
 
-			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 });
+			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 });
 		}
 
 		if(o.axis != "horizontal" && y !== undefined) {
-			if(o.stepping.y) y = Math.round(y / o.stepping.y) * o.stepping.y;
+			if(o.stepping.y) y = this.round(y / o.stepping.y) * o.stepping.y;
 			y = this._translateValue(y, "y");
 			y = this._translateLimits(y, "y");
 			y = this._translateRange(y, "y");
-			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 });
+			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 });
 		}
 		
 		if (this.rangeElement)
@@ -430,8 +433,8 @@ $.widget("ui.slider", {
 			
 		//Store the slider's value
 		this.currentHandle.data("mouse").sliderValue = {
-			x: Math.round(this._convertValue(x, "x")) || 0,
-			y: Math.round(this._convertValue(y, "y")) || 0
+			x: this.round(this._convertValue(x, "x")) || 0,
+			y: this.round(this._convertValue(y, "y")) || 0
 		};
 	
 		if (!noPropagation) {
@@ -440,6 +443,15 @@ $.widget("ui.slider", {
 			this._propagate('change', null);
 			this._propagate("slide", null);
 		}
+	},
+	round: function(value) {
+		var precision = this.precision;
+		return precision ? Math.round(value * precision) / precision : Math.round(value);
+	},
+	parse: function(value) {
+		if (this.precision == 0)
+			return parseInt(value);
+		return parseFloat(value);
 	}
 });
 
-- 
1.6.0.1

Download in other formats:

Original Format