* jQuery UI Spinner 1.12.1
* Copyright jQuery Foundation and other contributors
* Released under the MIT license.
* http://jquery.org/license
//>>description: Displays buttons to easily input numbers via the keyboard or mouse.
//>>docs: http://api.jqueryui.com/spinner/
//>>demos: http://jqueryui.com/spinner/
//>>css.structure: ../../themes/base/core.css
//>>css.structure: ../../themes/base/spinner.css
//>>css.theme: ../../themes/base/theme.css
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
function spinnerModifer( fn ) {
var previous = this.element.val();
fn.apply( this, arguments );
if ( previous !== this.element.val() ) {
this._trigger( "change" );
$.widget( "ui.spinner", {
defaultElement: "<input>",
widgetEventPrefix: "spin",
"ui-spinner": "ui-corner-all",
"ui-spinner-down": "ui-corner-br",
"ui-spinner-up": "ui-corner-tr"
down: "ui-icon-triangle-1-s",
up: "ui-icon-triangle-1-n"
// handle string values that need to be parsed
this._setOption( "max", this.options.max );
this._setOption( "min", this.options.min );
this._setOption( "step", this.options.step );
// Only format if there is a value, prevents the field from being marked
// as invalid in Firefox, see #9573.
if ( this.value() !== "" ) {
// Format the value, but don't constrain.
this._value( this.element.val(), true );
this._on( this._events );
// Turning off autocomplete prevents the browser from remembering the
// value when navigating through history, so we re-enable autocomplete
// if the page is unloaded before the widget is destroyed. #7790
beforeunload: function() {
this.element.removeAttr( "autocomplete" );
_getCreateOptions: function() {
var options = this._super();
var element = this.element;
$.each( [ "min", "max", "step" ], function( i, option ) {
var value = element.attr( option );
if ( value != null && value.length ) {
options[ option ] = value;
keydown: function( event ) {
if ( this._start( event ) && this._keydown( event ) ) {
this.previous = this.element.val();
blur: function( event ) {
if ( this.previous !== this.element.val() ) {
this._trigger( "change", event );
mousewheel: function( event, delta ) {
if ( !this.spinning && !this._start( event ) ) {
this._spin( ( delta > 0 ? 1 : -1 ) * this.options.step, event );
clearTimeout( this.mousewheelTimer );
this.mousewheelTimer = this._delay( function() {
"mousedown .ui-spinner-button": function( event ) {
// We never want the buttons to have focus; whenever the user is
// interacting with the spinner, the focus should be on the input.
// If the input is focused then this.previous is properly set from
// when the input first received focus. If the input is not focused
// then we need to set this.previous based on the value before spinning.
previous = this.element[ 0 ] === $.ui.safeActiveElement( this.document[ 0 ] ) ?
this.previous : this.element.val();
var isActive = this.element[ 0 ] === $.ui.safeActiveElement( this.document[ 0 ] );
this.element.trigger( "focus" );
this.previous = previous;
// IE sets focus asynchronously, so we need to check if focus
// moved off of the input because the user clicked on the button.
this._delay( function() {
this.previous = previous;
// Ensure focus is on (or stays on) the text field
// IE doesn't prevent moving focus even with event.preventDefault()
// so we set a flag to know when we should ignore the blur event
// and check (again) if focus moved off of the input.
this._delay( function() {
if ( this._start( event ) === false ) {
this._repeat( null, $( event.currentTarget )
.hasClass( "ui-spinner-up" ) ? 1 : -1, event );
"mouseup .ui-spinner-button": "_stop",
"mouseenter .ui-spinner-button": function( event ) {
// button will add ui-state-active if mouse was down while mouseleave and kept down
if ( !$( event.currentTarget ).hasClass( "ui-state-active" ) ) {
if ( this._start( event ) === false ) {
this._repeat( null, $( event.currentTarget )
.hasClass( "ui-spinner-up" ) ? 1 : -1, event );
// TODO: do we really want to consider this a stop?
// shouldn't we just stop the repeater and wait until mouseup before
// we trigger the stop event?
"mouseleave .ui-spinner-button": "_stop"
// Support mobile enhanced option and make backcompat more sane
this.uiSpinner = this.element
.attr( "autocomplete", "off" )
this._addClass( this.uiSpinner, "ui-spinner", "ui-widget ui-widget-content" );
this._addClass( "ui-spinner-input" );
this.element.attr( "role", "spinbutton" );
this.buttons = this.uiSpinner.children( "a" )
.attr( "aria-hidden", true )
// TODO: Right now button does not support classes this is already updated in button PR
this._removeClass( this.buttons, "ui-corner-all" );
this._addClass( this.buttons.first(), "ui-spinner-button ui-spinner-up" );
this._addClass( this.buttons.last(), "ui-spinner-button ui-spinner-down" );
this.buttons.first().button( {
"icon": this.options.icons.up,
this.buttons.last().button( {
"icon": this.options.icons.down,
// IE 6 doesn't understand height: 50% for the buttons
// unless the wrapper has an explicit height
if ( this.buttons.height() > Math.ceil( this.uiSpinner.height() * 0.5 ) &&
this.uiSpinner.height() > 0 ) {
this.uiSpinner.height( this.uiSpinner.height() );
_keydown: function( event ) {
var options = this.options,
switch ( event.keyCode ) {
this._repeat( null, 1, event );
this._repeat( null, -1, event );
this._repeat( null, options.page, event );
this._repeat( null, -options.page, event );
_start: function( event ) {
if ( !this.spinning && this._trigger( "start", event ) === false ) {
_repeat: function( i, steps, event ) {
clearTimeout( this.timer );
this.timer = this._delay( function() {
this._repeat( 40, steps, event );
this._spin( steps * this.options.step, event );
_spin: function( step, event ) {
var value = this.value() || 0;
value = this._adjustValue( value + step * this._increment( this.counter ) );
if ( !this.spinning || this._trigger( "spin", event, { value: value } ) !== false ) {
_increment: function( i ) {
var incremental = this.options.incremental;
return $.isFunction( incremental ) ?
Math.floor( i * i * i / 50000 - i * i / 500 + 17 * i / 200 + 1 );
var precision = this._precisionOf( this.options.step );
if ( this.options.min !== null ) {
precision = Math.max( precision, this._precisionOf( this.options.min ) );
_precisionOf: function( num ) {
var str = num.toString(),
decimal = str.indexOf( "." );
return decimal === -1 ? 0 : str.length - decimal - 1;
_adjustValue: function( value ) {
// Make sure we're at a valid step
// - find out where we are relative to the base (min or 0)
base = options.min !== null ? options.min : 0;
// - round to the nearest step
aboveMin = Math.round( aboveMin / options.step ) * options.step;
// - rounding is based on 0, so adjust back to our base
// Fix precision from bad JS floating point math
value = parseFloat( value.toFixed( this._precision() ) );
if ( options.max !== null && value > options.max ) {
if ( options.min !== null && value < options.min ) {
_stop: function( event ) {
clearTimeout( this.timer );
clearTimeout( this.mousewheelTimer );
this._trigger( "stop", event );
_setOption: function( key, value ) {
var prevValue, first, last;
if ( key === "culture" || key === "numberFormat" ) {
prevValue = this._parse( this.element.val() );
this.options[ key ] = value;
this.element.val( this._format( prevValue ) );
if ( key === "max" || key === "min" || key === "step" ) {
if ( typeof value === "string" ) {
value = this._parse( value );
first = this.buttons.first().find( ".ui-icon" );
this._removeClass( first, null, this.options.icons.up );
this._addClass( first, null, value.up );
last = this.buttons.last().find( ".ui-icon" );
this._removeClass( last, null, this.options.icons.down );
this._addClass( last, null, value.down );
this._super( key, value );
_setOptionDisabled: function( value ) {
this._toggleClass( this.uiSpinner, null, "ui-state-disabled", !!value );
this.element.prop( "disabled", !!value );
this.buttons.button( value ? "disable" : "enable" );
_setOptions: spinnerModifer( function( options ) {
_parse: function( val ) {
if ( typeof val === "string" && val !== "" ) {
val = window.Globalize && this.options.numberFormat ?
Globalize.parseFloat( val, 10, this.options.culture ) : +val;
return val === "" || isNaN( val ) ? null : val;
_format: function( value ) {
return window.Globalize && this.options.numberFormat ?
Globalize.format( value, this.options.numberFormat, this.options.culture ) :
"aria-valuemin": this.options.min,
"aria-valuemax": this.options.max,
// TODO: what should we do with values that can't be parsed?
"aria-valuenow": this._parse( this.element.val() )
var value = this.value();
// If value gets adjusted, it's invalid
return value === this._adjustValue( value );
// Update the value without triggering change
_value: function( value, allowAny ) {
parsed = this._parse( value );
parsed = this._adjustValue( parsed );
value = this._format( parsed );
this.element.val( value );
.prop( "disabled", false )
.removeAttr( "autocomplete role aria-valuemin aria-valuemax aria-valuenow" );