r2983 commited - spinner: swapped options.hide for options.buttons and refactored auto-...

r2983 commited - spinner: swapped options.hide for options.buttons and refactored auto-...


Revision: 2983
Author: pazu2k@gmail.com
Date: Sun Jul 26 14:43:51 2009
Log: spinner: swapped options.hide for options.buttons and refactored
auto-hide feature.
http://code.google.com/p/jquery-ui/source/detail?r=2983
Modified:
/branches/dev/spinner/ui/ui.spinner.js
=======================================
--- /branches/dev/spinner/ui/ui.spinner.js    Tue Jul 21 09:04:51 2009
+++ /branches/dev/spinner/ui/ui.spinner.js    Sun Jul 26 14:43:51 2009
@@ -15,23 +15,23 @@
// shortcut constants
var hover = 'ui-state-hover',
    active = 'ui-state-active',
-    namespace = '.spinner';
+    namespace = '.spinner',
+    buttonRegex = /hide|auto|fast|slow|(\d+)/,
+    buttonDefault = 'show';
$.widget('ui.spinner', {
-    _init: function() {
-
+    _init: function() {
        this._initOptions();
        this._value(this._value() || this.options.value);
        this._draw();
-
-        if (this.options.hide) {
-            this._hide();
-        }
-
+
        this._mousewheel();
+        if (this.options.buttons !== buttonDefault) {
+            this.buttons.hide();
+        }
    },
    _initOptions: function() {
        var self = this,
@@ -90,15 +90,11 @@
                        $(this).addClass(hover);
                    }
                    self.hovered = true;
-                    if (typeof options.hide != 'boolean' && !self.focused
&& !options.disabled) {
-                        self._delay(self._show, 100, 'hide', options.hide);
-                    }
+                    self._hide(false);
                }, function() {
                    $(this).removeClass(hover);
                    self.hovered = false;
-                    if (typeof options.hide != 'boolean' && !self.focused) {
-                        self._delay(self._hide, 100, 'hide', options.hide);
-                    }
+                    self._hide(true);
                });
        // TODO: need a better way to exclude IE8 without resorting to
$.browser.version
@@ -133,18 +129,12 @@
                }
                widget.addClass(active);
                self.focused = true;
-                if (!self.hovered && typeof options.hide != 'boolean'
&& !options.disabled) {
-                    self._delay(self._show, 100, 'hide', options.hide);
-                }
            })
            .bind('blur'+namespace, function(event) {
                if (!self.hovered) {
                    widget.removeClass(active);
                }
                self.focused = false;
-                if (!self.hovered && typeof options.hide != 'boolean') {
-                    self._delay(self._hide, 100, 'hide', options.hide);
-                }
                self._cleanUp();
            });
@@ -342,65 +332,12 @@
        this._format(newVal);
        this.element.parents('.' + this.widgetBaseClass
+ ':first').attr('aria-valuenow', this.value());
    },
-
-    // delays a function call, allowing only one at a time of the same type
-    _delay: function(callback, delay, type) {
-        type = type || 'a';
-        if (!this.currentDelay) {
-            this.currentDelay = {};
-        }
-        var self = this,
-            curDelay = self.currentDelay[type] || {},
-            args = Array.prototype.slice.call(arguments, 3);
-
-        // reassign in case it's a new delay
-        self.currentDelay[type] = curDelay;
-
-        if (curDelay.i) {
-            // don't do anything if resetting the same delay
-            if (curDelay.f === callback) { return; }
-            clearTimeout(curDelay.i);
-        }
-
-        curDelay.f = callback;
-        curDelay.i = setTimeout(function() {
-            curDelay.i = 0;
-            curDelay.f.apply(self, args);
-        }, delay);
-    },
-    _show: function(speed) {
-        var buttons = this.buttons.stop();
-        if (!speed) {
-            buttons.css('opacity', 1);
-        } else {
-            buttons.fadeTo(speed, 1);
-        }
-        return this;
-    },
-    _hide: function(speed) {
-        var buttons = this.buttons.stop().removeClass(hover);
-        if (!speed) {
-            buttons.css('opacity', 0);
-        } else {
-            buttons.fadeTo(speed, 0);
-        }
-        return this;
-    },
    _setData: function(key, value) {
        $.widget.prototype._setData.call(this, key, value);
-        if (key == 'hide' && typeof value != 'boolean') {
-            this[this.hovered || this.focused ? '_show' : '_hide']();
-            return;
-        }
-        if (key == 'hide' && value) {
+        if (key === 'buttons') {
            this._hide();
-            return;
-        }
-        if (key == 'hide') {
-            this._show();
-            return;
-        }
+        }
        if (/min|max|step/.test(key) && this.options[key] != null) {
            // write attributes back to element if original exist
@@ -470,6 +407,27 @@
        this.element.val((num < 0 ? '-' : '') + sym + result);
    },
+    _hide: function(hide) {
+        if (this.options.buttons === buttonDefault) {
+            this.buttons.show();
+            return;
+        }
+
+        if (this.options.buttons === 'hide') {
+            this.buttons.hide();
+            return;
+        }
+
+        var self = this,
+            speed = this.options.buttons === 'auto' ? 400 : this.options.buttons;
+
+        if (this.timeout) {
+            window.clearTimeout(this.timeout);
+        }
+        this.timeout = window.setTimeout(function() {
+            self.buttons.animate({opacity: hide ? 'hide' : 'show'}, speed);
+        }, 200);
+    },
    destroy: function() {
        if ($.fn.mousewheel) {
@@ -532,10 +490,10 @@
    version: "@VERSION",
    eventPrefix: "spin",
    defaults: {
+        buttons: buttonDefault,
        currency: false,
        dir: 'ltr',
        groupSeparator: '',
-        hide: false,
        incremental: true,
        max: null,
        min: null,