Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/visual/dialog/dialog_close_key_should_close_current.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Dialog Visual Test : Modal Dialog in Large DOM</title>
<link rel="stylesheet" href="../visual.css" type="text/css" />
<link rel="stylesheet" href="../../../themes/base/jquery.ui.all.css" type="text/css">
<script type="text/javascript" src="../../../jquery-1.5.1.js"></script>
<script type="text/javascript" src="../../../external/jquery.bgiframe-2.1.2.js"></script>
<script type="text/javascript" src="../../../ui/jquery.ui.core.js"></script>
<script type="text/javascript" src="../../../ui/jquery.ui.widget.js"></script>
<script type="text/javascript" src="../../../ui/jquery.ui.position.js"></script>
<script type="text/javascript" src="../../../ui/jquery.ui.dialog.js"></script>

<script>
$(function(){
var opt = {modal: true, width: 400, height: 400, overlay: { opacity: 0.5, background: 'black' }, autoOpen: false}
$( '#dialog1' ).dialog( opt ).css( 'background', 'white' );
$( '#dialog2' ).dialog( $.extend( opt, { width: 300, height: 300 }) ).css( 'background', 'white' );
});
</script>
</head>
<body>


<p><a href="#" onclick="$( '#dialog1' ).dialog( 'open' );">Click this link</a></p>


<div id="dialog1">
Hi
<a href="#" onclick="$( '#dialog2' ).dialog( 'open' );">Click Me</a>
<div id="dialog2"><a href="#" title="I am an anchor so I can show that I am focused">Close Me with "Esc"</a></div>
</div>


</body>
</html>
33 changes: 30 additions & 3 deletions ui/jquery.ui.dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ $.widget("ui.dialog", {
})
// setting tabIndex makes the div focusable
.attr( "tabIndex", -1)
.keydown(function( event ) {
.bind( "keydown.dialog-overlay", function( event ) {
if ( options.closeOnEscape && !event.isDefaultPrevented() && event.keyCode &&
event.keyCode === $.ui.keyCode.ESCAPE ) {
self.close( event );
Expand Down Expand Up @@ -191,7 +191,7 @@ $.widget("ui.dialog", {

close: function( event ) {
var self = this,
maxZ, thisZ;
maxZ, thisZ, top;

if ( false === self._trigger( "beforeClose", event ) ) {
return;
Expand Down Expand Up @@ -223,9 +223,13 @@ $.widget("ui.dialog", {
thisZ = $( this ).css( "z-index" );
if ( !isNaN( thisZ ) ) {
maxZ = Math.max( maxZ, thisZ );
top = $( this );
}
}
});
if ( top && top.is( ":visible" ) ) {
top.trigger( "focus", event );
}
$.ui.dialog.maxZ = maxZ;
}

Expand All @@ -235,6 +239,28 @@ $.widget("ui.dialog", {
isOpen: function() {
return this._isOpen;
},

isOnTop: function() {
var _isOnTop = false,
isAfter = false,
myZ;
if( this._isOpen ) {
myZ = $( this ).css( "z-index" )
$( ".ui-dialog" ).each(function() {
if ( this !== that.uiDialog[0] ) {
var thisZ = $( this ).css( "z-index" );
if ( !isNaN( thisZ ) && ( thisZ > myZ || ( isAfter && thisZ === myZ ) ) ) {
return false;
}
}
else {
isAfter = true;
}
_isOnTop = true;
});
}
return _isOnTop;
},

// the force parameter allows us to move modal dialogs to their correct
// position on open
Expand Down Expand Up @@ -686,6 +712,7 @@ $.extend( $.ui.dialog.overlay, {
}
).join( " " ),
create: function( dialog ) {
var that = this;
if ( this.instances.length === 0 ) {
// prevent use of anchors and inputs
// we use a setTimeout in case the overlay is created from an
Expand All @@ -706,7 +733,7 @@ $.extend( $.ui.dialog.overlay, {
// allow closing by pressing the escape key
$( document ).bind( "keydown.dialog-overlay", function( event ) {
if ( dialog.options.closeOnEscape && !event.isDefaultPrevented() && event.keyCode &&
event.keyCode === $.ui.keyCode.ESCAPE ) {
event.keyCode === $.ui.keyCode.ESCAPE && that.isOnTop() ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a issue that if the focus is not on the dialog then the following error occurs, however if the focus is on the dialog the dialog closes properly.

Uncaught TypeError: Object function (dialog) {
this.$el = $.ui.dialog.overlay.create(dialog);
} has no method 'isOnTop'


dialog.close( event );
event.preventDefault();
Expand Down