Tooltip: Added a destroy method to remove generated tooltip elements.

This commit is contained in:
Scott González 2011-05-28 16:20:01 -04:00
parent 302ad626bf
commit 2fbd310a15

View File

@ -35,6 +35,9 @@ $.widget( "ui.tooltip", {
mouseover: "open", mouseover: "open",
focusin: "open" focusin: "open"
}); });
// IDs of generated tooltips, needed for destroy
this.tooltips = {};
}, },
_setOption: function( key, value ) { _setOption: function( key, value ) {
@ -114,7 +117,8 @@ $.widget( "ui.tooltip", {
}, },
close: function( event ) { close: function( event ) {
var target = $( event ? event.currentTarget : this.element ); var that = this,
target = $( event ? event.currentTarget : this.element );
target.attr( "title", target.data( "tooltip-title" ) ); target.attr( "title", target.data( "tooltip-title" ) );
if ( this.options.disabled ) { if ( this.options.disabled ) {
@ -127,6 +131,7 @@ $.widget( "ui.tooltip", {
tooltip.stop( true ); tooltip.stop( true );
this._hide( tooltip, this.options.hide, function() { this._hide( tooltip, this.options.hide, function() {
$( this ).remove(); $( this ).remove();
delete that[ this.id ];
}); });
// TODO: why isn't click unbound here? // TODO: why isn't click unbound here?
@ -136,9 +141,10 @@ $.widget( "ui.tooltip", {
}, },
_tooltip: function() { _tooltip: function() {
var tooltip = $( "<div>" ) var id = "ui-tooltip-" + increments++,
tooltip = $( "<div>" )
.attr({ .attr({
id: "ui-tooltip-" + increments++, id: id,
role: "tooltip" role: "tooltip"
}) })
.addClass( "ui-tooltip ui-widget ui-corner-all ui-widget-content" + .addClass( "ui-tooltip ui-widget ui-corner-all ui-widget-content" +
@ -147,12 +153,20 @@ $.widget( "ui.tooltip", {
.addClass( "ui-tooltip-content" ) .addClass( "ui-tooltip-content" )
.appendTo( tooltip ); .appendTo( tooltip );
tooltip.appendTo( document.body ); tooltip.appendTo( document.body );
this.tooltips[ id ] = true;
return tooltip; return tooltip;
}, },
_find: function( target ) { _find: function( target ) {
var id = target.attr( "aria-describedby" ); var id = target.attr( "aria-describedby" );
return id ? $( "#" + id ) : $(); return id ? $( "#" + id ) : $();
},
destroy: function() {
$.each( this.tooltips, function( id ) {
$( "#" + id ).remove();
});
this._super( "destroy" );
} }
}); });