@@ -282,8 +282,7 @@ $.widget("ui.dialog", {
282
282
return ;
283
283
}
284
284
285
- var hasFocus ,
286
- options = this . options ,
285
+ var options = this . options ,
287
286
uiDialog = this . uiDialog ;
288
287
289
288
this . opener = $ ( this . document [ 0 ] . activeElement ) ;
@@ -294,22 +293,26 @@ $.widget("ui.dialog", {
294
293
this . moveToTop ( null , true ) ;
295
294
this . _show ( uiDialog , options . show ) ;
296
295
296
+ this . _focusTabbable ( ) ;
297
+
298
+ this . _isOpen = true ;
299
+ this . _trigger ( "open" ) ;
300
+ this . _trigger ( "focus" ) ;
301
+
302
+ return this ;
303
+ } ,
304
+
305
+ _focusTabbable : function ( ) {
297
306
// set focus to the first tabbable element in the content area or the first button
298
307
// if there are no tabbable elements, set focus on the dialog itself
299
- hasFocus = this . element . find ( ":tabbable" ) ;
308
+ var hasFocus = this . element . find ( ":tabbable" ) ;
300
309
if ( ! hasFocus . length ) {
301
310
hasFocus = this . uiDialogButtonPane . find ( ":tabbable" ) ;
302
311
if ( ! hasFocus . length ) {
303
- hasFocus = uiDialog ;
312
+ hasFocus = this . uiDialog ;
304
313
}
305
314
}
306
315
hasFocus . eq ( 0 ) . focus ( ) ;
307
-
308
- this . _isOpen = true ;
309
- this . _trigger ( "open" ) ;
310
- this . _trigger ( "focus" ) ;
311
-
312
- return this ;
313
316
} ,
314
317
315
318
_keepFocus : function ( event ) {
@@ -318,7 +321,7 @@ $.widget("ui.dialog", {
318
321
isActive = this . uiDialog [ 0 ] === activeElement ||
319
322
$ . contains ( this . uiDialog [ 0 ] , activeElement ) ;
320
323
if ( ! isActive ) {
321
- this . uiDialog . focus ( ) ;
324
+ this . _focusTabbable ( ) ;
322
325
}
323
326
}
324
327
event . preventDefault ( ) ;
@@ -656,6 +659,22 @@ $.extend( $.ui.dialog.overlay, {
656
659
// reuse old instances due to IE memory leak with alpha transparency (see #5185)
657
660
oldInstances : [ ] ,
658
661
create : function ( dialog ) {
662
+ if ( this . instances . length === 0 ) {
663
+ // prevent use of anchors and inputs
664
+ // we use a setTimeout in case the overlay is created from an
665
+ // event that we're going to be cancelling (see #2804)
666
+ setTimeout ( function ( ) {
667
+ // handle $(el).dialog().dialog('close') (see #4065)
668
+ if ( $ . ui . dialog . overlay . instances . length ) {
669
+ $ ( document ) . bind ( "focusin.dialog-overlay" , function ( event ) {
670
+ if ( ! $ ( event . target ) . closest ( ".ui-dialog" ) . length ) {
671
+ event . preventDefault ( ) ;
672
+ $ ( ".ui-dialog:visible:last .ui-dialog-content" ) . data ( "ui-dialog" ) . _focusTabbable ( ) ;
673
+ }
674
+ } ) ;
675
+ }
676
+ } , 1 ) ;
677
+ }
659
678
660
679
var $el = ( this . oldInstances . pop ( ) || $ ( "<div>" ) . addClass ( "ui-widget-overlay ui-front" ) ) ;
661
680
0 commit comments