Skip to content

Commit

Permalink
Backport of f999668 for 1-8-stable.
Browse files Browse the repository at this point in the history
Original commit message from f999668:
Dialog: Before handling escape key presses, check if the default action has been prevented. Fixes #6966 - Pressing ESC on dialog when 2 dialogs are open closes both dialogs.
  • Loading branch information
pelme committed Aug 2, 2011
1 parent 67ff57a commit abf97f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions tests/unit/dialog/dialog_tickets.js
Expand Up @@ -88,4 +88,29 @@ test("#6645: Missing element not found check in overlay", function(){
d1.add(d2).remove();
});

test("#6966: Escape key closes all dialogs, not the top one", function(){
expect(8);
// test with close function removing dialog
d1 = $('<div title="dialog 1">Dialog 1</div>').dialog({modal: true});
d2 = $('<div title="dialog 2">Dialog 2</div>').dialog({modal: true, close: function(){ d2.remove()}});
ok(d1.dialog("isOpen"), 'first dialog is open');
ok(d2.dialog("isOpen"), 'second dialog is open');
d2.simulate("keydown", {keyCode: $.ui.keyCode.ESCAPE});
ok(d1.dialog("isOpen"), 'first dialog still open');
ok(!d2.data('dialog'), 'second dialog is closed');
d2.remove();
d1.remove();

// test without close function removing dialog
d1 = $('<div title="dialog 1">Dialog 1</div>').dialog({modal: true});
d2 = $('<div title="dialog 2">Dialog 2</div>').dialog({modal: true});
ok(d1.dialog("isOpen"), 'first dialog is open');
ok(d2.dialog("isOpen"), 'second dialog is open');
d2.simulate("keydown", {keyCode: $.ui.keyCode.ESCAPE});
ok(d1.dialog("isOpen"), 'first dialog still open');
ok(!d2.dialog("isOpen"), 'second dialog is closed');
d2.remove();
d1.remove();
});

})(jQuery);
4 changes: 2 additions & 2 deletions ui/jquery.ui.dialog.js
Expand Up @@ -110,7 +110,7 @@ $.widget("ui.dialog", {
// setting tabIndex makes the div focusable
// setting outline to 0 prevents a border on focus in Mozilla
.attr('tabIndex', -1).css('outline', 0).keydown(function(event) {
if (options.closeOnEscape && event.keyCode &&
if (options.closeOnEscape && !event.isDefaultPrevented() && event.keyCode &&
event.keyCode === $.ui.keyCode.ESCAPE) {

self.close(event);
Expand Down Expand Up @@ -748,7 +748,7 @@ $.extend($.ui.dialog.overlay, {

// allow closing by pressing the escape key
$(document).bind('keydown.dialog-overlay', function(event) {
if (dialog.options.closeOnEscape && event.keyCode &&
if (dialog.options.closeOnEscape && !event.isDefaultPrevented() && event.keyCode &&
event.keyCode === $.ui.keyCode.ESCAPE) {

dialog.close(event);
Expand Down

0 comments on commit abf97f7

Please sign in to comment.