]> source.dussan.org Git - jquery-ui.git/commitdiff
slider: fixed slider, now uses subclassing of the mouse plugin
authorPaul Bakaus <paul.bakaus@googlemail.com>
Tue, 27 May 2008 11:11:49 +0000 (11:11 +0000)
committerPaul Bakaus <paul.bakaus@googlemail.com>
Tue, 27 May 2008 11:11:49 +0000 (11:11 +0000)
ui/source/ui.slider.js

index 87c1572fbf9a31e51d39e0fcf7eaf44a663156b0..19f6247c4099d683e046ef33a30cc851893f7315 100644 (file)
             $(this).parents(expr).eq(0).after(this).remove();\r
          });\r
        };\r
+       \r
 \r
-       $.widget("ui.slider", $.extend($.ui.mouse, {\r
+       $.widget("ui.slider", {\r
                plugins: {},\r
                ui: function(e) {\r
                        return {\r
+                               instance: this,\r
                                options: this.options,\r
                                handle: this.currentHandle,\r
                                value: this.options.axis != "both" || !this.options.axis ? Math.round(this.value(null,this.options.axis == "vertical" ? "y" : "x")) : {\r
                                .removeData("slider")\r
                                .unbind(".slider");\r
                        this.handle\r
-                               .unwrap("a");\r
-                       this.mouseDestroy();\r
+                               .unwrap("a")\r
+                               .mouse("destroy");\r
                        this.generated && this.generated.remove();\r
                },\r
                enable: function() {\r
                        this.element.removeClass("ui-slider-disabled");\r
-                       this.options.disabled = false;\r
+                       this.disabled = false;\r
                },\r
                disable: function() {\r
                        this.element.addClass("ui-slider-disabled");\r
-                       this.options.disabled = true;\r
+                       this.disabled = true;\r
                },\r
                setData: function(key, value) {\r
                        this.options[key] = value;\r
                        if (!this.handle.length) {\r
                                self.handle = self.generated = $(self.options.handles || [0]).map(function() {\r
                                        var handle = $("<div/>").addClass("ui-slider-handle").appendTo(self.element);\r
-                                       handle.data("mouse", {});\r
                                        if (this.id)\r
                                                handle.attr("id", this.id);\r
                                        return handle[0];\r
                                });\r
                        }\r
                        \r
-                       this.mouseInit();\r
+                       \r
+                       var handleclass = function(el) {\r
+                               this.element = $(el);\r
+                               this.element.data("mouse", this);\r
+                               this.options = self.options;\r
+                               \r
+                               this.element.bind("mousedown", function() {\r
+                                       if(self.currentHandle) this.blur(self.currentHandle);\r
+                                       self.focus(this,1);\r
+                               });\r
+                               \r
+                               this.mouseInit();\r
+                       };\r
+                       \r
+                       $.extend(handleclass.prototype, $.ui.mouse, {\r
+                               mouseStart: function(e) { return self.start.call(self, e, this.element[0]); },\r
+                               mouseStop: function(e) { return self.stop.call(self, e, this.element[0]); },\r
+                               mouseDrag: function(e) { return self.drag.call(self, e, this.element[0]); },\r
+                               trigger: function(e) { this.mouseDown(e); }\r
+                       });\r
+                       \r
                        \r
                        $(this.handle)\r
-                               .data("mouse", {})\r
+                               .each(function() {\r
+                                       new handleclass(this);\r
+                               })\r
                                .wrap('<a href="javascript:void(0)" style="cursor:default;"></a>')\r
                                .parent()\r
                                        .bind('focus', function(e) { self.focus(this.firstChild); })\r
                                        .bind('blur', function(e) { self.blur(this.firstChild); })\r
                                        .bind('keydown', function(e) { self.keydown(e.keyCode, this.firstChild); })\r
                        ;\r
-\r
+                       \r
                        // Bind the click to the slider itself\r
                        this.element.bind('mousedown.slider', function(e) {\r
                                self.click.apply(self, [e]);\r
-                               //TODO - fix this. Broken since experimental mouse\r
-                               //self.currentHandle.data("mouse").trigger(e);\r
+                               self.currentHandle.data("mouse").trigger(e);\r
                                self.firstValue = self.firstValue + 1; //This is for always triggering the change event\r
                        });\r
                        \r
                                if(this == e.target)\r
                                        clickedHandle = true;\r
                        });\r
-                       if (clickedHandle || this.options.disabled || !(this.currentHandle || this.previousHandle))\r
+                       if (clickedHandle || this.disabled || !(this.currentHandle || this.previousHandle))\r
                                return;\r
 \r
                        // If a previous handle was focussed, focus it again\r
                        }, null, !this.options.distance);\r
                },\r
                \r
+\r
+       \r
                createRange: function() {\r
                        this.rangeElement = $('<div></div>')\r
                                .addClass('ui-slider-range')\r
                                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);\r
                        }\r
 \r
-\r
                },\r
                convertValue: function(value,axis) {\r
                        return this.options.min[axis] + (value / (this.actualSize[axis == "x" ? "width" : "height"] - this.handleSize(null,axis))) * this.options.realMax[axis];\r
                },\r
 \r
 \r
-               mouseStart: function(e) {\r
+               start: function(e, handle) {\r
                \r
                        var o = this.options;\r
-                       var handle = e.target;\r
+                       if(this.disabled) return false;\r
 \r
                        // Prepare the outer size\r
                        this.actualSize = { width: this.element.outerWidth() , height: this.element.outerHeight() };\r
                                this.focus(this.previousHandle, true); \r
 \r
                        this.offset = this.element.offset();\r
+                       \r
                        this.handleOffset = this.currentHandle.offset();\r
                        this.clickOffset = { top: e.pageY - this.handleOffset.top, left: e.pageX - this.handleOffset.left };\r
+                       \r
                        this.firstValue = this.value();\r
                        \r
                        this.propagate('start', e);\r
-                       \r
-                       if(!this.options.disabled) {\r
-                               if(this.currentHandle) this.blur(this.currentHandle);\r
-                               this.focus(handle,1);\r
-                               return !this.options.disabled;\r
-                       }\r
+                       return true;\r
+                                               \r
                },\r
-               mouseDrag: function(e) {\r
+               stop: function(e) {\r
+                       this.propagate('stop', e);\r
+                       if (this.firstValue != this.value())\r
+                               this.propagate('change', e);\r
+                       // This is a especially ugly fix for strange blur events happening on mousemove events\r
+                       this.focus(this.currentHandle, true);\r
+                       return false;\r
+               },\r
+               drag: function(e, handle) {\r
 \r
                        var o = this.options;\r
-                       \r
                        var position = { top: e.pageY - this.offset.top - this.clickOffset.top, left: e.pageX - this.offset.left - this.clickOffset.left};\r
                        if(!this.currentHandle) this.focus(this.previousHandle, true); //This is a especially ugly fix for strange blur events happening on mousemove events\r
 \r
                        \r
                        //Store the slider's value\r
                        this.currentHandle.data("mouse").sliderValue = {\r
-                               x: this.convertValue(position.left, "x"),\r
-                               y: this.convertValue(position.top, "y")\r
+                               x: Math.round(this.convertValue(position.left, "x")) || 0,\r
+                               y: Math.round(this.convertValue(position.top, "y")) || 0\r
                        };\r
-\r
+                       \r
                        if (this.rangeElement)\r
                                this.updateRange();\r
                        this.propagate('slide', e);\r
                        return false;\r
                },\r
-               mouseStop: function(e) {\r
-                       this.propagate('stop', e);\r
-                       if (this.firstValue != this.value())\r
-                               this.propagate('change', e);\r
-                       // This is a especially ugly fix for strange blur events happening on mousemove events\r
-                       this.focus(this.currentHandle, true);\r
-                       return false;\r
-               },\r
                \r
                moveTo: function(value, handle, noPropagation) {\r
 \r
                                \r
                        //Store the slider's value\r
                        this.currentHandle.data("mouse").sliderValue = {\r
-                               x: this.convertValue(x, "x"),\r
-                               y: this.convertValue(y, "y")\r
+                               x: Math.round(this.convertValue(x, "x")) || 0,\r
+                               y: Math.round(this.convertValue(y, "y")) || 0\r
                        };\r
                        \r
                        if (!noPropagation) {\r
                                this.propagate("slide", null);\r
                        }\r
                }\r
-       }));\r
+       });\r
 \r
        $.ui.slider.getter = "value";\r
        \r
        $.ui.slider.defaults = {\r
-               distance: 0,\r
-               delay: 0,\r
-               cancel: ":input,button",\r
                handle: ".ui-slider-handle",\r
+               distance: 1\r
        };\r
 \r
 })(jQuery);\r