]> source.dussan.org Git - jquery-ui.git/commitdiff
Spinner: Only trigger change when the field has blurred and the value has changed.
authorScott González <scott.gonzalez@gmail.com>
Sat, 6 Aug 2011 19:32:19 +0000 (15:32 -0400)
committerScott González <scott.gonzalez@gmail.com>
Sat, 6 Aug 2011 19:32:19 +0000 (15:32 -0400)
tests/unit/spinner/spinner_events.js
ui/jquery.ui.spinner.js

index c0771ca2f8f75b7cea2f30d6f61cac6c2efadf53..0f64067d7be2cb6b83fc6487b9497b870a6092be 100644 (file)
@@ -57,6 +57,7 @@ test("change", function() {
        });\r
 \r
        simulateKeyDownUp(el, $.ui.keyCode.UP);\r
+       el.blur();\r
        \r
        equals(change, 1, "Change triggered");\r
 });\r
index da140308e8a80c2a1d278f392fede7de27ffc4da..b090c61229c632a0153cf9c3d7d071d4b05e2a66 100644 (file)
@@ -72,14 +72,10 @@ $.widget( "ui.spinner", {
                                        event.preventDefault();
                                }
                        },
-                       keyup: function( event ) {
-                               if ( this.spinning ) {
-                                       this._stop( event );
-                                       this._change( event );
-                               }
-                       },
+                       keyup: "_stop",
                        focus: function() {
                                uiSpinner.addClass( "ui-state-active" );
+                               this.previous = this.options.value;
                        },
                        blur: function( event ) {
                                // don't clear invalid values on blur
@@ -90,6 +86,9 @@ $.widget( "ui.spinner", {
                                        this.element.val( value );
                                }
                                uiSpinner.removeClass( "ui-state-active" );
+                               if ( this.previous !== this.options.value ) {
+                                       this._trigger( "change", event );
+                               }
                        }
                });
 
@@ -112,13 +111,7 @@ $.widget( "ui.spinner", {
 
                                this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
                        },
-                       mouseup: function( event ) {
-                               if ( this.spinning ) {
-                                       this._stop( event );
-                                       // TODO: don't trigger change until the field is blurred
-                                       this._change( event );
-                               }
-                       },
+                       mouseup: "_stop",
                        mouseenter: function( event ) {
                                // button will add ui-state-active if mouse was down while mouseleave and kept down
                                if ( !$( event.currentTarget ).hasClass( "ui-state-active" ) ) {
@@ -133,12 +126,7 @@ $.widget( "ui.spinner", {
                        // TODO: do we really want to consider this a stop?
                        // shouldn't we just stop the repeater and wait until mouseup before
                        // we trigger the stop event?
-                       mouseleave: function( event ) {
-                               if ( this.spinning ) {
-                                       this._stop( event );
-                                       this._change( event );
-                               }
-                       }
+                       mouseleave: "_stop"
                });
 
                // disable spinner if element was already disabled
@@ -186,12 +174,10 @@ $.widget( "ui.spinner", {
                                }
 
                                this._spin( (delta > 0 ? 1 : -1) * this.options.step, event );
-                               clearTimeout( this.timeout );
-                               this.timeout = setTimeout(function() {
+                               clearTimeout( this.mousewheelTimer );
+                               this.mousewheelTimer = setTimeout(function() {
                                        if ( this.spinning ) {
                                                this._stop( event );
-                                               // TODO: don't trigger change until the field is blurred
-                                               this._change( event );
                                        }
                                }, 100 );
                                event.preventDefault();
@@ -276,16 +262,17 @@ $.widget( "ui.spinner", {
        },
 
        _stop: function( event ) {
+               if ( !this.spinning ) {
+                       return;
+               }
+
                clearTimeout( this.timer );
+               clearTimeout( this.mousewheelTimer );
                this.counter = 0;
                this.spinning = false;
                this._trigger( "stop", event );
        },
 
-       _change: function( event ) {
-               this._trigger( "change", event );
-       },
-
        _setOption: function( key, value ) {
                if ( key === "value") {
                        value = this._trimValue( this._parse(value) );