@@ -284,59 +284,14 @@ $.widget("ui.dialog", {
284284 self . _position ( options . position ) ;
285285
286286 // Handle show animation
287- var fShowCallback = function ( event ) {
288- self . moveToTop ( true ) ;
289-
290- // prevent tabbing out of modal dialogs
291- if ( options . modal ) {
292- uiDialog . bind ( 'keypress.ui-dialog' , function ( event ) {
293- if ( event . keyCode !== $ . ui . keyCode . TAB ) {
294- return ;
295- }
296-
297- var tabbables = $ ( ':tabbable' , this ) ,
298- first = tabbables . filter ( ':first' ) ,
299- last = tabbables . filter ( ':last' ) ;
300-
301- if ( event . target === last [ 0 ] && ! event . shiftKey ) {
302- first . focus ( 1 ) ;
303- return false ;
304- } else if ( event . target === first [ 0 ] && event . shiftKey ) {
305- last . focus ( 1 ) ;
306- return false ;
307- }
308- } ) ;
309- }
310-
311- // set focus to the first tabbable element in the content area or the first button
312- // if there are no tabbable elements, set focus on the dialog itself
313- if ( ! options . focusSelector ) {
314- var arrTab = [ ] ,
315- $tab ;
316-
317- arrTab . push ( uiDialog . find ( '.ui-dialog-content :tabbable:not(' + options . focusFilter + '):first' ) . filter ( ':not(.close)' ) ) ;
318- arrTab . push ( uiDialog . find ( '.ui-dialog-buttonpane :tabbable:first' ) ) ;
319- arrTab . push ( uiDialog ) ;
320-
321- // Focus first populated selection
322- for ( var i = 0 ; i < arrTab . length ; i ++ ) {
323- $tab = arrTab [ i ] ;
324- if ( $tab . length ) {
325- $tab . focus ( ) ;
326- break ;
327- }
328- }
329- } else {
330- uiDialog . find ( options . focusSelector ) . focus ( ) ;
331- }
332- } ;
333-
334287 if ( options . show ) {
335- uiDialog . show ( options . show , fShowCallback ) ;
288+ uiDialog . show ( options . show , function ( ) {
289+ self . _handleFocus ( ) ;
290+ } ) ;
336291 } else {
337292 uiDialog . show ( ) ;
338293
339- fShowCallback ( ) ;
294+ self . _handleFocus ( ) ;
340295 }
341296
342297 self . _trigger ( 'open' ) ;
@@ -345,6 +300,57 @@ $.widget("ui.dialog", {
345300 return self ;
346301 } ,
347302
303+ _handleFocus : function ( ) {
304+ var self = this ,
305+ options = self . options ,
306+ uiDialog = self . uiDialog ;
307+
308+ self . moveToTop ( true ) ;
309+
310+ // prevent tabbing out of modal dialogs
311+ if ( options . modal ) {
312+ uiDialog . bind ( 'keypress.ui-dialog' , function ( event ) {
313+ if ( event . keyCode !== $ . ui . keyCode . TAB ) {
314+ return ;
315+ }
316+
317+ var tabbables = $ ( ':tabbable' , this ) ,
318+ first = tabbables . filter ( ':first' ) ,
319+ last = tabbables . filter ( ':last' ) ;
320+
321+ if ( event . target === last [ 0 ] && ! event . shiftKey ) {
322+ first . focus ( 1 ) ;
323+ return false ;
324+ } else if ( event . target === first [ 0 ] && event . shiftKey ) {
325+ last . focus ( 1 ) ;
326+ return false ;
327+ }
328+ } ) ;
329+ }
330+
331+ // set focus to the first tabbable element in the content area or the first button
332+ // if there are no tabbable elements, set focus on the dialog itself
333+ if ( ! options . focusSelector ) {
334+ var arrTab = [ ] ,
335+ $tab ;
336+
337+ arrTab . push ( uiDialog . find ( '.ui-dialog-content :tabbable:not(' + options . focusFilter + '):first' ) . filter ( ':not(.close)' ) ) ;
338+ arrTab . push ( uiDialog . find ( '.ui-dialog-buttonpane :tabbable:first' ) ) ;
339+ arrTab . push ( uiDialog ) ;
340+
341+ // Focus first populated selection
342+ for ( var i = 0 ; i < arrTab . length ; i ++ ) {
343+ $tab = arrTab [ i ] ;
344+ if ( $tab . length ) {
345+ $tab . focus ( ) ;
346+ break ;
347+ }
348+ }
349+ } else {
350+ uiDialog . find ( options . focusSelector ) . focus ( ) ;
351+ }
352+ } ,
353+
348354 _createButtons : function ( buttons ) {
349355 var self = this ,
350356 hasButtons = false ,
0 commit comments