mirror of
https://github.com/jquery/jquery-ui.git
synced 2024-11-21 11:04:24 +00:00
Spinner: Coding standards.
This commit is contained in:
parent
03939fc8c1
commit
a47bfa196b
295
ui/jquery.ui.spinner.js
vendored
295
ui/jquery.ui.spinner.js
vendored
@ -11,9 +11,9 @@
|
|||||||
* jquery.ui.core.js
|
* jquery.ui.core.js
|
||||||
* jquery.ui.widget.js
|
* jquery.ui.widget.js
|
||||||
*/
|
*/
|
||||||
(function($) {
|
(function( $ ) {
|
||||||
|
|
||||||
$.widget('ui.spinner', {
|
$.widget( "ui.spinner", {
|
||||||
defaultElement: "<input>",
|
defaultElement: "<input>",
|
||||||
widgetEventPrefix: "spin",
|
widgetEventPrefix: "spin",
|
||||||
options: {
|
options: {
|
||||||
@ -34,18 +34,20 @@ $.widget('ui.spinner', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
_markupOptions: function() {
|
_markupOptions: function() {
|
||||||
var _this = this;
|
var that = this;
|
||||||
$.each({
|
$.each({
|
||||||
min: -Number.MAX_VALUE,
|
min: -Number.MAX_VALUE,
|
||||||
max: Number.MAX_VALUE,
|
max: Number.MAX_VALUE,
|
||||||
step: 1
|
step: 1
|
||||||
}, function(attr, defaultValue) {
|
}, function( attr, defaultValue ) {
|
||||||
if (_this.options[attr] === null) {
|
if ( that.options[ attr ] === null ) {
|
||||||
var value = _this.element.attr(attr);
|
var value = that.element.attr( attr );
|
||||||
_this.options[attr] = typeof value == "string" && value.length > 0 ? _this._parse(value) : defaultValue;
|
that.options[ attr ] = typeof value === "string" && value.length > 0 ?
|
||||||
|
that._parse( value ) :
|
||||||
|
defaultValue;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.value(this.options.value !== null ? this.options.value : this.element.val() || 0);
|
this.value( this.options.value !== null ? this.options.value : this.element.val() || 0 );
|
||||||
},
|
},
|
||||||
|
|
||||||
_draw: function() {
|
_draw: function() {
|
||||||
@ -53,123 +55,124 @@ $.widget('ui.spinner', {
|
|||||||
options = self.options;
|
options = self.options;
|
||||||
|
|
||||||
var uiSpinner = this.uiSpinner = self.element
|
var uiSpinner = this.uiSpinner = self.element
|
||||||
.addClass('ui-spinner-input')
|
.addClass( "ui-spinner-input" )
|
||||||
.attr('autocomplete', 'off')
|
.attr( "autocomplete", "off" )
|
||||||
.wrap(self._uiSpinnerHtml())
|
.wrap( self._uiSpinnerHtml() )
|
||||||
.parent()
|
.parent()
|
||||||
// add buttons
|
// add buttons
|
||||||
.append(self._buttonHtml())
|
.append( self._buttonHtml() )
|
||||||
// add behaviours
|
// add behaviours
|
||||||
|
// TODO: user ._hoverable
|
||||||
.hover(function() {
|
.hover(function() {
|
||||||
if (!options.disabled) {
|
if ( !options.disabled ) {
|
||||||
$(this).addClass('ui-state-hover');
|
$( this ).addClass( "ui-state-hover" );
|
||||||
}
|
}
|
||||||
self.hovered = true;
|
self.hovered = true;
|
||||||
}, function() {
|
}, function() {
|
||||||
$(this).removeClass('ui-state-hover');
|
$( this ).removeClass( "ui-state-hover" );
|
||||||
self.hovered = false;
|
self.hovered = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO: use ._bind()
|
||||||
this.element
|
this.element
|
||||||
.attr( "role", "spinbutton" )
|
.attr( "role", "spinbutton" )
|
||||||
.bind('keydown.spinner', function(event) {
|
.bind( "keydown.spinner", function( event ) {
|
||||||
if (self.options.disabled) {
|
if ( options.disabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (self._start(event)) {
|
if ( self._start( event ) ) {
|
||||||
return self._keydown(event);
|
return self._keydown( event );
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.bind('keyup.spinner', function(event) {
|
.bind( "keyup.spinner", function( event ) {
|
||||||
if (self.options.disabled) {
|
if ( options.disabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (self.spinning) {
|
if ( self.spinning ) {
|
||||||
self._stop(event);
|
self._stop( event );
|
||||||
self._change(event);
|
self._change( event );
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.bind('focus.spinner', function() {
|
.bind( "focus.spinner", function() {
|
||||||
uiSpinner.addClass('ui-state-active');
|
uiSpinner.addClass( "ui-state-active" );
|
||||||
self.focused = true;
|
self.focused = true;
|
||||||
})
|
})
|
||||||
.bind('blur.spinner', function(event) {
|
.bind( "blur.spinner", function( event ) {
|
||||||
self.value(self.element.val());
|
self.value( self.element.val() );
|
||||||
if (!self.hovered) {
|
if ( !self.hovered ) {
|
||||||
uiSpinner.removeClass('ui-state-active');
|
uiSpinner.removeClass( "ui-state-active" );
|
||||||
}
|
}
|
||||||
self.focused = false;
|
self.focused = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
// button bindings
|
// button bindings
|
||||||
this.buttons = uiSpinner.find('.ui-spinner-button')
|
this.buttons = uiSpinner.find( ".ui-spinner-button" )
|
||||||
.attr("tabIndex", -1)
|
.attr( "tabIndex", -1 )
|
||||||
.button()
|
.button()
|
||||||
.removeClass("ui-corner-all")
|
.removeClass( "ui-corner-all" )
|
||||||
.bind('mousedown', function(event) {
|
.bind( "mousedown", function( event ) {
|
||||||
if (self.options.disabled) {
|
if ( options.disabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (self._start(event) === false) {
|
if ( self._start( event ) === false ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
self._repeat(null, $(this).hasClass('ui-spinner-up') ? 1 : -1, event);
|
self._repeat( null, $( this ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
|
||||||
})
|
})
|
||||||
.bind('mouseup', function(event) {
|
.bind( "mouseup", function( event ) {
|
||||||
if (self.options.disabled) {
|
if ( options.disabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (self.spinning) {
|
if ( self.spinning ) {
|
||||||
self._stop(event);
|
self._stop( event );
|
||||||
self._change(event);
|
self._change( event );
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.bind("mouseenter", function() {
|
.bind( "mouseenter", function() {
|
||||||
if (self.options.disabled) {
|
if ( self.options.disabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// button will add ui-state-active if mouse was down while mouseleave and kept down
|
// button will add ui-state-active if mouse was down while mouseleave and kept down
|
||||||
if ($(this).hasClass("ui-state-active")) {
|
if ( $( this ).hasClass( "ui-state-active" ) ) {
|
||||||
if (self._start(event) === false) {
|
if ( self._start( event ) === false ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
self._repeat(null, $(this).hasClass('ui-spinner-up') ? 1 : -1, event);
|
self._repeat( null, $( this ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.bind("mouseleave", function() {
|
.bind( "mouseleave", function() {
|
||||||
if (self.spinning) {
|
if ( self.spinning ) {
|
||||||
self._stop(event);
|
self._stop( event );
|
||||||
self._change(event);
|
self._change( event );
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// disable spinner if element was already disabled
|
// disable spinner if element was already disabled
|
||||||
if (options.disabled) {
|
if ( options.disabled ) {
|
||||||
this.disable();
|
this.disable();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_keydown: function(event) {
|
_keydown: function( event ) {
|
||||||
var o = this.options,
|
var options = this.options,
|
||||||
KEYS = $.ui.keyCode;
|
keyCode = $.ui.keyCode;
|
||||||
|
|
||||||
switch (event.keyCode) {
|
switch ( event.keyCode ) {
|
||||||
case KEYS.UP:
|
case keyCode.UP:
|
||||||
this._repeat(null, 1, event);
|
this._repeat( null, 1, event );
|
||||||
return false;
|
return false;
|
||||||
case KEYS.DOWN:
|
case keyCode.DOWN:
|
||||||
this._repeat(null, -1, event);
|
this._repeat( null, -1, event );
|
||||||
return false;
|
return false;
|
||||||
case KEYS.PAGE_UP:
|
case keyCode.PAGE_UP:
|
||||||
this._repeat(null, this.options.page, event);
|
this._repeat( null, options.page, event );
|
||||||
return false;
|
return false;
|
||||||
case KEYS.PAGE_DOWN:
|
case keyCode.PAGE_DOWN:
|
||||||
this._repeat(null, -this.options.page, event);
|
this._repeat( null, -options.page, event );
|
||||||
return false;
|
return false;
|
||||||
|
case keyCode.ENTER:
|
||||||
case KEYS.ENTER:
|
this.value( this.element.val() );
|
||||||
this.value(this.element.val());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -177,23 +180,23 @@ $.widget('ui.spinner', {
|
|||||||
|
|
||||||
_mousewheel: function() {
|
_mousewheel: function() {
|
||||||
// need the delta normalization that mousewheel plugin provides
|
// need the delta normalization that mousewheel plugin provides
|
||||||
if (!$.fn.mousewheel) {
|
if ( !$.fn.mousewheel ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var self = this;
|
var self = this;
|
||||||
this.element.bind("mousewheel.spinner", function(event, delta) {
|
this.element.bind( "mousewheel.spinner", function( event, delta ) {
|
||||||
if (self.options.disabled || !delta) {
|
if ( self.options.disabled || !delta ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!self.spinning && !self._start(event)) {
|
if ( !self.spinning && !self._start( event ) ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
self._spin((delta > 0 ? 1 : -1) * self.options.step, event);
|
self._spin( (delta > 0 ? 1 : -1) * self.options.step, event );
|
||||||
clearTimeout(self.timeout);
|
clearTimeout( self.timeout );
|
||||||
self.timeout = setTimeout(function() {
|
self.timeout = setTimeout(function() {
|
||||||
if (self.spinning) {
|
if ( self.spinning ) {
|
||||||
self._stop(event);
|
self._stop( event );
|
||||||
self._change(event);
|
self._change( event );
|
||||||
}
|
}
|
||||||
}, 100);
|
}, 100);
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@ -201,39 +204,45 @@ $.widget('ui.spinner', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
_uiSpinnerHtml: function() {
|
_uiSpinnerHtml: function() {
|
||||||
return '<span class="ui-spinner ui-state-default ui-widget ui-widget-content ui-corner-all"></span>';
|
return "<span class='ui-spinner ui-state-default ui-widget ui-widget-content ui-corner-all'></span>";
|
||||||
},
|
},
|
||||||
|
|
||||||
_buttonHtml: function() {
|
_buttonHtml: function() {
|
||||||
return '<a class="ui-spinner-button ui-spinner-up ui-corner-tr"><span class="ui-icon ui-icon-triangle-1-n">▲</span></a>' +
|
return "" +
|
||||||
'<a class="ui-spinner-button ui-spinner-down ui-corner-br"><span class="ui-icon ui-icon-triangle-1-s">▼</span></a>';
|
"<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>";
|
||||||
},
|
},
|
||||||
|
|
||||||
_start: function(event) {
|
_start: function( event ) {
|
||||||
if (!this.spinning && this._trigger('start', event) !== false) {
|
if ( !this.spinning && this._trigger( "start", event ) === false ) {
|
||||||
if (!this.counter) {
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !this.counter ) {
|
||||||
this.counter = 1;
|
this.counter = 1;
|
||||||
}
|
}
|
||||||
this.spinning = true;
|
this.spinning = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
return false;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
_repeat: function(i, steps, event) {
|
_repeat: function( i, steps, event ) {
|
||||||
var self = this;
|
var self = this;
|
||||||
i = i || 500;
|
i = i || 500;
|
||||||
|
|
||||||
clearTimeout(this.timer);
|
clearTimeout( this.timer );
|
||||||
this.timer = setTimeout(function() {
|
this.timer = setTimeout(function() {
|
||||||
self._repeat(40, steps, event);
|
self._repeat( 40, steps, event );
|
||||||
}, i);
|
}, i );
|
||||||
|
|
||||||
self._spin(steps * self.options.step, event);
|
self._spin( steps * self.options.step, event );
|
||||||
},
|
},
|
||||||
|
|
||||||
_spin: function(step, event) {
|
_spin: function( step, event ) {
|
||||||
if (!this.counter) {
|
if ( !this.counter ) {
|
||||||
this.counter = 1;
|
this.counter = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,45 +256,47 @@ $.widget('ui.spinner', {
|
|||||||
: 2
|
: 2
|
||||||
: 1);
|
: 1);
|
||||||
|
|
||||||
if (this._trigger('spin', event, { value: newVal }) !== false) {
|
if ( this._trigger( "spin", event, { value: newVal } ) !== false) {
|
||||||
this.value(newVal);
|
this.value( newVal );
|
||||||
this.counter++;
|
this.counter++;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_stop: function(event) {
|
_stop: function( event ) {
|
||||||
this.counter = 0;
|
this.counter = 0;
|
||||||
if (this.timer) {
|
if ( this.timer ) {
|
||||||
window.clearTimeout(this.timer);
|
clearTimeout( this.timer );
|
||||||
}
|
}
|
||||||
this.element.focus();
|
this.element.focus();
|
||||||
this.spinning = false;
|
this.spinning = false;
|
||||||
this._trigger('stop', event);
|
this._trigger( "stop", event );
|
||||||
},
|
},
|
||||||
|
|
||||||
_change: function(event) {
|
_change: function( event ) {
|
||||||
this._trigger('change', event);
|
this._trigger( "change", event );
|
||||||
},
|
},
|
||||||
|
|
||||||
_setOption: function(key, value) {
|
_setOption: function( key, value ) {
|
||||||
if (key == 'value') {
|
if ( key === "value") {
|
||||||
value = this._parse(value);
|
value = this._parse( value );
|
||||||
if (value < this.options.min) {
|
if ( value < this.options.min ) {
|
||||||
value = this.options.min;
|
value = this.options.min;
|
||||||
}
|
}
|
||||||
if (value > this.options.max) {
|
if ( value > this.options.max ) {
|
||||||
value = this.options.max;
|
value = this.options.max;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (key == 'disabled') {
|
|
||||||
if (value) {
|
if ( key === "disabled" ) {
|
||||||
this.element.attr("disabled", true);
|
if ( value ) {
|
||||||
this.buttons.button("disable");
|
this.element.attr( "disabled", true );
|
||||||
|
this.buttons.button( "disable" );
|
||||||
} else {
|
} else {
|
||||||
this.element.removeAttr("disabled");
|
this.element.removeAttr( "disabled" );
|
||||||
this.buttons.button("enable");
|
this.buttons.button( "enable" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this._super( "_setOption", key, value );
|
this._super( "_setOption", key, value );
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -298,58 +309,58 @@ $.widget('ui.spinner', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
_aria: function() {
|
_aria: function() {
|
||||||
this.element
|
this.element.attr({
|
||||||
.attr('aria-valuemin', this.options.min)
|
"aria-valuemin": this.options.min,
|
||||||
.attr('aria-valuemax', this.options.max)
|
"aria-valuemax": this.options.max,
|
||||||
.attr('aria-valuenow', this.options.value);
|
"aria-valuenow": this.options.value
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
_parse: function(val) {
|
_parse: function( val ) {
|
||||||
var input = val;
|
if ( typeof val === "string" ) {
|
||||||
if (typeof val == 'string') {
|
val = $.global && this.options.numberformat ? $.global.parseFloat( val ) : +val;
|
||||||
val = $.global && this.options.numberformat ? $.global.parseFloat(val) : +val;
|
|
||||||
}
|
}
|
||||||
return isNaN(val) ? null : val;
|
return isNaN( val ) ? null : val;
|
||||||
},
|
},
|
||||||
|
|
||||||
_format: function(num) {
|
_format: function( num ) {
|
||||||
this.element.val( $.global && this.options.numberformat ? $.global.format(num, this.options.numberformat) : num );
|
this.element.val( $.global && this.options.numberformat ? $.global.format( num, this.options.numberformat ) : num );
|
||||||
},
|
},
|
||||||
|
|
||||||
destroy: function() {
|
destroy: function() {
|
||||||
this.element
|
this.element
|
||||||
.removeClass('ui-spinner-input')
|
.removeClass( "ui-spinner-input" )
|
||||||
.removeAttr('disabled')
|
.removeAttr( "disabled" )
|
||||||
.removeAttr('autocomplete')
|
.removeAttr( "autocomplete" )
|
||||||
.removeAttr('role')
|
.removeAttr( "role" )
|
||||||
.removeAttr('aria-valuemin')
|
.removeAttr( "aria-valuemin" )
|
||||||
.removeAttr('aria-valuemax')
|
.removeAttr( "aria-valuemax" )
|
||||||
.removeAttr('aria-valuenow');
|
.removeAttr( "aria-valuenow" );
|
||||||
this._super( "destroy" );
|
this._super( "destroy" );
|
||||||
this.uiSpinner.replaceWith(this.element);
|
this.uiSpinner.replaceWith( this.element );
|
||||||
},
|
},
|
||||||
|
|
||||||
stepUp: function(steps) {
|
stepUp: function( steps ) {
|
||||||
this._spin((steps || 1) * this.options.step);
|
this._spin( (steps || 1) * this.options.step );
|
||||||
},
|
},
|
||||||
|
|
||||||
stepDown: function(steps) {
|
stepDown: function( steps ) {
|
||||||
this._spin((steps || 1) * -this.options.step);
|
this._spin( (steps || 1) * -this.options.step );
|
||||||
},
|
},
|
||||||
|
|
||||||
pageUp: function(pages) {
|
pageUp: function( pages ) {
|
||||||
this.stepUp((pages || 1) * this.options.page);
|
this.stepUp( (pages || 1) * this.options.page );
|
||||||
},
|
},
|
||||||
|
|
||||||
pageDown: function(pages) {
|
pageDown: function( pages ) {
|
||||||
this.stepDown((pages || 1) * this.options.page);
|
this.stepDown( (pages || 1) * this.options.page );
|
||||||
},
|
},
|
||||||
|
|
||||||
value: function(newVal) {
|
value: function( newVal ) {
|
||||||
if (!arguments.length) {
|
if ( !arguments.length ) {
|
||||||
return this._parse(this.element.val());
|
return this._parse( this.element.val() );
|
||||||
}
|
}
|
||||||
this.option('value', newVal);
|
this.option( "value", newVal );
|
||||||
},
|
},
|
||||||
|
|
||||||
widget: function() {
|
widget: function() {
|
||||||
@ -359,4 +370,4 @@ $.widget('ui.spinner', {
|
|||||||
|
|
||||||
$.ui.spinner.version = "@VERSION";
|
$.ui.spinner.version = "@VERSION";
|
||||||
|
|
||||||
})(jQuery);
|
}( jQuery ) );
|
||||||
|
Loading…
Reference in New Issue
Block a user