Skip to content

Commit 5c2cf39

Browse files
jzaeffererscottgonzalez
authored andcommitted
Tooltip: Add track option
1 parent ff39bed commit 5c2cf39

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

demos/tooltip/tracking.html

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,7 @@
1919
<script>
2020
$(function() {
2121
$( ".demo" ).tooltip({
22-
position: {
23-
my: "left+25 center",
24-
at: "center"
25-
},
26-
open: function( event, ui ) {
27-
if ( !( /^mouse/.test( event.originalEvent.type ) ) ) {
28-
return;
29-
}
30-
31-
var positionOption = $.extend( {}, $( this ).tooltip( "option", "position" ) );
32-
function position( event ) {
33-
positionOption.of = event;
34-
ui.tooltip.position( positionOption );
35-
}
36-
$( document ).bind( "mousemove.tooltip-position", position );
37-
// trigger once to override element-relative positioning
38-
position( event );
39-
},
40-
close: function() {
41-
$( document ).unbind( "mousemove.tooltip-position" );
42-
}
22+
track: true
4323
});
4424
});
4525
</script>

tests/unit/tooltip/tooltip_common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ TestHelpers.commonWidgetTests( "tooltip", {
1111
},
1212
show: true,
1313
tooltipClass: null,
14+
track: false,
1415

1516
// callbacks
1617
close: null,

ui/jquery.ui.tooltip.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ $.widget( "ui.tooltip", {
5454
},
5555
show: true,
5656
tooltipClass: null,
57+
track: false,
5758

5859
// callbacks
5960
close: null,
@@ -145,13 +146,14 @@ $.widget( "ui.tooltip", {
145146
},
146147

147148
_open: function( event, target, content ) {
149+
var tooltip, positionOption;
148150
if ( !content ) {
149151
return;
150152
}
151153

152154
// Content can be updated multiple times. If the tooltip already
153155
// exists, then just update the content and bail.
154-
var tooltip = this._find( target );
156+
tooltip = this._find( target );
155157
if ( tooltip.length ) {
156158
tooltip.find( ".ui-tooltip-content" ).html( content );
157159
return;
@@ -175,11 +177,25 @@ $.widget( "ui.tooltip", {
175177
tooltip = this._tooltip( target );
176178
addDescribedBy( target, tooltip.attr( "id" ) );
177179
tooltip.find( ".ui-tooltip-content" ).html( content );
178-
tooltip
179-
.position( $.extend({
180+
181+
function position( event ) {
182+
positionOption.of = event;
183+
tooltip.position( positionOption );
184+
}
185+
if ( this.options.track && /^mouse/.test( event.originalEvent.type ) ) {
186+
positionOption = $.extend( {}, this.options.position );
187+
this._on( this.document, {
188+
mousemove: position
189+
});
190+
// trigger once to override element-relative positioning
191+
position( event );
192+
} else {
193+
tooltip.position( $.extend({
180194
of: target
181-
}, this.options.position ) )
182-
.hide();
195+
}, this.options.position ) );
196+
}
197+
198+
tooltip.hide();
183199

184200
this._show( tooltip, this.options.show );
185201

@@ -235,6 +251,9 @@ $.widget( "ui.tooltip", {
235251
target.removeData( "tooltip-open" );
236252
this._off( target, "mouseleave focusout keyup" );
237253

254+
// TODO use _off
255+
this.document.unbind( "mousemove.tooltip" );
256+
238257
this.closing = true;
239258
this._trigger( "close", event, { tooltip: tooltip } );
240259
this.closing = false;

0 commit comments

Comments
 (0)