defaultElement: "<select>",
options: {
appendTo: null,
+ classes: {
+ "ui-selectmenu-button-open": "ui-corner-top",
+ "ui-selectmenu-button-closed": "ui-corner-all"
+ },
disabled: null,
icons: {
button: "ui-icon-triangle-1-s"
},
_drawButton: function() {
- var that = this,
+ var icon,
+ that = this,
item = this._parseOption(
this.element.find( "option:selected" ),
this.element[ 0 ].selectedIndex
// Create button
this.button = $( "<span>", {
- "class": "ui-selectmenu-button ui-widget ui-state-default ui-corner-all",
tabindex: this.options.disabled ? -1 : 0,
id: this.ids.button,
role: "combobox",
})
.insertAfter( this.element );
- $( "<span>", {
- "class": "ui-icon " + this.options.icons.button
- })
- .prependTo( this.button );
+ this._addClass( this.button, "ui-selectmenu-button ui-selectmenu-button-closed",
+ "ui-widget ui-state-default" );
+
+ icon = $( "<span>" ).prependTo( this.button );
+ this._addClass( icon, null, "ui-icon " + this.options.icons.button );
this.buttonItem = this._renderButtonItem( item )
.appendTo( this.button );
});
// Wrap menu
- this.menuWrap = $( "<div>", {
- "class": "ui-selectmenu-menu ui-front"
- })
- .append( this.menu )
- .appendTo( this._appendTo() );
+ 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
.menu({
+ classes: {
+ "ui-menu": "ui-corner-bottom"
+ },
role: "listbox",
select: function( event, ui ) {
event.preventDefault();
})
.menu( "instance" );
- // Adjust menu styles to dropdown
- this.menu
- .addClass( "ui-corner-bottom" )
- .removeClass( "ui-corner-all" );
-
// Don't close the menu on mouseleave
this.menuInstance._off( this.menu, "mouseleave" );
} else {
// Menu clears focus on close, reset focus to selected item
- this.menu.find( ".ui-state-active" ).removeClass( "ui-state-active" );
+ this._removeClass( this.menu.find( ".ui-state-active" ), null, "ui-state-active" );
this.menuInstance.focus( null, this._getSelectedItem() );
}
},
_renderButtonItem: function( item ) {
- var buttonItem = $( "<span>", {
- "class": "ui-selectmenu-text"
- });
+ var buttonItem = $( "<span>" );
+
this._setText( buttonItem, item.label );
+ this._addClass( buttonItem, "ui-selectmenu-text" );
return buttonItem;
},
currentOptgroup = "";
$.each( items, function( index, item ) {
+ var li;
+
if ( item.optgroup !== currentOptgroup ) {
- $( "<li>", {
- "class": "ui-selectmenu-optgroup ui-menu-divider" +
- ( item.element.parent( "optgroup" ).prop( "disabled" ) ?
- " ui-state-disabled" :
- "" ),
+ li = $( "<li>", {
text: item.optgroup
- })
- .appendTo( ul );
+ });
+ that._addClass( li, "ui-selectmenu-optgroup", "ui-menu-divider" +
+ ( item.element.parent( "optgroup" ).prop( "disabled" ) ?
+ " ui-state-disabled" :
+ "" ) );
+
+ li.appendTo( ul );
currentOptgroup = item.optgroup;
}
});
if ( item.disabled ) {
- li.addClass( "ui-state-disabled" );
+ this._addClass( li, null, "ui-state-disabled" );
}
this._setText( wrapper, item.label );
_setOption: function( key, value ) {
if ( key === "icons" ) {
- this.button.find( "span.ui-icon" )
- .removeClass( this.options.icons.button )
- .addClass( value.button );
+ var icon = this.button.find( "span.ui-icon" );
+ this._removeClass( icon, null, this.options.icons.button )
+ ._addClass( icon, null, value.button );
}
this._super( key, value );
if ( key === "disabled" ) {
this.menuInstance.option( "disabled", value );
- this.button
- .toggleClass( "ui-state-disabled", value )
- .attr( "aria-disabled", value );
+ this.button.attr( "aria-disabled", value );
+ this._toggleClass( this.button, null, "ui-state-disabled", value );
this.element.prop( "disabled", value );
if ( value ) {
},
_toggleAttr: function() {
- this.button
- .toggleClass( "ui-corner-top", this.isOpen )
- .toggleClass( "ui-corner-all", !this.isOpen )
- .attr( "aria-expanded", this.isOpen );
- this.menuWrap.toggleClass( "ui-selectmenu-open", this.isOpen );
+ this.button.attr( "aria-expanded", this.isOpen );
+
+ // We can't use two _toggleClass() calls here, because we need to make sure
+ // we always remove classes first and add them second, otherwise if both classes have the
+ // same theme class, it will be removed after we add it.
+ this._removeClass( this.button, "ui-selectmenu-button-" +
+ ( this.isOpen ? "closed" : "open" ) )
+ ._addClass( this.button, "ui-selectmenu-button-" +
+ ( this.isOpen ? "open" : "closed" ) )
+ ._toggleClass( this.menuWrap, "ui-selectmenu-open", null, this.isOpen );
+
this.menu.attr( "aria-hidden", !this.isOpen );
},