diff options
author | Alexander Schmitz <arschmitz@gmail.com> | 2014-12-03 11:26:46 -0500 |
---|---|---|
committer | Alexander Schmitz <arschmitz@gmail.com> | 2015-03-11 16:05:07 -0400 |
commit | 3483486a153f6f7b3a8bbe9783735eb63adfd033 (patch) | |
tree | 88e3a3ab136b1cc3741b4f7a49915d284e96c040 /ui/selectmenu.js | |
parent | 2a7873dd5711db472a87f749e698e80c49ccf8cd (diff) | |
download | jquery-ui-3483486a153f6f7b3a8bbe9783735eb63adfd033.tar.gz jquery-ui-3483486a153f6f7b3a8bbe9783735eb63adfd033.zip |
Selectmenu: Add classes option
Ref #7053
Ref gh-1411
Diffstat (limited to 'ui/selectmenu.js')
-rw-r--r-- | ui/selectmenu.js | 87 |
1 files changed, 48 insertions, 39 deletions
diff --git a/ui/selectmenu.js b/ui/selectmenu.js index 9bd2d1016..0d1354c6d 100644 --- a/ui/selectmenu.js +++ b/ui/selectmenu.js @@ -39,6 +39,10 @@ return $.widget( "ui.selectmenu", { 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" @@ -78,7 +82,8 @@ return $.widget( "ui.selectmenu", { }, _drawButton: function() { - var that = this, + var icon, + that = this, item = this._parseOption( this.element.find( "option:selected" ), this.element[ 0 ].selectedIndex @@ -98,7 +103,6 @@ return $.widget( "ui.selectmenu", { // 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", @@ -110,10 +114,11 @@ return $.widget( "ui.selectmenu", { }) .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 ); @@ -146,15 +151,16 @@ return $.widget( "ui.selectmenu", { }); // 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(); @@ -184,11 +190,6 @@ return $.widget( "ui.selectmenu", { }) .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" ); @@ -258,7 +259,7 @@ return $.widget( "ui.selectmenu", { } 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() ); } @@ -304,10 +305,10 @@ return $.widget( "ui.selectmenu", { }, _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; }, @@ -317,15 +318,18 @@ return $.widget( "ui.selectmenu", { 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; } @@ -345,7 +349,7 @@ return $.widget( "ui.selectmenu", { }); if ( item.disabled ) { - li.addClass( "ui-state-disabled" ); + this._addClass( li, null, "ui-state-disabled" ); } this._setText( wrapper, item.label ); @@ -542,9 +546,9 @@ return $.widget( "ui.selectmenu", { _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 ); @@ -555,9 +559,8 @@ return $.widget( "ui.selectmenu", { 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 ) { @@ -594,11 +597,17 @@ return $.widget( "ui.selectmenu", { }, _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 ); }, |