2 * jQuery UI Menu 1.12.1
5 * Copyright jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
12 //>>description: Creates nestable menus.
13 //>>docs: http://api.jqueryui.com/menu/
14 //>>demos: http://jqueryui.com/menu/
15 //>>css.structure: ../../themes/base/core.css
16 //>>css.structure: ../../themes/base/menu.css
17 //>>css.theme: ../../themes/base/theme.css
19 ( function( factory ) {
20 if ( typeof define === "function" && define.amd ) {
22 // AMD. Register as an anonymous module.
27 "../safe-active-element",
39 return $.widget( "ui.menu", {
41 defaultElement: "<ul>",
45 submenu: "ui-icon-caret-1-e"
62 this.activeMenu = this.element;
64 // Flag used to prevent firing of the click handler
65 // as the event bubbles up through nested menus
66 this.mouseHandled = false;
70 role: this.options.role,
74 this._addClass( "ui-menu", "ui-widget ui-widget-content" );
77 // Prevent focus from sticking to links inside menu after clicking
78 // them (focus should always stay on UL during navigation).
79 "mousedown .ui-menu-item": function( event ) {
80 event.preventDefault();
82 "click .ui-menu-item": function( event ) {
83 var target = $( event.target );
84 var active = $( $.ui.safeActiveElement( this.document[ 0 ] ) );
85 if ( !this.mouseHandled && target.not( ".ui-state-disabled" ).length ) {
88 // Only set the mouseHandled flag if the event will bubble, see #9469.
89 if ( !event.isPropagationStopped() ) {
90 this.mouseHandled = true;
93 // Open submenu on click
94 if ( target.has( ".ui-menu" ).length ) {
96 } else if ( !this.element.is( ":focus" ) &&
97 active.closest( ".ui-menu" ).length ) {
99 // Redirect focus to the menu
100 this.element.trigger( "focus", [ true ] );
102 // If the active item is on the top level, let it stay active.
103 // Otherwise, blur the active item since it is no longer visible.
104 if ( this.active && this.active.parents( ".ui-menu" ).length === 1 ) {
105 clearTimeout( this.timer );
110 "mouseenter .ui-menu-item": function( event ) {
112 // Ignore mouse events while typeahead is active, see #10458.
113 // Prevents focusing the wrong item when typeahead causes a scroll while the mouse
114 // is over an item in the menu
115 if ( this.previousFilter ) {
119 var actualTarget = $( event.target ).closest( ".ui-menu-item" ),
120 target = $( event.currentTarget );
122 // Ignore bubbled events on parent items, see #11641
123 if ( actualTarget[ 0 ] !== target[ 0 ] ) {
127 // Remove ui-state-active class from siblings of the newly focused menu item
128 // to avoid a jump caused by adjacent elements both having a class with a border
129 this._removeClass( target.siblings().children( ".ui-state-active" ),
130 null, "ui-state-active" );
131 this.focus( event, target );
133 mouseleave: "collapseAll",
134 "mouseleave .ui-menu": "collapseAll",
135 focus: function( event, keepActiveItem ) {
137 // If there's already an active item, keep it active
138 // If not, activate the first item
139 var item = this.active || this.element.find( this.options.items ).eq( 0 );
141 if ( !keepActiveItem ) {
142 this.focus( event, item );
145 blur: function( event ) {
146 this._delay( function() {
147 var notContained = !$.contains(
149 $.ui.safeActiveElement( this.document[ 0 ] )
151 if ( notContained ) {
152 this.collapseAll( event );
161 // Clicks outside of a menu collapse any open menus
162 this._on( this.document, {
163 click: function( event ) {
164 if ( this._closeOnDocumentClick( event ) ) {
165 this.collapseAll( event );
168 // Reset the mouseHandled flag
169 this.mouseHandled = false;
174 _destroy: function() {
175 var items = this.element.find( ".ui-menu-item" )
176 .removeAttr( "role aria-disabled" ),
177 submenus = items.children( ".ui-menu-item-wrapper" )
179 .removeAttr( "tabIndex role aria-haspopup" );
181 // Destroy (sub)menus
183 .removeAttr( "aria-activedescendant" )
184 .find( ".ui-menu" ).addBack()
185 .removeAttr( "role aria-labelledby aria-expanded aria-hidden aria-disabled " +
190 submenus.children().each( function() {
191 var elem = $( this );
192 if ( elem.data( "ui-menu-submenu-caret" ) ) {
198 _keydown: function( event ) {
199 var match, prev, character, skip,
200 preventDefault = true;
202 switch ( event.keyCode ) {
203 case $.ui.keyCode.PAGE_UP:
204 this.previousPage( event );
206 case $.ui.keyCode.PAGE_DOWN:
207 this.nextPage( event );
209 case $.ui.keyCode.HOME:
210 this._move( "first", "first", event );
212 case $.ui.keyCode.END:
213 this._move( "last", "last", event );
215 case $.ui.keyCode.UP:
216 this.previous( event );
218 case $.ui.keyCode.DOWN:
221 case $.ui.keyCode.LEFT:
222 this.collapse( event );
224 case $.ui.keyCode.RIGHT:
225 if ( this.active && !this.active.is( ".ui-state-disabled" ) ) {
226 this.expand( event );
229 case $.ui.keyCode.ENTER:
230 case $.ui.keyCode.SPACE:
231 this._activate( event );
233 case $.ui.keyCode.ESCAPE:
234 this.collapse( event );
237 preventDefault = false;
238 prev = this.previousFilter || "";
241 // Support number pad values
242 character = event.keyCode >= 96 && event.keyCode <= 105 ?
243 ( event.keyCode - 96 ).toString() : String.fromCharCode( event.keyCode );
245 clearTimeout( this.filterTimer );
247 if ( character === prev ) {
250 character = prev + character;
253 match = this._filterMenuItems( character );
254 match = skip && match.index( this.active.next() ) !== -1 ?
255 this.active.nextAll( ".ui-menu-item" ) :
258 // If no matches on the current filter, reset to the last character pressed
259 // to move down the menu to the first item that starts with that character
260 if ( !match.length ) {
261 character = String.fromCharCode( event.keyCode );
262 match = this._filterMenuItems( character );
265 if ( match.length ) {
266 this.focus( event, match );
267 this.previousFilter = character;
268 this.filterTimer = this._delay( function() {
269 delete this.previousFilter;
272 delete this.previousFilter;
276 if ( preventDefault ) {
277 event.preventDefault();
281 _activate: function( event ) {
282 if ( this.active && !this.active.is( ".ui-state-disabled" ) ) {
283 if ( this.active.children( "[aria-haspopup='true']" ).length ) {
284 this.expand( event );
286 this.select( event );
291 refresh: function() {
292 var menus, items, newSubmenus, newItems, newWrappers,
294 icon = this.options.icons.submenu,
295 submenus = this.element.find( this.options.menus );
297 this._toggleClass( "ui-menu-icons", null, !!this.element.find( ".ui-icon" ).length );
299 // Initialize nested menus
300 newSubmenus = submenus.filter( ":not(.ui-menu)" )
303 role: this.options.role,
304 "aria-hidden": "true",
305 "aria-expanded": "false"
308 var menu = $( this ),
310 submenuCaret = $( "<span>" ).data( "ui-menu-submenu-caret", true );
312 that._addClass( submenuCaret, "ui-menu-icon", "ui-icon " + icon );
314 .attr( "aria-haspopup", "true" )
315 .prepend( submenuCaret );
316 menu.attr( "aria-labelledby", item.attr( "id" ) );
319 this._addClass( newSubmenus, "ui-menu", "ui-widget ui-widget-content ui-front" );
321 menus = submenus.add( this.element );
322 items = menus.find( this.options.items );
324 // Initialize menu-items containing spaces and/or dashes only as dividers
325 items.not( ".ui-menu-item" ).each( function() {
326 var item = $( this );
327 if ( that._isDivider( item ) ) {
328 that._addClass( item, "ui-menu-divider", "ui-widget-content" );
332 // Don't refresh list items that are already adapted
333 newItems = items.not( ".ui-menu-item, .ui-menu-divider" );
334 newWrappers = newItems.children()
339 role: this._itemRole()
341 this._addClass( newItems, "ui-menu-item" )
342 ._addClass( newWrappers, "ui-menu-item-wrapper" );
344 // Add aria-disabled attribute to any disabled menu item
345 items.filter( ".ui-state-disabled" ).attr( "aria-disabled", "true" );
347 // If the active item has been removed, blur the menu
348 if ( this.active && !$.contains( this.element[ 0 ], this.active[ 0 ] ) ) {
353 _itemRole: function() {
357 }[ this.options.role ];
360 _setOption: function( key, value ) {
361 if ( key === "icons" ) {
362 var icons = this.element.find( ".ui-menu-icon" );
363 this._removeClass( icons, null, this.options.icons.submenu )
364 ._addClass( icons, null, value.submenu );
366 this._super( key, value );
369 _setOptionDisabled: function( value ) {
370 this._super( value );
372 this.element.attr( "aria-disabled", String( value ) );
373 this._toggleClass( null, "ui-state-disabled", !!value );
376 focus: function( event, item ) {
377 var nested, focused, activeParent;
378 this.blur( event, event && event.type === "focus" );
380 this._scrollIntoView( item );
382 this.active = item.first();
384 focused = this.active.children( ".ui-menu-item-wrapper" );
385 this._addClass( focused, null, "ui-state-active" );
387 // Only update aria-activedescendant if there's a role
388 // otherwise we assume focus is managed elsewhere
389 if ( this.options.role ) {
390 this.element.attr( "aria-activedescendant", focused.attr( "id" ) );
393 // Highlight active parent menu item, if any
394 activeParent = this.active
396 .closest( ".ui-menu-item" )
397 .children( ".ui-menu-item-wrapper" );
398 this._addClass( activeParent, null, "ui-state-active" );
400 if ( event && event.type === "keydown" ) {
403 this.timer = this._delay( function() {
408 nested = item.children( ".ui-menu" );
409 if ( nested.length && event && ( /^mouse/.test( event.type ) ) ) {
410 this._startOpening( nested );
412 this.activeMenu = item.parent();
414 this._trigger( "focus", event, { item: item } );
417 _scrollIntoView: function( item ) {
418 var borderTop, paddingTop, offset, scroll, elementHeight, itemHeight;
419 if ( this._hasScroll() ) {
420 borderTop = parseFloat( $.css( this.activeMenu[ 0 ], "borderTopWidth" ) ) || 0;
421 paddingTop = parseFloat( $.css( this.activeMenu[ 0 ], "paddingTop" ) ) || 0;
422 offset = item.offset().top - this.activeMenu.offset().top - borderTop - paddingTop;
423 scroll = this.activeMenu.scrollTop();
424 elementHeight = this.activeMenu.height();
425 itemHeight = item.outerHeight();
428 this.activeMenu.scrollTop( scroll + offset );
429 } else if ( offset + itemHeight > elementHeight ) {
430 this.activeMenu.scrollTop( scroll + offset - elementHeight + itemHeight );
435 blur: function( event, fromFocus ) {
437 clearTimeout( this.timer );
440 if ( !this.active ) {
444 this._removeClass( this.active.children( ".ui-menu-item-wrapper" ),
445 null, "ui-state-active" );
447 this._trigger( "blur", event, { item: this.active } );
451 _startOpening: function( submenu ) {
452 clearTimeout( this.timer );
454 // Don't open if already open fixes a Firefox bug that caused a .5 pixel
455 // shift in the submenu position when mousing over the caret icon
456 if ( submenu.attr( "aria-hidden" ) !== "true" ) {
460 this.timer = this._delay( function() {
462 this._open( submenu );
466 _open: function( submenu ) {
467 var position = $.extend( {
469 }, this.options.position );
471 clearTimeout( this.timer );
472 this.element.find( ".ui-menu" ).not( submenu.parents( ".ui-menu" ) )
474 .attr( "aria-hidden", "true" );
478 .removeAttr( "aria-hidden" )
479 .attr( "aria-expanded", "true" )
480 .position( position );
483 collapseAll: function( event, all ) {
484 clearTimeout( this.timer );
485 this.timer = this._delay( function() {
487 // If we were passed an event, look for the submenu that contains the event
488 var currentMenu = all ? this.element :
489 $( event && event.target ).closest( this.element.find( ".ui-menu" ) );
491 // If we found no valid submenu ancestor, use the main menu to close all
493 if ( !currentMenu.length ) {
494 currentMenu = this.element;
497 this._close( currentMenu );
501 // Work around active item staying active after menu is blurred
502 this._removeClass( currentMenu.find( ".ui-state-active" ), null, "ui-state-active" );
504 this.activeMenu = currentMenu;
508 // With no arguments, closes the currently active menu - if nothing is active
509 // it closes all menus. If passed an argument, it will search for menus BELOW
510 _close: function( startMenu ) {
512 startMenu = this.active ? this.active.parent() : this.element;
515 startMenu.find( ".ui-menu" )
517 .attr( "aria-hidden", "true" )
518 .attr( "aria-expanded", "false" );
521 _closeOnDocumentClick: function( event ) {
522 return !$( event.target ).closest( ".ui-menu" ).length;
525 _isDivider: function( item ) {
527 // Match hyphen, em dash, en dash
528 return !/[^\-\u2014\u2013\s]/.test( item.text() );
531 collapse: function( event ) {
532 var newItem = this.active &&
533 this.active.parent().closest( ".ui-menu-item", this.element );
534 if ( newItem && newItem.length ) {
536 this.focus( event, newItem );
540 expand: function( event ) {
541 var newItem = this.active &&
543 .children( ".ui-menu " )
544 .find( this.options.items )
547 if ( newItem && newItem.length ) {
548 this._open( newItem.parent() );
550 // Delay so Firefox will not hide activedescendant change in expanding submenu from AT
551 this._delay( function() {
552 this.focus( event, newItem );
557 next: function( event ) {
558 this._move( "next", "first", event );
561 previous: function( event ) {
562 this._move( "prev", "last", event );
565 isFirstItem: function() {
566 return this.active && !this.active.prevAll( ".ui-menu-item" ).length;
569 isLastItem: function() {
570 return this.active && !this.active.nextAll( ".ui-menu-item" ).length;
573 _move: function( direction, filter, event ) {
576 if ( direction === "first" || direction === "last" ) {
578 [ direction === "first" ? "prevAll" : "nextAll" ]( ".ui-menu-item" )
582 [ direction + "All" ]( ".ui-menu-item" )
586 if ( !next || !next.length || !this.active ) {
587 next = this.activeMenu.find( this.options.items )[ filter ]();
590 this.focus( event, next );
593 nextPage: function( event ) {
594 var item, base, height;
596 if ( !this.active ) {
600 if ( this.isLastItem() ) {
603 if ( this._hasScroll() ) {
604 base = this.active.offset().top;
605 height = this.element.height();
606 this.active.nextAll( ".ui-menu-item" ).each( function() {
608 return item.offset().top - base - height < 0;
611 this.focus( event, item );
613 this.focus( event, this.activeMenu.find( this.options.items )
614 [ !this.active ? "first" : "last" ]() );
618 previousPage: function( event ) {
619 var item, base, height;
620 if ( !this.active ) {
624 if ( this.isFirstItem() ) {
627 if ( this._hasScroll() ) {
628 base = this.active.offset().top;
629 height = this.element.height();
630 this.active.prevAll( ".ui-menu-item" ).each( function() {
632 return item.offset().top - base + height > 0;
635 this.focus( event, item );
637 this.focus( event, this.activeMenu.find( this.options.items ).first() );
641 _hasScroll: function() {
642 return this.element.outerHeight() < this.element.prop( "scrollHeight" );
645 select: function( event ) {
647 // TODO: It should never be possible to not have an active item at this
648 // point, but the tests don't trigger mouseenter before click.
649 this.active = this.active || $( event.target ).closest( ".ui-menu-item" );
650 var ui = { item: this.active };
651 if ( !this.active.has( ".ui-menu" ).length ) {
652 this.collapseAll( event, true );
654 this._trigger( "select", event, ui );
657 _filterMenuItems: function( character ) {
658 var escapedCharacter = character.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&" ),
659 regex = new RegExp( "^" + escapedCharacter, "i" );
661 return this.activeMenu
662 .find( this.options.items )
664 // Only match on items, not dividers or other content (#10571)
665 .filter( ".ui-menu-item" )
666 .filter( function() {
668 $.trim( $( this ).children( ".ui-menu-item-wrapper" ).text() ) );