jQuery: The Write Less, Do More JavaScript Library

Changeset 5538

Show
Ignore:
Timestamp:
05/09/08 15:37:34 (3 days ago)
Author:
paul.bakaus
Message:

ui-slider: changed file structure, cleaned up code

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/ui/ui.slider.js

    r5525 r5538  
    2222 
    2323    $.widget("ui.slider", { 
     24        plugins: {}, 
     25        ui: function(e) { 
     26            return { 
     27                instance: this, 
     28                options: this.options, 
     29                handle: this.currentHandle, 
     30                value: this.options.axis != "both" || !this.options.axis ? Math.round(this.value(null,this.options.axis == "vertical" ? "y" : "x")) : { 
     31                    x: Math.round(this.value(null,"x")), 
     32                    y: Math.round(this.value(null,"y")) 
     33                }, 
     34                range: this.getRange() 
     35            }; 
     36        }, 
     37        propagate: function(n,e) { 
     38            $.ui.plugin.call(this, n, [e, this.ui()]); 
     39            this.element.triggerHandler(n == "slide" ? n : "slide"+n, [e, this.ui()], this.options[n]); 
     40        }, 
     41        destroy: function() { 
     42            this.element 
     43                .removeClass("ui-slider ui-slider-disabled") 
     44                .removeData("slider") 
     45                .unbind(".slider"); 
     46            this.handle 
     47                .unwrap("a") 
     48                .mouse("destroy"); 
     49            this.generated && this.generated.remove(); 
     50        }, 
     51        enable: function() { 
     52            this.element.removeClass("ui-slider-disabled"); 
     53            this.disabled = false; 
     54        }, 
     55        disable: function() { 
     56            this.element.addClass("ui-slider-disabled"); 
     57            this.disabled = true; 
     58        }, 
     59        setData: function(key, value) { 
     60            this.options[key] = value; 
     61            if (/min|max|steps/.test(key)) { 
     62                this.initBoundaries(); 
     63            } 
     64        }, 
     65 
    2466        init: function() { 
     67             
    2568            var self = this; 
    2669            this.element.addClass("ui-slider"); 
     
    3780                }); 
    3881            } 
     82             
    3983            $(this.handle) 
    4084                .mouse({ 
     
    58102                    .bind('focus', function(e) { self.focus(this.firstChild); }) 
    59103                    .bind('blur', function(e) { self.blur(this.firstChild); }) 
    60                     .bind('keydown', function(e) { 
    61                         self.keydown(e.keyCode, this.firstChild) 
    62                     }) 
     104                    .bind('keydown', function(e) { self.keydown(e.keyCode, this.firstChild); }) 
    63105            ; 
    64106             
     
    80122            if(this.handle.length == 2 && this.options.range) this.createRange(); 
    81123        }, 
    82          
    83         keydown: function(keyCode, handle) { 
    84             if(/(37|38|39|40)/.test(keyCode)) { 
    85                 this.moveTo({ 
    86                     x: /(37|39)/.test(keyCode) ? (keyCode == 37 ? '-' : '+') + '=' + this.oneStep(1) : 0, 
    87                     y: /(38|40)/.test(keyCode) ? (keyCode == 38 ? '-' : '+') + '=' + this.oneStep(2) : 0 
    88                 }, handle); 
    89             } 
    90         }, 
    91          
    92         setData: function(key, value) { 
    93             this.options[key] = value; 
    94             if (/min|max|steps/.test(key)) { 
    95                 this.initBoundaries(); 
    96             } 
    97         }, 
    98          
    99124        initBoundaries: function() { 
    100             var element = this.element[0]; 
    101             var o = this.options; 
     125             
     126            var element = this.element[0], o = this.options; 
     127            this.actualSize = { width: this.element.outerWidth() , height: this.element.outerHeight() };             
     128             
    102129            $.extend(o, { 
    103130                axis: o.axis || (element.offsetWidth < element.offsetHeight ? 'vertical' : 'horizontal'), 
     
    116143            }; 
    117144        }, 
    118         plugins: {}, 
    119         createRange: function() { 
    120             this.rangeElement = $('<div></div>') 
    121                 .addClass('ui-slider-range') 
    122                 .css({ position: 'absolute' }) 
    123                 .appendTo(this.element); 
    124             this.updateRange(); 
    125         }, 
    126         updateRange: function() { 
    127                 var prop = this.options.axis == "vertical" ? "top" : "left"; 
    128                 var size = this.options.axis == "vertical" ? "height" : "width"; 
    129                 this.rangeElement.css(prop, parseInt($(this.handle[0]).css(prop),10) + this.handleSize(0, this.options.axis == "vertical" ? 2 : 1)/2); 
    130                 this.rangeElement.css(size, parseInt($(this.handle[1]).css(prop),10) - parseInt($(this.handle[0]).css(prop),10)); 
    131         }, 
    132         getRange: function() { 
    133             return this.rangeElement ? this.convertValue(parseInt(this.rangeElement.css(this.options.axis == "vertical" ? "height" : "width"),10)) : null; 
    134         }, 
    135         ui: function(e) { 
    136             return { 
    137                 instance: this, 
    138                 options: this.options, 
    139                 handle: this.currentHandle, 
    140                 value: this.options.axis != "both" || !this.options.axis ? Math.round(this.value(null,this.options.axis == "vertical" ? 2 : 1)) : { 
    141                     x: Math.round(this.value(null,1)), 
    142                     y: Math.round(this.value(null,2)) 
    143                 }, 
    144                 range: this.getRange() 
    145             }; 
    146         }, 
    147         propagate: function(n,e) { 
    148             $.ui.plugin.call(this, n, [e, this.ui()]); 
    149             this.element.triggerHandler(n == "slide" ? n : "slide"+n, [e, this.ui()], this.options[n]); 
    150         }, 
    151         destroy: function() { 
    152             this.element 
    153                 .removeClass("ui-slider ui-slider-disabled") 
    154                 .removeData("slider") 
    155                 .unbind(".slider"); 
    156             this.handle 
    157                 .unwrap("a") 
    158                 .mouse("destroy"); 
    159             this.generated && this.generated.remove(); 
    160         }, 
    161         enable: function() { 
    162             this.element.removeClass("ui-slider-disabled"); 
    163             this.disabled = false; 
    164         }, 
    165         disable: function() { 
    166             this.element.addClass("ui-slider-disabled"); 
    167             this.disabled = true; 
     145 
     146         
     147        keydown: function(keyCode, handle) { 
     148            if(/(37|38|39|40)/.test(keyCode)) { 
     149                this.moveTo({ 
     150                    x: /(37|39)/.test(keyCode) ? (keyCode == 37 ? '-' : '+') + '=' + this.oneStep("x") : 0, 
     151                    y: /(38|40)/.test(keyCode) ? (keyCode == 38 ? '-' : '+') + '=' + this.oneStep("y") : 0 
     152                }, handle); 
     153            } 
    168154        }, 
    169155        focus: function(handle,hard) { 
     
    175161            $(handle).removeClass('ui-slider-handle-active'); 
    176162            if(this.currentHandle && this.currentHandle[0] == handle) { this.previousHandle = this.currentHandle; this.currentHandle = null; }; 
    177         }, 
    178         value: function(handle, axis) { 
    179             if(this.handle.length == 1) this.currentHandle = this.handle; 
    180             if(!axis) axis = this.options.axis == "vertical" ? 2 : 1; 
    181              
    182             var value = ((parseInt($(handle != undefined && handle !== null ? this.handle[handle] || handle : this.currentHandle).css(axis == 1 ? "left" : "top"),10) / (this.actualSize[axis == 1 ? "width" : "height"] - this.handleSize(handle,axis))) * this.options.realMax[axis == 1 ? "x" : "y"]) + this.options.min[axis == 1 ? "x" : "y"]; 
    183              
    184             var o = this.options; 
    185             if (o.stepping[axis == 1 ? "x" : "y"]) { 
    186                 value = Math.round(value / o.stepping[axis == 1 ? "x" : "y"]) * o.stepping[axis == 1 ? "x" : "y"]; 
    187             } 
    188             return value; 
    189         }, 
    190         convertValue: function(value,axis) { 
    191             if(!axis) axis = this.options.axis == "vertical" ? 2 : 1; 
    192             return this.options.min[axis == 1 ? "x" : "y"] + (value / (this.actualSize[axis == 1 ? "width" : "height"] - this.handleSize(null,axis))) * this.options.realMax[axis == 1 ? "x" : "y"]; 
    193         }, 
    194         translateValue: function(value,axis) { 
    195             if(!axis) axis = this.options.axis == "vertical" ? 2 : 1; 
    196             return ((value - this.options.min[axis == 1 ? "x" : "y"]) / this.options.realMax[axis == 1 ? "x" : "y"]) * (this.actualSize[axis == 1 ? "width" : "height"] - this.handleSize(null,axis)); 
    197         }, 
    198         handleSize: function(handle,axis) { 
    199             if(!axis) axis = this.options.axis == "vertical" ? 2 : 1; 
    200             return $(handle != undefined && handle !== null ? this.handle[handle] : this.currentHandle)[0][axis == 1 ? "offsetWidth" : "offsetHeight"];  
    201163        }, 
    202164        click: function(e) { 
     
    220182                this.focus(this.previousHandle, true); 
    221183             
    222             // Move focussed handle to the clicked position 
     184            // propagate only for distance > 0, otherwise propagation is done my drag 
    223185            this.offset = this.element.offset(); 
    224              
    225             // propagate only for distance > 0, otherwise propagation is done my drag 
    226186            this.moveTo({ 
    227                 y: this.convertValue(e.pageY - this.offset.top - this.currentHandle.outerHeight()/2), 
    228                 x: this.convertValue(e.pageX - this.offset.left - this.currentHandle.outerWidth()/2) 
     187                y: this.convertValue(e.pageY - this.offset.top - this.currentHandle[0].offsetHeight/2, "y"), 
     188                x: this.convertValue(e.pageX - this.offset.left - this.currentHandle[0].offsetWidth/2, "x") 
    229189            }, null, !this.options.distance); 
    230190        }, 
     191         
     192 
     193     
     194        createRange: function() { 
     195            this.rangeElement = $('<div></div>') 
     196                .addClass('ui-slider-range') 
     197                .css({ position: 'absolute' }) 
     198                .appendTo(this.element); 
     199            this.updateRange(); 
     200        }, 
     201        updateRange: function() { 
     202                var prop = this.options.axis == "vertical" ? "top" : "left"; 
     203                var size = this.options.axis == "vertical" ? "height" : "width"; 
     204                this.rangeElement.css(prop, parseInt($(this.handle[0]).css(prop),10) + this.handleSize(0, this.options.axis == "vertical" ? "y" : "x")/2); 
     205                this.rangeElement.css(size, parseInt($(this.handle[1]).css(prop),10) - parseInt($(this.handle[0]).css(prop),10)); 
     206        }, 
     207        getRange: function() { 
     208            return this.rangeElement ? this.convertValue(parseInt(this.rangeElement.css(this.options.axis == "vertical" ? "height" : "width"),10)) : null; 
     209        }, 
     210 
     211        handleIndex: function() { 
     212            return this.handle.index(this.currentHandle[0]); 
     213        }, 
     214        value: function(handle, axis) { 
     215            if(this.handle.length == 1) this.currentHandle = this.handle; 
     216            if(!axis) axis = this.options.axis == "vertical" ? "y" : "x"; 
     217             
     218            var value = ((parseInt($(handle != undefined && handle !== null ? this.handle[handle] || handle : this.currentHandle).css(axis == "x" ? "left" : "top"),10) / (this.actualSize[axis == "x" ? "width" : "height"] - this.handleSize(handle,axis))) * this.options.realMax[axis]) + this.options.min[axis]; 
     219             
     220            var o = this.options; 
     221            if (o.stepping[axis]) { 
     222                value = Math.round(value / o.stepping[axis]) * o.stepping[axis]; 
     223            } 
     224            return value; 
     225        }, 
     226        convertValue: function(value,axis) { 
     227            return this.options.min[axis] + (value / (this.actualSize[axis == "x" ? "width" : "height"] - this.handleSize(null,axis))) * this.options.realMax[axis]; 
     228        }, 
     229         
     230        translateValue: function(value,axis) { 
     231            return ((value - this.options.min[axis]) / this.options.realMax[axis]) * (this.actualSize[axis == "x" ? "width" : "height"] - this.handleSize(null,axis)); 
     232        }, 
     233        translateRange: function(value,axis) { 
     234            if (this.rangeElement) { 
     235                if (this.currentHandle[0] == this.handle[0] && value >= this.translateValue(this.value(1),axis)) 
     236                    value = this.translateValue(this.value(1,axis) - this.oneStep(axis), axis); 
     237                if (this.currentHandle[0] == this.handle[1] && value <= this.translateValue(this.value(0),axis)) 
     238                    value = this.translateValue(this.value(0,axis) + this.oneStep(axis)); 
     239            } 
     240            if (this.options.handles) { 
     241                var handle = this.options.handles[this.handleIndex()]; 
     242                if (value < this.translateValue(handle.min,axis)) { 
     243                    value = this.translateValue(handle.min,axis); 
     244                } else if (value > this.translateValue(handle.max,axis)) { 
     245                    value = this.translateValue(handle.max,axis); 
     246                } 
     247            } 
     248            return value; 
     249        }, 
     250        translateLimits: function(value,axis) { 
     251            if (value >= this.actualSize[axis == "x" ? "width" : "height"] - this.handleSize(null,axis)) 
     252                value = this.actualSize[axis == "x" ? "width" : "height"] - this.handleSize(null,axis); 
     253            if (value <= 0) 
     254                value = 0; 
     255            return value; 
     256        }, 
     257        handleSize: function(handle,axis) { 
     258            return $(handle != undefined && handle !== null ? this.handle[handle] : this.currentHandle)[0]["offset"+(axis == "x" ? "Width" : "Height")];     
     259        }, 
     260        oneStep: function(axis) { 
     261            return this.options.stepping[axis] || 1; 
     262        }, 
     263 
     264 
     265 
     266 
     267 
     268 
    231269        start: function(e, handle) { 
    232270         
     
    257295            return false; 
    258296        }, 
    259          
    260         oneStep: function(axis) { 
    261             if (!axis) 
    262                 axis = this.options.axis == "vertical" ? 2 : 1; 
    263             return this.options.stepping[axis == 1 ? "x" : "y"] 
    264                 ? this.options.stepping[axis == 1 ? "x" : "y"] 
    265                 : (this.options.realMax[axis == 1 ? "x" : "y"] / this.actualSize[axis == 1 ? "width" : "height"]) * 5; 
    266         }, 
    267          
    268         translateRange: function(value,axis) { 
    269             if (this.rangeElement) { 
    270                 if (this.currentHandle[0] == this.handle[0] && value >= this.translateValue(this.value(1),axis)) 
    271                     value = this.translateValue(this.value(1,axis) - this.oneStep(axis), axis); 
    272                 if (this.currentHandle[0] == this.handle[1] && value <= this.translateValue(this.value(0),axis)) 
    273                     value = this.translateValue(this.value(0,axis) + this.oneStep(axis)); 
    274             } 
    275             if (this.options.handles) { 
    276                 var handle = this.options.handles[this.handleIndex()]; 
    277                 if (value < this.translateValue(handle.min,axis)) { 
    278                     value = this.translateValue(handle.min,axis); 
    279                 } else if (value > this.translateValue(handle.max,axis)) { 
    280                     value = this.translateValue(handle.max,axis); 
    281                 } 
    282             } 
    283             return value; 
    284         }, 
    285          
    286         handleIndex: function() { 
    287             return this.handle.index(this.currentHandle[0]); 
    288         }, 
    289          
    290         translateLimits: function(value,axis) { 
    291             if(!axis) axis = this.options.axis == "vertical" ? 2 : 1; 
    292             if (value >= this.actualSize[axis == 1 ? "width" : "height"] - this.handleSize(null,axis)) 
    293                 value = this.actualSize[axis == 1 ? "width" : "height"] - this.handleSize(null,axis); 
    294             if (value <= 0) 
    295                 value = 0; 
    296             return value; 
    297         }, 
    298          
    299297        drag: function(e, handle) { 
    300298 
     
    303301            if(!this.currentHandle) this.focus(this.previousHandle, true); //This is a especially ugly fix for strange blur events happening on mousemove events 
    304302 
    305             position.left = this.translateLimits(position.left,1); 
    306             position.top = this.translateLimits(position.top,2); 
     303            position.left = this.translateLimits(position.left, "x"); 
     304            position.top = this.translateLimits(position.top, "y"); 
    307305             
    308306            if (o.stepping.x) { 
    309                 var value = this.convertValue(position.left,1); 
     307                var value = this.convertValue(position.left, "x"); 
    310308                value = Math.round(value / o.stepping.x) * o.stepping.x; 
    311                 position.left = this.translateValue(value, 1);   
     309                position.left = this.translateValue(value, "x");     
    312310            } 
    313311            if (o.stepping.y) { 
    314                 var value = this.convertValue(position.top,2); 
     312                var value = this.convertValue(position.top, "y"); 
    315313                value = Math.round(value / o.stepping.y) * o.stepping.y; 
    316                 position.top = this.translateValue(value, 2);    
    317             } 
    318              
    319             position.left = this.translateRange(position.left, 1); 
    320             position.top = this.translateRange(position.top, 2); 
     314                position.top = this.translateValue(value, "y");  
     315            } 
     316             
     317            position.left = this.translateRange(position.left, "x"); 
     318            position.top = this.translateRange(position.top, "y"); 
    321319 
    322320            if(o.axis != "vertical") this.currentHandle.css({ left: position.left }); 
     
    336334            this.actualSize = { width: this.element.outerWidth() , height: this.element.outerHeight() }; 
    337335 
     336            //If no handle has been passed, no current handle is available and we have multiple handles, return false 
    338337            if (handle == undefined && !this.currentHandle && this.handle.length != 1) 
    339                 return false; //If no handle has been passed, no current handle is available and we have multiple handles, return false 
     338                return false;  
     339             
     340            //If only one handle is available, use it 
    340341            if (handle == undefined && !this.currentHandle) 
    341                 handle = 0; //If only one handle is available, use it 
     342                handle = 0; 
     343             
    342344            if (handle != undefined) 
    343345                this.currentHandle = this.previousHandle = $(this.handle[handle] || handle); 
    344346 
    345347 
    346  
    347348            if(value.x !== undefined && value.y !== undefined) { 
    348                 var x = value.x; 
    349                 var y = value.y; 
     349                var x = value.x, y = value.y; 
    350350            } else { 
    351351                var x = value, y = value; 
     
    354354            if(x !== undefined && x.constructor != Number) { 
    355355                var me = /^\-\=/.test(x), pe = /^\+\=/.test(x); 
    356                 if (me) { 
    357                     x = this.value(null,1) - parseInt(x.replace('-=', ''), 10); 
    358                 } else if (pe) { 
    359                     x = this.value(null,1) + parseInt(x.replace('+=', ''), 10); 
    360                 } 
     356                x = this.value(null, "x") + parseInt(x.replace(me ? '=' : '+=', ''), 10); 
    361357            } 
    362358             
    363359            if(y !== undefined && y.constructor != Number) { 
    364360                var me = /^\-\=/.test(y), pe = /^\+\=/.test(y); 
    365                 if (me) { 
    366                     y = this.value(null,2) - parseInt(y.replace('-=', ''), 10); 
    367                 } else if (pe) { 
    368                     y = this.value(null,2) + parseInt(y.replace('+=', ''), 10); 
    369                 } 
     361                y = this.value(null, "y") + parseInt(y.replace(me ? '=' : '+=', ''), 10); 
    370362            } 
    371363 
    372364            if(o.axis != "vertical" && x !== undefined) { 
    373365                if(o.stepping.x) x = Math.round(x / o.stepping.x) * o.stepping.x; 
    374                 x = this.translateValue(x, 1); 
    375                 x = this.translateLimits(x, 1); 
    376                 x = this.translateRange(x, 1); 
     366                x = this.translateValue(x, "x"); 
     367                x = this.translateLimits(x, "x"); 
     368                x = this.translateRange(x, "x"); 
    377369                this.currentHandle.css({ left: x }); 
    378370            } 
     
    380372            if(o.axis != "horizontal" && y !== undefined) { 
    381373                if(o.stepping.y) y = Math.round(y / o.stepping.y) * o.stepping.y; 
    382                 y = this.translateValue(y, 2); 
    383                 y = this.translateLimits(y, 2); 
    384                 y = this.translateRange(y, 2); 
     374                y = this.translateValue(y, "y"); 
     375                y = this.translateLimits(y, "y"); 
     376                y = this.translateRange(y, "y"); 
    385377                this.currentHandle.css({ top: y }); 
    386378            }