Skip to content

Commit f63bb4f

Browse files
committed
Menu: Minor cleanup.
1 parent 03da6e4 commit f63bb4f

File tree

1 file changed

+52
-43
lines changed

1 file changed

+52
-43
lines changed

ui/jquery.ui.menu.js

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ $.widget( "ui.menu", {
3636
_create: function() {
3737
this.activeMenu = this.element;
3838
this.menuId = this.element.attr( "id" ) || "ui-menu-" + idIncrement++;
39-
if ( this.element.find( ".ui-icon" ).length ) {
40-
this.element.addClass( "ui-menu-icons" );
41-
}
4239
this.element
4340
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
41+
.toggleClass( "ui-menu-icons", !!this.element.find( ".ui-icon" ).length )
4442
.attr({
4543
id: this.menuId,
4644
role: this.options.role,
@@ -95,9 +93,9 @@ $.widget( "ui.menu", {
9593
target.siblings().children( ".ui-state-active" ).removeClass( "ui-state-active" );
9694
this.focus( event, target );
9795
},
98-
"mouseleave": "collapseAll",
96+
mouseleave: "collapseAll",
9997
"mouseleave .ui-menu": "collapseAll",
100-
"focus": function( event ) {
98+
focus: function( event ) {
10199
var menu = this.element,
102100
firstItem = menu.children( ".ui-menu-item" ).eq( 0 );
103101
if ( this._hasScroll() && !this.active ) {
@@ -120,11 +118,13 @@ $.widget( "ui.menu", {
120118
}
121119
});
122120
},
123-
"keydown": "_keydown"
121+
keydown: "_keydown"
124122
});
125123

126124
this.refresh();
127125

126+
// TODO: We probably shouldn't bind to document for each menu.
127+
// TODO: This isn't being cleaned up on destroy.
128128
this._bind( this.document, {
129129
click: function( event ) {
130130
if ( !$( event.target ).closest( ".ui-menu" ).length ) {
@@ -149,7 +149,6 @@ $.widget( "ui.menu", {
149149

150150
// destroy menu items
151151
this.element.find( ".ui-menu-item" )
152-
.unbind( ".menu" )
153152
.removeClass( "ui-menu-item" )
154153
.removeAttr( "role" )
155154
.children( "a" )
@@ -158,6 +157,7 @@ $.widget( "ui.menu", {
158157
.removeAttr( "role" )
159158
.removeAttr( "aria-haspopup" )
160159
.removeAttr( "id" )
160+
// TODO: is this correct? Don't these exist in the original markup?
161161
.children( ".ui-icon" )
162162
.remove();
163163

@@ -166,82 +166,80 @@ $.widget( "ui.menu", {
166166
},
167167

168168
_keydown: function( event ) {
169+
var match, prev, character, skip,
170+
preventDefault = true;
171+
172+
function escape( value ) {
173+
return value.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&" );
174+
}
175+
169176
switch ( event.keyCode ) {
170177
case $.ui.keyCode.PAGE_UP:
171178
this.previousPage( event );
172-
event.preventDefault();
173179
break;
174180
case $.ui.keyCode.PAGE_DOWN:
175181
this.nextPage( event );
176-
event.preventDefault();
177182
break;
178183
case $.ui.keyCode.HOME:
179184
this._move( "first", "first", event );
180-
event.preventDefault();
181185
break;
182186
case $.ui.keyCode.END:
183187
this._move( "last", "last", event );
184-
event.preventDefault();
185188
break;
186189
case $.ui.keyCode.UP:
187190
this.previous( event );
188-
event.preventDefault();
189191
break;
190192
case $.ui.keyCode.DOWN:
191193
this.next( event );
192-
event.preventDefault();
193194
break;
194195
case $.ui.keyCode.LEFT:
195196
this.collapse( event );
196-
event.preventDefault();
197197
break;
198198
case $.ui.keyCode.RIGHT:
199199
if ( !this.active.is( ".ui-state-disabled" ) ) {
200200
this.expand( event );
201201
}
202-
event.preventDefault();
203202
break;
204203
case $.ui.keyCode.ENTER:
205204
this._activate( event );
206-
event.preventDefault();
207205
break;
208206
case $.ui.keyCode.SPACE:
209207
this._activate( event );
210-
event.preventDefault();
211208
break;
212209
case $.ui.keyCode.ESCAPE:
213210
this.collapse( event );
214-
event.preventDefault();
215211
break;
216212
default:
213+
preventDefault = false;
214+
prev = this.previousFilter || "";
215+
character = String.fromCharCode( event.keyCode );
216+
skip = false;
217+
217218
clearTimeout( this.filterTimer );
218-
var match,
219-
prev = this.previousFilter || "",
220-
character = String.fromCharCode( event.keyCode ),
221-
skip = false;
222219

223220
if ( character === prev ) {
224221
skip = true;
225222
} else {
226223
character = prev + character;
227224
}
228-
function escape( value ) {
229-
return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
230-
}
225+
231226
match = this.activeMenu.children( ".ui-menu-item" ).filter(function() {
232227
return new RegExp( "^" + escape( character ), "i" )
233228
.test( $( this ).children( "a" ).text() );
234229
});
235-
match = skip && match.index(this.active.next()) !== -1 ?
236-
this.active.nextAll(".ui-menu-item") :
230+
match = skip && match.index( this.active.next() ) !== -1 ?
231+
this.active.nextAll( ".ui-menu-item" ) :
237232
match;
233+
234+
// TODO: document what's going on here, character is reset to the original value
238235
if ( !match.length ) {
239-
character = String.fromCharCode(event.keyCode);
240-
match = this.activeMenu.children(".ui-menu-item").filter(function() {
236+
character = String.fromCharCode( event.keyCode );
237+
match = this.activeMenu.children( ".ui-menu-item" ).filter(function() {
241238
return new RegExp( "^" + escape(character), "i" )
242239
.test( $( this ).children( "a" ).text() );
243240
});
244241
}
242+
245243
if ( match.length ) {
246244
this.focus( event, match );
247245
if ( match.length > 1 ) {
@@ -256,6 +254,10 @@ $.widget( "ui.menu", {
256254
delete this.previousFilter;
257255
}
258256
}
257+
258+
if ( preventDefault ) {
259+
event.preventDefault();
260+
}
259261
},
260262

261263
_activate: function( event ) {
@@ -289,10 +291,12 @@ $.widget( "ui.menu", {
289291
.attr( "role", "presentation" )
290292
.children( "a" )
291293
.addClass( "ui-corner-all" )
292-
.attr( "tabIndex", -1 )
293-
.attr( "role", this._itemRole() )
294-
.attr( "id", function( i ) {
295-
return menuId + "-" + i;
294+
.attr({
295+
tabIndex: -1,
296+
role: this._itemRole(),
297+
id: function( i ) {
298+
return menuId + "-" + i;
299+
}
296300
});
297301

298302
// initialize unlinked menu-items as dividers
@@ -305,7 +309,8 @@ $.widget( "ui.menu", {
305309
var menu = $( this ),
306310
item = menu.prev( "a" );
307311

308-
item.attr( "aria-haspopup", "true" )
312+
item
313+
.attr( "aria-haspopup", "true" )
309314
.prepend( '<span class="ui-menu-icon ui-icon ui-icon-carat-1-e"></span>' );
310315
menu.attr( "aria-labelledby", item.attr( "id" ) );
311316
});
@@ -333,7 +338,11 @@ $.widget( "ui.menu", {
333338
}
334339

335340
// highlight active parent menu item, if any
336-
this.active.parent().closest( ".ui-menu-item" ).children( "a:first" ).addClass( "ui-state-active" );
341+
this.active
342+
.parent()
343+
.closest( ".ui-menu-item" )
344+
.children( "a:first" )
345+
.addClass( "ui-state-active" );
337346

338347
if ( event && event.type === "keydown" ) {
339348
this._close();
@@ -401,18 +410,18 @@ $.widget( "ui.menu", {
401410
},
402411

403412
_open: function( submenu ) {
413+
var position = $.extend({
414+
of: this.active
415+
}, $.type( this.options.position ) === "function" ?
416+
this.options.position( this.active ) :
417+
this.options.position
418+
);
419+
404420
clearTimeout( this.timer );
405421
this.element.find( ".ui-menu" ).not( submenu.parents() )
406422
.hide()
407423
.attr( "aria-hidden", "true" );
408424

409-
var position = $.extend( {}, {
410-
of: this.active
411-
}, $.type( this.options.position ) === "function" ?
412-
this.options.position( this.active ) :
413-
this.options.position
414-
);
415-
416425
submenu
417426
.show()
418427
.removeAttr( "aria-hidden" )
@@ -476,7 +485,7 @@ $.widget( "ui.menu", {
476485
if ( newItem && newItem.length ) {
477486
this._open( newItem.parent() );
478487

479-
//timeout so Firefox will not hide activedescendant change in expanding submenu from AT
488+
// timeout so Firefox will not hide activedescendant change in expanding submenu from AT
480489
this._delay(function() {
481490
this.focus( event, newItem );
482491
}, 20 );

0 commit comments

Comments
 (0)