jquery-ui/ui/selectmenu.js

547 lines
12 KiB
JavaScript
Raw Normal View History

/*!
2011-09-01 22:21:09 +00:00
* jQuery UI Selectmenu @VERSION
2012-07-16 18:36:01 +00:00
* http://jqueryui.com
2011-09-01 22:21:09 +00:00
*
2014-01-29 03:25:02 +00:00
* Copyright 2014 jQuery Foundation and other contributors
2012-08-11 17:57:17 +00:00
* Released under the MIT license.
2011-09-01 22:21:09 +00:00
* http://jquery.org/license
*
2012-11-29 18:41:20 +00:00
* http://api.jqueryui.com/selectmenu
2011-09-01 22:21:09 +00:00
*/
2014-02-21 19:56:13 +00:00
(function( factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define([
"jquery",
"./core",
"./widget",
"./position",
"./menu"
], factory );
} else {
// Browser globals
factory( jQuery );
}
}(function( $ ) {
2011-09-01 22:21:09 +00:00
return $.widget( "ui.selectmenu", {
2011-09-01 22:21:09 +00:00
version: "@VERSION",
defaultElement: "<select>",
options: {
appendTo: null,
icons: {
button: "ui-icon-triangle-1-s"
},
2011-09-01 22:21:09 +00:00
position: {
my: "left top",
at: "left bottom",
collision: "none"
},
width: null,
2011-09-01 22:21:09 +00:00
// callbacks
change: null,
close: null,
focus: null,
open: null,
select: null
2011-09-01 22:21:09 +00:00
},
_create: function() {
2012-11-12 22:06:05 +00:00
var selectmenuId = this.element.uniqueId().attr( "id" );
this.ids = {
element: selectmenuId,
button: selectmenuId + "-button",
menu: selectmenuId + "-menu"
2013-04-29 19:54:36 +00:00
};
2011-11-18 16:14:38 +00:00
2011-10-11 23:38:30 +00:00
this._drawButton();
this._drawMenu();
2012-01-27 22:11:31 +00:00
if ( this.options.disabled ) {
this.disable();
}
},
2011-11-18 16:14:38 +00:00
2011-10-11 23:38:30 +00:00
_drawButton: function() {
var that = this,
tabindex = this.element.attr( "tabindex" );
2013-09-16 18:45:48 +00:00
// Associate existing label with the new button
2013-04-29 19:54:36 +00:00
this.label = $( "label[for='" + this.ids.element + "']" ).attr( "for", this.ids.button );
this._on( this.label, {
click: function( event ) {
this.button.focus();
event.preventDefault();
}
});
2011-11-18 16:14:38 +00:00
2013-09-16 18:45:48 +00:00
// Hide original select element
this.element.hide();
2011-11-18 16:14:38 +00:00
// Create button
this.button = $( "<span>", {
"class": "ui-selectmenu-button ui-widget ui-state-default ui-corner-all",
tabindex: tabindex || this.options.disabled ? -1 : 0,
id: this.ids.button,
2012-11-12 22:06:05 +00:00
role: "combobox",
"aria-expanded": "false",
2012-11-12 22:06:05 +00:00
"aria-autocomplete": "list",
"aria-owns": this.ids.menu,
"aria-haspopup": "true"
})
.insertAfter( this.element );
$( "<span>", {
"class": "ui-icon " + this.options.icons.button
}).prependTo( this.button );
2012-11-29 19:10:05 +00:00
this.buttonText = $( "<span>", {
"class": "ui-selectmenu-text"
})
.appendTo( this.button );
this._setText( this.buttonText, this.element.find( "option:selected" ).text() );
this._setOption( "width", this.options.width );
2011-11-18 16:14:38 +00:00
this._on( this.button, this._buttonEvents );
this.button.one( "focusin", function() {
// Delay rendering the menu items until the button receives focus
that._refreshMenu();
});
this._hoverable( this.button );
this._focusable( this.button );
2011-09-01 22:21:09 +00:00
},
2011-11-18 16:14:38 +00:00
2011-10-11 23:38:30 +00:00
_drawMenu: function() {
2013-05-30 19:25:36 +00:00
var that = this;
2011-11-18 16:14:38 +00:00
2013-09-16 18:45:48 +00:00
// Create menu
2012-11-29 19:10:05 +00:00
this.menu = $( "<ul>", {
"aria-hidden": "true",
2012-11-12 22:06:05 +00:00
"aria-labelledby": this.ids.button,
id: this.ids.menu
});
2011-11-18 16:14:38 +00:00
// Wrap menu
2012-11-29 19:10:05 +00:00
this.menuWrap = $( "<div>", {
"class": "ui-selectmenu-menu ui-front"
2013-09-16 18:45:48 +00:00
})
.append( this.menu )
.appendTo( this._appendTo() );
2011-11-18 16:14:38 +00:00
2013-09-16 18:45:48 +00:00
// Initialize menu widget
this.menuInstance = this.menu.menu({
2013-09-16 18:45:48 +00:00
role: "listbox",
select: function( event, ui ) {
event.preventDefault();
that._select( ui.item.data( "ui-selectmenu-item" ), event );
},
focus: function( event, ui ) {
var item = ui.item.data( "ui-selectmenu-item" );
2013-09-16 18:45:48 +00:00
// Prevent inital focus from firing and checks if its a newly focused item
if ( that.focusIndex != null && item.index !== that.focusIndex ) {
that._trigger( "focus", event, { item: item } );
if ( !that.isOpen ) {
that._select( item, event );
}
}
2013-04-29 20:15:50 +00:00
that.focusIndex = item.index;
2013-09-16 18:45:48 +00:00
that.button.attr( "aria-activedescendant",
that.menuItems.eq( item.index ).attr( "id" ) );
}
2012-07-17 19:27:26 +00:00
})
.menu( "instance" );
2012-01-27 22:11:31 +00:00
2013-09-16 18:45:48 +00:00
// Adjust menu styles to dropdown
this.menu.addClass( "ui-corner-bottom" ).removeClass( "ui-corner-all" );
2011-11-18 16:14:38 +00:00
2013-09-16 18:45:48 +00:00
// TODO: Can we make this cleaner?
// If not, at least update the comment to say what we're removing
// Unbind uneeded menu events
this.menuInstance._off( this.menu, "mouseleave" );
// Cancel the menu's collapseAll on document click
this.menuInstance._closeOnDocumentClick = function() {
return false;
};
this.menuInstance._isDivider = function() {
return false;
2013-12-18 19:11:29 +00:00
};
},
2011-11-18 16:14:38 +00:00
refresh: function() {
this._refreshMenu();
this._setText( this.buttonText, this._getSelectedItem().text() );
this._setOption( "width", this.options.width );
},
_refreshMenu: function() {
2011-10-11 23:38:30 +00:00
this.menu.empty();
2011-11-18 16:14:38 +00:00
2012-05-24 16:30:39 +00:00
var item,
2012-11-12 22:06:05 +00:00
options = this.element.find( "option" );
if ( !options.length ) {
return;
}
this._readOptions( options );
this._renderMenu( this.menu, this.items );
2012-01-27 22:11:31 +00:00
2013-09-16 18:45:48 +00:00
this.menuInstance.refresh();
2013-10-09 23:01:41 +00:00
this.menuItems = this.menu.find( "li" ).not( ".ui-selectmenu-optgroup" );
2011-11-18 16:14:38 +00:00
item = this._getSelectedItem();
2013-09-16 18:45:48 +00:00
// Update the menu to have the correct item focused
this.menuInstance.focus( null, item );
this._setAria( item.data( "ui-selectmenu-item" ) );
2013-08-31 09:50:53 +00:00
// Set disabled state
2013-09-16 18:45:48 +00:00
this._setOption( "disabled", this.element.prop( "disabled" ) );
2011-11-18 16:14:38 +00:00
},
open: function( event ) {
if ( this.options.disabled ) {
return;
}
2013-09-16 18:45:48 +00:00
// If this is the first time the menu is being opened, render the items
if ( !this.menuItems ) {
this._refreshMenu();
} else {
2013-09-16 18:45:48 +00:00
// TODO: Why is this necessary?
// Shouldn't the underlying menu always have accurate state?
this.menu.find( ".ui-state-focus" ).removeClass( "ui-state-focus" );
2013-09-16 18:45:48 +00:00
this.menuInstance.focus( null, this._getSelectedItem() );
2013-10-21 19:18:33 +00:00
this.menuItems.eq( this.element[ 0 ].selectedIndex ).addClass( "ui-state-active" );
}
this.isOpen = true;
this._toggleAttr();
this._resizeMenu();
this._position();
this._on( this.document, this._documentClick );
this._trigger( "open", event );
2011-11-18 16:14:38 +00:00
},
_position: function() {
this.menuWrap.position( $.extend( { of: this.button }, this.options.position ) );
},
2011-11-18 16:14:38 +00:00
close: function( event ) {
if ( !this.isOpen ) {
return;
2011-09-22 19:20:49 +00:00
}
this.isOpen = false;
this._toggleAttr();
// Check if we have an item to select
if ( this.menuItems ) {
this.menuInstance.active = this._getSelectedItem();
}
this._off( this.document );
this._trigger( "close", event );
2011-09-01 22:21:09 +00:00
},
2011-11-18 16:14:38 +00:00
2011-10-17 20:13:36 +00:00
widget: function() {
return this.button;
},
menuWidget: function() {
return this.menu;
2011-10-17 20:13:36 +00:00
},
2011-11-18 16:14:38 +00:00
2011-09-01 22:21:09 +00:00
_renderMenu: function( ul, items ) {
2011-09-28 21:59:23 +00:00
var that = this,
2011-09-01 22:21:09 +00:00
currentOptgroup = "";
2011-11-18 16:14:38 +00:00
2011-09-01 22:21:09 +00:00
$.each( items, function( index, item ) {
2012-05-24 16:30:39 +00:00
if ( item.optgroup !== currentOptgroup ) {
2013-01-02 18:52:18 +00:00
$( "<li>", {
2013-10-09 23:01:41 +00:00
"class": "ui-selectmenu-optgroup ui-menu-divider" +
2013-09-16 18:45:48 +00:00
( item.element.parent( "optgroup" ).attr( "disabled" ) ?
" ui-state-disabled" :
"" ),
text: item.optgroup
2013-09-16 18:45:48 +00:00
})
.appendTo( ul );
2011-09-01 22:21:09 +00:00
currentOptgroup = item.optgroup;
}
that._renderItemData( ul, item );
2011-09-01 22:21:09 +00:00
});
},
2011-11-18 16:14:38 +00:00
_renderItemData: function( ul, item ) {
return this._renderItem( ul, item ).data( "ui-selectmenu-item", item );
},
_renderItem: function( ul, item ) {
2013-10-09 23:01:41 +00:00
var li = $( "<li>" );
if ( item.disabled ) {
2012-11-12 22:06:05 +00:00
li.addClass( "ui-state-disabled" );
2011-11-18 16:14:38 +00:00
}
2013-10-09 23:01:41 +00:00
this._setText( li, item.label );
2013-10-09 23:01:41 +00:00
return li.appendTo( ul );
},
2011-11-18 16:14:38 +00:00
_setText: function( element, value ) {
if ( value ) {
element.text( value );
} else {
element.html( "&#160;" );
}
2011-09-01 22:21:09 +00:00
},
2011-11-18 16:14:38 +00:00
2012-01-27 22:11:31 +00:00
_move: function( direction, event ) {
var filter = ".ui-menu-item",
item, next;
if ( this.isOpen ) {
item = this.menuItems.eq( this.focusIndex );
} else {
item = this.menuItems.eq( this.element[ 0 ].selectedIndex );
filter += ":not(.ui-state-disabled)";
}
2012-05-16 18:41:24 +00:00
if ( direction === "first" || direction === "last" ) {
next = item[ direction === "first" ? "prevAll" : "nextAll" ]( filter ).eq( -1 );
} else {
next = item[ direction + "All" ]( filter ).eq( 0 );
}
if ( next.length ) {
this.menu.menu( "focus", event, next );
}
2011-09-24 00:04:06 +00:00
},
2011-11-18 16:14:38 +00:00
_getSelectedItem: function() {
2013-10-09 23:01:41 +00:00
return this.menuItems.eq( this.element[ 0 ].selectedIndex );
},
2011-11-18 16:14:38 +00:00
2011-09-24 00:04:06 +00:00
_toggle: function( event ) {
2011-11-18 16:14:38 +00:00
if ( this.isOpen ) {
2011-09-24 00:04:06 +00:00
this.close( event );
2011-11-18 16:14:38 +00:00
} else {
2011-09-24 00:04:06 +00:00
this.open( event );
}
},
2011-11-18 16:14:38 +00:00
_documentClick: {
mousedown: function( event ) {
if ( this.isOpen && !$( event.target ).closest( ".ui-selectmenu-menu, #" + this.ids.button ).length ) {
this.close( event );
}
}
},
_buttonEvents: {
2013-09-16 18:45:48 +00:00
click: "_toggle",
keydown: function( event ) {
2013-09-16 18:45:48 +00:00
var preventDefault = true;
switch ( event.keyCode ) {
case $.ui.keyCode.TAB:
case $.ui.keyCode.ESCAPE:
2013-09-16 18:45:48 +00:00
this.close( event );
preventDefault = false;
break;
case $.ui.keyCode.ENTER:
if ( this.isOpen ) {
this._selectMenu( event );
2011-11-18 16:14:38 +00:00
}
break;
case $.ui.keyCode.UP:
if ( event.altKey ) {
this._toggle( event );
} else {
this._move( "prev", event );
}
break;
case $.ui.keyCode.DOWN:
if ( event.altKey ) {
this._toggle( event );
} else {
this._move( "next", event );
}
break;
2013-03-07 19:10:56 +00:00
case $.ui.keyCode.SPACE:
if ( this.isOpen ) {
this._selectMenu( event );
} else {
this._toggle( event );
2013-03-07 19:10:56 +00:00
}
break;
case $.ui.keyCode.LEFT:
this._move( "prev", event );
break;
case $.ui.keyCode.RIGHT:
this._move( "next", event );
2012-01-27 22:11:31 +00:00
break;
case $.ui.keyCode.HOME:
case $.ui.keyCode.PAGE_UP:
this._move( "first", event );
break;
case $.ui.keyCode.END:
case $.ui.keyCode.PAGE_DOWN:
this._move( "last", event );
break;
default:
2011-10-11 23:38:30 +00:00
this.menu.trigger( event );
2013-09-16 18:45:48 +00:00
preventDefault = false;
}
2013-09-16 18:45:48 +00:00
if ( preventDefault ) {
event.preventDefault();
}
}
2011-11-18 16:14:38 +00:00
},
2012-01-27 22:11:31 +00:00
_selectMenu: function( event ) {
if ( !this.menuItems.eq( this.focusIndex ).hasClass( "ui-state-disabled" ) ) {
this.menuInstance.select( event );
}
},
2012-01-27 22:11:31 +00:00
_select: function( item, event ) {
var oldIndex = this.element[ 0 ].selectedIndex;
2013-09-16 18:45:48 +00:00
// Change native select element
this.element[ 0 ].selectedIndex = item.index;
this._setText( this.buttonText, item.label );
this._setAria( item );
this._trigger( "select", event, { item: item } );
2012-05-16 18:41:24 +00:00
if ( item.index !== oldIndex ) {
this._trigger( "change", event, { item: item } );
}
2013-09-16 18:45:48 +00:00
this.close( event );
},
2012-01-27 22:11:31 +00:00
_setAria: function( item ) {
2013-10-09 23:01:41 +00:00
var id = this.menuItems.eq( item.index ).attr( "id" );
this.button.attr({
"aria-labelledby": id,
"aria-activedescendant": id
});
this.menu.attr( "aria-activedescendant", id );
2012-01-12 21:01:49 +00:00
},
2011-11-18 16:14:38 +00:00
2011-09-01 22:21:09 +00:00
_setOption: function( key, value ) {
if ( key === "icons" ) {
this.button.find( "span.ui-icon" )
.removeClass( this.options.icons.button )
.addClass( value.button );
}
this._super( key, value );
2011-11-18 16:14:38 +00:00
2011-09-01 22:21:09 +00:00
if ( key === "appendTo" ) {
this.menuWrap.appendTo( this._appendTo() );
2011-09-01 22:21:09 +00:00
}
if ( key === "disabled" ) {
2013-09-16 18:45:48 +00:00
this.menuInstance.option( "disabled", value );
this.button
2013-09-16 18:45:48 +00:00
.toggleClass( "ui-state-disabled", value )
.attr( "aria-disabled", value );
2013-09-16 18:45:48 +00:00
this.element.prop( "disabled", value );
if ( value ) {
2011-10-11 23:38:30 +00:00
this.button.attr( "tabindex", -1 );
2011-10-17 20:04:54 +00:00
this.close();
} else {
this.button.attr( "tabindex", 0 );
}
}
if ( key === "width" ) {
if ( !value ) {
value = this.element.outerWidth();
}
this.button.outerWidth( value );
}
2011-09-01 22:21:09 +00:00
},
2012-01-27 22:11:31 +00:00
_appendTo: function() {
var element = this.options.appendTo;
if ( element ) {
element = element.jquery || element.nodeType ?
$( element ) :
this.document.find( element ).eq( 0 );
}
if ( !element || !element[ 0 ] ) {
element = this.element.closest( ".ui-front" );
}
if ( !element.length ) {
2013-04-29 20:36:37 +00:00
element = this.document[ 0 ].body;
}
return element;
},
2013-11-13 20:26:48 +00:00
_toggleAttr: function() {
2013-09-16 18:45:48 +00:00
this.button
.toggleClass( "ui-corner-top", this.isOpen )
.toggleClass( "ui-corner-all", !this.isOpen );
2012-11-12 22:06:05 +00:00
this.menuWrap.toggleClass( "ui-selectmenu-open", this.isOpen );
2013-09-16 18:45:48 +00:00
this.menu.attr( "aria-hidden", !this.isOpen );
this.button.attr( "aria-expanded", this.isOpen );
},
_resizeMenu: function() {
this.menu.outerWidth( Math.max(
this.button.outerWidth(),
// IE10 wraps long text (possibly a rounding bug)
// so we add 1px to avoid the wrapping
this.menu.width( "" ).outerWidth() + 1
) );
},
_getCreateOptions: function() {
2013-09-16 18:45:48 +00:00
return { disabled: this.element.prop( "disabled" ) };
},
_readOptions: function( options ) {
2011-11-18 16:14:38 +00:00
var data = [];
options.each( function( index, item ) {
var option = $( item ),
optgroup = option.parent( "optgroup" );
data.push({
element: option,
index: index,
2012-11-12 22:06:05 +00:00
value: option.attr( "value" ),
label: option.text(),
2012-05-24 16:30:39 +00:00
optgroup: optgroup.attr( "label" ) || "",
disabled: optgroup.attr( "disabled" ) || option.attr( "disabled" )
2011-09-24 02:44:13 +00:00
});
});
this.items = data;
2011-09-01 22:21:09 +00:00
},
2011-11-18 16:14:38 +00:00
2011-09-24 00:04:06 +00:00
_destroy: function() {
2011-10-11 23:38:30 +00:00
this.menuWrap.remove();
this.button.remove();
this.element.show();
this.element.removeUniqueId();
2013-04-29 19:54:36 +00:00
this.label.attr( "for", this.ids.element );
}
2011-09-01 22:21:09 +00:00
});
2014-02-21 19:56:13 +00:00
}));