Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 7becf2d

Browse files
author
Gabriel Schulhof
committed
Dialog: Do not re-create the close button if its location or text changes.
1 parent c293304 commit 7becf2d

File tree

1 file changed

+59
-51
lines changed

1 file changed

+59
-51
lines changed

js/widgets/dialog.js

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ define( [ "jquery",
1414

1515
$.widget( "mobile.dialog", {
1616
options: {
17-
closeBtn: "left", /* Accepts left, right and none */
17+
18+
// Accepts left, right and none
19+
closeBtn: "left",
1820
closeBtnText: "Close",
1921
overlayTheme: "a",
2022
corners: true
@@ -44,7 +46,7 @@ $.widget( "mobile.dialog", {
4446
$target = $( event.target ).closest( event.type === "vclick" ? "a" : "form" );
4547

4648
if ( $target.length && !$target.jqmData( "transition" ) ) {
47-
49+
attrs = {};
4850
attrs[ "data-" + $.mobile.ns + "transition" ] =
4951
( $.mobile.urlHistory.getActive() || {} )[ "transition" ] ||
5052
$.mobile.defaultDialogTransition;
@@ -54,90 +56,96 @@ $.widget( "mobile.dialog", {
5456
},
5557

5658
_create: function() {
57-
var $el = this.element,
58-
cornerClass = !!this.options.corners ? " ui-corner-all" : "",
59-
dialogWrap = $( "<div/>", {
60-
"role" : "dialog",
61-
"class" : "ui-dialog-contain ui-overlay-shadow" + cornerClass
62-
});
63-
64-
$el.addClass( "ui-dialog" );
65-
66-
// Class the markup for dialog styling
67-
// Set aria role
68-
$el.wrapInner( dialogWrap );
59+
var elem = this.element,
60+
opts = this.options;
61+
62+
// Class the markup for dialog styling and wrap interior
63+
elem.addClass( "ui-dialog" )
64+
.wrapInner( $( "<div/>", {
65+
66+
// ARIA role
67+
"role" : "dialog",
68+
"class" : "ui-dialog-contain ui-overlay-shadow" +
69+
( !!opts.corners ? " ui-corner-all" : "" )
70+
}));
71+
72+
$.extend( this, {
73+
_isCloseable: false,
74+
_inner: elem.children(),
75+
_headerCloseButton: null
76+
});
6977

70-
this._on( $el, {
78+
this._on( elem, {
7179
vclick: "_handleVClickSubmit",
7280
submit: "_handleVClickSubmit",
7381
pagebeforeshow: "_handlePageBeforeShow",
7482
pagebeforehide: "_handlePageBeforeHide"
7583
});
7684

77-
this._setCloseBtn( this.options.closeBtn );
85+
this._setCloseBtn( opts.closeBtn );
7886
},
7987

8088
_setOptions: function( options ) {
81-
var updateCloseButton;
89+
var closeButtonLocation, closeButtonText,
90+
currentOpts = this.options;
8291

8392
if ( options.corners !== undefined ) {
84-
this.element.children().toggleClass( "ui-corner-all", !!options.corners );
93+
this._inner.toggleClass( "ui-corner-all", !!options.corners );
8594
}
8695

8796
if ( options.overlayTheme !== undefined ) {
8897
if ( $.mobile.activePage[ 0 ] === this.element[ 0 ] ) {
89-
this.options.overlayTheme = options.overlayTheme;
98+
currentOpts.overlayTheme = options.overlayTheme;
9099
this._handlePageBeforeShow();
91100
}
92101
}
93102

94103
if ( options.closeBtnText !== undefined ) {
95-
this.options.closeBtnText = options.closeBtnText;
96-
updateCloseButton = this.options.closeBtn;
104+
closeButtonLocation = currentOpts.closeBtn;
105+
closeButtonText = options.closeBtnText;
97106
}
98107

99108
if ( options.closeBtn !== undefined ) {
100-
updateCloseButton = options.closeBtn;
109+
closeButtonLocation = options.closeBtn;
101110
}
102111

103-
if ( updateCloseButton ) {
104-
this._setCloseBtn( updateCloseButton );
112+
if ( closeButtonLocation ) {
113+
this._setCloseBtn( closeButtonLocation, closeButtonText );
105114
}
106115

107116
this._super( options );
108117
},
109118

110-
_setCloseBtn: function( value ) {
111-
var self = this, btn, location, dst;
119+
_setCloseBtn: function( location, text ) {
120+
var dst,
121+
btn = this._headerCloseButton;
112122

113-
if ( this._headerCloseButton ) {
114-
this._headerCloseButton.remove();
115-
this._headerCloseButton = null;
116-
}
117-
if ( value !== "none" ) {
118-
// Sanitize value
119-
location = ( value === "left" ? "left" : "right" );
120-
dst = this.element.children().find( ":jqmData(role='header')" ).first();
123+
// Sanitize value
124+
location = "left" === location ? "left" : "right" === location ? "right" : "none";
125+
126+
if ( "none" === location ) {
127+
if ( btn ) {
128+
btn.remove();
129+
btn = null;
130+
}
131+
} else if ( btn ) {
132+
btn.removeClass( "ui-btn-left ui-btn-right" ).addClass( "ui-btn-" + location );
133+
if ( text ) {
134+
btn.text( text );
135+
}
136+
} else {
137+
dst = this._inner.find( ":jqmData(role='header')" ).first();
121138
btn = $( "<a></a>", {
122-
"role": "button",
123-
"href": "#",
124-
"class": "ui-btn ui-btn-" + location +
125-
" ui-corner-all ui-icon-delete ui-btn-icon-notext"
139+
"role": "button",
140+
"href": "#",
141+
"class": "ui-btn ui-corner-all ui-icon-delete ui-btn-icon-notext ui-btn-" + location
126142
})
127-
.text( this.options.closeBtnText )
128-
.prependTo( dst )
129-
// this must be an anonymous function so that select menu dialogs can replace
130-
// the close method. This is a change from previously just defining data-rel=back
131-
// on the button and letting nav handle it
132-
//
133-
// Use click rather than vclick in order to prevent the possibility of unintentionally
134-
// reopening the dialog if the dialog opening item was directly under the close button.
135-
.bind( "click", function() {
136-
self.close();
137-
});
138-
139-
this._headerCloseButton = btn;
143+
.text( text || this.options.closeBtnText || "" )
144+
.prependTo( dst );
145+
this._on( btn, { click: "close" } );
140146
}
147+
148+
this._headerCloseButton = btn;
141149
},
142150

143151
// Close method goes back in history

0 commit comments

Comments
 (0)