2 * jQuery UI Slider 1.10.2
5 * Copyright 2013 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
9 * http://api.jqueryui.com/slider/
16 (function( $, undefined ) {
18 // number of pages in a slider
19 // (how many times can you page up/down to go through the whole range)
22 $.widget( "ui.slider", $.ui.mouse, {
24 widgetEventPrefix: "slide",
31 orientation: "horizontal",
45 this._keySliding = false;
46 this._mouseSliding = false;
47 this._animateOff = true;
48 this._handleIndex = null;
49 this._detectOrientation();
53 .addClass( "ui-slider" +
54 " ui-slider-" + this.orientation +
56 " ui-widget-content" +
60 this._setOption( "disabled", this.options.disabled );
62 this._animateOff = false;
65 _refresh: function() {
67 this._createHandles();
72 _createHandles: function() {
74 options = this.options,
75 existingHandles = this.element.find( ".ui-slider-handle" ).addClass( "ui-state-default ui-corner-all" ),
76 handle = "<a class='ui-slider-handle ui-state-default ui-corner-all' href='#'></a>",
79 handleCount = ( options.values && options.values.length ) || 1;
81 if ( existingHandles.length > handleCount ) {
82 existingHandles.slice( handleCount ).remove();
83 existingHandles = existingHandles.slice( 0, handleCount );
86 for ( i = existingHandles.length; i < handleCount; i++ ) {
87 handles.push( handle );
90 this.handles = existingHandles.add( $( handles.join( "" ) ).appendTo( this.element ) );
92 this.handle = this.handles.eq( 0 );
94 this.handles.each(function( i ) {
95 $( this ).data( "ui-slider-handle-index", i );
99 _createRange: function() {
100 var options = this.options,
103 if ( options.range ) {
104 if ( options.range === true ) {
105 if ( !options.values ) {
106 options.values = [ this._valueMin(), this._valueMin() ];
107 } else if ( options.values.length && options.values.length !== 2 ) {
108 options.values = [ options.values[0], options.values[0] ];
109 } else if ( $.isArray( options.values ) ) {
110 options.values = options.values.slice(0);
114 if ( !this.range || !this.range.length ) {
115 this.range = $( "<div></div>" )
116 .appendTo( this.element );
118 classes = "ui-slider-range" +
119 // note: this isn't the most fittingly semantic framework class for this element,
120 // but worked best visually with a variety of themes
121 " ui-widget-header ui-corner-all";
123 this.range.removeClass( "ui-slider-range-min ui-slider-range-max" )
124 // Handle range switching from true to min/max
131 this.range.addClass( classes +
132 ( ( options.range === "min" || options.range === "max" ) ? " ui-slider-range-" + options.range : "" ) );
138 _setupEvents: function() {
139 var elements = this.handles.add( this.range ).filter( "a" );
140 this._off( elements );
141 this._on( elements, this._handleEvents );
142 this._hoverable( elements );
143 this._focusable( elements );
146 _destroy: function() {
147 this.handles.remove();
151 .removeClass( "ui-slider" +
152 " ui-slider-horizontal" +
153 " ui-slider-vertical" +
155 " ui-widget-content" +
158 this._mouseDestroy();
161 _mouseCapture: function( event ) {
162 var position, normValue, distance, closestHandle, index, allowed, offset, mouseOverHandle,
171 width: this.element.outerWidth(),
172 height: this.element.outerHeight()
174 this.elementOffset = this.element.offset();
176 position = { x: event.pageX, y: event.pageY };
177 normValue = this._normValueFromMouse( position );
178 distance = this._valueMax() - this._valueMin() + 1;
179 this.handles.each(function( i ) {
180 var thisDistance = Math.abs( normValue - that.values(i) );
181 if (( distance > thisDistance ) ||
182 ( distance === thisDistance &&
183 (i === that._lastChangedValue || that.values(i) === o.min ))) {
184 distance = thisDistance;
185 closestHandle = $( this );
190 allowed = this._start( event, index );
191 if ( allowed === false ) {
194 this._mouseSliding = true;
196 this._handleIndex = index;
199 .addClass( "ui-state-active" )
202 offset = closestHandle.offset();
203 mouseOverHandle = !$( event.target ).parents().addBack().is( ".ui-slider-handle" );
204 this._clickOffset = mouseOverHandle ? { left: 0, top: 0 } : {
205 left: event.pageX - offset.left - ( closestHandle.width() / 2 ),
206 top: event.pageY - offset.top -
207 ( closestHandle.height() / 2 ) -
208 ( parseInt( closestHandle.css("borderTopWidth"), 10 ) || 0 ) -
209 ( parseInt( closestHandle.css("borderBottomWidth"), 10 ) || 0) +
210 ( parseInt( closestHandle.css("marginTop"), 10 ) || 0)
213 if ( !this.handles.hasClass( "ui-state-hover" ) ) {
214 this._slide( event, index, normValue );
216 this._animateOff = true;
220 _mouseStart: function() {
224 _mouseDrag: function( event ) {
225 var position = { x: event.pageX, y: event.pageY },
226 normValue = this._normValueFromMouse( position );
228 this._slide( event, this._handleIndex, normValue );
233 _mouseStop: function( event ) {
234 this.handles.removeClass( "ui-state-active" );
235 this._mouseSliding = false;
237 this._stop( event, this._handleIndex );
238 this._change( event, this._handleIndex );
240 this._handleIndex = null;
241 this._clickOffset = null;
242 this._animateOff = false;
247 _detectOrientation: function() {
248 this.orientation = ( this.options.orientation === "vertical" ) ? "vertical" : "horizontal";
251 _normValueFromMouse: function( position ) {
258 if ( this.orientation === "horizontal" ) {
259 pixelTotal = this.elementSize.width;
260 pixelMouse = position.x - this.elementOffset.left - ( this._clickOffset ? this._clickOffset.left : 0 );
262 pixelTotal = this.elementSize.height;
263 pixelMouse = position.y - this.elementOffset.top - ( this._clickOffset ? this._clickOffset.top : 0 );
266 percentMouse = ( pixelMouse / pixelTotal );
267 if ( percentMouse > 1 ) {
270 if ( percentMouse < 0 ) {
273 if ( this.orientation === "vertical" ) {
274 percentMouse = 1 - percentMouse;
277 valueTotal = this._valueMax() - this._valueMin();
278 valueMouse = this._valueMin() + percentMouse * valueTotal;
280 return this._trimAlignValue( valueMouse );
283 _start: function( event, index ) {
285 handle: this.handles[ index ],
288 if ( this.options.values && this.options.values.length ) {
289 uiHash.value = this.values( index );
290 uiHash.values = this.values();
292 return this._trigger( "start", event, uiHash );
295 _slide: function( event, index, newVal ) {
300 if ( this.options.values && this.options.values.length ) {
301 otherVal = this.values( index ? 0 : 1 );
303 if ( ( this.options.values.length === 2 && this.options.range === true ) &&
304 ( ( index === 0 && newVal > otherVal) || ( index === 1 && newVal < otherVal ) )
309 if ( newVal !== this.values( index ) ) {
310 newValues = this.values();
311 newValues[ index ] = newVal;
312 // A slide can be canceled by returning false from the slide callback
313 allowed = this._trigger( "slide", event, {
314 handle: this.handles[ index ],
318 otherVal = this.values( index ? 0 : 1 );
319 if ( allowed !== false ) {
320 this.values( index, newVal, true );
324 if ( newVal !== this.value() ) {
325 // A slide can be canceled by returning false from the slide callback
326 allowed = this._trigger( "slide", event, {
327 handle: this.handles[ index ],
330 if ( allowed !== false ) {
331 this.value( newVal );
337 _stop: function( event, index ) {
339 handle: this.handles[ index ],
342 if ( this.options.values && this.options.values.length ) {
343 uiHash.value = this.values( index );
344 uiHash.values = this.values();
347 this._trigger( "stop", event, uiHash );
350 _change: function( event, index ) {
351 if ( !this._keySliding && !this._mouseSliding ) {
353 handle: this.handles[ index ],
356 if ( this.options.values && this.options.values.length ) {
357 uiHash.value = this.values( index );
358 uiHash.values = this.values();
361 //store the last changed value index for reference when handles overlap
362 this._lastChangedValue = index;
364 this._trigger( "change", event, uiHash );
368 value: function( newValue ) {
369 if ( arguments.length ) {
370 this.options.value = this._trimAlignValue( newValue );
371 this._refreshValue();
372 this._change( null, 0 );
376 return this._value();
379 values: function( index, newValue ) {
384 if ( arguments.length > 1 ) {
385 this.options.values[ index ] = this._trimAlignValue( newValue );
386 this._refreshValue();
387 this._change( null, index );
391 if ( arguments.length ) {
392 if ( $.isArray( arguments[ 0 ] ) ) {
393 vals = this.options.values;
394 newValues = arguments[ 0 ];
395 for ( i = 0; i < vals.length; i += 1 ) {
396 vals[ i ] = this._trimAlignValue( newValues[ i ] );
397 this._change( null, i );
399 this._refreshValue();
401 if ( this.options.values && this.options.values.length ) {
402 return this._values( index );
408 return this._values();
412 _setOption: function( key, value ) {
416 if ( key === "range" && this.options.range === true ) {
417 if ( value === "min" ) {
418 this.options.value = this._values( 0 );
419 this.options.values = null;
420 } else if ( value === "max" ) {
421 this.options.value = this._values( this.options.values.length-1 );
422 this.options.values = null;
426 if ( $.isArray( this.options.values ) ) {
427 valsLength = this.options.values.length;
430 $.Widget.prototype._setOption.apply( this, arguments );
434 this._detectOrientation();
436 .removeClass( "ui-slider-horizontal ui-slider-vertical" )
437 .addClass( "ui-slider-" + this.orientation );
438 this._refreshValue();
441 this._animateOff = true;
442 this._refreshValue();
443 this._change( null, 0 );
444 this._animateOff = false;
447 this._animateOff = true;
448 this._refreshValue();
449 for ( i = 0; i < valsLength; i += 1 ) {
450 this._change( null, i );
452 this._animateOff = false;
456 this._animateOff = true;
457 this._refreshValue();
458 this._animateOff = false;
461 this._animateOff = true;
463 this._animateOff = false;
468 //internal value getter
469 // _value() returns value trimmed by min and max, aligned by step
471 var val = this.options.value;
472 val = this._trimAlignValue( val );
477 //internal values getter
478 // _values() returns array of values trimmed by min and max, aligned by step
479 // _values( index ) returns single value trimmed by min and max, aligned by step
480 _values: function( index ) {
485 if ( arguments.length ) {
486 val = this.options.values[ index ];
487 val = this._trimAlignValue( val );
490 } else if ( this.options.values && this.options.values.length ) {
491 // .slice() creates a copy of the array
492 // this copy gets trimmed by min and max and then returned
493 vals = this.options.values.slice();
494 for ( i = 0; i < vals.length; i+= 1) {
495 vals[ i ] = this._trimAlignValue( vals[ i ] );
504 // returns the step-aligned value that val is closest to, between (inclusive) min and max
505 _trimAlignValue: function( val ) {
506 if ( val <= this._valueMin() ) {
507 return this._valueMin();
509 if ( val >= this._valueMax() ) {
510 return this._valueMax();
512 var step = ( this.options.step > 0 ) ? this.options.step : 1,
513 valModStep = (val - this._valueMin()) % step,
514 alignValue = val - valModStep;
516 if ( Math.abs(valModStep) * 2 >= step ) {
517 alignValue += ( valModStep > 0 ) ? step : ( -step );
520 // Since JavaScript has problems with large floats, round
521 // the final value to 5 digits after the decimal point (see #4124)
522 return parseFloat( alignValue.toFixed(5) );
525 _valueMin: function() {
526 return this.options.min;
529 _valueMax: function() {
530 return this.options.max;
533 _refreshValue: function() {
534 var lastValPercent, valPercent, value, valueMin, valueMax,
535 oRange = this.options.range,
538 animate = ( !this._animateOff ) ? o.animate : false,
541 if ( this.options.values && this.options.values.length ) {
542 this.handles.each(function( i ) {
543 valPercent = ( that.values(i) - that._valueMin() ) / ( that._valueMax() - that._valueMin() ) * 100;
544 _set[ that.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%";
545 $( this ).stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate );
546 if ( that.options.range === true ) {
547 if ( that.orientation === "horizontal" ) {
549 that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { left: valPercent + "%" }, o.animate );
552 that.range[ animate ? "animate" : "css" ]( { width: ( valPercent - lastValPercent ) + "%" }, { queue: false, duration: o.animate } );
556 that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { bottom: ( valPercent ) + "%" }, o.animate );
559 that.range[ animate ? "animate" : "css" ]( { height: ( valPercent - lastValPercent ) + "%" }, { queue: false, duration: o.animate } );
563 lastValPercent = valPercent;
566 value = this.value();
567 valueMin = this._valueMin();
568 valueMax = this._valueMax();
569 valPercent = ( valueMax !== valueMin ) ?
570 ( value - valueMin ) / ( valueMax - valueMin ) * 100 :
572 _set[ this.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%";
573 this.handle.stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate );
575 if ( oRange === "min" && this.orientation === "horizontal" ) {
576 this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { width: valPercent + "%" }, o.animate );
578 if ( oRange === "max" && this.orientation === "horizontal" ) {
579 this.range[ animate ? "animate" : "css" ]( { width: ( 100 - valPercent ) + "%" }, { queue: false, duration: o.animate } );
581 if ( oRange === "min" && this.orientation === "vertical" ) {
582 this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { height: valPercent + "%" }, o.animate );
584 if ( oRange === "max" && this.orientation === "vertical" ) {
585 this.range[ animate ? "animate" : "css" ]( { height: ( 100 - valPercent ) + "%" }, { queue: false, duration: o.animate } );
591 keydown: function( event ) {
592 /*jshint maxcomplexity:25*/
593 var allowed, curVal, newVal, step,
594 index = $( event.target ).data( "ui-slider-handle-index" );
596 switch ( event.keyCode ) {
597 case $.ui.keyCode.HOME:
598 case $.ui.keyCode.END:
599 case $.ui.keyCode.PAGE_UP:
600 case $.ui.keyCode.PAGE_DOWN:
601 case $.ui.keyCode.UP:
602 case $.ui.keyCode.RIGHT:
603 case $.ui.keyCode.DOWN:
604 case $.ui.keyCode.LEFT:
605 event.preventDefault();
606 if ( !this._keySliding ) {
607 this._keySliding = true;
608 $( event.target ).addClass( "ui-state-active" );
609 allowed = this._start( event, index );
610 if ( allowed === false ) {
617 step = this.options.step;
618 if ( this.options.values && this.options.values.length ) {
619 curVal = newVal = this.values( index );
621 curVal = newVal = this.value();
624 switch ( event.keyCode ) {
625 case $.ui.keyCode.HOME:
626 newVal = this._valueMin();
628 case $.ui.keyCode.END:
629 newVal = this._valueMax();
631 case $.ui.keyCode.PAGE_UP:
632 newVal = this._trimAlignValue( curVal + ( (this._valueMax() - this._valueMin()) / numPages ) );
634 case $.ui.keyCode.PAGE_DOWN:
635 newVal = this._trimAlignValue( curVal - ( (this._valueMax() - this._valueMin()) / numPages ) );
637 case $.ui.keyCode.UP:
638 case $.ui.keyCode.RIGHT:
639 if ( curVal === this._valueMax() ) {
642 newVal = this._trimAlignValue( curVal + step );
644 case $.ui.keyCode.DOWN:
645 case $.ui.keyCode.LEFT:
646 if ( curVal === this._valueMin() ) {
649 newVal = this._trimAlignValue( curVal - step );
653 this._slide( event, index, newVal );
655 click: function( event ) {
656 event.preventDefault();
658 keyup: function( event ) {
659 var index = $( event.target ).data( "ui-slider-handle-index" );
661 if ( this._keySliding ) {
662 this._keySliding = false;
663 this._stop( event, index );
664 this._change( event, index );
665 $( event.target ).removeClass( "ui-state-active" );