Skip to content

Commit

Permalink
Tooltip: handle removal of elements with delegated tooltips. Fixed #8…
Browse files Browse the repository at this point in the history
…646 - Delegated tooltips don't close when the tooltipped element is removed
  • Loading branch information
couchand authored and jzaefferer committed Oct 21, 2012
1 parent 6b48ef5 commit 3b2d1e7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions tests/unit/tooltip/tooltip.html
Expand Up @@ -43,6 +43,7 @@ <h2 id="qunit-userAgent"></h2>
<input title="inputtitle">
<span id="multiple-describedby" aria-describedby="fixture-span" title="...">aria-describedby</span>
<span id="fixture-span" title="title-text">span</span>
<span id="contains-tooltipped"><span id="contained-tooltipped" title="foobar">baz</span></span>
</div>

</div>
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/tooltip/tooltip_core.js
Expand Up @@ -44,4 +44,17 @@ test( "accessibility", function() {
equal( element.attr( "title" ), "...", "title restored when closed" );
});

test( "delegated removal", function() {
expect( 2 );

var container = $( "#contains-tooltipped" ).tooltip(),
element = $( "#contained-tooltipped" );

element.trigger( "mouseover" );
equal( $( ".ui-tooltip" ).length, 1 );

container.empty();
equal( $( ".ui-tooltip" ).length, 0 );
});

}( jQuery ) );
16 changes: 14 additions & 2 deletions ui/jquery.ui.tooltip.js
Expand Up @@ -207,6 +207,7 @@ $.widget( "ui.tooltip", {

_open: function( event, target, content ) {
var tooltip, positionOption, events;

if ( !content ) {
return;
}
Expand Down Expand Up @@ -268,6 +269,9 @@ $.widget( "ui.tooltip", {
fakeEvent.currentTarget = target[0];
this.close( fakeEvent, true );
}
},
remove: function( event ) {
this._removeTooltip( tooltip );
}
};
if ( !event || event.type === "mouseover" ) {
Expand Down Expand Up @@ -299,12 +303,15 @@ $.widget( "ui.tooltip", {

tooltip.stop( true );
this._hide( tooltip, this.options.hide, function() {
$( this ).remove();
delete that.tooltips[ this.id ];
that._removeTooltip( $( this ) );
});

target.removeData( "tooltip-open" );
this._off( target, "mouseleave focusout keyup" );
// Remove 'remove' binding only on delegated targets
if ( target[0] !== this.element[0] ) {
this._off( target, "remove" );
}
this._off( this.document, "mousemove" );

if ( event && event.type === "mouseleave" ) {
Expand Down Expand Up @@ -344,6 +351,11 @@ $.widget( "ui.tooltip", {
return id ? $( "#" + id ) : $();
},

_removeTooltip: function( tooltip ) {
tooltip.remove();
delete this.tooltips[ tooltip.attr( "id" ) ];
},

_destroy: function() {
var that = this;

Expand Down

0 comments on commit 3b2d1e7

Please sign in to comment.