2 * jQuery UI Tooltip 1.10.3
5 * Copyright 2013 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
9 * http://api.jqueryui.com/tooltip/
14 * jquery.ui.position.js
20 function addDescribedBy( elem, id ) {
21 var describedby = (elem.attr( "aria-describedby" ) || "").split( /\s+/ );
22 describedby.push( id );
24 .data( "ui-tooltip-id", id )
25 .attr( "aria-describedby", $.trim( describedby.join( " " ) ) );
28 function removeDescribedBy( elem ) {
29 var id = elem.data( "ui-tooltip-id" ),
30 describedby = (elem.attr( "aria-describedby" ) || "").split( /\s+/ ),
31 index = $.inArray( id, describedby );
33 describedby.splice( index, 1 );
36 elem.removeData( "ui-tooltip-id" );
37 describedby = $.trim( describedby.join( " " ) );
39 elem.attr( "aria-describedby", describedby );
41 elem.removeAttr( "aria-describedby" );
45 $.widget( "ui.tooltip", {
49 // support: IE<9, Opera in jQuery <1.7
50 // .text() can't accept undefined, so coerce to a string
51 var title = $( this ).attr( "title" ) || "";
52 // Escape title, since we're going from an attribute to raw HTML
53 return $( "<a>" ).text( title ).html();
56 // Disabled elements have inconsistent behavior across browsers (#8661)
57 items: "[title]:not([disabled])",
61 collision: "flipfit flip"
78 // IDs of generated tooltips, needed for destroy
80 // IDs of parent tooltips where we removed the title attribute
83 if ( this.options.disabled ) {
88 _setOption: function( key, value ) {
91 if ( key === "disabled" ) {
92 this[ value ? "_disable" : "_enable" ]();
93 this.options[ key ] = value;
94 // disable element style changes
98 this._super( key, value );
100 if ( key === "content" ) {
101 $.each( this.tooltips, function( id, element ) {
102 that._updateContent( element );
107 _disable: function() {
110 // close open tooltips
111 $.each( this.tooltips, function( id, element ) {
112 var event = $.Event( "blur" );
113 event.target = event.currentTarget = element[0];
114 that.close( event, true );
117 // remove title attributes to prevent native tooltips
118 this.element.find( this.options.items ).addBack().each(function() {
119 var element = $( this );
120 if ( element.is( "[title]" ) ) {
122 .data( "ui-tooltip-title", element.attr( "title" ) )
123 .attr( "title", "" );
128 _enable: function() {
129 // restore title attributes
130 this.element.find( this.options.items ).addBack().each(function() {
131 var element = $( this );
132 if ( element.data( "ui-tooltip-title" ) ) {
133 element.attr( "title", element.data( "ui-tooltip-title" ) );
138 open: function( event ) {
140 target = $( event ? event.target : this.element )
141 // we need closest here due to mouseover bubbling,
142 // but always pointing at the same event target
143 .closest( this.options.items );
145 // No element to show a tooltip for or the tooltip is already open
146 if ( !target.length || target.data( "ui-tooltip-id" ) ) {
150 if ( target.attr( "title" ) ) {
151 target.data( "ui-tooltip-title", target.attr( "title" ) );
154 target.data( "ui-tooltip-open", true );
156 // kill parent tooltips, custom or native, for hover
157 if ( event && event.type === "mouseover" ) {
158 target.parents().each(function() {
159 var parent = $( this ),
161 if ( parent.data( "ui-tooltip-open" ) ) {
162 blurEvent = $.Event( "blur" );
163 blurEvent.target = blurEvent.currentTarget = this;
164 that.close( blurEvent, true );
166 if ( parent.attr( "title" ) ) {
168 that.parents[ this.id ] = {
170 title: parent.attr( "title" )
172 parent.attr( "title", "" );
177 this._updateContent( target, event );
180 _updateContent: function( target, event ) {
182 contentOption = this.options.content,
184 eventType = event ? event.type : null;
186 if ( typeof contentOption === "string" ) {
187 return this._open( event, target, contentOption );
190 content = contentOption.call( target[0], function( response ) {
191 // ignore async response if tooltip was closed already
192 if ( !target.data( "ui-tooltip-open" ) ) {
195 // IE may instantly serve a cached response for ajax requests
196 // delay this call to _open so the other call to _open runs first
197 that._delay(function() {
198 // jQuery creates a special event for focusin when it doesn't
199 // exist natively. To improve performance, the native event
200 // object is reused and the type is changed. Therefore, we can't
201 // rely on the type being correct after the event finished
202 // bubbling, so we set it back to the previous value. (#8740)
204 event.type = eventType;
206 this._open( event, target, response );
210 this._open( event, target, content );
214 _open: function( event, target, content ) {
215 var tooltip, events, delayedShow,
216 positionOption = $.extend( {}, this.options.position );
222 // Content can be updated multiple times. If the tooltip already
223 // exists, then just update the content and bail.
224 tooltip = this._find( target );
225 if ( tooltip.length ) {
226 tooltip.find( ".ui-tooltip-content" ).html( content );
230 // if we have a title, clear it to prevent the native tooltip
231 // we have to check first to avoid defining a title if none exists
232 // (we don't want to cause an element to start matching [title])
234 // We use removeAttr only for key events, to allow IE to export the correct
235 // accessible attributes. For mouse events, set to empty string to avoid
236 // native tooltip showing up (happens only when removing inside mouseover).
237 if ( target.is( "[title]" ) ) {
238 if ( event && event.type === "mouseover" ) {
239 target.attr( "title", "" );
241 target.removeAttr( "title" );
245 tooltip = this._tooltip( target );
246 addDescribedBy( target, tooltip.attr( "id" ) );
247 tooltip.find( ".ui-tooltip-content" ).html( content );
249 function position( event ) {
250 positionOption.of = event;
251 if ( tooltip.is( ":hidden" ) ) {
254 tooltip.position( positionOption );
256 if ( this.options.track && event && /^mouse/.test( event.type ) ) {
257 this._on( this.document, {
260 // trigger once to override element-relative positioning
263 tooltip.position( $.extend({
265 }, this.options.position ) );
270 this._show( tooltip, this.options.show );
271 // Handle tracking tooltips that are shown with a delay (#8644). As soon
272 // as the tooltip is visible, position the tooltip using the most recent
274 if ( this.options.show && this.options.show.delay ) {
275 delayedShow = this.delayedShow = setInterval(function() {
276 if ( tooltip.is( ":visible" ) ) {
277 position( positionOption.of );
278 clearInterval( delayedShow );
283 this._trigger( "open", event, { tooltip: tooltip } );
286 keyup: function( event ) {
287 if ( event.keyCode === $.ui.keyCode.ESCAPE ) {
288 var fakeEvent = $.Event(event);
289 fakeEvent.currentTarget = target[0];
290 this.close( fakeEvent, true );
294 this._removeTooltip( tooltip );
297 if ( !event || event.type === "mouseover" ) {
298 events.mouseleave = "close";
300 if ( !event || event.type === "focusin" ) {
301 events.focusout = "close";
303 this._on( true, target, events );
306 close: function( event ) {
308 target = $( event ? event.currentTarget : this.element ),
309 tooltip = this._find( target );
311 // disabling closes the tooltip, so we need to track when we're closing
312 // to avoid an infinite loop in case the tooltip becomes disabled on close
313 if ( this.closing ) {
317 // Clear the interval for delayed tracking tooltips
318 clearInterval( this.delayedShow );
320 // only set title if we had one before (see comment in _open())
321 if ( target.data( "ui-tooltip-title" ) ) {
322 target.attr( "title", target.data( "ui-tooltip-title" ) );
325 removeDescribedBy( target );
327 tooltip.stop( true );
328 this._hide( tooltip, this.options.hide, function() {
329 that._removeTooltip( $( this ) );
332 target.removeData( "ui-tooltip-open" );
333 this._off( target, "mouseleave focusout keyup" );
334 // Remove 'remove' binding only on delegated targets
335 if ( target[0] !== this.element[0] ) {
336 this._off( target, "remove" );
338 this._off( this.document, "mousemove" );
340 if ( event && event.type === "mouseleave" ) {
341 $.each( this.parents, function( id, parent ) {
342 $( parent.element ).attr( "title", parent.title );
343 delete that.parents[ id ];
348 this._trigger( "close", event, { tooltip: tooltip } );
349 this.closing = false;
352 _tooltip: function( element ) {
353 var id = "ui-tooltip-" + increments++,
354 tooltip = $( "<div>" )
359 .addClass( "ui-tooltip ui-widget ui-corner-all ui-widget-content " +
360 ( this.options.tooltipClass || "" ) );
362 .addClass( "ui-tooltip-content" )
363 .appendTo( tooltip );
364 tooltip.appendTo( this.document[0].body );
365 this.tooltips[ id ] = element;
369 _find: function( target ) {
370 var id = target.data( "ui-tooltip-id" );
371 return id ? $( "#" + id ) : $();
374 _removeTooltip: function( tooltip ) {
376 delete this.tooltips[ tooltip.attr( "id" ) ];
379 _destroy: function() {
382 // close open tooltips
383 $.each( this.tooltips, function( id, element ) {
384 // Delegate to close method to handle common cleanup
385 var event = $.Event( "blur" );
386 event.target = event.currentTarget = element[0];
387 that.close( event, true );
389 // Remove immediately; destroying an open tooltip doesn't use the
391 $( "#" + id ).remove();
394 if ( element.data( "ui-tooltip-title" ) ) {
395 element.attr( "title", element.data( "ui-tooltip-title" ) );
396 element.removeData( "ui-tooltip-title" );