Skip to content

Commit

Permalink
Slider: More codecleanup. Inlining defaults for min/max, removing pag…
Browse files Browse the repository at this point in the history
…e (use 10*step instead), removing mouseWheel (just use when available)
  • Loading branch information
jzaefferer committed Oct 25, 2010
1 parent 299d5c8 commit 44ca830
Showing 1 changed file with 37 additions and 72 deletions.
109 changes: 37 additions & 72 deletions ui/jquery.ui.spinner.js
Expand Up @@ -17,18 +17,16 @@
var hover = 'ui-state-hover', var hover = 'ui-state-hover',
active = 'ui-state-active', active = 'ui-state-active',
namespace = '.spinner', namespace = '.spinner',
uiSpinnerClasses = 'ui-spinner ui-state-default ui-widget ui-widget-content ui-corner-all '; pageModifier = 10;


$.widget('ui.spinner', { $.widget('ui.spinner', {
options: { options: {
dir: 'ltr', dir: 'ltr',
incremental: true, incremental: true,
max: null, max: Number.MAX_VALUE,
min: null, min: -Number.MAX_VALUE,
mouseWheel: true,
numberformat: "n", numberformat: "n",
page: 5, step: 1,
step: null,
value: null value: null
}, },


Expand Down Expand Up @@ -114,7 +112,7 @@ $.widget('ui.spinner', {
}) })
.bind('mouseup', function(event) { .bind('mouseup', function(event) {
if (self.counter == 1) { if (self.counter == 1) {
self._spin(($(this).hasClass('ui-spinner-up') ? 1 : -1) * self._step(), event); self._spin(($(this).hasClass('ui-spinner-up') ? 1 : -1) * self.options.step, event);
} }
if (self.spinning) { if (self.spinning) {
self._stop(event); self._stop(event);
Expand All @@ -140,7 +138,7 @@ $.widget('ui.spinner', {
self.uiSpinner = uiSpinner; self.uiSpinner = uiSpinner;
}, },
_uiSpinnerHtml: function() { _uiSpinnerHtml: function() {
return '<div role="spinbutton" class="ui-spinner-' + this.options.dir + '"></div>'; return '<div role="spinbutton" class="ui-spinner ui-state-default ui-widget ui-widget-content ui-corner-all ui-spinner-' + this.options.dir + '"></div>';
}, },
_buttonHtml: function() { _buttonHtml: function() {
return '<a class="ui-spinner-button ui-spinner-up ui-corner-t' + this.options.dir.substr(-1,1) + return '<a class="ui-spinner-button ui-spinner-up ui-corner-t' + this.options.dir.substr(-1,1) +
Expand Down Expand Up @@ -202,32 +200,24 @@ $.widget('ui.spinner', {
self._repeat(self.options.incremental && self.counter > 20 ? 20 : i, steps, event); self._repeat(self.options.incremental && self.counter > 20 ? 20 : i, steps, event);
}, i); }, i);


self._spin(steps*self._step(), event); self._spin(steps*self.options.step, event);
}, },
_keydown: function(event) { _keydown: function(event) {
var o = this.options, var o = this.options,
KEYS = $.ui.keyCode; KEYS = $.ui.keyCode;


switch (event.keyCode) { switch (event.keyCode) {
case KEYS.UP: case KEYS.UP:
this._repeat(null, event.shiftKey ? o.page : 1, event); this._repeat(null, 1, event);
return false; return false;
case KEYS.DOWN: case KEYS.DOWN:
this._repeat(null, event.shiftKey ? -o.page : -1, event); this._repeat(null, -1, event);
return false; return false;
case KEYS.PAGE_UP: case KEYS.PAGE_UP:
this._repeat(null, o.page, event); this._repeat(null, pageModifier, event);
return false; return false;
case KEYS.PAGE_DOWN: case KEYS.PAGE_DOWN:
this._repeat(null, -o.page, event); this._repeat(null, -pageModifier, event);
return false;

case KEYS.HOME:
case KEYS.END:
if (event.shiftKey) {
return true;
}
this.value(this['_' + (event.keyCode == KEYS.HOME ? 'min' : 'max')]());
return false; return false;


case KEYS.ENTER: case KEYS.ENTER:
Expand All @@ -237,26 +227,26 @@ $.widget('ui.spinner', {
return true; return true;
}, },
_mousewheel: function() { _mousewheel: function() {
if (!$.fn.mousewheel)
return;
var self = this; var self = this;
if ($.fn.mousewheel && self.options.mouseWheel) { this.element.mousewheel(function(event, delta) {
this.element.mousewheel(function(event, delta) { delta = ($.browser.opera ? -delta / Math.abs(delta) : delta);
delta = ($.browser.opera ? -delta / Math.abs(delta) : delta); if (!self._start(event)) {
if (!self._start(event)) { return false;
return false; }
} self._spin((delta > 0 ? 1 : -1) * self.options.step, event);
self._spin((delta > 0 ? 1 : -1) * self._step(), event); if (self.timeout) {
if (self.timeout) { window.clearTimeout(self.timeout);
window.clearTimeout(self.timeout); }
self.timeout = window.setTimeout(function() {
if (self.spinning) {
self._stop(event);
self._change(event);
} }
self.timeout = window.setTimeout(function() { }, 400);
if (self.spinning) { event.preventDefault();
self._stop(event); });
self._change(event);
}
}, 400);
event.preventDefault();
});
}
}, },
value: function(newVal) { value: function(newVal) {
if (!arguments.length) { if (!arguments.length) {
Expand All @@ -277,11 +267,11 @@ $.widget('ui.spinner', {
_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._min()) { if (value < this.options.min) {
value = this._min(); value = this.options.min;
} }
if (value > this._max()) { if (value > this.options.max) {
value = this._max(); value = this.options.max;
} }
} }
$.Widget.prototype._setOption.call( this, key, value ); $.Widget.prototype._setOption.call( this, key, value );
Expand All @@ -307,8 +297,8 @@ $.widget('ui.spinner', {
}, },
_aria: function() { _aria: function() {
this.uiSpinner this.uiSpinner
.attr('aria-valuemin', this._min()) .attr('aria-valuemin', this.options.min)
.attr('aria-valuemax', this._max()) .attr('aria-valuemax', this.options.max)
.attr('aria-valuenow', this.value()); .attr('aria-valuenow', this.value());
}, },


Expand All @@ -329,31 +319,6 @@ $.widget('ui.spinner', {
_format: function(num) { _format: function(num) {
this.element.val( window.Globalization ? Globalization.format(num, this.options.numberformat) : num ); this.element.val( window.Globalization ? Globalization.format(num, this.options.numberformat) : num );
}, },
_getOption: function(key, defaultValue) {
return this._parse(this.options[key] !== null
? this.options[key]
: this.element.attr(key)
? this.element.attr(key)
: defaultValue);
},
_step: function(newVal) {
if (!arguments.length) {
return this._getOption('step', 1);
}
this._setOption('step', newVal);
},
_min: function(newVal) {
if (!arguments.length) {
return this._getOption('min', -Number.MAX_VALUE);
}
this._setOption('min', newVal);
},
_max: function(newVal) {
if (!arguments.length) {
return this._getOption('max', Number.MAX_VALUE);
}
this._setOption('max', newVal);
},


destroy: function() { destroy: function() {
if ($.fn.mousewheel) { if ($.fn.mousewheel) {
Expand Down Expand Up @@ -388,11 +353,11 @@ $.widget('ui.spinner', {
this.options.disabled = true; this.options.disabled = true;
}, },
stepUp: function(steps) { stepUp: function(steps) {
this._spin((steps || 1) * this._step(), null); this._spin((steps || 1) * this.options.step, null);
return this; return this;
}, },
stepDown: function(steps) { stepDown: function(steps) {
this._spin((steps || 1) * -this._step(), null); this._spin((steps || 1) * -this.options.step, null);
return this; return this;
}, },
pageUp: function(pages) { pageUp: function(pages) {
Expand Down

0 comments on commit 44ca830

Please sign in to comment.