Skip to content

Commit 1f946d5

Browse files
committed
Dialog: Focus tabbable only when dialog lost focus before.
1 parent f003dff commit 1f946d5

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

tests/unit/dialog/dialog_events.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,51 @@ test("open", function() {
3939
el.remove();
4040
});
4141

42+
43+
test( "focus", function() {
44+
expect( 5 );
45+
var el, other;
46+
el = $("#dialog1").dialog({
47+
autoOpen: false
48+
});
49+
other = $("#dialog2").dialog({
50+
autoOpen: false
51+
});
52+
53+
el.one( "dialogopen", function() {
54+
ok( true, "open, just once" );
55+
});
56+
el.one( "dialogfocus", function() {
57+
ok( true, "focus on open" );
58+
});
59+
other.dialog( "open" );
60+
61+
el.one( "dialogfocus", function() {
62+
ok( true, "when opening and already open and wasn't on top" );
63+
});
64+
other.dialog( "open" );
65+
el.dialog( "open" );
66+
67+
el.one( "dialogfocus", function() {
68+
ok( true, "when calling moveToTop and wasn't on top" );
69+
});
70+
other.dialog( "moveToTop" );
71+
el.dialog( "moveToTop" );
72+
73+
el.bind( "dialogfocus", function() {
74+
ok( true, "when mousedown anywhere on the dialog and it wasn't on top" );
75+
});
76+
other.dialog( "moveToTop" );
77+
el.trigger( "mousedown" );
78+
79+
// triggers just once when already on top
80+
el.dialog( "open" );
81+
el.dialog( "moveToTop" );
82+
el.trigger( "mousedown" );
83+
84+
el.add( other ).remove();
85+
});
86+
4287
test("dragStart", function() {
4388
expect(9);
4489

ui/jquery.ui.dialog.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ $.widget("ui.dialog", {
117117
}
118118
})
119119
.mousedown(function( event ) {
120-
that.moveToTop( event );
120+
if ( that._moveToTop( event ) ) {
121+
that._focusTabbable();
122+
}
121123
})
122124
.appendTo( this.document[ 0 ].body );
123125

@@ -292,18 +294,23 @@ $.widget("ui.dialog", {
292294
return this._isOpen;
293295
},
294296

295-
moveToTop: function( event, silent ) {
296-
var moved = this.uiDialog.nextAll( ":visible" ).insertBefore( this.uiDialog );
297-
if ( !silent && moved.length ) {
297+
moveToTop: function() {
298+
this._moveToTop();
299+
},
300+
301+
_moveToTop: function( event, silent ) {
302+
var moved = !!this.uiDialog.nextAll( ":visible" ).insertBefore( this.uiDialog ).length;
303+
if ( !silent && moved ) {
298304
this._trigger( "focus", event );
299305
}
306+
return moved;
300307
},
301308

302309
open: function() {
303310
if ( this._isOpen ) {
304-
this.moveToTop( null );
305-
// TODO run this only when dialog wasn't focused?
306-
this._focusTabbable();
311+
if ( this._moveToTop() ) {
312+
this._focusTabbable();
313+
}
307314
return;
308315
}
309316

@@ -316,7 +323,7 @@ $.widget("ui.dialog", {
316323
this._size();
317324
this._position( options.position );
318325
this.overlay = options.modal ? new $.ui.dialog.overlay( this ) : null;
319-
this.moveToTop( null, true );
326+
this._moveToTop( null, true );
320327
this._show( uiDialog, options.show );
321328

322329
this._focusTabbable();

0 commit comments

Comments
 (0)