2010-11-30 12:43:43 +00:00
|
|
|
/*
|
|
|
|
* jQuery UI Spinner @VERSION
|
|
|
|
*
|
2011-01-17 14:13:18 +00:00
|
|
|
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
|
2010-11-30 12:43:43 +00:00
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
* http://jquery.org/license
|
|
|
|
*
|
|
|
|
* http://docs.jquery.com/UI/Spinner
|
|
|
|
*
|
|
|
|
* Depends:
|
|
|
|
* jquery.ui.core.js
|
|
|
|
* jquery.ui.widget.js
|
|
|
|
*/
|
2011-05-18 01:07:44 +00:00
|
|
|
(function( $ ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
|
2011-05-18 01:07:44 +00:00
|
|
|
$.widget( "ui.spinner", {
|
2011-01-23 22:55:53 +00:00
|
|
|
defaultElement: "<input>",
|
2011-05-18 01:07:44 +00:00
|
|
|
widgetEventPrefix: "spin",
|
2010-11-30 12:43:43 +00:00
|
|
|
options: {
|
|
|
|
incremental: true,
|
|
|
|
max: null,
|
|
|
|
min: null,
|
|
|
|
numberformat: null,
|
2010-12-01 22:28:05 +00:00
|
|
|
page: 10,
|
2010-11-30 12:43:43 +00:00
|
|
|
step: null,
|
|
|
|
value: null
|
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
_create: function() {
|
|
|
|
this._draw();
|
|
|
|
this._markupOptions();
|
|
|
|
this._mousewheel();
|
|
|
|
this._aria();
|
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
_markupOptions: function() {
|
2011-05-18 01:07:44 +00:00
|
|
|
var that = this;
|
2010-11-30 12:43:43 +00:00
|
|
|
$.each({
|
|
|
|
min: -Number.MAX_VALUE,
|
|
|
|
max: Number.MAX_VALUE,
|
|
|
|
step: 1
|
2011-05-18 01:07:44 +00:00
|
|
|
}, function( attr, defaultValue ) {
|
|
|
|
if ( that.options[ attr ] === null ) {
|
|
|
|
var value = that.element.attr( attr );
|
|
|
|
that.options[ attr ] = typeof value === "string" && value.length > 0 ?
|
|
|
|
that._parse( value ) :
|
|
|
|
defaultValue;
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
|
|
|
});
|
2011-05-18 01:07:44 +00:00
|
|
|
this.value( this.options.value !== null ? this.options.value : this.element.val() || 0 );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
_draw: function() {
|
|
|
|
var self = this,
|
|
|
|
options = self.options;
|
|
|
|
|
|
|
|
var uiSpinner = this.uiSpinner = self.element
|
2011-05-18 01:07:44 +00:00
|
|
|
.addClass( "ui-spinner-input" )
|
|
|
|
.attr( "autocomplete", "off" )
|
|
|
|
.wrap( self._uiSpinnerHtml() )
|
2010-11-30 12:43:43 +00:00
|
|
|
.parent()
|
|
|
|
// add buttons
|
2011-05-18 01:07:44 +00:00
|
|
|
.append( self._buttonHtml() )
|
2010-11-30 12:43:43 +00:00
|
|
|
// add behaviours
|
2011-05-18 01:07:44 +00:00
|
|
|
// TODO: user ._hoverable
|
2010-11-30 12:43:43 +00:00
|
|
|
.hover(function() {
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( !options.disabled ) {
|
|
|
|
$( this ).addClass( "ui-state-hover" );
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
|
|
|
self.hovered = true;
|
|
|
|
}, function() {
|
2011-05-18 01:07:44 +00:00
|
|
|
$( this ).removeClass( "ui-state-hover" );
|
2010-11-30 12:43:43 +00:00
|
|
|
self.hovered = false;
|
|
|
|
});
|
|
|
|
|
2011-05-18 01:07:44 +00:00
|
|
|
// TODO: use ._bind()
|
2010-11-30 12:43:43 +00:00
|
|
|
this.element
|
2011-01-15 13:28:13 +00:00
|
|
|
.attr( "role", "spinbutton" )
|
2011-05-18 01:07:44 +00:00
|
|
|
.bind( "keydown.spinner", function( event ) {
|
|
|
|
if ( options.disabled ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( self._start( event ) ) {
|
|
|
|
return self._keydown( event );
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
})
|
2011-05-18 01:07:44 +00:00
|
|
|
.bind( "keyup.spinner", function( event ) {
|
|
|
|
if ( options.disabled ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( self.spinning ) {
|
|
|
|
self._stop( event );
|
|
|
|
self._change( event );
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
|
|
|
})
|
2011-05-18 01:07:44 +00:00
|
|
|
.bind( "focus.spinner", function() {
|
|
|
|
uiSpinner.addClass( "ui-state-active" );
|
2010-11-30 12:43:43 +00:00
|
|
|
self.focused = true;
|
|
|
|
})
|
2011-05-18 01:07:44 +00:00
|
|
|
.bind( "blur.spinner", function( event ) {
|
|
|
|
self.value( self.element.val() );
|
|
|
|
if ( !self.hovered ) {
|
|
|
|
uiSpinner.removeClass( "ui-state-active" );
|
|
|
|
}
|
2010-11-30 12:43:43 +00:00
|
|
|
self.focused = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// button bindings
|
2011-05-18 01:07:44 +00:00
|
|
|
this.buttons = uiSpinner.find( ".ui-spinner-button" )
|
|
|
|
.attr( "tabIndex", -1 )
|
2010-11-30 12:43:43 +00:00
|
|
|
.button()
|
2011-05-18 01:07:44 +00:00
|
|
|
.removeClass( "ui-corner-all" )
|
|
|
|
.bind( "mousedown", function( event ) {
|
|
|
|
if ( options.disabled ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( self._start( event ) === false ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
self._repeat( null, $( this ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
|
2010-11-30 12:43:43 +00:00
|
|
|
})
|
2011-05-18 01:07:44 +00:00
|
|
|
.bind( "mouseup", function( event ) {
|
|
|
|
if ( options.disabled ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( self.spinning ) {
|
|
|
|
self._stop( event );
|
|
|
|
self._change( event );
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
|
|
|
})
|
2011-05-18 01:07:44 +00:00
|
|
|
.bind( "mouseenter", function() {
|
|
|
|
if ( self.options.disabled ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// button will add ui-state-active if mouse was down while mouseleave and kept down
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( $( this ).hasClass( "ui-state-active" ) ) {
|
|
|
|
if ( self._start( event ) === false ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
self._repeat( null, $( this ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
|
|
|
})
|
2011-05-18 01:07:44 +00:00
|
|
|
.bind( "mouseleave", function() {
|
|
|
|
if ( self.spinning ) {
|
|
|
|
self._stop( event );
|
|
|
|
self._change( event );
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
|
|
|
});
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
// disable spinner if element was already disabled
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( options.disabled ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
this.disable();
|
|
|
|
}
|
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
_keydown: function( event ) {
|
|
|
|
var options = this.options,
|
|
|
|
keyCode = $.ui.keyCode;
|
|
|
|
|
|
|
|
switch ( event.keyCode ) {
|
|
|
|
case keyCode.UP:
|
|
|
|
this._repeat( null, 1, event );
|
2010-11-30 12:43:43 +00:00
|
|
|
return false;
|
2011-05-18 01:07:44 +00:00
|
|
|
case keyCode.DOWN:
|
|
|
|
this._repeat( null, -1, event );
|
2010-11-30 12:43:43 +00:00
|
|
|
return false;
|
2011-05-18 01:07:44 +00:00
|
|
|
case keyCode.PAGE_UP:
|
|
|
|
this._repeat( null, options.page, event );
|
2010-11-30 12:43:43 +00:00
|
|
|
return false;
|
2011-05-18 01:07:44 +00:00
|
|
|
case keyCode.PAGE_DOWN:
|
|
|
|
this._repeat( null, -options.page, event );
|
2010-11-30 12:43:43 +00:00
|
|
|
return false;
|
2011-05-18 01:07:44 +00:00
|
|
|
case keyCode.ENTER:
|
|
|
|
this.value( this.element.val() );
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
return true;
|
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
_mousewheel: function() {
|
|
|
|
// need the delta normalization that mousewheel plugin provides
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( !$.fn.mousewheel ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var self = this;
|
2011-05-18 01:07:44 +00:00
|
|
|
this.element.bind( "mousewheel.spinner", function( event, delta ) {
|
|
|
|
if ( self.options.disabled || !delta ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( !self.spinning && !self._start( event ) ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
self._spin( (delta > 0 ? 1 : -1) * self.options.step, event );
|
|
|
|
clearTimeout( self.timeout );
|
2010-11-30 12:43:43 +00:00
|
|
|
self.timeout = setTimeout(function() {
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( self.spinning ) {
|
|
|
|
self._stop( event );
|
|
|
|
self._change( event );
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
_uiSpinnerHtml: function() {
|
2011-05-18 01:07:44 +00:00
|
|
|
return "<span class='ui-spinner ui-state-default ui-widget ui-widget-content ui-corner-all'></span>";
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
_buttonHtml: function() {
|
2011-05-18 01:07:44 +00:00
|
|
|
return "" +
|
|
|
|
"<a class='ui-spinner-button ui-spinner-up ui-corner-tr'>" +
|
|
|
|
"<span class='ui-icon ui-icon-triangle-1-n'>▲</span>" +
|
|
|
|
"</a>" +
|
|
|
|
"<a class='ui-spinner-button ui-spinner-down ui-corner-br'>" +
|
|
|
|
"<span class='ui-icon ui-icon-triangle-1-s'>▼</span>" +
|
|
|
|
"</a>";
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
_start: function( event ) {
|
|
|
|
if ( !this.spinning && this._trigger( "start", event ) === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !this.counter ) {
|
|
|
|
this.counter = 1;
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
this.spinning = true;
|
|
|
|
return true;
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
_repeat: function( i, steps, event ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
var self = this;
|
|
|
|
i = i || 500;
|
|
|
|
|
2011-05-18 01:07:44 +00:00
|
|
|
clearTimeout( this.timer );
|
2010-11-30 12:43:43 +00:00
|
|
|
this.timer = setTimeout(function() {
|
2011-05-18 01:07:44 +00:00
|
|
|
self._repeat( 40, steps, event );
|
|
|
|
}, i );
|
|
|
|
|
|
|
|
self._spin( steps * self.options.step, event );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
_spin: function( step, event ) {
|
|
|
|
if ( !this.counter ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
this.counter = 1;
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
// TODO refactor, maybe figure out some non-linear math
|
|
|
|
var newVal = this.value() + step * (this.options.incremental &&
|
|
|
|
this.counter > 20
|
|
|
|
? this.counter > 100
|
|
|
|
? this.counter > 200
|
|
|
|
? 100
|
|
|
|
: 10
|
|
|
|
: 2
|
|
|
|
: 1);
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
if ( this._trigger( "spin", event, { value: newVal } ) !== false) {
|
|
|
|
this.value( newVal );
|
|
|
|
this.counter++;
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
_stop: function( event ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
this.counter = 0;
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( this.timer ) {
|
|
|
|
clearTimeout( this.timer );
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
2011-02-21 21:45:19 +00:00
|
|
|
this.element.focus();
|
2010-11-30 12:43:43 +00:00
|
|
|
this.spinning = false;
|
2011-05-18 01:07:44 +00:00
|
|
|
this._trigger( "stop", event );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
_change: function( event ) {
|
|
|
|
this._trigger( "change", event );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
_setOption: function( key, value ) {
|
|
|
|
if ( key === "value") {
|
|
|
|
value = this._parse( value );
|
|
|
|
if ( value < this.options.min ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
value = this.options.min;
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
if ( value > this.options.max ) {
|
2010-11-30 12:43:43 +00:00
|
|
|
value = this.options.max;
|
|
|
|
}
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
if ( key === "disabled" ) {
|
|
|
|
if ( value ) {
|
|
|
|
this.element.attr( "disabled", true );
|
|
|
|
this.buttons.button( "disable" );
|
2010-11-30 12:43:43 +00:00
|
|
|
} else {
|
2011-05-18 01:07:44 +00:00
|
|
|
this.element.removeAttr( "disabled" );
|
|
|
|
this.buttons.button( "enable" );
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2011-02-11 13:53:43 +00:00
|
|
|
this._super( "_setOption", key, value );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
_setOptions: function( options ) {
|
2011-02-11 13:53:43 +00:00
|
|
|
this._super( "_setOptions", options );
|
2010-11-30 12:43:43 +00:00
|
|
|
if ( "value" in options ) {
|
|
|
|
this._format( this.options.value );
|
|
|
|
}
|
|
|
|
this._aria();
|
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
_aria: function() {
|
2011-05-18 01:07:44 +00:00
|
|
|
this.element.attr({
|
|
|
|
"aria-valuemin": this.options.min,
|
|
|
|
"aria-valuemax": this.options.max,
|
|
|
|
"aria-valuenow": this.options.value
|
|
|
|
});
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
_parse: function( val ) {
|
|
|
|
if ( typeof val === "string" ) {
|
|
|
|
val = $.global && this.options.numberformat ? $.global.parseFloat( val ) : +val;
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
return isNaN( val ) ? null : val;
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
_format: function( num ) {
|
|
|
|
this.element.val( $.global && this.options.numberformat ? $.global.format( num, this.options.numberformat ) : num );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
destroy: function() {
|
|
|
|
this.element
|
2011-05-18 01:07:44 +00:00
|
|
|
.removeClass( "ui-spinner-input" )
|
|
|
|
.removeAttr( "disabled" )
|
|
|
|
.removeAttr( "autocomplete" )
|
|
|
|
.removeAttr( "role" )
|
|
|
|
.removeAttr( "aria-valuemin" )
|
|
|
|
.removeAttr( "aria-valuemax" )
|
|
|
|
.removeAttr( "aria-valuenow" );
|
2011-01-15 11:34:07 +00:00
|
|
|
this._super( "destroy" );
|
2011-05-18 01:07:44 +00:00
|
|
|
this.uiSpinner.replaceWith( this.element );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
stepUp: function( steps ) {
|
|
|
|
this._spin( (steps || 1) * this.options.step );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
stepDown: function( steps ) {
|
|
|
|
this._spin( (steps || 1) * -this.options.step );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
pageUp: function( pages ) {
|
|
|
|
this.stepUp( (pages || 1) * this.options.page );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
pageDown: function( pages ) {
|
|
|
|
this.stepDown( (pages || 1) * this.options.page );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
|
|
|
value: function( newVal ) {
|
|
|
|
if ( !arguments.length ) {
|
|
|
|
return this._parse( this.element.val() );
|
2010-11-30 12:43:43 +00:00
|
|
|
}
|
2011-05-18 01:07:44 +00:00
|
|
|
this.option( "value", newVal );
|
2010-11-30 12:43:43 +00:00
|
|
|
},
|
2011-05-18 01:07:44 +00:00
|
|
|
|
2010-11-30 12:43:43 +00:00
|
|
|
widget: function() {
|
|
|
|
return this.uiSpinner;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-02-03 17:01:26 +00:00
|
|
|
$.ui.spinner.version = "@VERSION";
|
|
|
|
|
2011-05-18 01:07:44 +00:00
|
|
|
}( jQuery ) );
|