Skip to content

Commit a75731a

Browse files
committed
Dialog: Extracting wrapper creation into _createWrapper. Merging the two keydown handlers into one.
1 parent bc3ea22 commit a75731a

File tree

1 file changed

+62
-64
lines changed

1 file changed

+62
-64
lines changed

ui/jquery.ui.dialog.js

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -90,81 +90,26 @@ $.widget("ui.dialog", {
9090
parent: this.element.parent(),
9191
index: this.element.parent().children().index( this.element )
9292
};
93-
var that = this,
94-
options = this.options,
95-
uiDialogButtonPane;
96-
97-
// TODO extract into _createWrapper
98-
this.uiDialog = $( "<div>" )
99-
.addClass( uiDialogClasses + options.dialogClass )
100-
.hide()
101-
.attr({
102-
// setting tabIndex makes the div focusable
103-
tabIndex: -1,
104-
role: "dialog"
105-
})
106-
.keydown(function( event ) {
107-
if ( options.closeOnEscape && !event.isDefaultPrevented() && event.keyCode &&
108-
event.keyCode === $.ui.keyCode.ESCAPE ) {
109-
that.close( event );
110-
event.preventDefault();
111-
}
112-
})
113-
.mousedown(function( event ) {
114-
if ( that._moveToTop( event ) ) {
115-
that._focusTabbable();
116-
}
117-
})
118-
.appendTo( this.document[ 0 ].body );
11993

120-
this.element
121-
.show()
122-
.removeAttr( "title" )
123-
.addClass( "ui-dialog-content ui-widget-content" )
124-
.appendTo( this.uiDialog );
94+
this._createWrapper();
95+
96+
this.element
97+
.show()
98+
.removeAttr( "title" )
99+
.addClass( "ui-dialog-content ui-widget-content" )
100+
.appendTo( this.uiDialog );
125101

126102
this._createTitlebar();
127103
this._createButtonPane();
128104

129-
// TODO move into _createWrapper
130-
// We assume that any existing aria-describedby attribute means
131-
// that the dialog content is marked up properly
132-
// otherwise we brute force the content as the description
133-
if ( !this.element.find( "[aria-describedby]" ).length ) {
134-
this.uiDialog.attr({
135-
"aria-describedby": this.element.uniqueId().attr( "id" )
136-
});
137-
}
138-
139-
if ( options.draggable && $.fn.draggable ) {
105+
if ( this.options.draggable && $.fn.draggable ) {
140106
this._makeDraggable();
141107
}
142-
if ( options.resizable && $.fn.resizable ) {
108+
if ( this.options.resizable && $.fn.resizable ) {
143109
this._makeResizable();
144110
}
145111

146112
this._isOpen = false;
147-
148-
// prevent tabbing out of dialogs
149-
// TODO move into _createWrapper
150-
// TODO fix formatting
151-
this._on( this.uiDialog, { keydown: function( event ) {
152-
if ( event.keyCode !== $.ui.keyCode.TAB ) {
153-
return;
154-
}
155-
156-
var tabbables = $( ":tabbable", this.uiDialog ),
157-
first = tabbables.filter( ":first" ),
158-
last = tabbables.filter( ":last" );
159-
160-
if ( ( event.target === last[ 0 ] || event.target === this.uiDialog[ 0 ] ) && !event.shiftKey ) {
161-
first.focus( 1 );
162-
return false;
163-
} else if ( ( event.target === first[ 0 ] || event.target === this.uiDialog[ 0 ] ) && event.shiftKey ) {
164-
last.focus( 1 );
165-
return false;
166-
}
167-
}});
168113
},
169114

170115
_init: function() {
@@ -312,6 +257,59 @@ $.widget("ui.dialog", {
312257
this._delay( checkFocus );
313258
},
314259

260+
_createWrapper: function() {
261+
this.uiDialog = $( "<div>" )
262+
.addClass( uiDialogClasses + this.options.dialogClass )
263+
.hide()
264+
.attr({
265+
// setting tabIndex makes the div focusable
266+
tabIndex: -1,
267+
role: "dialog"
268+
})
269+
.appendTo( this.document[ 0 ].body );
270+
271+
this._on( this.uiDialog, {
272+
keydown: function( event ) {
273+
if ( this.options.closeOnEscape && !event.isDefaultPrevented() && event.keyCode &&
274+
event.keyCode === $.ui.keyCode.ESCAPE ) {
275+
event.preventDefault();
276+
this.close( event );
277+
return;
278+
}
279+
280+
// prevent tabbing out of dialogs
281+
if ( event.keyCode !== $.ui.keyCode.TAB ) {
282+
return;
283+
}
284+
var tabbables = $( ":tabbable", this.uiDialog ),
285+
first = tabbables.filter( ":first" ),
286+
last = tabbables.filter( ":last" );
287+
288+
if ( ( event.target === last[ 0 ] || event.target === this.uiDialog[ 0 ] ) && !event.shiftKey ) {
289+
first.focus( 1 );
290+
return false;
291+
} else if ( ( event.target === first[ 0 ] || event.target === this.uiDialog[ 0 ] ) && event.shiftKey ) {
292+
last.focus( 1 );
293+
return false;
294+
}
295+
},
296+
mousedown: function( event ) {
297+
if ( this._moveToTop( event ) ) {
298+
this._focusTabbable();
299+
}
300+
}
301+
});
302+
303+
// We assume that any existing aria-describedby attribute means
304+
// that the dialog content is marked up properly
305+
// otherwise we brute force the content as the description
306+
if ( !this.element.find( "[aria-describedby]" ).length ) {
307+
this.uiDialog.attr({
308+
"aria-describedby": this.element.uniqueId().attr( "id" )
309+
});
310+
}
311+
},
312+
315313
_createTitlebar: function() {
316314
var uiDialogTitle;
317315

0 commit comments

Comments
 (0)