jquery-ui/ui/jquery.ui.tooltip.js

154 lines
3.5 KiB
JavaScript
Raw Normal View History

/*
* jQuery UI Tooltip @VERSION
*
2011-01-17 14:13:18 +00:00
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Tooltip
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
* jquery.ui.position.js
*/
2011-05-28 00:05:20 +00:00
(function( $ ) {
var increments = 0;
$.widget("ui.tooltip", {
options: {
tooltipClass: null,
items: "[title]",
content: function() {
return $( this ).attr( "title" );
},
position: {
my: "left+15 center",
at: "right center"
}
},
2011-05-28 00:05:20 +00:00
2010-10-26 12:41:14 +00:00
_create: function() {
2011-05-28 00:05:20 +00:00
this._bind({
mouseover: "open",
focusin: "open"
});
},
2011-05-28 00:05:20 +00:00
enable: function() {
this.options.disabled = false;
},
2011-05-28 00:05:20 +00:00
disable: function() {
// only set option, disable element style changes
this.options.disabled = true;
},
2011-05-28 00:05:20 +00:00
open: function( event ) {
var target = $( event ? event.target : this.element ).closest( this.options.items );
if ( !target.length ) {
return;
}
2011-05-28 00:05:20 +00:00
var that = this;
if ( !target.data( "tooltip-title" ) ) {
target.data( "tooltip-title", target.attr( "title" ) );
}
2011-05-28 00:05:20 +00:00
var content = this.options.content.call( target[0], function( response ) {
2011-02-26 14:04:33 +00:00
// IE may instantly serve a cached response, need to give it a chance to finish with _open before that
setTimeout(function() {
// when undefined, it got removeAttr, then ignore (ajax response)
2011-05-28 00:05:20 +00:00
// initially its an empty string, so not undefined
// TODO is there a better approach to enable ajax tooltips to have two updates?
2011-05-28 00:05:20 +00:00
if ( target.attr( "aria-describedby" ) !== undefined ) {
that._open( event, target, response );
}
2011-05-28 00:05:20 +00:00
}, 13 );
});
2011-05-28 00:05:20 +00:00
if ( content ) {
that._open( event, target, content );
}
},
2011-05-28 00:05:20 +00:00
_open: function( event, target, content ) {
2011-05-28 00:05:20 +00:00
if ( !content ) {
return;
2011-05-28 00:05:20 +00:00
}
2011-05-28 00:05:20 +00:00
target.attr( "title", "" );
2011-05-28 00:05:20 +00:00
// TODO: why is this check after we clear the title?
if ( this.options.disabled ) {
return;
2011-05-28 00:05:20 +00:00
}
// ajaxy tooltip can update an existing one
var tooltip = this._find( target );
2011-05-28 00:05:20 +00:00
if ( !tooltip.length ) {
tooltip = this._tooltip();
target.attr( "aria-describedby", tooltip.attr( "id" ) );
}
2011-05-28 00:05:20 +00:00
tooltip.find( ".ui-tooltip-content" ).html( content );
tooltip.position( $.extend({
of: target
}, this.options.position ) ).hide();
tooltip.stop( true );
this._show( tooltip, this.options.show );
this._trigger( "open", event );
this._bind( target, {
mouseleave: "close",
blur: "close",
click: "close"
});
},
2011-05-28 00:05:20 +00:00
close: function( event ) {
2011-05-28 00:05:20 +00:00
var target = $( event ? event.currentTarget : this.element );
target.attr( "title", target.data( "tooltip-title" ) );
2011-05-28 00:05:20 +00:00
if ( this.options.disabled ) {
return;
2011-05-28 00:05:20 +00:00
}
var tooltip = this._find( target );
target.removeAttr( "aria-describedby" );
2011-05-28 00:05:20 +00:00
tooltip.stop( true );
this._hide( tooltip, this.options.hide, function() {
$( this ).remove();
});
2011-05-28 00:05:20 +00:00
target.unbind( "mouseleave.tooltip blur.tooltip" );
2011-05-28 00:05:20 +00:00
this._trigger( "close", event );
},
_tooltip: function() {
2011-05-28 00:05:20 +00:00
var tooltip = $( "<div>" )
.attr({
id: "ui-tooltip-" + increments++,
role: "tooltip"
})
.addClass( "ui-tooltip ui-widget ui-corner-all ui-widget-content" +
( this.options.tooltipClass || "" ) );
$( "<div>" )
.addClass( "ui-tooltip-content" )
.appendTo( tooltip );
tooltip.appendTo( document.body );
return tooltip;
},
_find: function( target ) {
var id = target.attr( "aria-describedby" );
return id ? $( document.getElementById( id ) ) : $();
}
});
$.ui.tooltip.version = "@VERSION";
2011-05-28 00:05:20 +00:00
}( jQuery ) );