jQuery: The Write Less, Do More JavaScript Library

Changeset 5141

Show
Ignore:
Timestamp:
03/28/08 16:02:51 (4 months ago)
Author:
paul.bakaus
Message:

- added colorpicker plugin (incomplete, missing callbacks)
- fixed base js callback issue
- fixed two-axis sliding

Location:
trunk/ui
Files:
12 added
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/ui/demos/ui.slider.html

    r5088 r5141  
    5151<div class="playground" style='height: 280px;'> 
    5252    <div id='example4' style="position: relative; margin: 40px; width: 20px; height: 200px; background: #eee;"> 
    53     <div style='position: absolute; z-index: 1; background: green; width: 100%; height: 12px; top: 0px;'></div> 
    54     <div style='position: absolute; z-index: 1; background: green; width: 100%; height: 12px; top: 188px;'></div>    
     53    <div class='ui-slider-handle' style='position: absolute; z-index: 1; background: green; width: 100%; height: 12px; top: 0px;'></div> 
     54    <div class='ui-slider-handle' style='position: absolute; z-index: 1; background: green; width: 100%; height: 12px; top: 188px;'></div>   
    5555    </div> 
    5656</div> 
  • trunk/ui/ui.base.js

    r5136 r5141  
    141141                if(!self.initalized && Math.abs(self._MP.left-e.pageX) >= self.options.distance || Math.abs(self._MP.top-e.pageY) >= self.options.distance) {                
    142142                    if(self.options.start) self.options.start.call(self.options.executor || self, e, self.element); 
     143                    if(self.options.drag) self.options.drag.call(self.options.executor || self, e, this.element); //This is actually not correct, but expected 
    143144                    self.initialized = true; 
    144145                } 
  • trunk/ui/ui.slider.js

    r5136 r5141  
    3333        $.extend(o, { 
    3434            axis: o.axis || (element.offsetWidth < element.offsetHeight ? 'vertical' : 'horizontal'), 
    35             maxValue: !isNaN(parseInt(o.maxValue,10)) ? parseInt(o.maxValue, 10) :  100, 
    36             minValue: parseInt(o.minValue,10) || 0 
     35            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 }), 
     36            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 }) 
    3737        }); 
    38          
     38     
    3939        //Prepare the real maxValue 
    40         o.realMaxValue = o.maxValue - o.minValue; 
     40        o.realMax = { 
     41            x: o.max.x - o.min.x, 
     42            y: o.max.y - o.min.y 
     43        }; 
    4144         
    4245        //Calculate stepping based on steps 
    43         o.stepping = parseInt(o.stepping, 10) || (o.steps ? o.realMaxValue/o.steps : 0); 
     46        o.stepping = { 
     47            x: o.stepping && o.stepping.x || parseInt(o.stepping, 10) || (o.steps && o.steps.x ? o.realMax.x/o.steps.x : 0), 
     48            y: o.stepping && o.stepping.y || parseInt(o.stepping, 10) || (o.steps && o.steps.y ? o.realMax.y/o.steps.y : 0) 
     49        }; 
    4450         
    4551        $(element).bind("setData.slider", function(event, key, value){ 
     
    8187                .bind('blur', function(e) { self.blur(this.firstChild); }) 
    8288                .bind('keydown', function(e) { 
    83                     if(/(37|39)/.test(e.keyCode)) { 
    84                         self.moveTo((e.keyCode == 37 ? '-' : '+') + '=' + self.oneStep(),this.firstChild); 
     89                    if(/(37|38|39|40)/.test(e.keyCode)) { 
     90                        self.moveTo({ 
     91                            x: /(37|39)/.test(e.keyCode) ? (e.keyCode == 37 ? '-' : '+') + '=' + self.oneStep(1) : null, 
     92                            y: /(38|40)/.test(e.keyCode) ? (e.keyCode == 38 ? '-' : '+') + '=' + self.oneStep(2) : null 
     93                        }, this.firstChild); 
    8594                    } 
    8695                }) 
     
    8897         
    8998        //Prepare dynamic properties for later use 
    90         if(o.axis == 'horizontal') { 
    91             this.actualSize = this.element.outerWidth(); 
    92             this.properties = ['left', 'width']; 
    93         } else { 
    94             this.actualSize = this.element.outerHeight(); 
    95             this.properties = ['top', 'height']; 
    96         } 
     99        this.actualSize = { width: this.element.outerWidth() , height: this.element.outerHeight() }; 
    97100         
    98101        //Bind the click to the slider itself 
     
    100103            self.click.apply(self, [e]); 
    101104            self.currentHandle.data("ui-mouse").trigger(e); 
     105            self.firstValue = self.firstValue + 1; //This is for always triggering the change event 
    102106        }); 
    103107         
     
    111115        //If we only have one handle, set the previous handle to this one to allow clicking before selecting the handle 
    112116        if(this.handle.length == 1) this.previousHandle = this.handle; 
    113          
    114          
    115117        if(this.handle.length == 2 && o.range) this.createRange(); 
    116118     
     
    127129        }, 
    128130        updateRange: function() { 
    129                 this.rangeElement.css(this.properties[0], parseInt($(this.handle[0]).css(this.properties[0]),10) + this.handleSize(0)/2); 
    130                 this.rangeElement.css(this.properties[1], parseInt($(this.handle[1]).css(this.properties[0]),10) - parseInt($(this.handle[0]).css(this.properties[0]),10)); 
     131                var prop = this.options.axis == "vertical" ? "top" : "left"; 
     132                var size = this.options.axis == "vertical" ? "height" : "width"; 
     133                this.rangeElement.css(prop, parseInt($(this.handle[0]).css(prop),10) + this.handleSize(0, this.options.axis == "vertical" ? 2 : 1)/2); 
     134                this.rangeElement.css(size, parseInt($(this.handle[1]).css(prop),10) - parseInt($(this.handle[0]).css(prop),10)); 
    131135        }, 
    132136        getRange: function() { 
    133             return this.rangeElement ? this.convertValue(parseInt(this.rangeElement.css(this.properties[1]),10)) : null; 
     137            return this.rangeElement ? this.convertValue(parseInt(this.rangeElement.css(this.options.axis == "vertical" ? "height" : "width"),10)) : null; 
    134138        }, 
    135139        ui: function(e) { 
     
    138142                options: this.options, 
    139143                handle: this.currentHandle, 
    140                 value: this.value(), 
     144                value: this.options.axis != "both" || !this.options.axis ? Math.round(this.value(null,this.options.axis == "vertical" ? 2 : 1)) : { 
     145                    x: Math.round(this.value(null,1)), 
     146                    y: Math.round(this.value(null,2)) 
     147                }, 
    141148                range: this.getRange() 
    142149            }; 
     
    170177            if(this.currentHandle && this.currentHandle[0] == handle) { this.previousHandle = this.currentHandle; this.currentHandle = null; }; 
    171178        }, 
    172         value: function(handle) { 
     179        value: function(handle, axis) { 
    173180            if(this.handle.length == 1) this.currentHandle = this.handle; 
    174             var value = ((parseInt($(handle != undefined ? this.handle[handle] || handle : this.currentHandle).css(this.properties[0]),10) / (this.size() - this.handleSize())) * this.options.realMaxValue) + this.options.minValue; 
     181            if(!axis) axis = this.options.axis == "vertical" ? 2 : 1; 
     182             
     183            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(null,axis))) * this.options.realMax[axis == 1 ? "x" : "y"]) + this.options.min[axis == 1 ? "x" : "y"]; 
     184             
    175185            var o = this.options; 
    176             if (o.stepping) { 
    177                 value = Math.round(value / o.stepping) * o.stepping; 
     186            if (o.stepping[axis == 1 ? "x" : "y"]) { 
     187                value = Math.round(value / o.stepping[axis == 1 ? "x" : "y"]) * o.stepping[axis == 1 ? "x" : "y"]; 
    178188            } 
    179189            return value; 
    180190        }, 
    181         convertValue: function(value) { 
    182             return this.options.minValue + (value / (this.size() - this.handleSize())) * this.options.realMaxValue; 
    183         }, 
    184         translateValue: function(value) { 
    185             return ((value - this.options.minValue) / this.options.realMaxValue) * (this.size() - this.handleSize()); 
    186         }, 
    187         handleSize: function(handle) { 
    188             return $(handle != undefined ? this.handle[handle] : this.currentHandle)['outer'+this.properties[1].substr(0,1).toUpperCase()+this.properties[1].substr(1)]();   
    189         }, 
    190         size: function() { 
    191             // if actualSize is 0, the slider was hidden during initialization 
    192             return this.actualSize || (this.actualSize = this.element.outerWidth()); 
     191        convertValue: function(value,axis) { 
     192            if(!axis) axis = this.options.axis == "vertical" ? 2 : 1; 
     193            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"]; 
     194        }, 
     195        translateValue: function(value,axis) { 
     196            if(!axis) axis = this.options.axis == "vertical" ? 2 : 1; 
     197            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)); 
     198        }, 
     199        handleSize: function(handle,axis) { 
     200            if(!axis) axis = this.options.axis == "vertical" ? 2 : 1; 
     201            return $(handle != undefined && handle !== null ? this.handle[handle] : this.currentHandle)[axis == 1 ? "outerWidth" : "outerHeight"]();     
    193202        }, 
    194203        click: function(e) { 
     
    207216            //Move focussed handle to the clicked position 
    208217            this.offset = this.element.offset(); 
    209             this.moveTo(this.convertValue(e[this.properties[0] == 'top' ? 'pageY' : 'pageX'] - this.offset[this.properties[0]] - this.handleSize()/2), null, true); 
     218            this.moveTo({ 
     219                y: this.convertValue(e.pageY - this.offset.top - this.currentHandle.outerHeight()/2), 
     220                x: this.convertValue(e.pageX - this.offset.left - this.currentHandle.outerWidth()/2) 
     221            }, null, true); 
    210222        }, 
    211223        start: function(e, handle) { 
    212224         
    213225            var o = this.options; 
    214             if(!this.currentHandle) this.currentHandle = this.previousHandle; //This is a especially ugly fix for strange blur events happening on mousemove events 
     226            if(!this.currentHandle) this.focus(this.previousHandle, true); //This is a especially ugly fix for strange blur events happening on mousemove events 
    215227 
    216228            this.offset = this.element.offset(); 
     
    227239            if (this.firstValue != this.value()) 
    228240                this.propagate('change', e); 
     241            this.focus(this.currentHandle, true); //This is a especially ugly fix for strange blur events happening on mousemove events 
    229242            return false; 
    230243        }, 
    231244         
    232         oneStep: function() { 
    233             return this.options.stepping ? this.options.stepping : (this.options.realMaxValue / this.size()) * 5; 
    234         }, 
    235          
    236         translateRange: function(value) { 
     245        oneStep: function(axis) { 
     246            if(!axis) axis = this.options.axis == "vertical" ? 2 : 1; 
     247            return this.options.stepping[axis == 1 ? "x" : "y"] ? this.options.stepping[axis == 1 ? "x" : "y"] : (this.options.realMax[axis == 1 ? "x" : "y"] / this.actualSize[axis == 1 ? "width" : "height"]) * 5; 
     248        }, 
     249         
     250        translateRange: function(value,axis) { 
    237251            if (this.rangeElement) { 
    238                 if (this.currentHandle[0] == this.handle[0] && value >= this.translateValue(this.value(1))) 
    239                     value = this.translateValue(this.value(1) - this.oneStep()); 
    240                 if (this.currentHandle[0] == this.handle[1] && value <= this.translateValue(this.value(0))) 
    241                     value = this.translateValue(this.value(0) + this.oneStep()); 
     252                if (this.currentHandle[0] == this.handle[0] && value >= this.translateValue(this.value(1),axis)) 
     253                    value = this.translateValue(this.value(1,axis) - this.oneStep(axis), axis); 
     254                if (this.currentHandle[0] == this.handle[1] && value <= this.translateValue(this.value(0),axis)) 
     255                    value = this.translateValue(this.value(0,axis) + this.oneStep(axis)); 
    242256            } 
    243257            if (this.options.handles) { 
    244258                var handle = this.options.handles[this.handleIndex()]; 
    245                 if (value < this.translateValue(handle.min)) { 
    246                     value = this.translateValue(handle.min); 
    247                 } else if (value > this.translateValue(handle.max)) { 
    248                     value = this.translateValue(handle.max); 
     259                if (value < this.translateValue(handle.min,axis)) { 
     260                    value = this.translateValue(handle.min,axis); 
     261                } else if (value > this.translateValue(handle.max,axis)) { 
     262                    value = this.translateValue(handle.max,axis); 
    249263                } 
    250264            } 
     
    256270        }, 
    257271         
    258         translateLimits: function(value) { 
    259             if (value >= this.size() - this.handleSize()) 
    260                 value = this.size() - this.handleSize(); 
     272        translateLimits: function(value,axis) { 
     273            if(!axis) axis = this.options.axis == "vertical" ? 2 : 1; 
     274            if (value >= this.actualSize[axis == 1 ? "width" : "height"] - this.handleSize(null,axis)) 
     275                value = this.actualSize[axis == 1 ? "width" : "height"] - this.handleSize(null,axis); 
    261276            if (value <= 0) 
    262277                value = 0; 
     
    265280         
    266281        drag: function(e, handle) { 
     282 
    267283            var o = this.options; 
    268284            var position = { top: e.pageY - this.offset.top - this.clickOffset.top, left: e.pageX - this.offset.left - this.clickOffset.left}; 
    269             if(!this.currentHandle) this.currentHandle = this.previousHandle; //This is a especially ugly fix for strange blur events happening on mousemove events 
    270  
    271             var modifier = position[this.properties[0]]; 
    272              
    273             modifier = this.translateLimits(modifier); 
    274              
    275             if (o.stepping) { 
    276                 var value = this.convertValue(modifier); 
    277                 value = Math.round(value / o.stepping) * o.stepping; 
    278                 modifier = this.translateValue(value);   
    279             } 
    280              
    281             modifier = this.translateRange(modifier); 
    282  
    283             this.currentHandle.css(this.properties[0], modifier); 
     285            if(!this.currentHandle) this.focus(this.previousHandle, true); //This is a especially ugly fix for strange blur events happening on mousemove events 
     286 
     287            position.left = this.translateLimits(position.left,1); 
     288            position.top = this.translateLimits(position.top,2); 
     289             
     290            if (o.stepping.x) { 
     291                var value = this.convertValue(position.left,1); 
     292                value = Math.round(value / o.stepping.x) * o.stepping.x; 
     293                position.left = this.translateValue(value, 1);   
     294            } 
     295            if (o.stepping.y) { 
     296                var value = this.convertValue(position.top,2); 
     297                value = Math.round(value / o.stepping.y) * o.stepping.y; 
     298                position.top = this.translateValue(value, 2);    
     299            } 
     300             
     301            position.left = this.translateRange(position.left, 1); 
     302            position.top = this.translateRange(position.top, 2); 
     303 
     304            if(o.axis != "vertical") this.currentHandle.css({ left: position.left }); 
     305            if(o.axis != "horizontal") this.currentHandle.css({ top: position.top }); 
     306             
    284307            if (this.rangeElement) 
    285308                this.updateRange(); 
     
    296319            if (handle != undefined) 
    297320                this.currentHandle = this.previousHandle = $(this.handle[handle] || handle); 
    298      
    299             if(value.constructor == String) { 
    300                 if (/^\-\=/.test(value) ) { 
    301                     value = this.value() - parseInt(value.replace('-=', ''), 10); 
    302                 } else if (/^\+\=/.test(value) ) { 
    303                     value = this.value() + parseInt(value.replace('+=', ''), 10); 
     321 
     322 
     323 
     324            if(value.x !== undefined && value.y !== undefined) { 
     325                var x = value.x; 
     326                var y = value.y; 
     327            } else { 
     328                var x = value, y = value; 
     329            } 
     330 
     331            if(x && x.constructor != Number) { 
     332                if (/^\-\=/.test(x) ) { 
     333                    x = this.value(null,1) - parseInt(x.replace('-=', ''), 10); 
     334                } else if (/^\+\=/.test(x) ) { 
     335                    x = this.value(null,1) + parseInt(x.replace('+=', ''), 10); 
    304336                } 
    305337            } 
    306338             
    307             if(o.stepping) 
    308                 value = Math.round(value / o.stepping) * o.stepping; 
    309             value = this.translateValue(value); 
    310             value = this.translateLimits(value); 
    311             value = this.translateRange(value); 
    312              
    313             this.currentHandle.css(this.properties[0], value); 
     339            if(y && y.constructor != Number) { 
     340                if (/^\-\=/.test(y) ) { 
     341                    y = this.value(null,2) - parseInt(y.replace('-=', ''), 10); 
     342                } else if (/^\+\=/.test(y) ) { 
     343                    y = this.value(null,2) + parseInt(y.replace('+=', ''), 10); 
     344                } 
     345            } 
     346 
     347            if(o.axis != "vertical" && x) { 
     348                if(o.stepping.x) x = Math.round(x / o.stepping.x) * o.stepping.x; 
     349                x = this.translateValue(x, 1); 
     350                x = this.translateLimits(x, 1); 
     351                x = this.translateRange(x, 1); 
     352                this.currentHandle.css({ left: x }); 
     353            } 
     354 
     355            if(o.axis != "horizontal" && y) { 
     356                if(o.stepping.y) y = Math.round(y / o.stepping.y) * o.stepping.y; 
     357                y = this.translateValue(y, 2); 
     358                y = this.translateLimits(y, 2); 
     359                y = this.translateRange(y, 2); 
     360                this.currentHandle.css({ top: y }); 
     361            } 
     362             
    314363            if (this.rangeElement) 
    315364                this.updateRange();