Skip to content

Commit 88291ff

Browse files
mziechscottgonzalez
authored andcommitted
Tooltip: Register event handlers before content is loaded
Fixes #8740 Closes jquerygh-1053 Closes jquerygh-1456 (cherry picked from commit c4e367b)
1 parent 1c92d68 commit 88291ff

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

tests/unit/tooltip/tooltip_options.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ asyncTest( "content: sync + async callback", function() {
7171
}).tooltip( "open" );
7272
});
7373

74+
// http://bugs.jqueryui.com/ticket/8740
75+
asyncTest( "content: async callback loses focus before load", function() {
76+
expect( 1 );
77+
78+
var element = $( "#tooltipped1" ).tooltip({
79+
content: function( response ) {
80+
setTimeout(function() {
81+
element.trigger( "mouseleave" );
82+
setTimeout(function() {
83+
response( "sometext" );
84+
setTimeout(function() {
85+
ok( !$( "#" + element.data( "ui-tooltip-id" ) ).is( ":visible" ),
86+
"Tooltip should not display" );
87+
start();
88+
});
89+
});
90+
});
91+
}
92+
});
93+
element.trigger( "mouseover" );
94+
});
95+
7496
test( "content: change while open", function() {
7597
expect( 2 ) ;
7698
var element = $( "#tooltipped1" ).tooltip({

ui/tooltip.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ return $.widget( "ui.tooltip", {
194194
});
195195
}
196196

197+
this._registerCloseHandlers( event, target );
197198
this._updateContent( target, event );
198199
},
199200

@@ -208,13 +209,16 @@ return $.widget( "ui.tooltip", {
208209
}
209210

210211
content = contentOption.call( target[0], function( response ) {
211-
// ignore async response if tooltip was closed already
212-
if ( !target.data( "ui-tooltip-open" ) ) {
213-
return;
214-
}
212+
215213
// IE may instantly serve a cached response for ajax requests
216214
// delay this call to _open so the other call to _open runs first
217215
that._delay(function() {
216+
217+
// Ignore async response if tooltip was closed already
218+
if ( !target.data( "ui-tooltip-open" ) ) {
219+
return;
220+
}
221+
218222
// jQuery creates a special event for focusin when it doesn't
219223
// exist natively. To improve performance, the native event
220224
// object is reused and the type is changed. Therefore, we can't
@@ -232,7 +236,7 @@ return $.widget( "ui.tooltip", {
232236
},
233237

234238
_open: function( event, target, content ) {
235-
var tooltipData, tooltip, events, delayedShow, a11yContent,
239+
var tooltipData, tooltip, delayedShow, a11yContent,
236240
positionOption = $.extend( {}, this.options.position );
237241

238242
if ( !content ) {
@@ -314,8 +318,10 @@ return $.widget( "ui.tooltip", {
314318
}
315319

316320
this._trigger( "open", event, { tooltip: tooltip } );
321+
},
317322

318-
events = {
323+
_registerCloseHandlers: function( event, target ) {
324+
var events = {
319325
keyup: function( event ) {
320326
if ( event.keyCode === $.ui.keyCode.ESCAPE ) {
321327
var fakeEvent = $.Event(event);
@@ -329,7 +335,7 @@ return $.widget( "ui.tooltip", {
329335
// tooltips will handle this in destroy.
330336
if ( target[ 0 ] !== this.element[ 0 ] ) {
331337
events.remove = function() {
332-
this._removeTooltip( tooltip );
338+
this._removeTooltip( this._find( target ).tooltip );
333339
};
334340
}
335341

@@ -350,6 +356,12 @@ return $.widget( "ui.tooltip", {
350356

351357
// The tooltip may already be closed
352358
if ( !tooltipData ) {
359+
360+
// We set ui-tooltip-open immediately upon open (in open()), but only set the
361+
// additional data once there's actually content to show (in _open()). So even if the
362+
// tooltip doesn't have full data, we always remove ui-tooltip-open in case we're in
363+
// the period between open() and _open().
364+
target.removeData( "ui-tooltip-open" );
353365
return;
354366
}
355367

0 commit comments

Comments
 (0)