Skip to content

Commit a4b7fea

Browse files
author
Alberto Monteiro
committed
Dialog: When destroy is called place the element back in original DOM position. Fixed #4980 - Dialog: Destroy should place element back in original DOM position
1 parent aa8c477 commit a4b7fea

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

tests/unit/dialog/dialog_tickets.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,13 @@ test("#6966: Escape key closes all dialogs, not the top one", function(){
140140
d1.remove();
141141
});
142142

143+
test("#4980: Destroy should place element back in original DOM position", function(){
144+
container = $('<div id="container"><div id="modal">Content</div></div>');
145+
modal = container.find('#modal');
146+
modal.dialog();
147+
ok(!$.contains(container[0], modal[0]), 'dialog should move modal element to outside container element');
148+
modal.dialog('destroy');
149+
ok($.contains(container[0], modal[0]), 'dialog(destroy) should place element back in original DOM position');
150+
});
151+
143152
})(jQuery);

ui/jquery.ui.dialog.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ $.widget("ui.dialog", {
7777
// #5742 - .attr() might return a DOMElement
7878
if ( typeof this.originalTitle !== "string" ) {
7979
this.originalTitle = "";
80-
}
81-
80+
}
81+
this.oldPosition = {
82+
parent: this.element.parent(),
83+
index: this.element.parent().children().index( this.element )
84+
};
8285
this.options.title = this.options.title || this.originalTitle;
8386
var self = this,
8487
options = self.options,
@@ -168,7 +171,8 @@ $.widget("ui.dialog", {
168171
},
169172

170173
_destroy: function() {
171-
var self = this;
174+
var self = this, next,
175+
oldPosition = this.oldPosition;
172176

173177
if ( self.overlay ) {
174178
self.overlay.destroy();
@@ -183,6 +187,13 @@ $.widget("ui.dialog", {
183187
if ( self.originalTitle ) {
184188
self.element.attr( "title", self.originalTitle );
185189
}
190+
191+
next = oldPosition.parent.children().eq( oldPosition.index );
192+
if ( next.length ) {
193+
next.before( self.element );
194+
} else {
195+
oldPosition.parent.append( self.element );
196+
}
186197
},
187198

188199
widget: function() {

0 commit comments

Comments
 (0)