2 * jQuery UI Spinner 1.9.2
5 * Copyright 2012 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 // format the value, but don't constrain
59 this._value( this.element.val(), true );
62 this._on( this._events );
65 // turning off autocomplete prevents the browser from remembering the
66 // value when navigating through history, so we re-enable autocomplete
67 // if the page is unloaded before the widget is destroyed. #7790
68 this._on( this.window, {
69 beforeunload: function() {
70 this.element.removeAttr( "autocomplete" );
75 _getCreateOptions: function() {
77 element = this.element;
79 $.each( [ "min", "max", "step" ], function( i, option ) {
80 var value = element.attr( option );
81 if ( value !== undefined && value.length ) {
82 options[ option ] = value;
90 keydown: function( event ) {
91 if ( this._start( event ) && this._keydown( event ) ) {
92 event.preventDefault();
97 this.previous = this.element.val();
99 blur: function( event ) {
100 if ( this.cancelBlur ) {
101 delete this.cancelBlur;
106 if ( this.previous !== this.element.val() ) {
107 this._trigger( "change", event );
110 mousewheel: function( event, delta ) {
114 if ( !this.spinning && !this._start( event ) ) {
118 this._spin( (delta > 0 ? 1 : -1) * this.options.step, event );
119 clearTimeout( this.mousewheelTimer );
120 this.mousewheelTimer = this._delay(function() {
121 if ( this.spinning ) {
125 event.preventDefault();
127 "mousedown .ui-spinner-button": function( event ) {
130 // We never want the buttons to have focus; whenever the user is
131 // interacting with the spinner, the focus should be on the input.
132 // If the input is focused then this.previous is properly set from
133 // when the input first received focus. If the input is not focused
134 // then we need to set this.previous based on the value before spinning.
135 previous = this.element[0] === this.document[0].activeElement ?
136 this.previous : this.element.val();
137 function checkFocus() {
138 var isActive = this.element[0] === this.document[0].activeElement;
140 this.element.focus();
141 this.previous = previous;
143 // IE sets focus asynchronously, so we need to check if focus
144 // moved off of the input because the user clicked on the button.
145 this._delay(function() {
146 this.previous = previous;
151 // ensure focus is on (or stays on) the text field
152 event.preventDefault();
153 checkFocus.call( this );
156 // IE doesn't prevent moving focus even with event.preventDefault()
157 // so we set a flag to know when we should ignore the blur event
158 // and check (again) if focus moved off of the input.
159 this.cancelBlur = true;
160 this._delay(function() {
161 delete this.cancelBlur;
162 checkFocus.call( this );
165 if ( this._start( event ) === false ) {
169 this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
171 "mouseup .ui-spinner-button": "_stop",
172 "mouseenter .ui-spinner-button": function( event ) {
173 // button will add ui-state-active if mouse was down while mouseleave and kept down
174 if ( !$( event.currentTarget ).hasClass( "ui-state-active" ) ) {
178 if ( this._start( event ) === false ) {
181 this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
183 // TODO: do we really want to consider this a stop?
184 // shouldn't we just stop the repeater and wait until mouseup before
185 // we trigger the stop event?
186 "mouseleave .ui-spinner-button": "_stop"
190 var uiSpinner = this.uiSpinner = this.element
191 .addClass( "ui-spinner-input" )
192 .attr( "autocomplete", "off" )
193 .wrap( this._uiSpinnerHtml() )
196 .append( this._buttonHtml() );
198 this.element.attr( "role", "spinbutton" );
201 this.buttons = uiSpinner.find( ".ui-spinner-button" )
202 .attr( "tabIndex", -1 )
204 .removeClass( "ui-corner-all" );
206 // IE 6 doesn't understand height: 50% for the buttons
207 // unless the wrapper has an explicit height
208 if ( this.buttons.height() > Math.ceil( uiSpinner.height() * 0.5 ) &&
209 uiSpinner.height() > 0 ) {
210 uiSpinner.height( uiSpinner.height() );
213 // disable spinner if element was already disabled
214 if ( this.options.disabled ) {
219 _keydown: function( event ) {
220 var options = this.options,
221 keyCode = $.ui.keyCode;
223 switch ( event.keyCode ) {
225 this._repeat( null, 1, event );
228 this._repeat( null, -1, event );
230 case keyCode.PAGE_UP:
231 this._repeat( null, options.page, event );
233 case keyCode.PAGE_DOWN:
234 this._repeat( null, -options.page, event );
241 _uiSpinnerHtml: function() {
242 return "<span class='ui-spinner ui-widget ui-widget-content ui-corner-all'></span>";
245 _buttonHtml: function() {
247 "<a class='ui-spinner-button ui-spinner-up ui-corner-tr'>" +
248 "<span class='ui-icon " + this.options.icons.up + "'>▲</span>" +
250 "<a class='ui-spinner-button ui-spinner-down ui-corner-br'>" +
251 "<span class='ui-icon " + this.options.icons.down + "'>▼</span>" +
255 _start: function( event ) {
256 if ( !this.spinning && this._trigger( "start", event ) === false ) {
260 if ( !this.counter ) {
263 this.spinning = true;
267 _repeat: function( i, steps, event ) {
270 clearTimeout( this.timer );
271 this.timer = this._delay(function() {
272 this._repeat( 40, steps, event );
275 this._spin( steps * this.options.step, event );
278 _spin: function( step, event ) {
279 var value = this.value() || 0;
281 if ( !this.counter ) {
285 value = this._adjustValue( value + step * this._increment( this.counter ) );
287 if ( !this.spinning || this._trigger( "spin", event, { value: value } ) !== false) {
288 this._value( value );
293 _increment: function( i ) {
294 var incremental = this.options.incremental;
297 return $.isFunction( incremental ) ?
299 Math.floor( i*i*i/50000 - i*i/500 + 17*i/200 + 1 );
305 _precision: function() {
306 var precision = this._precisionOf( this.options.step );
307 if ( this.options.min !== null ) {
308 precision = Math.max( precision, this._precisionOf( this.options.min ) );
313 _precisionOf: function( num ) {
314 var str = num.toString(),
315 decimal = str.indexOf( "." );
316 return decimal === -1 ? 0 : str.length - decimal - 1;
319 _adjustValue: function( value ) {
321 options = this.options;
323 // make sure we're at a valid step
324 // - find out where we are relative to the base (min or 0)
325 base = options.min !== null ? options.min : 0;
326 aboveMin = value - base;
327 // - round to the nearest step
328 aboveMin = Math.round(aboveMin / options.step) * options.step;
329 // - rounding is based on 0, so adjust back to our base
330 value = base + aboveMin;
332 // fix precision from bad JS floating point math
333 value = parseFloat( value.toFixed( this._precision() ) );
336 if ( options.max !== null && value > options.max) {
339 if ( options.min !== null && value < options.min ) {
346 _stop: function( event ) {
347 if ( !this.spinning ) {
351 clearTimeout( this.timer );
352 clearTimeout( this.mousewheelTimer );
354 this.spinning = false;
355 this._trigger( "stop", event );
358 _setOption: function( key, value ) {
359 if ( key === "culture" || key === "numberFormat" ) {
360 var prevValue = this._parse( this.element.val() );
361 this.options[ key ] = value;
362 this.element.val( this._format( prevValue ) );
366 if ( key === "max" || key === "min" || key === "step" ) {
367 if ( typeof value === "string" ) {
368 value = this._parse( value );
372 this._super( key, value );
374 if ( key === "disabled" ) {
376 this.element.prop( "disabled", true );
377 this.buttons.button( "disable" );
379 this.element.prop( "disabled", false );
380 this.buttons.button( "enable" );
385 _setOptions: modifier(function( options ) {
386 this._super( options );
387 this._value( this.element.val() );
390 _parse: function( val ) {
391 if ( typeof val === "string" && val !== "" ) {
392 val = window.Globalize && this.options.numberFormat ?
393 Globalize.parseFloat( val, 10, this.options.culture ) : +val;
395 return val === "" || isNaN( val ) ? null : val;
398 _format: function( value ) {
399 if ( value === "" ) {
402 return window.Globalize && this.options.numberFormat ?
403 Globalize.format( value, this.options.numberFormat, this.options.culture ) :
407 _refresh: function() {
409 "aria-valuemin": this.options.min,
410 "aria-valuemax": this.options.max,
411 // TODO: what should we do with values that can't be parsed?
412 "aria-valuenow": this._parse( this.element.val() )
416 // update the value without triggering change
417 _value: function( value, allowAny ) {
419 if ( value !== "" ) {
420 parsed = this._parse( value );
421 if ( parsed !== null ) {
423 parsed = this._adjustValue( parsed );
425 value = this._format( parsed );
428 this.element.val( value );
432 _destroy: function() {
434 .removeClass( "ui-spinner-input" )
435 .prop( "disabled", false )
436 .removeAttr( "autocomplete" )
437 .removeAttr( "role" )
438 .removeAttr( "aria-valuemin" )
439 .removeAttr( "aria-valuemax" )
440 .removeAttr( "aria-valuenow" );
441 this.uiSpinner.replaceWith( this.element );
444 stepUp: modifier(function( steps ) {
445 this._stepUp( steps );
447 _stepUp: function( steps ) {
448 this._spin( (steps || 1) * this.options.step );
451 stepDown: modifier(function( steps ) {
452 this._stepDown( steps );
454 _stepDown: function( steps ) {
455 this._spin( (steps || 1) * -this.options.step );
458 pageUp: modifier(function( pages ) {
459 this._stepUp( (pages || 1) * this.options.page );
462 pageDown: modifier(function( pages ) {
463 this._stepDown( (pages || 1) * this.options.page );
466 value: function( newVal ) {
467 if ( !arguments.length ) {
468 return this._parse( this.element.val() );
470 modifier( this._value ).call( this, newVal );
474 return this.uiSpinner;