Selectmenu: rework appendTo functionality

This commit is contained in:
Felix Nagel 2012-12-15 02:21:51 +01:00
parent 8aa24dd1d5
commit 57ecee889d

View File

@ -20,7 +20,7 @@ $.widget( "ui.selectmenu", {
version: "@VERSION", version: "@VERSION",
defaultElement: "<select>", defaultElement: "<select>",
options: { options: {
appendTo: "body", appendTo: null,
dropdown: true, dropdown: true,
position: { position: {
my: "left top", my: "left top",
@ -118,7 +118,7 @@ $.widget( "ui.selectmenu", {
width: ( this.options.dropdown ) ? this.button.outerWidth() : this.buttonText.width() + parseFloat( this.buttonText.css( "padding-left" ) ) || 0 + parseFloat( this.buttonText.css( "margin-left") ) || 0 width: ( this.options.dropdown ) ? this.button.outerWidth() : this.buttonText.width() + parseFloat( this.buttonText.css( "padding-left" ) ) || 0 + parseFloat( this.buttonText.css( "margin-left") ) || 0
}) })
.append( this.menu ) .append( this.menu )
.appendTo( this.options.appendTo ); .appendTo( this._appendTo() );
// init menu widget // init menu widget
menuInstance = this.menu.menu({ menuInstance = this.menu.menu({
@ -405,7 +405,7 @@ $.widget( "ui.selectmenu", {
this._super( key, value ); this._super( key, value );
if ( key === "appendTo" ) { if ( key === "appendTo" ) {
this.menuWrap.appendTo( this.document.find( value || "body" )[0] ); this.menuWrap.appendTo( this._appendTo() );
} }
if ( key === "disabled" ) { if ( key === "disabled" ) {
this.menu.menu( "option", "disabled", value ); this.menu.menu( "option", "disabled", value );
@ -420,6 +420,26 @@ $.widget( "ui.selectmenu", {
} }
}, },
_appendTo: function() {
var element = this.options.appendTo;
if ( element ) {
element = element.jquery || element.nodeType ?
$( element ) :
this.document.find( element ).eq( 0 );
}
if ( !element ) {
element = this.element.closest( ".ui-front" );
}
if ( !element.length ) {
element = this.document[0].body;
}
return element;
},
_toggleAttr: function(){ _toggleAttr: function(){
if ( this.options.dropdown ) { if ( this.options.dropdown ) {
this.button.toggleClass( "ui-corner-top", this.isOpen ).toggleClass( "ui-corner-all", !this.isOpen ); this.button.toggleClass( "ui-corner-top", this.isOpen ).toggleClass( "ui-corner-all", !this.isOpen );