Skip to content

Commit c4e367b

Browse files
mziechscottgonzalez
authored andcommitted
Tooltip: Register event handlers before content is loaded
Fixes #8740 Closes gh-1053 Closes gh-1456
1 parent c1dfb98 commit c4e367b

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
@@ -199,6 +199,7 @@ return $.widget( "ui.tooltip", {
199199
});
200200
}
201201

202+
this._registerCloseHandlers( event, target );
202203
this._updateContent( target, event );
203204
},
204205

@@ -214,13 +215,16 @@ return $.widget( "ui.tooltip", {
214215
}
215216

216217
content = contentOption.call( target[0], function( response ) {
217-
// ignore async response if tooltip was closed already
218-
if ( !target.data( "ui-tooltip-open" ) ) {
219-
return;
220-
}
218+
221219
// IE may instantly serve a cached response for ajax requests
222220
// delay this call to _open so the other call to _open runs first
223221
that._delay(function() {
222+
223+
// Ignore async response if tooltip was closed already
224+
if ( !target.data( "ui-tooltip-open" ) ) {
225+
return;
226+
}
227+
224228
// jQuery creates a special event for focusin when it doesn't
225229
// exist natively. To improve performance, the native event
226230
// object is reused and the type is changed. Therefore, we can't
@@ -238,7 +242,7 @@ return $.widget( "ui.tooltip", {
238242
},
239243

240244
_open: function( event, target, content ) {
241-
var tooltipData, tooltip, events, delayedShow, a11yContent,
245+
var tooltipData, tooltip, delayedShow, a11yContent,
242246
positionOption = $.extend( {}, this.options.position );
243247

244248
if ( !content ) {
@@ -316,8 +320,10 @@ return $.widget( "ui.tooltip", {
316320
}
317321

318322
this._trigger( "open", event, { tooltip: tooltip } );
323+
},
319324

320-
events = {
325+
_registerCloseHandlers: function( event, target ) {
326+
var events = {
321327
keyup: function( event ) {
322328
if ( event.keyCode === $.ui.keyCode.ESCAPE ) {
323329
var fakeEvent = $.Event(event);
@@ -331,7 +337,7 @@ return $.widget( "ui.tooltip", {
331337
// tooltips will handle this in destroy.
332338
if ( target[ 0 ] !== this.element[ 0 ] ) {
333339
events.remove = function() {
334-
this._removeTooltip( tooltip );
340+
this._removeTooltip( this._find( target ).tooltip );
335341
};
336342
}
337343

@@ -352,6 +358,12 @@ return $.widget( "ui.tooltip", {
352358

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

0 commit comments

Comments
 (0)