Skip to content

Commit 85eeaf0

Browse files
committed
menu: Second pass style guidance
1 parent e993a9b commit 85eeaf0

File tree

1 file changed

+98
-70
lines changed

1 file changed

+98
-70
lines changed

ui/jquery.ui.menu.js

Lines changed: 98 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
var idIncrement = 0;
1717

18-
$.widget("ui.menu", {
18+
$.widget( "ui.menu", {
1919
version: "@VERSION",
2020
defaultElement: "<ul>",
2121
delay: 150,
@@ -29,8 +29,8 @@ $.widget("ui.menu", {
2929
var self = this;
3030
this.activeMenu = this.element;
3131
this.menuId = this.element.attr( "id" ) || "ui-menu-" + idIncrement++;
32-
if (this.element.find(".ui-icon").length) {
33-
this.element.addClass("ui-menu-icons");
32+
if ( this.element.find( ".ui-icon" ).length ) {
33+
this.element.addClass( "ui-menu-icons" );
3434
}
3535
this.element
3636
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
@@ -61,7 +61,7 @@ $.widget("ui.menu", {
6161
self.focus( event, target );
6262
}
6363
})
64-
.bind("mouseout.menu", function( event ) {
64+
.bind( "mouseout.menu", function( event ) {
6565
if ( self.options.disabled ) {
6666
return;
6767
}
@@ -110,8 +110,8 @@ $.widget("ui.menu", {
110110
event.preventDefault();
111111
break;
112112
case $.ui.keyCode.ENTER:
113-
if (self.active.children("a[aria-haspopup='true']").length) {
114-
if (self.right( event )) {
113+
if ( self.active.children( "a[aria-haspopup='true']" ).length ) {
114+
if ( self.right( event ) ) {
115115
event.stopImmediatePropagation();
116116
}
117117
}
@@ -129,7 +129,7 @@ $.widget("ui.menu", {
129129
break;
130130
default:
131131
event.stopPropagation();
132-
clearTimeout(self.filterTimer);
132+
clearTimeout( self.filterTimer );
133133
var match,
134134
prev = self.previousFilter || "",
135135
character = String.fromCharCode( event.keyCode ),
@@ -140,26 +140,28 @@ $.widget("ui.menu", {
140140
} else {
141141
character = prev + character;
142142
}
143-
function escape(value) {
144-
return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
143+
function escape( value ) {
144+
return value.replace( /[-[\]{}()*+?.,\\^$|#\s]/g , "\\$&" );
145145
}
146-
match = self.activeMenu.children(".ui-menu-item").filter(function() {
147-
return new RegExp("^" + escape(character), "i").test($(this).children("a").text());
146+
match = self.activeMenu.children( ".ui-menu-item" ).filter( function() {
147+
return new RegExp("^" + escape(character), "i")
148+
.test( $( this ).children( "a" ).text() );
148149
});
149150
match = skip && match.index(self.active.next()) != -1 ? self.active.nextAll(".ui-menu-item") : match;
150-
if (!match.length) {
151+
if ( !match.length ) {
151152
character = String.fromCharCode(event.keyCode);
152-
match = self.activeMenu.children(".ui-menu-item").filter(function() {
153-
return new RegExp("^" + escape(character), "i").test($(this).children("a").text());
153+
match = self.activeMenu.children(".ui-menu-item").filter( function() {
154+
return new RegExp("^" + escape(character), "i")
155+
.test( $( this ).children( "a" ).text() );
154156
});
155157
}
156-
if (match.length) {
157-
self.focus(event, match);
158+
if ( match.length ) {
159+
self.focus( event, match );
158160
if (match.length > 1) {
159161
self.previousFilter = character;
160-
self.filterTimer = setTimeout(function() {
162+
self.filterTimer = setTimeout( function() {
161163
delete self.previousFilter;
162-
}, 1000);
164+
}, 1000 );
163165
} else {
164166
delete self.previousFilter;
165167
}
@@ -174,11 +176,11 @@ $.widget("ui.menu", {
174176
//destroy (sub)menus
175177
this.element
176178
.removeAttr( "aria-activedescendant" )
177-
.find("ul")
179+
.find( "ul" )
178180
.andSelf()
179181
.removeClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
180182
.removeAttr( "role" )
181-
.removeAttr("tabIndex")
183+
.removeAttr( "tabIndex" )
182184
.removeAttr( "aria-labelledby" )
183185
.removeAttr( "aria-expanded" )
184186
.removeAttr( "aria-hidden" )
@@ -195,24 +197,25 @@ $.widget("ui.menu", {
195197
.removeAttr( "role" )
196198
.removeAttr( "aria-haspopup" )
197199
.removeAttr( "id" )
198-
.children(".ui-icon").remove();
200+
.children( ".ui-icon" )
201+
.remove();
199202
},
200203

201204
refresh: function() {
202-
var self = this;
203-
// initialize nested menus
204-
var submenus = this.element.find( "ul:not(.ui-menu)" )
205-
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
206-
.attr( "role", "menu" )
207-
.hide()
208-
.attr( "aria-hidden", "true" )
209-
.attr( "aria-expanded", "false" )
210-
;
205+
var self = this,
206+
207+
// initialize nested menus
208+
submenus = this.element.find( "ul:not(.ui-menu)" )
209+
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
210+
.attr( "role", "menu" )
211+
.hide()
212+
.attr( "aria-hidden", "true" )
213+
.attr( "aria-expanded", "false" ),
211214

212215
// don't refresh list items that are already adapted
213-
var items = submenus.add( this.element ).children( "li:not(.ui-menu-item):has(a)" )
214-
.addClass( "ui-menu-item" )
215-
.attr( "role", "presentation" );
216+
items = submenus.add( this.element ).children( "li:not(.ui-menu-item):has(a)" )
217+
.addClass( "ui-menu-item" )
218+
.attr( "role", "presentation" );
216219

217220
items.children( "a" )
218221
.addClass( "ui-corner-all" )
@@ -233,17 +236,19 @@ $.widget("ui.menu", {
233236
},
234237

235238
focus: function( event, item ) {
236-
var nested, self = this;
239+
var nested,
240+
self = this;
237241

238242
this.blur();
239243

240244
if ( this._hasScroll() ) {
241-
var borderTop = parseFloat( $.curCSS( this.element[0], "borderTopWidth", true) ) || 0,
242-
paddingTop = parseFloat( $.curCSS( this.element[0], "paddingTop", true) ) || 0,
245+
var borderTop = parseFloat( $.curCSS( this.element[0], "borderTopWidth", true ) ) || 0,
246+
paddingTop = parseFloat( $.curCSS( this.element[0], "paddingTop", true ) ) || 0,
243247
offset = item.offset().top - this.element.offset().top - borderTop - paddingTop,
244248
scroll = this.element.scrollTop(),
245249
elementHeight = this.element.height(),
246250
itemHeight = item.height();
251+
247252
if ( offset < 0 ) {
248253
this.element.scrollTop( scroll + offset );
249254
} else if ( offset + itemHeight > elementHeight ) {
@@ -273,67 +278,90 @@ $.widget("ui.menu", {
273278
this._trigger( "focus", event, { item: item } );
274279
},
275280

276-
blur: function(event) {
277-
if (!this.active) {
281+
blur: function( event ) {
282+
if ( !this.active ) {
278283
return;
279284
}
280285

281-
clearTimeout(this.timer);
286+
clearTimeout( this.timer );
282287

283288
this.active.children( "a" ).removeClass( "ui-state-focus" );
284289
this.active = null;
285290
},
286291

287-
_startOpening: function(submenu) {
288-
clearTimeout(this.timer);
292+
_startOpening: function( submenu ) {
293+
clearTimeout( this.timer );
289294
var self = this;
290-
self.timer = setTimeout(function() {
295+
self.timer = setTimeout( function() {
291296
self._close();
292-
self._open(submenu);
293-
}, self.delay);
297+
self._open( submenu );
298+
}, self.delay );
294299
},
295300

296-
_open: function(submenu) {
297-
clearTimeout(this.timer);
298-
this.element.find(".ui-menu").not(submenu.parents()).hide().attr("aria-hidden", "true");
301+
_open: function( submenu ) {
302+
clearTimeout( this.timer );
303+
this.element
304+
.find( ".ui-menu" )
305+
.not( submenu.parents() )
306+
.hide()
307+
.attr( "aria-hidden", "true" );
308+
299309
var position = $.extend({}, {
300-
of: this.active
301-
}, $.type(this.options.position) == "function"
302-
? this.options.position(this.active)
303-
: this.options.position
304-
);
305-
submenu.show().removeAttr("aria-hidden").attr("aria-expanded", "true").position(position);
310+
of: this.active
311+
}, $.type(this.options.position) == "function"
312+
? this.options.position(this.active)
313+
: this.options.position
314+
);
315+
316+
submenu.show()
317+
.removeAttr( "aria-hidden" )
318+
.attr( "aria-expanded", "true" )
319+
.position( position );
306320
},
307321

308322
closeAll: function() {
309323
this.element
310-
.find("ul").hide().attr("aria-hidden", "true").attr("aria-expanded", "false").end()
311-
.find("a.ui-state-active").removeClass("ui-state-active");
324+
.find( "ul" )
325+
.hide()
326+
.attr( "aria-hidden", "true" )
327+
.attr( "aria-expanded", "false" )
328+
.end()
329+
.find( "a.ui-state-active" )
330+
.removeClass( "ui-state-active" );
331+
312332
this.blur();
313333
this.activeMenu = this.element;
314334
},
315335

316336
_close: function() {
317337
this.active.parent()
318-
.find("ul").hide().attr("aria-hidden", "true").attr("aria-expanded", "false").end()
319-
.find("a.ui-state-active").removeClass("ui-state-active");
338+
.find( "ul" )
339+
.hide()
340+
.attr( "aria-hidden", "true" )
341+
.attr( "aria-expanded", "false" )
342+
.end()
343+
.find( "a.ui-state-active" )
344+
.removeClass( "ui-state-active" );
320345
},
321346

322-
left: function(event) {
347+
left: function( event ) {
323348
var newItem = this.active && this.active.parents("li:not(.ui-menubar-item)").first();
324-
if (newItem && newItem.length) {
325-
this.active.parent().attr("aria-hidden", "true").attr("aria-expanded", "false").hide();
326-
this.focus(event, newItem);
349+
if ( newItem && newItem.length ) {
350+
this.active.parent()
351+
.attr("aria-hidden", "true")
352+
.attr("aria-expanded", "false")
353+
.hide();
354+
this.focus( event, newItem );
327355
return true;
328356
}
329357
},
330358

331-
right: function(event) {
332-
var self= this;
333-
var newItem = this.active && this.active.children("ul").children("li").first();
334-
if (newItem && newItem.length) {
335-
this._open(newItem.parent());
336-
var current = this.active;
359+
right: function( event ) {
360+
var self = this,
361+
newItem = this.active && this.active.children("ul").children("li").first();
362+
363+
if ( newItem && newItem.length ) {
364+
this._open( newItem.parent() );
337365

338366
//timeout so Firefox will not hide activedescendant change in expanding submenu from AT
339367
setTimeout( function() {
@@ -359,16 +387,16 @@ $.widget("ui.menu", {
359387
return this.active && !this.active.nextAll( ".ui-menu-item" ).length;
360388
},
361389

362-
_move: function(direction, edge, filter, event) {
390+
_move: function( direction, edge, filter, event ) {
363391
if ( !this.active ) {
364-
this.focus( event, this.activeMenu.children(edge)[filter]() );
392+
this.focus( event, this.activeMenu.children( edge )[ filter ]() );
365393
return;
366394
}
367395
var next = this.active[ direction + "All" ]( ".ui-menu-item" ).eq( 0 );
368396
if ( next.length ) {
369397
this.focus( event, next );
370398
} else {
371-
this.focus( event, this.activeMenu.children(edge)[filter]() );
399+
this.focus( event, this.activeMenu.children( edge )[ filter ]() );
372400
}
373401
},
374402

0 commit comments

Comments
 (0)