r3196 committed - spinner: created getter/setter functions for min, max and step

r3196 committed - spinner: created getter/setter functions for min, max and step


Revision: 3196
Author: pazu2k@gmail.com
Date: Sun Sep 6 01:43:08 2009
Log: spinner: created getter/setter functions for min, max and step
http://code.google.com/p/jquery-ui/source/detail?r=3196
Modified:
/branches/dev/spinner/ui/ui.spinner.js
=======================================
--- /branches/dev/spinner/ui/ui.spinner.js    Thu Sep 3 22:19:40 2009
+++ /branches/dev/spinner/ui/ui.spinner.js    Sun Sep 6 01:43:08 2009
@@ -39,25 +39,12 @@
    _initOptions: function() {
        var self = this,
            options = self.options;
-
-        // 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 },
-            function(option, defaultValue) {
-                options[option] = self._parse(options[option] !== null
-                    ? options[option]
-                    : self.element.attr(option)
-                        ? self.element.attr(option)
-                        : defaultValue);
-            });
-
-        options.page = self._parse(options.page || 1);
-
+
        // check for precision in stepping and set _precision as internal
        var precision = parseInt(options.precision, 10);
-        if (options.step.toString().indexOf('.') != -1 && precision === 0) {
-            var s = options.step.toString();
+        if (self._step().toString().indexOf('.') != -1 && precision === 0) {
+            var s = self._step().toString();
            precision = s.slice(s.indexOf('.')+1, s.length).length;
        }
@@ -154,7 +141,7 @@
            })
            .bind('mouseup', function(event) {
                if (self.counter == 1) {
-                    self._spin(($(this).hasClass('ui-spinner-up') ? 1 : -1) *
options.step, event);
+                    self._spin(($(this).hasClass('ui-spinner-up') ? 1 : -1) *
self._step(), event);
                }
                if (self.spinning) {
                    self._stop(event);
@@ -242,7 +229,7 @@
            self._repeat(self.options.incremental && self.counter > 20 ? 20 : i,
steps, event);
        }, i);
-        self._spin(steps*self.options.step, event);
+        self._spin(steps*self._step(), event);
    },
    _keydown: function(event) {
        var o = this.options,
@@ -254,8 +241,8 @@
            case KEYS.PAGE_UP:         this._repeat(null, o.page, event); break;
            case KEYS.PAGE_DOWN:     this._repeat(null, -o.page, event); break;
-            case KEYS.HOME:         this._value(o.min); break;
-            case KEYS.END:             this._value(o.max); break;
+            case KEYS.HOME:         this._value(this._min()); break;
+            case KEYS.END:             this._value(this._max()); break;
            case KEYS.TAB:
            case KEYS.BACKSPACE:
@@ -287,7 +274,7 @@
                if (!self._start(event)) {
                    return false;
                }
-                self._spin((delta > 0 ? 1 : -1) * self.options.step, event);
+                self._spin((delta > 0 ? 1 : -1) * self._step(), event);
                if (self.timeout) {
                    window.clearTimeout(self.timeout);
                }
@@ -301,11 +288,21 @@
            });
        }
    },
-    _value: function(newValue) {
+    _value: function(newVal) {
        if (!arguments.length) {
            return this._parse(this.element.val());
        }
-        this._setData('value', newValue);
+        this._setData('value', newVal);
+    },
+    _getData: function(key) {
+        switch (key) {
+            case 'min':
+            case 'max':
+            case 'step':
+                return this['_'+key]();
+                break;
+        }
+        return $.widget.prototype._getData.call(this, key);
    },
    _setData: function(key, value) {
        switch (key) {
@@ -314,11 +311,11 @@
                break;
            case 'value':
                value = this._parse(value);
-                if (value < this.options.min) {
-                    value = this.options.min;
-                }
-                if (value > this.options.max) {
-                    value = this.options.max;
+                if (value < this._min()) {
+                    value = this._min();
+                }
+                if (value > this._max()) {
+                    value = this._max();
                }
                break;
            case 'spinnerClass':
@@ -357,8 +354,8 @@
    _aria: function() {
        this.uiSpinner
            && this.uiSpinner
-                .attr('aria-valuemin', this.options.min)
-                .attr('aria-valuemax', this.options.max)
+                .attr('aria-valuemin', this._min())
+                .attr('aria-valuemax', this._max())
                .attr('aria-valuenow', this.value());
    },
    _validChars: function() {
@@ -436,6 +433,31 @@
            self.buttons.animate({opacity: hide ? 'hide' : 'show'}, speed);
        }, 200);
    },
+    _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._setData('step', newVal);
+    },
+    _min: function(newVal) {
+        if (!arguments.length) {
+            return this._getOption('min', -Number.MAX_VALUE);
+        }
+        this._setData('min', newVal);
+    },
+    _max: function(newVal) {
+        if (!arguments.length) {
+            return this._getOption('max', Number.MAX_VALUE);
+        }
+        this._setData('max', newVal);
+    },
    destroy: function() {
        if ($.fn.mousewheel) {
@@ -474,16 +496,15 @@
    value: function(newVal) {
        if (!arguments.length) {
            return this._value();
-        }
-
+        }
        this._value(newVal);
    },
    stepUp: function(steps) {
-        this._spin((steps || 1) * this.options.step, null);
+        this._spin((steps || 1) * this._step(), null);
        return this;
    },
    stepDown: function(steps) {
-        this._spin((steps || 1) * -this.options.step, null);
+        this._spin((steps || 1) * -this._step(), null);
        return this;
    },
    pageUp: function(pages) {