Skip to content
Permalink
Browse files

menu: Second pass style guidance

  • Loading branch information
gnarf committed Jun 10, 2011
1 parent e993a9b commit 85eeaf0b05960f1e1a470991f88657a590c27fce
Showing with 98 additions and 70 deletions.
  1. +98 −70 ui/jquery.ui.menu.js
@@ -15,7 +15,7 @@

var idIncrement = 0;

$.widget("ui.menu", {
$.widget( "ui.menu", {
version: "@VERSION",
defaultElement: "<ul>",
delay: 150,
@@ -29,8 +29,8 @@ $.widget("ui.menu", {
var self = this;
this.activeMenu = this.element;
this.menuId = this.element.attr( "id" ) || "ui-menu-" + idIncrement++;
if (this.element.find(".ui-icon").length) {
this.element.addClass("ui-menu-icons");
if ( this.element.find( ".ui-icon" ).length ) {
this.element.addClass( "ui-menu-icons" );
}
this.element
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
@@ -61,7 +61,7 @@ $.widget("ui.menu", {
self.focus( event, target );
}
})
.bind("mouseout.menu", function( event ) {
.bind( "mouseout.menu", function( event ) {
if ( self.options.disabled ) {
return;
}
@@ -110,8 +110,8 @@ $.widget("ui.menu", {
event.preventDefault();
break;
case $.ui.keyCode.ENTER:
if (self.active.children("a[aria-haspopup='true']").length) {
if (self.right( event )) {
if ( self.active.children( "a[aria-haspopup='true']" ).length ) {
if ( self.right( event ) ) {
event.stopImmediatePropagation();
}
}
@@ -129,7 +129,7 @@ $.widget("ui.menu", {
break;
default:
event.stopPropagation();
clearTimeout(self.filterTimer);
clearTimeout( self.filterTimer );
var match,
prev = self.previousFilter || "",
character = String.fromCharCode( event.keyCode ),
@@ -140,26 +140,28 @@ $.widget("ui.menu", {
} else {
character = prev + character;
}
function escape(value) {
return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
function escape( value ) {
return value.replace( /[-[\]{}()*+?.,\\^$|#\s]/g , "\\$&" );
}
match = self.activeMenu.children(".ui-menu-item").filter(function() {
return new RegExp("^" + escape(character), "i").test($(this).children("a").text());
match = self.activeMenu.children( ".ui-menu-item" ).filter( function() {
return new RegExp("^" + escape(character), "i")
.test( $( this ).children( "a" ).text() );
});
match = skip && match.index(self.active.next()) != -1 ? self.active.nextAll(".ui-menu-item") : match;
if (!match.length) {
if ( !match.length ) {
character = String.fromCharCode(event.keyCode);
match = self.activeMenu.children(".ui-menu-item").filter(function() {
return new RegExp("^" + escape(character), "i").test($(this).children("a").text());
match = self.activeMenu.children(".ui-menu-item").filter( function() {
return new RegExp("^" + escape(character), "i")
.test( $( this ).children( "a" ).text() );
});
}
if (match.length) {
self.focus(event, match);
if ( match.length ) {
self.focus( event, match );
if (match.length > 1) {
self.previousFilter = character;
self.filterTimer = setTimeout(function() {
self.filterTimer = setTimeout( function() {
delete self.previousFilter;
}, 1000);
}, 1000 );
} else {
delete self.previousFilter;
}
@@ -174,11 +176,11 @@ $.widget("ui.menu", {
//destroy (sub)menus
this.element
.removeAttr( "aria-activedescendant" )
.find("ul")
.find( "ul" )
.andSelf()
.removeClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
.removeAttr( "role" )
.removeAttr("tabIndex")
.removeAttr( "tabIndex" )
.removeAttr( "aria-labelledby" )
.removeAttr( "aria-expanded" )
.removeAttr( "aria-hidden" )
@@ -195,24 +197,25 @@ $.widget("ui.menu", {
.removeAttr( "role" )
.removeAttr( "aria-haspopup" )
.removeAttr( "id" )
.children(".ui-icon").remove();
.children( ".ui-icon" )
.remove();
},

refresh: function() {
var self = this;
// initialize nested menus
var submenus = this.element.find( "ul:not(.ui-menu)" )
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
.attr( "role", "menu" )
.hide()
.attr( "aria-hidden", "true" )
.attr( "aria-expanded", "false" )
;
var self = this,

// initialize nested menus
submenus = this.element.find( "ul:not(.ui-menu)" )
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
.attr( "role", "menu" )
.hide()
.attr( "aria-hidden", "true" )
.attr( "aria-expanded", "false" ),

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

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

focus: function( event, item ) {
var nested, self = this;
var nested,
self = this;

this.blur();

if ( this._hasScroll() ) {
var borderTop = parseFloat( $.curCSS( this.element[0], "borderTopWidth", true) ) || 0,
paddingTop = parseFloat( $.curCSS( this.element[0], "paddingTop", true) ) || 0,
var borderTop = parseFloat( $.curCSS( this.element[0], "borderTopWidth", true ) ) || 0,
paddingTop = parseFloat( $.curCSS( this.element[0], "paddingTop", true ) ) || 0,
offset = item.offset().top - this.element.offset().top - borderTop - paddingTop,
scroll = this.element.scrollTop(),
elementHeight = this.element.height(),
itemHeight = item.height();

if ( offset < 0 ) {
this.element.scrollTop( scroll + offset );
} else if ( offset + itemHeight > elementHeight ) {
@@ -273,67 +278,90 @@ $.widget("ui.menu", {
this._trigger( "focus", event, { item: item } );
},

blur: function(event) {
if (!this.active) {
blur: function( event ) {
if ( !this.active ) {
return;
}

clearTimeout(this.timer);
clearTimeout( this.timer );

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

_startOpening: function(submenu) {
clearTimeout(this.timer);
_startOpening: function( submenu ) {
clearTimeout( this.timer );
var self = this;
self.timer = setTimeout(function() {
self.timer = setTimeout( function() {
self._close();
self._open(submenu);
}, self.delay);
self._open( submenu );
}, self.delay );
},

_open: function(submenu) {
clearTimeout(this.timer);
this.element.find(".ui-menu").not(submenu.parents()).hide().attr("aria-hidden", "true");
_open: function( submenu ) {
clearTimeout( this.timer );
this.element
.find( ".ui-menu" )
.not( submenu.parents() )
.hide()
.attr( "aria-hidden", "true" );

var position = $.extend({}, {
of: this.active
}, $.type(this.options.position) == "function"
? this.options.position(this.active)
: this.options.position
);
submenu.show().removeAttr("aria-hidden").attr("aria-expanded", "true").position(position);
of: this.active
}, $.type(this.options.position) == "function"
? this.options.position(this.active)
: this.options.position
);

submenu.show()
.removeAttr( "aria-hidden" )
.attr( "aria-expanded", "true" )
.position( position );
},

closeAll: function() {
this.element
.find("ul").hide().attr("aria-hidden", "true").attr("aria-expanded", "false").end()
.find("a.ui-state-active").removeClass("ui-state-active");
.find( "ul" )
.hide()
.attr( "aria-hidden", "true" )
.attr( "aria-expanded", "false" )
.end()
.find( "a.ui-state-active" )
.removeClass( "ui-state-active" );

this.blur();
this.activeMenu = this.element;
},

_close: function() {
this.active.parent()
.find("ul").hide().attr("aria-hidden", "true").attr("aria-expanded", "false").end()
.find("a.ui-state-active").removeClass("ui-state-active");
.find( "ul" )
.hide()
.attr( "aria-hidden", "true" )
.attr( "aria-expanded", "false" )
.end()
.find( "a.ui-state-active" )
.removeClass( "ui-state-active" );
},

left: function(event) {
left: function( event ) {
var newItem = this.active && this.active.parents("li:not(.ui-menubar-item)").first();
if (newItem && newItem.length) {
this.active.parent().attr("aria-hidden", "true").attr("aria-expanded", "false").hide();
this.focus(event, newItem);
if ( newItem && newItem.length ) {
this.active.parent()
.attr("aria-hidden", "true")
.attr("aria-expanded", "false")
.hide();
this.focus( event, newItem );
return true;
}
},

right: function(event) {
var self= this;
var newItem = this.active && this.active.children("ul").children("li").first();
if (newItem && newItem.length) {
this._open(newItem.parent());
var current = this.active;
right: function( event ) {
var self = this,
newItem = this.active && this.active.children("ul").children("li").first();

if ( newItem && newItem.length ) {
this._open( newItem.parent() );

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

_move: function(direction, edge, filter, event) {
_move: function( direction, edge, filter, event ) {
if ( !this.active ) {
this.focus( event, this.activeMenu.children(edge)[filter]() );
this.focus( event, this.activeMenu.children( edge )[ filter ]() );
return;
}
var next = this.active[ direction + "All" ]( ".ui-menu-item" ).eq( 0 );
if ( next.length ) {
this.focus( event, next );
} else {
this.focus( event, this.activeMenu.children(edge)[filter]() );
this.focus( event, this.activeMenu.children( edge )[ filter ]() );
}
},

0 comments on commit 85eeaf0

Please sign in to comment.
You can’t perform that action at this time.