2 * jQuery UI Spinner 1.10.4
5 * Copyright 2014 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
9 * http://api.jqueryui.com/spinner/
18 function modifier( fn ) {
20 var previous = this.element.val();
21 fn.apply( this, arguments );
23 if ( previous !== this.element.val() ) {
24 this._trigger( "change" );
29 $.widget( "ui.spinner", {
31 defaultElement: "<input>",
32 widgetEventPrefix: "spin",
36 down: "ui-icon-triangle-1-s",
37 up: "ui-icon-triangle-1-n"
53 // handle string values that need to be parsed
54 this._setOption( "max", this.options.max );
55 this._setOption( "min", this.options.min );
56 this._setOption( "step", this.options.step );
58 // Only format if there is a value, prevents the field from being marked
59 // as invalid in Firefox, see #9573.
60 if ( this.value() !== "" ) {
61 // Format the value, but don't constrain.
62 this._value( this.element.val(), true );
66 this._on( this._events );
69 // turning off autocomplete prevents the browser from remembering the
70 // value when navigating through history, so we re-enable autocomplete
71 // if the page is unloaded before the widget is destroyed. #7790
72 this._on( this.window, {
73 beforeunload: function() {
74 this.element.removeAttr( "autocomplete" );
79 _getCreateOptions: function() {
81 element = this.element;
83 $.each( [ "min", "max", "step" ], function( i, option ) {
84 var value = element.attr( option );
85 if ( value !== undefined && value.length ) {
86 options[ option ] = value;
94 keydown: function( event ) {
95 if ( this._start( event ) && this._keydown( event ) ) {
96 event.preventDefault();
101 this.previous = this.element.val();
103 blur: function( event ) {
104 if ( this.cancelBlur ) {
105 delete this.cancelBlur;
111 if ( this.previous !== this.element.val() ) {
112 this._trigger( "change", event );
115 mousewheel: function( event, delta ) {
119 if ( !this.spinning && !this._start( event ) ) {
123 this._spin( (delta > 0 ? 1 : -1) * this.options.step, event );
124 clearTimeout( this.mousewheelTimer );
125 this.mousewheelTimer = this._delay(function() {
126 if ( this.spinning ) {
130 event.preventDefault();
132 "mousedown .ui-spinner-button": function( event ) {
135 // We never want the buttons to have focus; whenever the user is
136 // interacting with the spinner, the focus should be on the input.
137 // If the input is focused then this.previous is properly set from
138 // when the input first received focus. If the input is not focused
139 // then we need to set this.previous based on the value before spinning.
140 previous = this.element[0] === this.document[0].activeElement ?
141 this.previous : this.element.val();
142 function checkFocus() {
143 var isActive = this.element[0] === this.document[0].activeElement;
145 this.element.focus();
146 this.previous = previous;
148 // IE sets focus asynchronously, so we need to check if focus
149 // moved off of the input because the user clicked on the button.
150 this._delay(function() {
151 this.previous = previous;
156 // ensure focus is on (or stays on) the text field
157 event.preventDefault();
158 checkFocus.call( this );
161 // IE doesn't prevent moving focus even with event.preventDefault()
162 // so we set a flag to know when we should ignore the blur event
163 // and check (again) if focus moved off of the input.
164 this.cancelBlur = true;
165 this._delay(function() {
166 delete this.cancelBlur;
167 checkFocus.call( this );
170 if ( this._start( event ) === false ) {
174 this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
176 "mouseup .ui-spinner-button": "_stop",
177 "mouseenter .ui-spinner-button": function( event ) {
178 // button will add ui-state-active if mouse was down while mouseleave and kept down
179 if ( !$( event.currentTarget ).hasClass( "ui-state-active" ) ) {
183 if ( this._start( event ) === false ) {
186 this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
188 // TODO: do we really want to consider this a stop?
189 // shouldn't we just stop the repeater and wait until mouseup before
190 // we trigger the stop event?
191 "mouseleave .ui-spinner-button": "_stop"
195 var uiSpinner = this.uiSpinner = this.element
196 .addClass( "ui-spinner-input" )
197 .attr( "autocomplete", "off" )
198 .wrap( this._uiSpinnerHtml() )
201 .append( this._buttonHtml() );
203 this.element.attr( "role", "spinbutton" );
206 this.buttons = uiSpinner.find( ".ui-spinner-button" )
207 .attr( "tabIndex", -1 )
209 .removeClass( "ui-corner-all" );
211 // IE 6 doesn't understand height: 50% for the buttons
212 // unless the wrapper has an explicit height
213 if ( this.buttons.height() > Math.ceil( uiSpinner.height() * 0.5 ) &&
214 uiSpinner.height() > 0 ) {
215 uiSpinner.height( uiSpinner.height() );
218 // disable spinner if element was already disabled
219 if ( this.options.disabled ) {
224 _keydown: function( event ) {
225 var options = this.options,
226 keyCode = $.ui.keyCode;
228 switch ( event.keyCode ) {
230 this._repeat( null, 1, event );
233 this._repeat( null, -1, event );
235 case keyCode.PAGE_UP:
236 this._repeat( null, options.page, event );
238 case keyCode.PAGE_DOWN:
239 this._repeat( null, -options.page, event );
246 _uiSpinnerHtml: function() {
247 return "<span class='ui-spinner ui-widget ui-widget-content ui-corner-all'></span>";
250 _buttonHtml: function() {
252 "<a class='ui-spinner-button ui-spinner-up ui-corner-tr'>" +
253 "<span class='ui-icon " + this.options.icons.up + "'>▲</span>" +
255 "<a class='ui-spinner-button ui-spinner-down ui-corner-br'>" +
256 "<span class='ui-icon " + this.options.icons.down + "'>▼</span>" +
260 _start: function( event ) {
261 if ( !this.spinning && this._trigger( "start", event ) === false ) {
265 if ( !this.counter ) {
268 this.spinning = true;
272 _repeat: function( i, steps, event ) {
275 clearTimeout( this.timer );
276 this.timer = this._delay(function() {
277 this._repeat( 40, steps, event );
280 this._spin( steps * this.options.step, event );
283 _spin: function( step, event ) {
284 var value = this.value() || 0;
286 if ( !this.counter ) {
290 value = this._adjustValue( value + step * this._increment( this.counter ) );
292 if ( !this.spinning || this._trigger( "spin", event, { value: value } ) !== false) {
293 this._value( value );
298 _increment: function( i ) {
299 var incremental = this.options.incremental;
302 return $.isFunction( incremental ) ?
304 Math.floor( i*i*i/50000 - i*i/500 + 17*i/200 + 1 );
310 _precision: function() {
311 var precision = this._precisionOf( this.options.step );
312 if ( this.options.min !== null ) {
313 precision = Math.max( precision, this._precisionOf( this.options.min ) );
318 _precisionOf: function( num ) {
319 var str = num.toString(),
320 decimal = str.indexOf( "." );
321 return decimal === -1 ? 0 : str.length - decimal - 1;
324 _adjustValue: function( value ) {
326 options = this.options;
328 // make sure we're at a valid step
329 // - find out where we are relative to the base (min or 0)
330 base = options.min !== null ? options.min : 0;
331 aboveMin = value - base;
332 // - round to the nearest step
333 aboveMin = Math.round(aboveMin / options.step) * options.step;
334 // - rounding is based on 0, so adjust back to our base
335 value = base + aboveMin;
337 // fix precision from bad JS floating point math
338 value = parseFloat( value.toFixed( this._precision() ) );
341 if ( options.max !== null && value > options.max) {
344 if ( options.min !== null && value < options.min ) {
351 _stop: function( event ) {
352 if ( !this.spinning ) {
356 clearTimeout( this.timer );
357 clearTimeout( this.mousewheelTimer );
359 this.spinning = false;
360 this._trigger( "stop", event );
363 _setOption: function( key, value ) {
364 if ( key === "culture" || key === "numberFormat" ) {
365 var prevValue = this._parse( this.element.val() );
366 this.options[ key ] = value;
367 this.element.val( this._format( prevValue ) );
371 if ( key === "max" || key === "min" || key === "step" ) {
372 if ( typeof value === "string" ) {
373 value = this._parse( value );
376 if ( key === "icons" ) {
377 this.buttons.first().find( ".ui-icon" )
378 .removeClass( this.options.icons.up )
379 .addClass( value.up );
380 this.buttons.last().find( ".ui-icon" )
381 .removeClass( this.options.icons.down )
382 .addClass( value.down );
385 this._super( key, value );
387 if ( key === "disabled" ) {
389 this.element.prop( "disabled", true );
390 this.buttons.button( "disable" );
392 this.element.prop( "disabled", false );
393 this.buttons.button( "enable" );
398 _setOptions: modifier(function( options ) {
399 this._super( options );
400 this._value( this.element.val() );
403 _parse: function( val ) {
404 if ( typeof val === "string" && val !== "" ) {
405 val = window.Globalize && this.options.numberFormat ?
406 Globalize.parseFloat( val, 10, this.options.culture ) : +val;
408 return val === "" || isNaN( val ) ? null : val;
411 _format: function( value ) {
412 if ( value === "" ) {
415 return window.Globalize && this.options.numberFormat ?
416 Globalize.format( value, this.options.numberFormat, this.options.culture ) :
420 _refresh: function() {
422 "aria-valuemin": this.options.min,
423 "aria-valuemax": this.options.max,
424 // TODO: what should we do with values that can't be parsed?
425 "aria-valuenow": this._parse( this.element.val() )
429 // update the value without triggering change
430 _value: function( value, allowAny ) {
432 if ( value !== "" ) {
433 parsed = this._parse( value );
434 if ( parsed !== null ) {
436 parsed = this._adjustValue( parsed );
438 value = this._format( parsed );
441 this.element.val( value );
445 _destroy: function() {
447 .removeClass( "ui-spinner-input" )
448 .prop( "disabled", false )
449 .removeAttr( "autocomplete" )
450 .removeAttr( "role" )
451 .removeAttr( "aria-valuemin" )
452 .removeAttr( "aria-valuemax" )
453 .removeAttr( "aria-valuenow" );
454 this.uiSpinner.replaceWith( this.element );
457 stepUp: modifier(function( steps ) {
458 this._stepUp( steps );
460 _stepUp: function( steps ) {
461 if ( this._start() ) {
462 this._spin( (steps || 1) * this.options.step );
467 stepDown: modifier(function( steps ) {
468 this._stepDown( steps );
470 _stepDown: function( steps ) {
471 if ( this._start() ) {
472 this._spin( (steps || 1) * -this.options.step );
477 pageUp: modifier(function( pages ) {
478 this._stepUp( (pages || 1) * this.options.page );
481 pageDown: modifier(function( pages ) {
482 this._stepDown( (pages || 1) * this.options.page );
485 value: function( newVal ) {
486 if ( !arguments.length ) {
487 return this._parse( this.element.val() );
489 modifier( this._value ).call( this, newVal );
493 return this.uiSpinner;