jquery-ui/ui/jquery.ui.menu.js

619 lines
16 KiB
JavaScript
Raw Normal View History

/*!
* jQuery UI Menu @VERSION
2012-07-04 13:08:08 +00:00
* http://jqueryui.com
*
2012-07-04 13:08:08 +00:00
* Copyright 2012 jQuery Foundation and other contributors
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Menu
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
* jquery.ui.position.js
*/
2012-06-18 16:33:52 +00:00
(function( $, undefined ) {
var currentEventTarget = null;
2011-06-10 12:23:01 +00:00
$.widget( "ui.menu", {
version: "@VERSION",
defaultElement: "<ul>",
delay: 300,
options: {
2011-12-01 18:39:16 +00:00
menus: "ul",
position: {
my: "left top",
at: "right top"
},
role: "menu",
// callbacks
blur: null,
focus: null,
select: null
},
2012-06-18 16:33:52 +00:00
_create: function() {
this.activeMenu = this.element;
this.element
.uniqueId()
2010-10-28 16:12:52 +00:00
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
2012-05-25 01:15:55 +00:00
.toggleClass( "ui-menu-icons", !!this.element.find( ".ui-icon" ).length )
.attr({
role: this.options.role,
2012-04-10 12:39:18 +00:00
tabIndex: 0
})
// need to catch all clicks on disabled menu
// not possible through _on
.bind( "click" + this.eventNamespace, $.proxy(function( event ) {
if ( this.options.disabled ) {
event.preventDefault();
}
2012-04-10 12:39:18 +00:00
}, this ));
if ( this.options.disabled ) {
this.element
.addClass( "ui-state-disabled" )
.attr( "aria-disabled", "true" );
}
2012-04-10 12:39:18 +00:00
this._on({
// Prevent focus from sticking to links inside menu after clicking
// them (focus should always stay on UL during navigation).
"mousedown .ui-menu-item > a": function( event ) {
event.preventDefault();
},
"click .ui-state-disabled > a": function( event ) {
event.preventDefault();
},
"click .ui-menu-item:has(a)": function( event ) {
var target = $( event.target );
2012-04-19 14:29:55 +00:00
if ( target[0] !== currentEventTarget ) {
currentEventTarget = target[0];
2012-06-18 16:33:52 +00:00
// TODO: What are we trying to accomplish with this check?
// Clicking a menu item twice results in a select event with
// an empty ui.item.
target.one( "click" + this.eventNamespace, function( event ) {
currentEventTarget = null;
});
// Don't select disabled menu items
if ( !target.closest( ".ui-menu-item" ).is( ".ui-state-disabled" ) ) {
this.select( event );
// Redirect focus to the menu with a delay for firefox
this._delay(function() {
if ( !this.element.is(":focus") ) {
this.element.focus();
}
}, 20 );
}
}
},
"mouseenter .ui-menu-item": function( event ) {
var target = $( event.currentTarget );
2012-04-10 12:39:18 +00:00
// Remove ui-state-active class from siblings of the newly focused menu item
// to avoid a jump caused by adjacent elements both having a class with a border
target.siblings().children( ".ui-state-active" ).removeClass( "ui-state-active" );
this.focus( event, target );
},
2012-05-25 01:15:55 +00:00
mouseleave: "collapseAll",
"mouseleave .ui-menu": "collapseAll",
2012-05-25 01:15:55 +00:00
focus: function( event ) {
2012-06-18 16:33:52 +00:00
var menuTop,
menu = this.element,
// Default to focusing the first item
item = menu.children( ".ui-menu-item" ).eq( 0 );
// If there's already an active item, keep it active
if ( this.active ) {
item = this.active;
// If there's no active item and the menu is scrolled,
// then find the first visible item
} else if ( this._hasScroll() ) {
menuTop = menu.offset().top;
2012-04-10 12:39:18 +00:00
menu.children().each(function() {
var currentItem = $( this );
2012-06-18 16:33:52 +00:00
if ( currentItem.offset().top - menuTop >= 0 ) {
item = currentItem;
return false;
}
});
}
2012-06-18 16:33:52 +00:00
this.focus( event, item );
},
blur: function( event ) {
2012-04-10 12:39:18 +00:00
this._delay(function() {
if ( !$.contains( this.element[0], this.document[0].activeElement ) ) {
this.collapseAll( event );
}
2012-04-10 12:39:18 +00:00
});
},
2012-05-25 01:15:55 +00:00
keydown: "_keydown"
});
this.refresh();
2012-06-18 16:33:52 +00:00
// Clicks outside of a menu collapse any open menus
this._on( this.document, {
click: function( event ) {
if ( !$( event.target ).closest( ".ui-menu" ).length ) {
this.collapseAll( event );
}
}
});
},
_destroy: function() {
2012-06-18 16:33:52 +00:00
// Destroy (sub)menus
this.element
.removeAttr( "aria-activedescendant" )
2012-04-10 12:39:18 +00:00
.find( ".ui-menu" ).andSelf()
.removeClass( "ui-menu ui-widget ui-widget-content ui-corner-all ui-menu-icons" )
2012-04-10 12:39:18 +00:00
.removeAttr( "role" )
.removeAttr( "tabIndex" )
.removeAttr( "aria-labelledby" )
.removeAttr( "aria-expanded" )
.removeAttr( "aria-hidden" )
.removeAttr( "aria-disabled" )
.removeUniqueId()
2012-04-10 12:39:18 +00:00
.show();
2012-06-18 16:33:52 +00:00
// Destroy menu items
this.element.find( ".ui-menu-item" )
2010-10-28 16:12:52 +00:00
.removeClass( "ui-menu-item" )
.removeAttr( "role" )
.removeAttr( "aria-disabled" )
2010-10-28 16:12:52 +00:00
.children( "a" )
.removeUniqueId()
2012-04-10 12:39:18 +00:00
.removeClass( "ui-corner-all ui-state-hover" )
.removeAttr( "tabIndex" )
.removeAttr( "role" )
.removeAttr( "aria-haspopup" )
.children().each( function() {
var elem = $( this );
if ( elem.data( "ui-menu-submenu-carat" ) ) {
elem.remove();
}
});
2012-06-18 16:33:52 +00:00
// Destroy menu dividers
this.element.find( ".ui-menu-divider" ).removeClass( "ui-menu-divider ui-widget-content" );
2012-06-18 16:33:52 +00:00
// Unbind currentEventTarget click event handler
this._off( $( currentEventTarget ), "click" );
2012-04-10 12:39:18 +00:00
},
_keydown: function( event ) {
2012-05-25 01:15:55 +00:00
var match, prev, character, skip,
preventDefault = true;
function escape( value ) {
return value.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&" );
}
2012-04-10 12:39:18 +00:00
switch ( event.keyCode ) {
case $.ui.keyCode.PAGE_UP:
this.previousPage( event );
break;
case $.ui.keyCode.PAGE_DOWN:
this.nextPage( event );
break;
case $.ui.keyCode.HOME:
this._move( "first", "first", event );
break;
case $.ui.keyCode.END:
this._move( "last", "last", event );
break;
case $.ui.keyCode.UP:
this.previous( event );
break;
case $.ui.keyCode.DOWN:
this.next( event );
break;
case $.ui.keyCode.LEFT:
this.collapse( event );
2012-04-10 12:39:18 +00:00
break;
case $.ui.keyCode.RIGHT:
2012-04-19 02:30:07 +00:00
if ( !this.active.is( ".ui-state-disabled" ) ) {
this.expand( event );
}
2012-04-10 12:39:18 +00:00
break;
case $.ui.keyCode.ENTER:
case $.ui.keyCode.SPACE:
this._activate( event );
2012-04-10 12:39:18 +00:00
break;
case $.ui.keyCode.ESCAPE:
this.collapse( event );
2012-04-10 12:39:18 +00:00
break;
default:
2012-05-25 01:15:55 +00:00
preventDefault = false;
prev = this.previousFilter || "";
character = String.fromCharCode( event.keyCode );
skip = false;
2012-04-10 12:39:18 +00:00
clearTimeout( this.filterTimer );
if ( character === prev ) {
skip = true;
} else {
character = prev + character;
}
2012-05-25 01:15:55 +00:00
2012-04-10 12:39:18 +00:00
match = this.activeMenu.children( ".ui-menu-item" ).filter(function() {
return new RegExp( "^" + escape( character ), "i" )
.test( $( this ).children( "a" ).text() );
});
2012-05-25 01:15:55 +00:00
match = skip && match.index( this.active.next() ) !== -1 ?
this.active.nextAll( ".ui-menu-item" ) :
2012-04-10 12:39:18 +00:00
match;
2012-05-25 01:15:55 +00:00
// If no matches on the current filter, reset to the last character pressed
// to move down the menu to the first item that starts with that character
2012-04-10 12:39:18 +00:00
if ( !match.length ) {
2012-05-25 01:15:55 +00:00
character = String.fromCharCode( event.keyCode );
match = this.activeMenu.children( ".ui-menu-item" ).filter(function() {
2012-06-18 16:33:52 +00:00
return new RegExp( "^" + escape( character ), "i" )
2012-04-10 12:39:18 +00:00
.test( $( this ).children( "a" ).text() );
});
}
2012-05-25 01:15:55 +00:00
2012-04-10 12:39:18 +00:00
if ( match.length ) {
this.focus( event, match );
if ( match.length > 1 ) {
this.previousFilter = character;
this.filterTimer = this._delay(function() {
delete this.previousFilter;
}, 1000 );
} else {
delete this.previousFilter;
}
} else {
delete this.previousFilter;
}
}
2012-05-25 01:15:55 +00:00
if ( preventDefault ) {
event.preventDefault();
}
2010-04-30 08:21:40 +00:00
},
_activate: function( event ) {
if ( !this.active.is( ".ui-state-disabled" ) ) {
if ( this.active.children( "a[aria-haspopup='true']" ).length ) {
this.expand( event );
} else {
this.select( event );
}
}
},
refresh: function() {
2012-06-18 16:33:52 +00:00
// Initialize nested menus
var menus,
2012-04-10 12:39:18 +00:00
submenus = this.element.find( this.options.menus + ":not(.ui-menu)" )
2012-04-02 23:12:21 +00:00
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
.hide()
.attr({
role: this.options.role,
2012-04-02 23:12:21 +00:00
"aria-hidden": "true",
"aria-expanded": "false"
});
2012-06-18 16:33:52 +00:00
// Don't refresh list items that are already adapted
menus = submenus.add( this.element );
menus.children( ":not( .ui-menu-item ):has( a )" )
.addClass( "ui-menu-item" )
.attr( "role", "presentation" )
.children( "a" )
.uniqueId()
.addClass( "ui-corner-all" )
2012-05-25 01:15:55 +00:00
.attr({
tabIndex: -1,
role: this._itemRole()
});
2012-06-18 16:33:52 +00:00
// Initialize unlinked menu-items containing spaces and/or dashes only as dividers
menus.children( ":not(.ui-menu-item)" ).each(function() {
var item = $( this );
2012-06-01 00:55:18 +00:00
// hyphen, em dash, en dash
if ( !/[^\-—–\s]/.test( item.text() ) ) {
item.addClass( "ui-widget-content ui-menu-divider" );
}
});
2012-06-18 16:33:52 +00:00
// Add aria-disabled attribute to any disabled menu item
menus.children( ".ui-state-disabled" ).attr( "aria-disabled", "true" );
2012-04-10 12:39:18 +00:00
submenus.each(function() {
var menu = $( this ),
item = menu.prev( "a" ),
2012-06-18 16:33:52 +00:00
submenuCarat = $( "<span>" )
.addClass( "ui-menu-icon ui-icon ui-icon-carat-1-e" )
.data( "ui-menu-submenu-carat", true );
2012-05-25 01:15:55 +00:00
item
.attr( "aria-haspopup", "true" )
.prepend( submenuCarat );
menu.attr( "aria-labelledby", item.attr( "id" ) );
});
},
_itemRole: function() {
return {
menu: "menuitem",
listbox: "option"
}[ this.options.role ];
},
focus: function( event, item ) {
var nested, focused;
2012-04-19 14:29:55 +00:00
this.blur( event, event && event.type === "focus" );
this._scrollIntoView( item );
2012-04-10 12:39:18 +00:00
this.active = item.first();
focused = this.active.children( "a" ).addClass( "ui-state-focus" );
2012-06-18 16:33:52 +00:00
// Only update aria-activedescendant if there's a role
// otherwise we assume focus is managed elsewhere
if ( this.options.role ) {
this.element.attr( "aria-activedescendant", focused.attr( "id" ) );
}
2012-06-18 16:33:52 +00:00
// Highlight active parent menu item, if any
2012-05-25 01:15:55 +00:00
this.active
.parent()
.closest( ".ui-menu-item" )
.children( "a:first" )
.addClass( "ui-state-active" );
if ( event && event.type === "keydown" ) {
2012-05-09 04:57:56 +00:00
this._close();
} else {
this.timer = this._delay(function() {
this._close();
}, this.delay );
2012-05-09 04:57:56 +00:00
}
2012-06-18 16:33:52 +00:00
nested = item.children( ".ui-menu" );
if ( nested.length && ( /^mouse/.test( event.type ) ) ) {
this._startOpening(nested);
}
this.activeMenu = item.parent();
2010-10-28 16:12:52 +00:00
this._trigger( "focus", event, { item: item } );
},
_scrollIntoView: function( item ) {
var borderTop, paddingTop, offset, scroll, elementHeight, itemHeight;
if ( this._hasScroll() ) {
borderTop = parseFloat( $.css( this.activeMenu[0], "borderTopWidth" ) ) || 0;
paddingTop = parseFloat( $.css( this.activeMenu[0], "paddingTop" ) ) || 0;
offset = item.offset().top - this.activeMenu.offset().top - borderTop - paddingTop;
scroll = this.activeMenu.scrollTop();
elementHeight = this.activeMenu.height();
itemHeight = item.height();
if ( offset < 0 ) {
this.activeMenu.scrollTop( scroll + offset );
} else if ( offset + itemHeight > elementHeight ) {
this.activeMenu.scrollTop( scroll + offset - elementHeight + itemHeight );
}
}
},
blur: function( event, fromFocus ) {
if ( !fromFocus ) {
clearTimeout( this.timer );
}
2011-06-10 12:23:01 +00:00
if ( !this.active ) {
return;
}
this.active.children( "a" ).removeClass( "ui-state-focus" );
this.active = null;
this._trigger( "blur", event, { item: this.active } );
},
2011-06-10 12:23:01 +00:00
_startOpening: function( submenu ) {
clearTimeout( this.timer );
// Don't open if already open fixes a Firefox bug that caused a .5 pixel
// shift in the submenu position when mousing over the carat icon
if ( submenu.attr( "aria-hidden" ) !== "true" ) {
return;
}
2012-04-10 12:39:18 +00:00
this.timer = this._delay(function() {
this._close();
this._open( submenu );
}, this.delay );
},
2011-06-10 12:23:01 +00:00
_open: function( submenu ) {
2012-05-25 01:15:55 +00:00
var position = $.extend({
of: this.active
}, $.type( this.options.position ) === "function" ?
this.options.position( this.active ) :
this.options.position
);
2011-06-10 12:23:01 +00:00
clearTimeout( this.timer );
2012-06-18 16:33:52 +00:00
this.element.find( ".ui-menu" ).not( submenu.parents( ".ui-menu" ) )
2011-06-10 12:23:01 +00:00
.hide()
.attr( "aria-hidden", "true" );
2012-04-10 12:39:18 +00:00
submenu
.show()
2011-06-10 12:23:01 +00:00
.removeAttr( "aria-hidden" )
.attr( "aria-expanded", "true" )
.position( position );
},
collapseAll: function( event, all ) {
clearTimeout( this.timer );
2012-04-10 12:39:18 +00:00
this.timer = this._delay(function() {
2012-06-18 16:33:52 +00:00
// If we were passed an event, look for the submenu that contains the event
var currentMenu = all ? this.element :
$( event && event.target ).closest( this.element.find( ".ui-menu" ) );
2012-06-18 16:33:52 +00:00
// If we found no valid submenu ancestor, use the main menu to close all sub menus anyway
if ( !currentMenu.length ) {
currentMenu = this.element;
}
2011-06-10 12:23:01 +00:00
this._close( currentMenu );
this.blur( event );
this.activeMenu = currentMenu;
2012-04-10 12:39:18 +00:00
}, this.delay );
},
// With no arguments, closes the currently active menu - if nothing is active
// it closes all menus. If passed an argument, it will search for menus BELOW
_close: function( startMenu ) {
if ( !startMenu ) {
startMenu = this.active ? this.active.parent() : this.element;
}
startMenu
.find( ".ui-menu" )
2011-06-10 12:23:01 +00:00
.hide()
.attr( "aria-hidden", "true" )
.attr( "aria-expanded", "false" )
.end()
.find( "a.ui-state-active" )
2012-04-10 12:39:18 +00:00
.removeClass( "ui-state-active" );
},
collapse: function( event ) {
2012-04-10 12:39:18 +00:00
var newItem = this.active &&
this.active.parent().closest( ".ui-menu-item", this.element );
2011-06-10 12:23:01 +00:00
if ( newItem && newItem.length ) {
this._close();
2011-06-10 12:23:01 +00:00
this.focus( event, newItem );
return true;
}
},
expand: function( event ) {
2012-04-10 12:39:18 +00:00
var newItem = this.active &&
this.active
.children( ".ui-menu " )
.children( ".ui-menu-item" )
.first();
2011-06-10 12:23:01 +00:00
if ( newItem && newItem.length ) {
this._open( newItem.parent() );
2012-06-18 16:33:52 +00:00
// Delay so Firefox will not hide activedescendant change in expanding submenu from AT
2012-04-10 12:39:18 +00:00
this._delay(function() {
this.focus( event, newItem );
}, 20 );
return true;
}
},
2012-02-06 20:13:45 +00:00
next: function( event ) {
this._move( "next", "first", event );
},
2012-02-06 20:13:45 +00:00
previous: function( event ) {
this._move( "prev", "last", event );
},
isFirstItem: function() {
2010-10-28 16:12:52 +00:00
return this.active && !this.active.prevAll( ".ui-menu-item" ).length;
},
isLastItem: function() {
2010-10-28 16:12:52 +00:00
return this.active && !this.active.nextAll( ".ui-menu-item" ).length;
},
_move: function( direction, filter, event ) {
var next;
if ( this.active ) {
if ( direction === "first" || direction === "last" ) {
2012-04-10 12:39:18 +00:00
next = this.active
[ direction === "first" ? "prevAll" : "nextAll" ]( ".ui-menu-item" )
.eq( -1 );
} else {
2012-04-10 12:39:18 +00:00
next = this.active
[ direction + "All" ]( ".ui-menu-item" )
.eq( 0 );
}
}
if ( !next || !next.length || !this.active ) {
next = this.activeMenu.children( ".ui-menu-item" )[ filter ]();
}
this.focus( event, next );
},
2010-10-28 16:12:52 +00:00
nextPage: function( event ) {
2012-06-18 16:33:52 +00:00
var item, base, height;
if ( !this.active ) {
2012-06-18 16:33:52 +00:00
this.next( event );
return;
}
if ( this.isLastItem() ) {
return;
}
2010-10-28 16:12:52 +00:00
if ( this._hasScroll() ) {
2012-06-18 16:33:52 +00:00
base = this.active.offset().top;
height = this.element.height();
this.active.nextAll( ".ui-menu-item" ).each(function() {
2012-06-18 16:33:52 +00:00
item = $( this );
return item.offset().top - base - height < 0;
});
2012-06-18 16:33:52 +00:00
this.focus( event, item );
} else {
this.focus( event, this.activeMenu.children( ".ui-menu-item" )
[ !this.active ? "first" : "last" ]() );
}
},
2010-10-28 16:12:52 +00:00
previousPage: function( event ) {
2012-06-18 16:33:52 +00:00
var item, base, height;
if ( !this.active ) {
2012-06-18 16:33:52 +00:00
this.next( event );
return;
}
if ( this.isFirstItem() ) {
return;
}
2010-10-28 16:12:52 +00:00
if ( this._hasScroll() ) {
2012-06-18 16:33:52 +00:00
base = this.active.offset().top;
height = this.element.height();
this.active.prevAll( ".ui-menu-item" ).each(function() {
2012-06-18 16:33:52 +00:00
item = $( this );
return item.offset().top - base + height > 0;
});
2012-06-18 16:33:52 +00:00
this.focus( event, item );
} else {
this.focus( event, this.activeMenu.children( ".ui-menu-item" ).first() );
}
},
_hasScroll: function() {
return this.element.outerHeight() < this.element.prop( "scrollHeight" );
},
select: function( event ) {
2012-06-18 16:33:52 +00:00
// Save active reference before collapseAll triggers blur
var ui = {
item: this.active
};
this.collapseAll( event, true );
this._trigger( "select", event, ui );
}
});
2010-10-28 16:12:52 +00:00
}( jQuery ));