mirror of
https://github.com/jquery/jquery-ui.git
synced 2024-11-21 11:04:24 +00:00
Slider: More codecleanup. Inlining defaults for min/max, removing page (use 10*step instead), removing mouseWheel (just use when available)
This commit is contained in:
parent
299d5c88b8
commit
44ca830ee8
109
ui/jquery.ui.spinner.js
vendored
109
ui/jquery.ui.spinner.js
vendored
@ -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
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -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);
|
||||||
@ -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) +
|
||||||
@ -202,7 +200,7 @@ $.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,
|
||||||
@ -210,24 +208,16 @@ $.widget('ui.spinner', {
|
|||||||
|
|
||||||
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:
|
||||||
@ -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);
|
||||||
|
if (self.timeout) {
|
||||||
|
window.clearTimeout(self.timeout);
|
||||||
|
}
|
||||||
|
self.timeout = window.setTimeout(function() {
|
||||||
|
if (self.spinning) {
|
||||||
|
self._stop(event);
|
||||||
|
self._change(event);
|
||||||
}
|
}
|
||||||
self._spin((delta > 0 ? 1 : -1) * self._step(), event);
|
}, 400);
|
||||||
if (self.timeout) {
|
event.preventDefault();
|
||||||
window.clearTimeout(self.timeout);
|
});
|
||||||
}
|
|
||||||
self.timeout = window.setTimeout(function() {
|
|
||||||
if (self.spinning) {
|
|
||||||
self._stop(event);
|
|
||||||
self._change(event);
|
|
||||||
}
|
|
||||||
}, 400);
|
|
||||||
event.preventDefault();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
value: function(newVal) {
|
value: function(newVal) {
|
||||||
if (!arguments.length) {
|
if (!arguments.length) {
|
||||||
@ -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 );
|
||||||
@ -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());
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -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) {
|
||||||
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user