Permalink
Browse files

Dialog: Modified how finding the max z-index was done, should improve…

… performance as well. Prevented dialogs from infinitely increasing z-index. Fixed #5388 - Dialog: Don't change z-index when already at the top
  • Loading branch information...
1 parent dcac8c1 commit b2eda4d30a1a86fdcd866f80012545d47cf7e0d6 @Akkuma committed Oct 31, 2011
Showing with 85 additions and 31 deletions.
  1. +36 −0 tests/unit/dialog/dialog_tickets.js
  2. +49 −31 ui/jquery.ui.dialog.js
@@ -34,6 +34,42 @@ asyncTest( "#3123: Prevent tabbing out of modal dialogs", function() {
}
});
+test("#5388: Don't change z-index when already at the top", function() {
+ expect(8);
+
+ d1 = $('<div></div>').appendTo(document.body).dialog({ modal: true, autoOpen: false });
+ d2 = $('<div></div>').appendTo(document.body).dialog({ modal: true, autoOpen: false });
+ d3 = $('<div></div>').appendTo(document.body).dialog({ modal: true, autoOpen: false });
+
+ for (var i=0; i < 10; i++) {
+ d1.dialog('open').dialog('close');
+ d2.dialog('open').dialog('close');
+ }
+
+ equals($.ui.dialog.maxZ, 1000, 'MaxZ set to original');
+ d1.dialog('open');
+ equals(d1.dialog('widget').css('zIndex'), '1002', 'One dialog open maintained proper z-index');
+ d2.dialog('open');
+ equals(d2.dialog('widget').css('zIndex'), '1004', 'Two dialogs opened maintained proper z-index');
+ d1.dialog('close');
+
+ equals($.ui.dialog.maxZ, 1004, 'MaxZ set correctly');
+
+ equals(d2.dialog('widget').css('zIndex'), '1004', 'Second dialog remained open and first closed maintained proper z-index');
+ d3.dialog('open');
+ equals(d3.dialog('widget').css('zIndex'), '1006', 'Opened a third dialog and maintained proper z-index');
+ d1.dialog('open');
+ equals(d1.dialog('widget').css('zIndex'), '1008', 'Reopened first dialog and maintained proper z-index');
+
+ d1.dialog('close');
+ d2.remove();
+ d3.dialog('close');
+ equals($.ui.dialog.maxZ, 1000, 'Reset to original');
+
+ d1.remove();
+ d3.remove();
+});
+
test("#4826: setting resizable false toggles resizable on dialog", function() {
expect(6);
View
@@ -33,7 +33,27 @@ var uiDialogClasses = "ui-dialog ui-widget ui-widget-content ui-corner-all ",
maxWidth: true,
minHeight: true,
minWidth: true
- };
+ },
+ dialogs = {};
+
+ function getUIDialogUUID($uiDialog) {
+ return $uiDialog.attr('aria-labelledby');
+ }
+
+ function updateMaxZ($currentDialog) {
+ var self = this,
+ maxZ = self.options.zIndex;
+
+ $.each(dialogs, function() {
+ if (this.uiDialog[0] !== self.uiDialog[0] && this.isOpen) {
+ thisZ = this.uiDialog.css('z-index');
+ if(!isNaN(thisZ)) {
+ maxZ = Math.max(maxZ, thisZ);
+ }
+ }
+ });
+ $.ui.dialog.maxZ = maxZ;
+ }
$.widget("ui.dialog", {
version: "@VERSION",
@@ -159,6 +179,11 @@ $.widget("ui.dialog", {
if ( $.fn.bgiframe ) {
uiDialog.bgiframe();
}
+
+ dialogs[titleId] = {
+ 'uiDialog': uiDialog,
+ isOpen: false
+ };
},
_init: function() {
@@ -174,6 +199,8 @@ $.widget("ui.dialog", {
self.overlay.destroy();
}
self.uiDialog.hide();
+ delete dialogs[getUIDialogUUID(self.uiDialog)];
+
self.element
.removeClass( "ui-dialog-content ui-widget-content" )
.hide()
@@ -202,6 +229,7 @@ $.widget("ui.dialog", {
}
self._isOpen = false;
+ dialogs[getUIDialogUUID(self.uiDialog)].isOpen = false;
if ( self.overlay ) {
self.overlay.destroy();
@@ -220,17 +248,8 @@ $.widget("ui.dialog", {
$.ui.dialog.overlay.resize();
// adjust the maxZ to allow other modal dialogs to continue to work (see #4309)
- if ( self.options.modal ) {
- maxZ = 0;
- $( ".ui-dialog" ).each(function() {
- if ( this !== self.uiDialog[0] ) {
- thisZ = $( this ).css( "z-index" );
- if ( !isNaN( thisZ ) ) {
- maxZ = Math.max( maxZ, thisZ );
- }
- }
- });
- $.ui.dialog.maxZ = maxZ;
+ if (self.options.modal) {
+ updateMaxZ.call(self);
}
return self;
@@ -252,26 +271,24 @@ $.widget("ui.dialog", {
return self._trigger( "focus", event );
}
- if ( options.zIndex > $.ui.dialog.maxZ ) {
- $.ui.dialog.maxZ = options.zIndex;
- }
- if ( self.overlay ) {
+ //moveToTop method does not have a true/false passed down from dialog('moveToTop')
+ if (force || force === undefined) {
+ updateMaxZ.call(self);
+
+ if (self.overlay) {
+ $.ui.dialog.maxZ += 1;
+ self.overlay.$el.css('z-index', $.ui.dialog.overlay.maxZ = $.ui.dialog.maxZ);
+ }
+
+ //Save and then restore scroll since Opera 9.5+ resets when parent z-Index is changed.
+ // http://ui.jquery.com/bugs/ticket/3193
+ saveScroll = { scrollTop: self.element.scrollTop(), scrollLeft: self.element.scrollLeft() };
$.ui.dialog.maxZ += 1;
- $.ui.dialog.overlay.maxZ = $.ui.dialog.maxZ;
- self.overlay.$el.css( "z-index", $.ui.dialog.overlay.maxZ );
+ self.uiDialog.css('z-index', $.ui.dialog.maxZ);
+ self.element.attr(saveScroll);
}
-
- // Save and then restore scroll
- // Opera 9.5+ resets when parent z-index is changed.
- // http://bugs.jqueryui.com/ticket/3193
- saveScroll = {
- scrollTop: self.element.scrollTop(),
- scrollLeft: self.element.scrollLeft()
- };
- $.ui.dialog.maxZ += 1;
- self.uiDialog.css( "z-index", $.ui.dialog.maxZ );
- self.element.attr( saveScroll );
- self._trigger( "focus", event );
+
+ self._trigger('focus', event);
return self;
},
@@ -324,7 +341,8 @@ $.widget("ui.dialog", {
hasFocus.eq( 0 ).focus();
self._isOpen = true;
- self._trigger( "open" );
+ dialogs[getUIDialogUUID(uiDialog)].isOpen = true;
+ self._trigger('open');
return self;
},

0 comments on commit b2eda4d

Please sign in to comment.