diff options
author | Alexander Schmitz <arschmitz@gmail.com> | 2017-04-19 11:32:13 -0400 |
---|---|---|
committer | Scott González <scott.gonzalez@gmail.com> | 2017-05-02 15:11:24 -0400 |
commit | abc9e7ce2f3b60a18bf1f461c7cbfccb3fa02b53 (patch) | |
tree | ca02e5f358c31b742d706724eaa291c52e175c8d /ui/widgets | |
parent | c866e455373028a62a0956455a229fef63e91fac (diff) | |
download | jquery-ui-abc9e7ce2f3b60a18bf1f461c7cbfccb3fa02b53.tar.gz jquery-ui-abc9e7ce2f3b60a18bf1f461c7cbfccb3fa02b53.zip |
Button: Fix backcompat when called on collection of mixed elements
Fixes #15109
Closes gh-1808
Diffstat (limited to 'ui/widgets')
-rw-r--r-- | ui/widgets/button.js | 87 |
1 files changed, 73 insertions, 14 deletions
diff --git a/ui/widgets/button.js b/ui/widgets/button.js index 50da9f9e2..42cfec06d 100644 --- a/ui/widgets/button.js +++ b/ui/widgets/button.js @@ -342,22 +342,81 @@ if ( $.uiBackCompat !== false ) { } ); $.fn.button = ( function( orig ) { - return function() { - if ( !this.length || ( this.length && this[ 0 ].tagName !== "INPUT" ) || - ( this.length && this[ 0 ].tagName === "INPUT" && ( - this.attr( "type" ) !== "checkbox" && this.attr( "type" ) !== "radio" - ) ) ) { - return orig.apply( this, arguments ); - } - if ( !$.ui.checkboxradio ) { - $.error( "Checkboxradio widget missing" ); - } - if ( arguments.length === 0 ) { - return this.checkboxradio( { - "icon": false + return function( options ) { + var isMethodCall = typeof options === "string"; + var args = Array.prototype.slice.call( arguments, 1 ); + var returnValue = this; + + if ( isMethodCall ) { + + // If this is an empty collection, we need to have the instance method + // return undefined instead of the jQuery instance + if ( !this.length && options === "instance" ) { + returnValue = undefined; + } else { + this.each( function() { + var methodValue; + var type = $( this ).attr( "type" ); + var name = type !== "checkbox" && type !== "radio" ? + "button" : + "checkboxradio"; + var instance = $.data( this, "ui-" + name ); + + if ( options === "instance" ) { + returnValue = instance; + return false; + } + + if ( !instance ) { + return $.error( "cannot call methods on button" + + " prior to initialization; " + + "attempted to call method '" + options + "'" ); + } + + if ( !$.isFunction( instance[ options ] ) || options.charAt( 0 ) === "_" ) { + return $.error( "no such method '" + options + "' for button" + + " widget instance" ); + } + + methodValue = instance[ options ].apply( instance, args ); + + if ( methodValue !== instance && methodValue !== undefined ) { + returnValue = methodValue && methodValue.jquery ? + returnValue.pushStack( methodValue.get() ) : + methodValue; + return false; + } + } ); + } + } else { + + // Allow multiple hashes to be passed on init + if ( args.length ) { + options = $.widget.extend.apply( null, [ options ].concat( args ) ); + } + + this.each( function() { + var type = $( this ).attr( "type" ); + var name = type !== "checkbox" && type !== "radio" ? "button" : "checkboxradio"; + var instance = $.data( this, "ui-" + name ); + + if ( instance ) { + instance.option( options || {} ); + if ( instance._init ) { + instance._init(); + } + } else { + if ( name === "button" ) { + orig.call( $( this ), options ); + return; + } + + $( this ).checkboxradio( $.extend( { icon: false }, options ) ); + } } ); } - return this.checkboxradio.apply( this, arguments ); + + return returnValue; }; } )( $.fn.button ); |