* jQuery UI Selectmenu 1.12.1
* Copyright jQuery Foundation and other contributors
* Released under the MIT license.
* http://jquery.org/license
// jscs:disable maximumLineLength
//>>description: Duplicates and extends the functionality of a native HTML select element, allowing it to be customizable in behavior and appearance far beyond the limitations of a native select.
// jscs:enable maximumLineLength
//>>docs: http://api.jqueryui.com/selectmenu/
//>>demos: http://jqueryui.com/selectmenu/
//>>css.structure: ../../themes/base/core.css
//>>css.structure: ../../themes/base/selectmenu.css, ../../themes/base/button.css
//>>css.theme: ../../themes/base/theme.css
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
return $.widget( "ui.selectmenu", [ $.ui.formResetMixin, {
defaultElement: "<select>",
"ui-selectmenu-button-open": "ui-corner-top",
"ui-selectmenu-button-closed": "ui-corner-all"
button: "ui-icon-triangle-1-s"
var selectmenuId = this.element.uniqueId().attr( "id" );
button: selectmenuId + "-button",
menu: selectmenuId + "-menu"
this._bindFormResetHandler();
_drawButton: function() {
item = this._parseOption(
this.element.find( "option:selected" ),
this.element[ 0 ].selectedIndex
// Associate existing label with the new button
this.labels = this.element.labels().attr( "for", this.ids.button );
click: function( event ) {
// Hide original select element
this.button = $( "<span>", {
tabindex: this.options.disabled ? -1 : 0,
"aria-expanded": "false",
"aria-autocomplete": "list",
"aria-owns": this.ids.menu,
title: this.element.attr( "title" )
.insertAfter( this.element );
this._addClass( this.button, "ui-selectmenu-button ui-selectmenu-button-closed",
icon = $( "<span>" ).appendTo( this.button );
this._addClass( icon, "ui-selectmenu-icon", "ui-icon " + this.options.icons.button );
this.buttonItem = this._renderButtonItem( item )
.appendTo( this.button );
if ( this.options.width !== false ) {
this._on( this.button, this._buttonEvents );
this.button.one( "focusin", function() {
// Delay rendering the menu items until the button receives focus.
// The menu may have already been rendered via a programmatic open.
"aria-labelledby": this.ids.button,
this.menuWrap = $( "<div>" ).append( this.menu );
this._addClass( this.menuWrap, "ui-selectmenu-menu", "ui-front" );
this.menuWrap.appendTo( this._appendTo() );
// Initialize menu widget
this.menuInstance = this.menu
"ui-menu": "ui-corner-bottom"
select: function( event, ui ) {
// If the item was selected via a click, the text selection
// will be destroyed in IE
that._select( ui.item.data( "ui-selectmenu-item" ), event );
focus: function( event, ui ) {
var item = ui.item.data( "ui-selectmenu-item" );
// Prevent inital focus from firing and check if its a newly focused item
if ( that.focusIndex != null && item.index !== that.focusIndex ) {
that._trigger( "focus", event, { item: item } );
that._select( item, event );
that.focusIndex = item.index;
that.button.attr( "aria-activedescendant",
that.menuItems.eq( item.index ).attr( "id" ) );
// Don't close the menu on mouseleave
this.menuInstance._off( this.menu, "mouseleave" );
// Cancel the menu's collapseAll on document click
this.menuInstance._closeOnDocumentClick = function() {
// Selects often contain empty items, but never contain dividers
this.menuInstance._isDivider = function() {
this.buttonItem.replaceWith(
this.buttonItem = this._renderButtonItem(
// Fall back to an empty object in case there are no options
this._getSelectedItem().data( "ui-selectmenu-item" ) || {}
if ( this.options.width === null ) {
_refreshMenu: function() {
options = this.element.find( "option" );
this._parseOptions( options );
this._renderMenu( this.menu, this.items );
this.menuInstance.refresh();
this.menuItems = this.menu.find( "li" )
.not( ".ui-selectmenu-optgroup" )
.find( ".ui-menu-item-wrapper" );
item = this._getSelectedItem();
// Update the menu to have the correct item focused
this.menuInstance.focus( null, item );
this._setAria( item.data( "ui-selectmenu-item" ) );
this._setOption( "disabled", this.element.prop( "disabled" ) );
open: function( event ) {
if ( this.options.disabled ) {
// If this is the first time the menu is being opened, render the items
// Menu clears focus on close, reset focus to selected item
this._removeClass( this.menu.find( ".ui-state-active" ), null, "ui-state-active" );
this.menuInstance.focus( null, this._getSelectedItem() );
// If there are no options, don't open the menu
if ( !this.menuItems.length ) {
this._on( this.document, this._documentClick );
this._trigger( "open", event );
this.menuWrap.position( $.extend( { of: this.button }, this.options.position ) );
close: function( event ) {
this._off( this.document );
this._trigger( "close", event );
_renderButtonItem: function( item ) {
var buttonItem = $( "<span>" );
this._setText( buttonItem, item.label );
this._addClass( buttonItem, "ui-selectmenu-text" );
_renderMenu: function( ul, items ) {
$.each( items, function( index, item ) {
if ( item.optgroup !== currentOptgroup ) {
that._addClass( li, "ui-selectmenu-optgroup", "ui-menu-divider" +
( item.element.parent( "optgroup" ).prop( "disabled" ) ?
currentOptgroup = item.optgroup;
that._renderItemData( ul, item );
_renderItemData: function( ul, item ) {
return this._renderItem( ul, item ).data( "ui-selectmenu-item", item );
_renderItem: function( ul, item ) {
title: item.element.attr( "title" )
this._addClass( li, null, "ui-state-disabled" );
this._setText( wrapper, item.label );
return li.append( wrapper ).appendTo( ul );
_setText: function( element, value ) {
element.html( " " );
_move: function( direction, event ) {
filter = ".ui-menu-item";
item = this.menuItems.eq( this.focusIndex ).parent( "li" );
item = this.menuItems.eq( this.element[ 0 ].selectedIndex ).parent( "li" );
filter += ":not(.ui-state-disabled)";
if ( direction === "first" || direction === "last" ) {
next = item[ direction === "first" ? "prevAll" : "nextAll" ]( filter ).eq( -1 );
next = item[ direction + "All" ]( filter ).eq( 0 );
this.menuInstance.focus( event, next );
_getSelectedItem: function() {
return this.menuItems.eq( this.element[ 0 ].selectedIndex ).parent( "li" );
_toggle: function( event ) {
this[ this.isOpen ? "close" : "open" ]( event );
_setSelection: function() {
if ( window.getSelection ) {
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange( this.range );
// Setting the text selection kills the button focus in IE, but
// restoring the focus doesn't kill the selection.
mousedown: function( event ) {
if ( !$( event.target ).closest( ".ui-selectmenu-menu, #" +
$.ui.escapeSelector( this.ids.button ) ).length ) {
// Prevent text selection from being reset when interacting with the selectmenu (#10144)
if ( window.getSelection ) {
selection = window.getSelection();
if ( selection.rangeCount ) {
this.range = selection.getRangeAt( 0 );
this.range = document.selection.createRange();
click: function( event ) {
keydown: function( event ) {
var preventDefault = true;
switch ( event.keyCode ) {
case $.ui.keyCode.ESCAPE:
this._selectFocusedItem( event );
this._move( "prev", event );
this._move( "next", event );
this._selectFocusedItem( event );
this._move( "prev", event );
this._move( "next", event );
case $.ui.keyCode.PAGE_UP:
this._move( "first", event );
case $.ui.keyCode.PAGE_DOWN:
this._move( "last", event );
this.menu.trigger( event );