Skip to content

Commit cd162f6

Browse files
committed
Tooltip: Properly track hiding and closing for delegated tooltips
Fixes #10602
1 parent e3e5a9f commit cd162f6

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

ui/tooltip.js

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ return $.widget( "ui.tooltip", {
8686

8787
// IDs of generated tooltips, needed for destroy
8888
this.tooltips = {};
89+
8990
// IDs of parent tooltips where we removed the title attribute
9091
this.parents = {};
9192

@@ -117,8 +118,8 @@ return $.widget( "ui.tooltip", {
117118
this._super( key, value );
118119

119120
if ( key === "content" ) {
120-
$.each( this.tooltips, function( id, element ) {
121-
that._updateContent( element );
121+
$.each( this.tooltips, function( id, tooltipData ) {
122+
that._updateContent( tooltipData.element );
122123
});
123124
}
124125
},
@@ -127,9 +128,9 @@ return $.widget( "ui.tooltip", {
127128
var that = this;
128129

129130
// close open tooltips
130-
$.each( this.tooltips, function( id, element ) {
131+
$.each( this.tooltips, function( id, tooltipData ) {
131132
var event = $.Event( "blur" );
132-
event.target = event.currentTarget = element[0];
133+
event.target = event.currentTarget = tooltipData.element[ 0 ];
133134
that.close( event, true );
134135
});
135136

@@ -231,7 +232,7 @@ return $.widget( "ui.tooltip", {
231232
},
232233

233234
_open: function( event, target, content ) {
234-
var tooltip, events, delayedShow, a11yContent,
235+
var tooltipData, tooltip, events, delayedShow, a11yContent,
235236
positionOption = $.extend( {}, this.options.position );
236237

237238
if ( !content ) {
@@ -240,9 +241,9 @@ return $.widget( "ui.tooltip", {
240241

241242
// Content can be updated multiple times. If the tooltip already
242243
// exists, then just update the content and bail.
243-
tooltip = this._find( target );
244-
if ( tooltip.length ) {
245-
tooltip.find( ".ui-tooltip-content" ).html( content );
244+
tooltipData = this._find( target );
245+
if ( tooltipData ) {
246+
tooltipData.tooltip.find( ".ui-tooltip-content" ).html( content );
246247
return;
247248
}
248249

@@ -261,7 +262,8 @@ return $.widget( "ui.tooltip", {
261262
}
262263
}
263264

264-
tooltip = this._tooltip( target );
265+
tooltipData = this._tooltip( target );
266+
tooltip = tooltipData.tooltip;
265267
this._addDescribedBy( target, tooltip.attr( "id" ) );
266268
tooltip.find( ".ui-tooltip-content" ).html( content );
267269

@@ -296,8 +298,6 @@ return $.widget( "ui.tooltip", {
296298
}, this.options.position ) );
297299
}
298300

299-
this.hiding = false;
300-
this.closing = false;
301301
tooltip.hide();
302302

303303
this._show( tooltip, this.options.show );
@@ -343,13 +343,21 @@ return $.widget( "ui.tooltip", {
343343
},
344344

345345
close: function( event ) {
346-
var that = this,
346+
var tooltip,
347+
that = this,
347348
target = $( event ? event.currentTarget : this.element ),
348-
tooltip = this._find( target );
349+
tooltipData = this._find( target );
350+
351+
// The tooltip may already be closed
352+
if ( !tooltipData ) {
353+
return;
354+
}
355+
356+
tooltip = tooltipData.tooltip;
349357

350358
// disabling closes the tooltip, so we need to track when we're closing
351359
// to avoid an infinite loop in case the tooltip becomes disabled on close
352-
if ( this.closing ) {
360+
if ( tooltipData.closing ) {
353361
return;
354362
}
355363

@@ -364,12 +372,10 @@ return $.widget( "ui.tooltip", {
364372

365373
this._removeDescribedBy( target );
366374

367-
this.hiding = true;
375+
tooltipData.hiding = true;
368376
tooltip.stop( true );
369377
this._hide( tooltip, this.options.hide, function() {
370378
that._removeTooltip( $( this ) );
371-
this.hiding = false;
372-
this.closing = false;
373379
});
374380

375381
target.removeData( "ui-tooltip-open" );
@@ -388,10 +394,10 @@ return $.widget( "ui.tooltip", {
388394
});
389395
}
390396

391-
this.closing = true;
397+
tooltipData.closing = true;
392398
this._trigger( "close", event, { tooltip: tooltip } );
393-
if ( !this.hiding ) {
394-
this.closing = false;
399+
if ( !tooltipData.hiding ) {
400+
tooltipData.closing = false;
395401
}
396402
},
397403

@@ -407,13 +413,16 @@ return $.widget( "ui.tooltip", {
407413
.appendTo( tooltip );
408414

409415
tooltip.appendTo( this.document[0].body );
410-
this.tooltips[ id ] = element;
411-
return tooltip;
416+
417+
return this.tooltips[ id ] = {
418+
element: element,
419+
tooltip: tooltip
420+
};
412421
},
413422

414423
_find: function( target ) {
415424
var id = target.data( "ui-tooltip-id" );
416-
return id ? $( "#" + id ) : $();
425+
return id ? this.tooltips[ id ] : null;
417426
},
418427

419428
_removeTooltip: function( tooltip ) {
@@ -425,10 +434,11 @@ return $.widget( "ui.tooltip", {
425434
var that = this;
426435

427436
// close open tooltips
428-
$.each( this.tooltips, function( id, element ) {
437+
$.each( this.tooltips, function( id, tooltipData ) {
429438
// Delegate to close method to handle common cleanup
430-
var event = $.Event( "blur" );
431-
event.target = event.currentTarget = element[0];
439+
var event = $.Event( "blur" ),
440+
element = tooltipData.element;
441+
event.target = event.currentTarget = element[ 0 ];
432442
that.close( event, true );
433443

434444
// Remove immediately; destroying an open tooltip doesn't use the

0 commit comments

Comments
 (0)