r3066 committed - spinner: start/spin events now cancelable

r3066 committed - spinner: start/spin events now cancelable


Revision: 3066
Author: pazu2k@gmail.com
Date: Mon Aug 17 07:56:54 2009
Log: spinner: start/spin events now cancelable
http://code.google.com/p/jquery-ui/source/detail?r=3066
Modified:
/branches/dev/spinner/ui/ui.spinner.js
=======================================
--- /branches/dev/spinner/ui/ui.spinner.js    Mon Aug 17 05:50:30 2009
+++ /branches/dev/spinner/ui/ui.spinner.js    Mon Aug 17 07:56:54 2009
@@ -113,8 +113,7 @@
                }
            })
            .bind('keydown'+namespace, function(event) {
-                self._start(event);
-                return self._keydown(event);
+                return self._start(event) ? self._keydown(event) : false;
            })
            .bind('keyup'+namespace, function(event) {
                self._stop(event);
@@ -149,7 +148,9 @@
        // button bindings
        this.buttons = uiSpinner.find('.ui-spinner-button')
            .bind('mousedown', function(event) {
-                self._start(event);
+                if (self._start(event) === false) {
+                    return false;
+                }
                self._repeat(null, $(this).hasClass('ui-spinner-up') ? 1 : -1, event);
                if (!self.options.disabled && !self.options.readOnly) {
@@ -207,15 +208,14 @@
        this._constrain();
    },
    _start: function(event) {
-        if (!this.spinning && !this.options.readOnly) {
+        if (!this.spinning && !this.options.readOnly && this._trigger('start',
event, { value: this.value()}) !== false) {
            if (!this.counter) {
                this.counter = 1;
            }
            this.spinning = true;
-            this._trigger('start', event, {
-                value: this.value()
-            });
-        }
+            return true;
+        }
+        return false
    },
    _spin: function(step, event) {
        if (this.options.disabled || this.options.readOnly) {
@@ -225,16 +225,18 @@
            this.counter = 1;
        }
-        this._value(this._value() + step * (this.options.incremental &&
this.counter > 100
+        var newVal = this._value() + step * (this.options.incremental &&
this.counter > 100
            ? this.counter > 200
                ? 100
                : 10
-            : 1));
-
-        this._constrain();
-        this.counter++;
-
-        this._trigger('spin', event, { value: this.value() });
+            : 1);
+
+        // cancelable
+        if (this._trigger('spin', event, { value: newVal }) !== false) {
+            this._value(newVal);
+            this._constrain();
+            this.counter++;
+        }
    },
    _stop: function(event) {
        this.counter = 0;