diff options
Diffstat (limited to 'ui/jquery.ui.widget.js')
-rw-r--r-- | ui/jquery.ui.widget.js | 73 |
1 files changed, 66 insertions, 7 deletions
diff --git a/ui/jquery.ui.widget.js b/ui/jquery.ui.widget.js index ece6e9e16..c8e5348ac 100644 --- a/ui/jquery.ui.widget.js +++ b/ui/jquery.ui.widget.js @@ -139,14 +139,16 @@ $.Widget.prototype = { this.options, this._getCreateOptions(), options ); - if (element !== this) { - $.data(element, this.widgetName, this); - - var self = this; - this.element.bind("remove." + this.widgetName, function(){ - self.destroy(); - }); + + this.bindings = $(); + this.hoverable = $(); + this.focusable = $(); + + if ( element !== this ) { + $.data( element, this.widgetName, this ); + this._bind({ remove: "destroy" }); } + this._create(); this._trigger( "create" ); this._init(); @@ -166,6 +168,8 @@ $.Widget.prototype = { destroy: function() { this._destroy(); + // we can probably remove the unbind calls in 2.0 + // all event bindings should go through this._bind() this.element .unbind( "." + this.widgetName ) .removeData( this.widgetName ); @@ -175,6 +179,11 @@ $.Widget.prototype = { .removeClass( this.widgetBaseClass + "-disabled " + "ui-state-disabled" ); + + // clean up events and states + this.bindings.unbind( "." + this.widgetName ); + this.hoverable.removeClass( "ui-state-hover" ); + this.focusable.removeClass( "ui-state-focus" ); }, _destroy: $.noop, @@ -217,6 +226,8 @@ $.Widget.prototype = { this.widget() .toggleClass( this.widgetBaseClass + "-disabled ui-state-disabled", !!value ) .attr( "aria-disabled", value ); + this.hoverable.removeClass( "ui-state-hover" ); + this.focusable.removeClass( "ui-state-focus" ); } return this; @@ -229,6 +240,54 @@ $.Widget.prototype = { return this._setOption( "disabled", true ); }, + _bind: function( element, handlers ) { + // no element argument, shuffle and use this.element + if ( !handlers ) { + handlers = element; + element = this.element; + } else { + this.bindings = this.bindings.add( element ); + } + var instance = this; + $.each( handlers, function( event, handler ) { + element.bind( event + "." + instance.widgetName, function() { + // allow widgets to customize the disabled handling + // - disabled as an array instead of boolean + // - disabled class as method for disabling individual parts + if ( instance.options.disabled === true || + $( this ).hasClass( "ui-state-disabled" ) ) { + return; + } + return ( typeof handler === "string" ? instance[ handler ] : handler ) + .apply( instance, arguments ); + }); + }); + }, + + _hoverable: function( element ) { + this.hoverable = this.hoverable.add( element ); + this._bind( element, { + mouseenter: function( event ) { + $( event.currentTarget ).addClass( "ui-state-hover" ); + }, + mouseleave: function( event ) { + $( event.currentTarget ).removeClass( "ui-state-hover" ); + } + }); + }, + + _focusable: function( element ) { + this.focusable = this.focusable.add( element ); + this._bind( element, { + focusin: function( event ) { + $( event.currentTarget ).addClass( "ui-state-focus" ); + }, + focusout: function( event ) { + $( event.currentTarget ).removeClass( "ui-state-focus" ); + } + }); + }, + _trigger: function( type, event, data ) { var callback = this.options[ type ]; |