Skip to content

Commit

Permalink
Button: Don't fire click/change events if mouse was dragged during cl…
Browse files Browse the repository at this point in the history
…ick of toggle (checkbox/radio) button. Fixed #6970 - Button state inconsistencies after (accidental) drag-clicking the button.

(cherry picked from commit a69a178)
  • Loading branch information
kborchers authored and scottgonzalez committed May 13, 2011
1 parent 16b4ffb commit 427f3d4
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions ui/jquery.ui.button.js
Expand Up @@ -13,7 +13,7 @@
*/
(function( $, undefined ) {

var lastActive,
var lastActive, startXPos, startYPos, clickDragged,
baseClasses = "ui-button ui-widget ui-state-default ui-corner-all",
stateClasses = "ui-state-hover ui-state-active ",
typeClasses = "ui-button-icons-only ui-button-icon-only ui-button-text-icons ui-button-text-icon-primary ui-button-text-icon-secondary ui-button-text-only",
Expand Down Expand Up @@ -111,21 +111,44 @@ $.widget( "ui.button", {

if ( toggleButton ) {
this.element.bind( "change.button", function() {
if ( clickDragged ) {
return;
}
self.refresh();
});
// if mouse moves between mousedown and mouseup (drag) set clickDragged flag
// prevents issue where button state changes but checkbox/radio checked state
// does not in Firefox (see ticket #6970)
this.buttonElement
.bind( "mousedown.button", function( event ) {
if ( options.disabled ) {
return;
}
clickDragged = false;
startXPos = event.pageX;
startYPos = event.pageY;
})
.bind( "mouseup.button", function( event ) {
if ( options.disabled ) {
return;
}
if ( startXPos !== event.pageX || startYPos !== event.pageY ) {
clickDragged = true;
}
});
}

if ( this.type === "checkbox" ) {
this.buttonElement.bind( "click.button", function() {
if ( options.disabled ) {
if ( options.disabled || clickDragged ) {
return false;
}
$( this ).toggleClass( "ui-state-active" );
self.buttonElement.attr( "aria-pressed", self.element[0].checked );
});
} else if ( this.type === "radio" ) {
this.buttonElement.bind( "click.button", function() {
if ( options.disabled ) {
if ( options.disabled || clickDragged ) {
return false;
}
$( this ).addClass( "ui-state-active" );
Expand Down

0 comments on commit 427f3d4

Please sign in to comment.