]> source.dussan.org Git - jquery-ui.git/commitdiff
Dialog: Prevent dialog form losing focus (or move it back in IE <= 8).
authorJörn Zaefferer <joern.zaefferer@gmail.com>
Tue, 23 Oct 2012 00:33:15 +0000 (20:33 -0400)
committerJörn Zaefferer <joern.zaefferer@gmail.com>
Wed, 24 Oct 2012 20:57:13 +0000 (16:57 -0400)
tests/visual/dialog/complex-dialogs.html
ui/jquery.ui.dialog.js

index 556336d0092df059f50d81a376a47108a224cbc8..634bfb3d60306747b4043ab1946d73e12f51a6e5 100644 (file)
@@ -30,7 +30,7 @@
 
                var datepickerDialog = $( "#dialog-datepicker" ).dialog({
                                autoOpen: false,
-                               modal: true,
+                               modal: true
                        }),
 
                        autocompleteDialog = $( "#dialog-autocomplete" ).dialog({
index 93a49cfd6bd8803a38d2d0e0af4c9685c164612e..2b9846b3a0afa4f0c860d574a642ac5f8688b37d 100644 (file)
@@ -256,8 +256,6 @@ $.widget("ui.dialog", {
                        this.uiDialog.hide();
                        this._trigger( "close", event );
                }
-
-               return this;
        },
 
        isOpen: function() {
@@ -305,6 +303,23 @@ $.widget("ui.dialog", {
                return this;
        },
 
+       _keepFocus: function( event ) {
+               function checkFocus() {
+                       var activeElement = this.document[ 0 ].activeElement,
+                               isActive = this.uiDialog[ 0 ] === activeElement ||
+                                       $.contains( this.uiDialog[ 0 ], activeElement );
+                       if ( !isActive ) {
+                               this.uiDialog.focus();
+                       }
+               }
+               event.preventDefault();
+               checkFocus.call( this );
+               // support: IE
+               // IE <= 8 doesn't prevent moving focus even with event.preventDefault()
+               // so we check again later
+               this._delay( checkFocus );
+       },
+
        _createButtons: function( buttons ) {
                var that = this,
                        hasButtons = false;
@@ -648,6 +663,10 @@ $.extend( $.ui.dialog.overlay, {
 
                $el.appendTo( document.body );
 
+               $el.bind( "mousedown", function( event ) {
+                       dialog._keepFocus( event );
+               });
+
                if ( $.fn.bgiframe ) {
                        $el.bgiframe();
                }