Skip to content

Commit f64c953

Browse files
committed
Tooltip: Use .uniqueId() and move aria-describedby helper methods into the widget prototype.
1 parent ecd4f95 commit f64c953

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

ui/jquery.ui.tooltip.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,6 @@
1515
*/
1616
(function( $ ) {
1717

18-
var increments = 0;
19-
20-
function addDescribedBy( elem, id ) {
21-
var describedby = (elem.attr( "aria-describedby" ) || "").split( /\s+/ );
22-
describedby.push( id );
23-
elem
24-
.data( "ui-tooltip-id", id )
25-
.attr( "aria-describedby", $.trim( describedby.join( " " ) ) );
26-
}
27-
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 );
32-
if ( index !== -1 ) {
33-
describedby.splice( index, 1 );
34-
}
35-
36-
elem.removeData( "ui-tooltip-id" );
37-
describedby = $.trim( describedby.join( " " ) );
38-
if ( describedby ) {
39-
elem.attr( "aria-describedby", describedby );
40-
} else {
41-
elem.removeAttr( "aria-describedby" );
42-
}
43-
}
44-
4518
$.widget( "ui.tooltip", {
4619
version: "@VERSION",
4720
options: {
@@ -69,6 +42,32 @@ $.widget( "ui.tooltip", {
6942
open: null
7043
},
7144

45+
_addDescribedBy: function( elem, id ) {
46+
var describedby = (elem.attr( "aria-describedby" ) || "").split( /\s+/ );
47+
describedby.push( id );
48+
elem
49+
.data( "ui-tooltip-id", id )
50+
.attr( "aria-describedby", $.trim( describedby.join( " " ) ) );
51+
},
52+
53+
_removeDescribedBy: function( elem ) {
54+
var id = elem.data( "ui-tooltip-id" ),
55+
describedby = (elem.attr( "aria-describedby" ) || "").split( /\s+/ ),
56+
index = $.inArray( id, describedby );
57+
58+
if ( index !== -1 ) {
59+
describedby.splice( index, 1 );
60+
}
61+
62+
elem.removeData( "ui-tooltip-id" );
63+
describedby = $.trim( describedby.join( " " ) );
64+
if ( describedby ) {
65+
elem.attr( "aria-describedby", describedby );
66+
} else {
67+
elem.removeAttr( "aria-describedby" );
68+
}
69+
},
70+
7271
_create: function() {
7372
this._on({
7473
mouseover: "open",
@@ -243,7 +242,7 @@ $.widget( "ui.tooltip", {
243242
}
244243

245244
tooltip = this._tooltip( target );
246-
addDescribedBy( target, tooltip.attr( "id" ) );
245+
this._addDescribedBy( target, tooltip.attr( "id" ) );
247246
tooltip.find( ".ui-tooltip-content" ).html( content );
248247

249248
function position( event ) {
@@ -322,7 +321,7 @@ $.widget( "ui.tooltip", {
322321
target.attr( "title", target.data( "ui-tooltip-title" ) );
323322
}
324323

325-
removeDescribedBy( target );
324+
this._removeDescribedBy( target );
326325

327326
tooltip.stop( true );
328327
this._hide( tooltip, this.options.hide, function() {
@@ -350,17 +349,16 @@ $.widget( "ui.tooltip", {
350349
},
351350

352351
_tooltip: function( element ) {
353-
var id = "ui-tooltip-" + increments++,
354-
tooltip = $( "<div>" )
355-
.attr({
356-
id: id,
357-
role: "tooltip"
358-
})
352+
var tooltip = $( "<div>" )
353+
.attr( "role", "tooltip" )
359354
.addClass( "ui-tooltip ui-widget ui-corner-all ui-widget-content " +
360-
( this.options.tooltipClass || "" ) );
355+
( this.options.tooltipClass || "" ) ),
356+
id = tooltip.uniqueId().attr( "id" );
357+
361358
$( "<div>" )
362359
.addClass( "ui-tooltip-content" )
363360
.appendTo( tooltip );
361+
364362
tooltip.appendTo( this.document[0].body );
365363
this.tooltips[ id ] = element;
366364
return tooltip;

0 commit comments

Comments
 (0)