2 * jQuery UI Spinner 1.12.1
5 * Copyright jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
12 //>>description: Displays buttons to easily input numbers via the keyboard or mouse.
13 //>>docs: http://api.jqueryui.com/spinner/
14 //>>demos: http://jqueryui.com/spinner/
15 //>>css.structure: ../../themes/base/core.css
16 //>>css.structure: ../../themes/base/spinner.css
17 //>>css.theme: ../../themes/base/theme.css
19 ( function( factory ) {
20 if ( typeof define === "function" && define.amd ) {
22 // AMD. Register as an anonymous module.
28 "../safe-active-element",
38 function spinnerModifer( fn ) {
40 var previous = this.element.val();
41 fn.apply( this, arguments );
43 if ( previous !== this.element.val() ) {
44 this._trigger( "change" );
49 $.widget( "ui.spinner", {
51 defaultElement: "<input>",
52 widgetEventPrefix: "spin",
55 "ui-spinner": "ui-corner-all",
56 "ui-spinner-down": "ui-corner-br",
57 "ui-spinner-up": "ui-corner-tr"
61 down: "ui-icon-triangle-1-s",
62 up: "ui-icon-triangle-1-n"
79 // handle string values that need to be parsed
80 this._setOption( "max", this.options.max );
81 this._setOption( "min", this.options.min );
82 this._setOption( "step", this.options.step );
84 // Only format if there is a value, prevents the field from being marked
85 // as invalid in Firefox, see #9573.
86 if ( this.value() !== "" ) {
88 // Format the value, but don't constrain.
89 this._value( this.element.val(), true );
93 this._on( this._events );
96 // Turning off autocomplete prevents the browser from remembering the
97 // value when navigating through history, so we re-enable autocomplete
98 // if the page is unloaded before the widget is destroyed. #7790
99 this._on( this.window, {
100 beforeunload: function() {
101 this.element.removeAttr( "autocomplete" );
106 _getCreateOptions: function() {
107 var options = this._super();
108 var element = this.element;
110 $.each( [ "min", "max", "step" ], function( i, option ) {
111 var value = element.attr( option );
112 if ( value != null && value.length ) {
113 options[ option ] = value;
121 keydown: function( event ) {
122 if ( this._start( event ) && this._keydown( event ) ) {
123 event.preventDefault();
128 this.previous = this.element.val();
130 blur: function( event ) {
131 if ( this.cancelBlur ) {
132 delete this.cancelBlur;
138 if ( this.previous !== this.element.val() ) {
139 this._trigger( "change", event );
142 mousewheel: function( event, delta ) {
146 if ( !this.spinning && !this._start( event ) ) {
150 this._spin( ( delta > 0 ? 1 : -1 ) * this.options.step, event );
151 clearTimeout( this.mousewheelTimer );
152 this.mousewheelTimer = this._delay( function() {
153 if ( this.spinning ) {
157 event.preventDefault();
159 "mousedown .ui-spinner-button": function( event ) {
162 // We never want the buttons to have focus; whenever the user is
163 // interacting with the spinner, the focus should be on the input.
164 // If the input is focused then this.previous is properly set from
165 // when the input first received focus. If the input is not focused
166 // then we need to set this.previous based on the value before spinning.
167 previous = this.element[ 0 ] === $.ui.safeActiveElement( this.document[ 0 ] ) ?
168 this.previous : this.element.val();
169 function checkFocus() {
170 var isActive = this.element[ 0 ] === $.ui.safeActiveElement( this.document[ 0 ] );
172 this.element.trigger( "focus" );
173 this.previous = previous;
176 // IE sets focus asynchronously, so we need to check if focus
177 // moved off of the input because the user clicked on the button.
178 this._delay( function() {
179 this.previous = previous;
184 // Ensure focus is on (or stays on) the text field
185 event.preventDefault();
186 checkFocus.call( this );
189 // IE doesn't prevent moving focus even with event.preventDefault()
190 // so we set a flag to know when we should ignore the blur event
191 // and check (again) if focus moved off of the input.
192 this.cancelBlur = true;
193 this._delay( function() {
194 delete this.cancelBlur;
195 checkFocus.call( this );
198 if ( this._start( event ) === false ) {
202 this._repeat( null, $( event.currentTarget )
203 .hasClass( "ui-spinner-up" ) ? 1 : -1, event );
205 "mouseup .ui-spinner-button": "_stop",
206 "mouseenter .ui-spinner-button": function( event ) {
208 // button will add ui-state-active if mouse was down while mouseleave and kept down
209 if ( !$( event.currentTarget ).hasClass( "ui-state-active" ) ) {
213 if ( this._start( event ) === false ) {
216 this._repeat( null, $( event.currentTarget )
217 .hasClass( "ui-spinner-up" ) ? 1 : -1, event );
220 // TODO: do we really want to consider this a stop?
221 // shouldn't we just stop the repeater and wait until mouseup before
222 // we trigger the stop event?
223 "mouseleave .ui-spinner-button": "_stop"
226 // Support mobile enhanced option and make backcompat more sane
227 _enhance: function() {
228 this.uiSpinner = this.element
229 .attr( "autocomplete", "off" )
242 this._addClass( this.uiSpinner, "ui-spinner", "ui-widget ui-widget-content" );
243 this._addClass( "ui-spinner-input" );
245 this.element.attr( "role", "spinbutton" );
248 this.buttons = this.uiSpinner.children( "a" )
249 .attr( "tabIndex", -1 )
250 .attr( "aria-hidden", true )
257 // TODO: Right now button does not support classes this is already updated in button PR
258 this._removeClass( this.buttons, "ui-corner-all" );
260 this._addClass( this.buttons.first(), "ui-spinner-button ui-spinner-up" );
261 this._addClass( this.buttons.last(), "ui-spinner-button ui-spinner-down" );
262 this.buttons.first().button( {
263 "icon": this.options.icons.up,
266 this.buttons.last().button( {
267 "icon": this.options.icons.down,
271 // IE 6 doesn't understand height: 50% for the buttons
272 // unless the wrapper has an explicit height
273 if ( this.buttons.height() > Math.ceil( this.uiSpinner.height() * 0.5 ) &&
274 this.uiSpinner.height() > 0 ) {
275 this.uiSpinner.height( this.uiSpinner.height() );
279 _keydown: function( event ) {
280 var options = this.options,
281 keyCode = $.ui.keyCode;
283 switch ( event.keyCode ) {
285 this._repeat( null, 1, event );
288 this._repeat( null, -1, event );
290 case keyCode.PAGE_UP:
291 this._repeat( null, options.page, event );
293 case keyCode.PAGE_DOWN:
294 this._repeat( null, -options.page, event );
301 _start: function( event ) {
302 if ( !this.spinning && this._trigger( "start", event ) === false ) {
306 if ( !this.counter ) {
309 this.spinning = true;
313 _repeat: function( i, steps, event ) {
316 clearTimeout( this.timer );
317 this.timer = this._delay( function() {
318 this._repeat( 40, steps, event );
321 this._spin( steps * this.options.step, event );
324 _spin: function( step, event ) {
325 var value = this.value() || 0;
327 if ( !this.counter ) {
331 value = this._adjustValue( value + step * this._increment( this.counter ) );
333 if ( !this.spinning || this._trigger( "spin", event, { value: value } ) !== false ) {
334 this._value( value );
339 _increment: function( i ) {
340 var incremental = this.options.incremental;
343 return $.isFunction( incremental ) ?
345 Math.floor( i * i * i / 50000 - i * i / 500 + 17 * i / 200 + 1 );
351 _precision: function() {
352 var precision = this._precisionOf( this.options.step );
353 if ( this.options.min !== null ) {
354 precision = Math.max( precision, this._precisionOf( this.options.min ) );
359 _precisionOf: function( num ) {
360 var str = num.toString(),
361 decimal = str.indexOf( "." );
362 return decimal === -1 ? 0 : str.length - decimal - 1;
365 _adjustValue: function( value ) {
367 options = this.options;
369 // Make sure we're at a valid step
370 // - find out where we are relative to the base (min or 0)
371 base = options.min !== null ? options.min : 0;
372 aboveMin = value - base;
374 // - round to the nearest step
375 aboveMin = Math.round( aboveMin / options.step ) * options.step;
377 // - rounding is based on 0, so adjust back to our base
378 value = base + aboveMin;
380 // Fix precision from bad JS floating point math
381 value = parseFloat( value.toFixed( this._precision() ) );
384 if ( options.max !== null && value > options.max ) {
387 if ( options.min !== null && value < options.min ) {
394 _stop: function( event ) {
395 if ( !this.spinning ) {
399 clearTimeout( this.timer );
400 clearTimeout( this.mousewheelTimer );
402 this.spinning = false;
403 this._trigger( "stop", event );
406 _setOption: function( key, value ) {
407 var prevValue, first, last;
409 if ( key === "culture" || key === "numberFormat" ) {
410 prevValue = this._parse( this.element.val() );
411 this.options[ key ] = value;
412 this.element.val( this._format( prevValue ) );
416 if ( key === "max" || key === "min" || key === "step" ) {
417 if ( typeof value === "string" ) {
418 value = this._parse( value );
421 if ( key === "icons" ) {
422 first = this.buttons.first().find( ".ui-icon" );
423 this._removeClass( first, null, this.options.icons.up );
424 this._addClass( first, null, value.up );
425 last = this.buttons.last().find( ".ui-icon" );
426 this._removeClass( last, null, this.options.icons.down );
427 this._addClass( last, null, value.down );
430 this._super( key, value );
433 _setOptionDisabled: function( value ) {
434 this._super( value );
436 this._toggleClass( this.uiSpinner, null, "ui-state-disabled", !!value );
437 this.element.prop( "disabled", !!value );
438 this.buttons.button( value ? "disable" : "enable" );
441 _setOptions: spinnerModifer( function( options ) {
442 this._super( options );
445 _parse: function( val ) {
446 if ( typeof val === "string" && val !== "" ) {
447 val = window.Globalize && this.options.numberFormat ?
448 Globalize.parseFloat( val, 10, this.options.culture ) : +val;
450 return val === "" || isNaN( val ) ? null : val;
453 _format: function( value ) {
454 if ( value === "" ) {
457 return window.Globalize && this.options.numberFormat ?
458 Globalize.format( value, this.options.numberFormat, this.options.culture ) :
462 _refresh: function() {
464 "aria-valuemin": this.options.min,
465 "aria-valuemax": this.options.max,
467 // TODO: what should we do with values that can't be parsed?
468 "aria-valuenow": this._parse( this.element.val() )
472 isValid: function() {
473 var value = this.value();
476 if ( value === null ) {
480 // If value gets adjusted, it's invalid
481 return value === this._adjustValue( value );
484 // Update the value without triggering change
485 _value: function( value, allowAny ) {
487 if ( value !== "" ) {
488 parsed = this._parse( value );
489 if ( parsed !== null ) {
491 parsed = this._adjustValue( parsed );
493 value = this._format( parsed );
496 this.element.val( value );
500 _destroy: function() {
502 .prop( "disabled", false )
503 .removeAttr( "autocomplete role aria-valuemin aria-valuemax aria-valuenow" );
505 this.uiSpinner.replaceWith( this.element );
508 stepUp: spinnerModifer( function( steps ) {
509 this._stepUp( steps );
511 _stepUp: function( steps ) {
512 if ( this._start() ) {
513 this._spin( ( steps || 1 ) * this.options.step );
518 stepDown: spinnerModifer( function( steps ) {
519 this._stepDown( steps );
521 _stepDown: function( steps ) {
522 if ( this._start() ) {
523 this._spin( ( steps || 1 ) * -this.options.step );
528 pageUp: spinnerModifer( function( pages ) {
529 this._stepUp( ( pages || 1 ) * this.options.page );
532 pageDown: spinnerModifer( function( pages ) {
533 this._stepDown( ( pages || 1 ) * this.options.page );
536 value: function( newVal ) {
537 if ( !arguments.length ) {
538 return this._parse( this.element.val() );
540 spinnerModifer( this._value ).call( this, newVal );
544 return this.uiSpinner;
549 // TODO: switch return back to widget declaration at top of file when this is removed
550 if ( $.uiBackCompat !== false ) {
552 // Backcompat for spinner html extension points
553 $.widget( "ui.spinner", $.ui.spinner, {
554 _enhance: function() {
555 this.uiSpinner = this.element
556 .attr( "autocomplete", "off" )
557 .wrap( this._uiSpinnerHtml() )
561 .append( this._buttonHtml() );
563 _uiSpinnerHtml: function() {
567 _buttonHtml: function() {
568 return "<a></a><a></a>";