Skip to content

Commit

Permalink
Tooltip: Add track option
Browse files Browse the repository at this point in the history
  • Loading branch information
jzaefferer committed Jun 13, 2012
1 parent 43c6663 commit 556497f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
22 changes: 1 addition & 21 deletions demos/tooltip/tracking.html
Expand Up @@ -19,27 +19,7 @@
<script>
$(function() {
$( ".demo" ).tooltip({
position: {
my: "left+25 center",
at: "center"
},
open: function( event, ui ) {
if ( !( /^mouse/.test( event.originalEvent.type ) ) ) {
return;
}

var positionOption = $.extend( {}, $( this ).tooltip( "option", "position" ) );
function position( event ) {
positionOption.of = event;
ui.tooltip.position( positionOption );
}
$( document ).bind( "mousemove.tooltip-position", position );
// trigger once to override element-relative positioning
position( event );
},
close: function() {
$( document ).unbind( "mousemove.tooltip-position" );
}
track: true
});
});
</script>
Expand Down
1 change: 1 addition & 0 deletions tests/unit/tooltip/tooltip_common.js
Expand Up @@ -11,6 +11,7 @@ TestHelpers.commonWidgetTests( "tooltip", {
},
show: true,
tooltipClass: null,
track: false,

// callbacks
close: null,
Expand Down
29 changes: 24 additions & 5 deletions ui/jquery.ui.tooltip.js
Expand Up @@ -54,6 +54,7 @@ $.widget( "ui.tooltip", {
},
show: true,
tooltipClass: null,
track: false,

// callbacks
close: null,
Expand Down Expand Up @@ -145,13 +146,14 @@ $.widget( "ui.tooltip", {
},

_open: function( event, target, content ) {
var tooltip, positionOption;
if ( !content ) {
return;
}

// Content can be updated multiple times. If the tooltip already
// exists, then just update the content and bail.
var tooltip = this._find( target );
tooltip = this._find( target );
if ( tooltip.length ) {
tooltip.find( ".ui-tooltip-content" ).html( content );
return;
Expand All @@ -175,11 +177,25 @@ $.widget( "ui.tooltip", {
tooltip = this._tooltip( target );
addDescribedBy( target, tooltip.attr( "id" ) );
tooltip.find( ".ui-tooltip-content" ).html( content );
tooltip
.position( $.extend({

function position( event ) {
positionOption.of = event;
tooltip.position( positionOption );
}
if ( this.options.track && /^mouse/.test( event.originalEvent.type ) ) {
positionOption = $.extend( {}, this.options.position );
this._on( this.document, {
mousemove: position
});
// trigger once to override element-relative positioning
position( event );
} else {
tooltip.position( $.extend({
of: target
}, this.options.position ) )
.hide();
}, this.options.position ) );
}

tooltip.hide();

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

Expand Down Expand Up @@ -235,6 +251,9 @@ $.widget( "ui.tooltip", {
target.removeData( "tooltip-open" );
target.unbind( "mouseleave.tooltip focusout.tooltip keyup.tooltip" );

// TODO use _off
this.document.unbind( "mousemove.tooltip" );

this.closing = true;
this._trigger( "close", event, { tooltip: tooltip } );
this.closing = false;
Expand Down

0 comments on commit 556497f

Please sign in to comment.