Skip to content

Commit

Permalink
Dialog: Save the active element that opened the dialog and restore fo…
Browse files Browse the repository at this point in the history
…cus to that. Fixes #8730 - Dialog: Restore focus to opener.
  • Loading branch information
jzaefferer committed Oct 24, 2012
1 parent 2a2a2c0 commit 14691ae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
18 changes: 12 additions & 6 deletions tests/visual/dialog/complex-dialogs.html
Expand Up @@ -22,13 +22,13 @@

<script>
$(function() {
$( "#dialog" ).dialog({
modal: true,
height: 300,
width: 500
});
var dialog = $( "#dialog" ).dialog({
modal: true,
height: 300,
width: 500
}),

var datepickerDialog = $( "#dialog-datepicker" ).dialog({
datepickerDialog = $( "#dialog-datepicker" ).dialog({
autoOpen: false,
modal: true
}),
Expand All @@ -39,6 +39,10 @@
width: 600
});

$( "#open-dialog" ).click(function() {
dialog.dialog( "open" );
});

$( "#open-datepicker" ).click(function() {
datepickerDialog.dialog( "open" );
});
Expand Down Expand Up @@ -70,6 +74,8 @@

<a href="#">Outside link</a>

<button id="open-dialog">Reopen dialog</button>

<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
<p><button id="open-datepicker">Open another window with a datepicker.</button></p>
Expand Down
9 changes: 9 additions & 0 deletions ui/jquery.ui.dialog.js
Expand Up @@ -248,6 +248,13 @@ $.widget("ui.dialog", {
this.overlay.destroy();
}

if ( !this.opener.filter( ":focusable" ).focus().length ) {

This comment has been minimized.

Copy link
@usmonster

usmonster Nov 5, 2014

Contributor

This call to .focus() will trigger unwanted scrolling in some cases. Maybe wrap this in a save/restore of the scrollTop/scrollLeft of the opener's scroll parent (if there is one)? Issue filed as http://bugs.jqueryui.com/ticket/10686. Please add your thoughts!

// Hiding a focused element doesn't trigger blur in WebKit
// so in case we have nothing to focus on, explicitly blur the active element
// https://bugs.webkit.org/show_bug.cgi?id=47182
$( this.document[ 0 ].activeElement ).blur();
}

if ( this.options.hide ) {
this._hide( this.uiDialog, this.options.hide, function() {
that._trigger( "close", event );
Expand Down Expand Up @@ -278,6 +285,8 @@ $.widget("ui.dialog", {
options = this.options,
uiDialog = this.uiDialog;

this.opener = $( this.document[ 0 ].activeElement );

this._size();
this._position( options.position );
uiDialog.show( options.show );
Expand Down

0 comments on commit 14691ae

Please sign in to comment.