Skip to content

Commit d10440f

Browse files
committed
Draggable: Only focus the draggable if the event occurs on a handle
Refs #10527
1 parent 075421d commit d10440f

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

tests/unit/draggable/draggable_core.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,26 @@ test( "#8399: A draggable should become the active element after you are finishe
262262
strictEqual( document.activeElement, element.get( 0 ), "finishing moving a draggable anchor made it the active element" );
263263
});
264264

265-
asyncTest( "#4261: active element should blur when mousing down on a draggable", function() {
266-
expect( 2 );
265+
asyncTest( "blur behavior", function() {
266+
expect( 3 );
267+
268+
var element = $( "#draggable1" ).draggable(),
269+
focusElement = $( "<div tabindex='1'></div>" ).appendTo( element );
270+
271+
TestHelpers.onFocus( focusElement, function() {
272+
strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" );
267273

268-
var textInput = $( "<input>" ).appendTo( "#qunit-fixture" ),
269-
element = $( "#draggable1" ).draggable();
274+
TestHelpers.draggable.move( focusElement, 1, 1 );
270275

271-
TestHelpers.onFocus( textInput, function() {
272-
strictEqual( document.activeElement, textInput.get( 0 ), "ensure that a focussed text input is the active element before mousing down on a draggable" );
276+
// http://bugs.jqueryui.com/ticket/10527
277+
// Draggable: Can't select option in modal dialog (IE8)
278+
strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused after mousing down on itself" );
273279

274280
TestHelpers.draggable.move( element, 50, 50 );
275281

276-
notStrictEqual( document.activeElement, textInput.get( 0 ), "ensure the text input is no longer the active element after mousing down on a draggable" );
282+
// http://bugs.jqueryui.com/ticket/4261
283+
// active element should blur when mousing down on a draggable
284+
notStrictEqual( document.activeElement, focusElement.get( 0 ), "test element is no longer focused after mousing down on a draggable" );
277285
start();
278286
});
279287
});

ui/draggable.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,14 @@ $.widget("ui.draggable", $.ui.mouse, {
123123

124124
},
125125

126-
_blurActiveElement: function() {
126+
_blurActiveElement: function( event ) {
127127
var document = this.document[ 0 ];
128128

129+
// Only need to blur if the event occurred on the draggable itself, see #10527
130+
if ( !this.handleElement.is( event.target ) ) {
131+
return;
132+
}
133+
129134
// support: IE9
130135
// IE9 throws an "Unspecified error" accessing document.activeElement from an <iframe>
131136
try {
@@ -134,12 +139,8 @@ $.widget("ui.draggable", $.ui.mouse, {
134139
// If the <body> is blurred, IE will switch windows, see #9520
135140
if ( document.activeElement && document.activeElement.nodeName.toLowerCase() !== "body" ) {
136141

137-
// Only need to blur if the event occurred on the draggable, see #10527
138-
if ( this.handleElement.is( event.target ) ) {
139-
140-
// Blur any element that currently has focus, see #4261
141-
$( document.activeElement ).blur();
142-
}
142+
// Blur any element that currently has focus, see #4261
143+
$( document.activeElement ).blur();
143144
}
144145
} catch ( error ) {}
145146
},
@@ -289,7 +290,7 @@ $.widget("ui.draggable", $.ui.mouse, {
289290
return false;
290291
},
291292

292-
_mouseUp: function(event) {
293+
_mouseUp: function( event ) {
293294
//Remove frame helpers
294295
$("div.ui-draggable-iframeFix").each(function() {
295296
this.parentNode.removeChild(this);
@@ -300,8 +301,11 @@ $.widget("ui.draggable", $.ui.mouse, {
300301
$.ui.ddmanager.dragStop(this, event);
301302
}
302303

303-
// The interaction is over; whether or not the click resulted in a drag, focus the element
304-
this.element.focus();
304+
// Only need to focus if the event occurred on the draggable itself, see #10527
305+
if ( this.handleElement.is( event.target ) ) {
306+
// The interaction is over; whether or not the click resulted in a drag, focus the element
307+
this.element.focus();
308+
}
305309

306310
return $.ui.mouse.prototype._mouseUp.call(this, event);
307311
},

0 commit comments

Comments
 (0)