Skip to content

Commit

Permalink
Widget: Improve remove event bindings for classes options
Browse files Browse the repository at this point in the history
Fixes #15078
Fixes #15082
Fixes #15095
Fixes #15136
Fixes #15152
Closes gh-1769
  • Loading branch information
scottgonzalez committed Apr 19, 2017
1 parent c426b9a commit ef2e9ba
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions ui/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,29 @@ $.Widget.prototype = {
classes: this.options.classes || {}
}, options );

function bindRemoveEvent() {
options.element.each( function( _, element ) {
var isTracked = $.map( that.classesElementLookup, function( elements ) {
return elements;
} )
.some( function( elements ) {
return elements.is( element );
} );

if ( !isTracked ) {
that._on( $( element ), {
remove: "_untrackClassesElement"
} );
}
} );
}

function processClassString( classes, checkOption ) {
var current, i;
for ( i = 0; i < classes.length; i++ ) {
current = that.classesElementLookup[ classes[ i ] ] || $();
if ( options.add ) {
bindRemoveEvent();
current = $( $.unique( current.get().concat( options.element.get() ) ) );
} else {
current = $( current.not( options.element ).get() );
Expand All @@ -510,10 +528,6 @@ $.Widget.prototype = {
}
}

this._on( options.element, {
"remove": "_untrackClassesElement"
} );

if ( options.keys ) {
processClassString( options.keys.match( /\S+/g ) || [], true );
}
Expand All @@ -531,6 +545,8 @@ $.Widget.prototype = {
that.classesElementLookup[ key ] = $( value.not( event.target ).get() );
}
} );

this._off( $( event.target ) );
},

_removeClass: function( element, keys, extra ) {
Expand Down Expand Up @@ -611,7 +627,7 @@ $.Widget.prototype = {
_off: function( element, eventName ) {
eventName = ( eventName || "" ).split( " " ).join( this.eventNamespace + " " ) +
this.eventNamespace;
element.off( eventName ).off( eventName );
element.off( eventName );

// Clear the stack to avoid memory leaks (#10056)
this.bindings = $( this.bindings.not( element ).get() );
Expand Down

0 comments on commit ef2e9ba

Please sign in to comment.