r2945 - spinner: further code clean-up
Author: pazu2k@gmail.com
Date: Sun Jul 19 11:06:19 2009
New Revision: 2945
Modified:
branches/dev/spinner/ui/ui.spinner.js
Log:
spinner: further code clean-up
Modified: branches/dev/spinner/ui/ui.spinner.js
==============================================================================
--- branches/dev/spinner/ui/ui.spinner.js (original)
+++ branches/dev/spinner/ui/ui.spinner.js Sun Jul 19 11:06:19 2009
@@ -19,13 +19,32 @@
$.widget('ui.spinner', {
_init: function() {
+ // currentDelay can't be initialized as part of the prototype because
all widgets would share the same object
+ this.currentDelay = {};
+
+ this._initOptions();
+
+ // set value on init
+ this._value(this._value());
+
+ // draw widget
+ this._draw();
+
+ if (this.options.hide) {
+ this._hide();
+ }
+
+ this._mousewheel();
+
+ // disable spinner if element was already disabled
+ if (this.element.attr("disabled")) {
+ this.disable();
+ }
+ },
+ _initOptions: function() {
var self = this,
options = self.options;
- // initialize variables
- // _curDelay can't be initialized as part of the prototype because all
widgets would share the same object
- self.currentDelay = {};
-
// parse min, max, step, and page based on radix
// min, max and step pull from attributes if the option is set to null
$.each({ min: -Number.MAX_VALUE, max: Number.MAX_VALUE, step: 1},
@@ -45,26 +64,11 @@
// check for precision in stepping and set _precision as internal
self._precision = parseInt(options.precision, 10);
+
if (options.step.toString().indexOf('.') != -1 && self._precision === 0)
{
var s = options.step.toString();
self._precision = s.slice(s.indexOf('.')+1, s.length).length;
- }
-
- self._setValue( isNaN(self._getValue()) ? options.value :
self._getValue() );
-
- // draw widget
- self._draw();
-
- if (options.hide) {
- self._hide();
- }
-
- self._mousewheel();
-
- // disable spinner if element was already disabled
- if (self.element.attr("disabled")) {
- self.disable();
- }
+ }
},
_validChars: function() {
var radix = parseInt(this.options.radix);
@@ -112,14 +116,13 @@
}
});
- // add aria properties
widget
+ // add aria properties
.attr('aria-valuemin',options.min)
.attr('aria-valuemax',options.max)
- .attr('aria-valuenow',options.value);
+ .attr('aria-valuenow',options.value)
- // add behaviours
- widget
+ // add behaviours
.hover(function() {
if (!options.readOnly && !self.disabled) {
$(this).addClass(hover);
@@ -215,15 +218,15 @@
self.widget = widget;
},
_constrain: function() {
- if (this.options.min !== null && this._getValue() < this.options.min) {
- this._setValue(this.options.min);
+ if (this.options.min !== null && this._value() < this.options.min) {
+ this._value(this.options.min);
}
- if (this.options.max !== null && this._getValue() > this.options.max) {
- this._setValue(this.options.max);
+ if (this.options.max !== null && this._value() > this.options.max) {
+ this._value(this.options.max);
}
},
_cleanUp: function() {
- this._setValue(this._getValue());
+ this._value(this._value());
this._constrain();
},
_start: function(event) {
@@ -240,7 +243,7 @@
if (!this.counter) {
this.counter = 1;
}
- this._setValue(this._getValue() + (this.options.incremental &&
this.counter > 100 ? (this.counter > 200 ? 100 : 10) : 1) * step);
+ this._value(this._value() + (this.options.incremental && this.counter >
100 ? (this.counter > 200 ? 100 : 10) : 1) * step);
this._constrain();
this.counter++;
@@ -281,8 +284,8 @@
case KEYS.DOWN: this._spin(event.shiftKey ? -jump : -o.step, event);
break;
case KEYS.PAGE_UP: this._spin(jump, event); break;
case KEYS.PAGE_DOWN: this._spin(-jump, event); break;
- case KEYS.HOME: this._setValue(o.min); break;
- case KEYS.END: this._setValue(o.max); break;
+ case KEYS.HOME: this._value(o.min); break;
+ case KEYS.END: this._value(o.max); break;
case KEYS.TAB:
case KEYS.BACKSPACE:
@@ -341,19 +344,13 @@
}
return isNaN(val) ? null : val;
},
- _getValue: function() {
- var value = this._parse(this.element.val());
- return isNaN(value) ? this.options.value : value;
- },
- _setValue: function(newVal) {
- if (isNaN(newVal)) {
- newVal = this.options.value;
- }
- this.element.val(
- this.options.currency ?
- $.ui.spinner.format.currency(newVal, this.options.currency,
this.options.groupSeparator, this.options.radixPoint) :
- $.ui.spinner.format.number(newVal, this._precision,
this.options.radix, this.options.groupSeparator, this.options.radixPoint,
this.options.padLength)
- );
+ _value: function(newVal) {
+ if (!arguments.length) {
+ var value = this._parse(this.element.val());
+ return isNaN(value) ? this.options.value : value;
+ }
+
+ this.element.val(this._format(newVal));
this.element.parents('.' + this.widgetBaseClass
+ ':first').attr('aria-valuenow', this.value());
},
@@ -423,6 +420,33 @@
$.widget.prototype._setData.call(this, key, value);
},
+ _format: function(num) {
+ var sym = this.options.currency ? this.options.currency : '',
+ dec = this.options.currency ? 2 : this._precision,
+ radix = this.options.currency ? 10 : this.options.radix,
+ group = this.options.currency ? (this.options.groupSeparator || ',') :
this.options.groupSeprator,
+ whole = Math.floor(Math.abs(num)),
+ result = whole.toString(radix),
+ part = Math.floor(((Math.abs(num) - whole) * Math.pow(radix,
dec))).toString(radix),
+ regex = /(\d+)(\d{3})/;
+
+ while (regex.test(result) && group) {
+ result = result.replace(regex, '$1'+group+'$2');
+ }
+
+ if (dec > 0) {
+ while (part.length < dec) {
+ part = '0' + part;
+ }
+ result += this.options.radixPoint + part;
+ }
+
+ while (this.options.padLength && (result.length <
this.options.padLength)) {
+ result = '0' + result;
+ }
+
+ return (num < 0 ? '-' : '') + sym + result;
+ },
destroy: function() {
if ($.fn.mousewheel) {
@@ -460,10 +484,10 @@
},
value: function(newVal) {
if (!arguments.length) {
- return this._getValue();
+ return this._value();
}
- this._setValue(newVal);
+ this._value(newVal);
},
stepUp: function(steps) {
this._spin((steps || 1) * this.options.step, null);
@@ -505,35 +529,6 @@
step: null,
value: 0,
width: false
- },
- format: {
- currency: function(num, sym, group, pt) {
- num = isNaN(num) ? 0 : num;
- return (num !== Math.abs(num) ? '-' : '') + sym +
this.number(Math.abs(num), 2, 10, group || ',', pt);
- },
- number: function(num, dec, radix, group, pt, padLength) {
- var whole = Math.floor(Math.abs(num)),
- result = whole.toString(radix),
- part = Math.floor(((Math.abs(num) - whole) * Math.pow(radix,
dec))).toString(radix),
- regex = /(\d+)(\d{3})/;
-
- while (regex.test(result) && group) {
- result=result.replace(regex, '$1'+group+'$2');
- }
-
- if (dec > 0) {
- while (part.length < dec) {
- part = '0' + part;
- }
- result += pt + part;
- }
-
- while (padLength && (result.length < padLength)) {
- result = '0' + result;
- }
-
- return (num < 0 ? '-' : '') + result;
- }
}
});