Skip to content

Draggable: fix spurious blur in dialogs on mousedown #1730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/unit/draggable/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,36 @@ QUnit.test( "blur behavior - descendant of handle", function( assert ) {
} );
} );

QUnit.test( "blur behavior - off handle", function( assert ) {
var ready = assert.async();
assert.expect( 3 );

var element = $( "#draggable2" ).draggable( { handle: "span" } ),
focusElement = $( "<div tabindex='1'></div>" ).appendTo( element );

// mock $.ui.safeBlur with a spy
var _safeBlur = $.ui.safeBlur;
var blurCalledCount = 0;
$.ui.safeBlur = function() {
blurCalledCount++;
};

testHelper.onFocus( focusElement, function() {
assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" );

testHelper.move( element, 1, 1 );
assert.strictEqual( blurCalledCount, 0, "draggable doesn't blur when mousing down off handle" );

testHelper.move( element.find( "span" ), 1, 1 );
assert.strictEqual( blurCalledCount, 1, "draggable blurs when mousing down on handle" );

// Restore safeBlur
$.ui.safeBlur = _safeBlur;

ready();
} );
} );

QUnit.test( "ui-draggable-handle assigned to appropriate element", function( assert ) {
assert.expect( 5 );

Expand Down
11 changes: 5 additions & 6 deletions ui/widgets/draggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ $.widget( "ui.draggable", $.ui.mouse, {
_mouseCapture: function( event ) {
var o = this.options;

this._blurActiveElement( event );

// Among others, prevent a drag on a resizable-handle
if ( this.helper || o.disabled ||
$( event.target ).closest( ".ui-resizable-handle" ).length > 0 ) {
Expand All @@ -117,6 +115,8 @@ $.widget( "ui.draggable", $.ui.mouse, {
return false;
}

this._blurActiveElement( event );

this._blockFrames( o.iframeFix === true ? "iframe" : o.iframeFix );

return true;
Expand Down Expand Up @@ -147,11 +147,10 @@ $.widget( "ui.draggable", $.ui.mouse, {
var activeElement = $.ui.safeActiveElement( this.document[ 0 ] ),
target = $( event.target );

// Only blur if the event occurred on an element that is:
// 1) within the draggable handle
// 2) but not within the currently focused element
// Don't blur if the event occurred on an element that is within
// the currently focused element
// See #10527, #12472
if ( this._getHandle( event ) && target.closest( activeElement ).length ) {
if ( target.closest( activeElement ).length ) {
return;
}

Expand Down