Bug Tracker

Ticket #3317: 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, 5 years ago)

The patch in git format

  • ui/ui.slider.js

    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", { 
    2424                return { 
    2525                        options: this.options, 
    2626                        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")) 
    3030                        }, 
    3131                        range: this._getRange() 
    3232                }; 
    $.widget("ui.slider", { 
    137137                 
    138138                var element = this.element[0], o = this.options; 
    139139                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 
    141144                $.extend(o, { 
    142145                        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 }) 
    145148                }); 
    146149                //Prepare the real maxValue 
    147150                o.realMax = { 
    $.widget("ui.slider", { 
    150153                }; 
    151154                //Calculate stepping based on steps 
    152155                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) 
    155158                }; 
    156159        }, 
    157160 
    $.widget("ui.slider", { 
    230233        _updateRange: function() { 
    231234                var prop = this.options.axis == "vertical" ? "top" : "left"; 
    232235                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)); 
    235238        }, 
    236239        _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; 
    238241        }, 
    239242 
    240243        _handleIndex: function() { 
    $.widget("ui.slider", { 
    247250                var curHandle = $(handle != undefined && handle !== null ? this.handle[handle] || handle : this.currentHandle); 
    248251                 
    249252                if(curHandle.data("mouse").sliderValue) { 
    250                         return parseInt(curHandle.data("mouse").sliderValue[axis],10); 
     253                        return this.parse(curHandle.data("mouse").sliderValue[axis]); 
    251254                } 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]); 
    253256                } 
    254257 
    255258        }, 
    $.widget("ui.slider", { 
    338341                 
    339342                if (o.stepping.x) { 
    340343                        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; 
    342345                        position.left = this._translateValue(value, "x");        
    343346                } 
    344347                if (o.stepping.y) { 
    345348                        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; 
    347350                        position.top = this._translateValue(value, "y");         
    348351                } 
    349352                 
    $.widget("ui.slider", { 
    355358                 
    356359                //Store the slider's value 
    357360                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 
    360363                }; 
    361364                 
    362365                if (this.rangeElement) 
    $.widget("ui.slider", { 
    393396                if(x !== undefined && x.constructor != Number) { 
    394397                        var me = /^\-\=/.test(x), pe = /^\+\=/.test(x); 
    395398                        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 ? '=' : '+=', '')); 
    397400                        } else { 
    398                                 x = isNaN(parseInt(x, 10)) ? undefined : parseInt(x, 10); 
     401                                x = isNaN(this.parse(x)) ? undefined : this.parse(x); 
    399402                        } 
    400403                } 
    401404                 
    402405                if(y !== undefined && y.constructor != Number) { 
    403406                        var me = /^\-\=/.test(y), pe = /^\+\=/.test(y); 
    404407                        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 ? '=' : '+=', '')); 
    406409                        } else { 
    407                                 y = isNaN(parseInt(y, 10)) ? undefined : parseInt(y, 10); 
     410                                y = isNaN(this.parse(y)) ? undefined : this.parse(y); 
    408411                        } 
    409412                } 
    410413 
    411414                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; 
    413416                        x = this._translateValue(x, "x"); 
    414417                        x = this._translateLimits(x, "x"); 
    415418                        x = this._translateRange(x, "x"); 
    416419 
    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 }); 
    418421                } 
    419422 
    420423                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; 
    422425                        y = this._translateValue(y, "y"); 
    423426                        y = this._translateLimits(y, "y"); 
    424427                        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 }); 
    426429                } 
    427430                 
    428431                if (this.rangeElement) 
    $.widget("ui.slider", { 
    430433                         
    431434                //Store the slider's value 
    432435                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 
    435438                }; 
    436439         
    437440                if (!noPropagation) { 
    $.widget("ui.slider", { 
    440443                        this._propagate('change', null); 
    441444                        this._propagate("slide", null); 
    442445                } 
     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); 
    443455        } 
    444456}); 
    445457