Skip to content

Commit 8c085cd

Browse files
committed
Tooltip: Fixed handling of disabled tooltips.
1 parent 7658607 commit 8c085cd

File tree

2 files changed

+69
-18
lines changed

2 files changed

+69
-18
lines changed

tests/unit/tooltip/tooltip_methods.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ test( "open/close", function() {
2828
$.fx.off = false;
2929
});
3030

31+
test( "enable/disable", function() {
32+
expect( 7 );
33+
$.fx.off = true;
34+
var element = $( "#tooltipped1" ).tooltip();
35+
equal( $( ".ui-tooltip" ).length, 0, "no tooltip on init" );
36+
37+
element.tooltip( "open" );
38+
var tooltip = $( "#" + element.attr( "aria-describedby" ) );
39+
ok( tooltip.is( ":visible" ) );
40+
41+
element.tooltip( "disable" );
42+
equal( $( ".ui-tooltip" ).length, 0, "no tooltip when disabled" );
43+
equal( tooltip.attr( "title" ), "", "title removed on disable" );
44+
45+
element.tooltip( "open" );
46+
equal( $( ".ui-tooltip" ).length, 0, "open does nothing when disabled" );
47+
48+
element.tooltip( "enable" );
49+
equal( element.attr( "title" ), "anchortitle", "title restored on enable" );
50+
51+
element.tooltip( "open" );
52+
tooltip = $( "#" + element.attr( "aria-describedby" ) );
53+
ok( tooltip.is( ":visible" ) );
54+
$.fx.off = false;
55+
});
56+
3157
/*
3258
TODO currently tooltip doesn't override widget
3359
can't return anything useful if no element is kept around and there's no useful reference

ui/jquery.ui.tooltip.js

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,46 @@ $.widget( "ui.tooltip", {
4242
},
4343

4444
_setOption: function( key, value ) {
45-
// only set option, disable element style changes
4645
if ( key === "disabled" ) {
46+
this[ value ? "_disable" : "_enable" ]();
4747
this.options[ key ] = value;
48+
// disable element style changes
4849
return;
4950
}
5051
this._super( "_setOption", key, value );
5152
},
5253

54+
_disable: function() {
55+
var that = this;
56+
57+
// close open tooltips
58+
$.each( this.tooltips, function( id, element ) {
59+
var event = $.Event( "blur" );
60+
event.target = event.currentTarget = element[0];
61+
that.close( event, true );
62+
});
63+
64+
// remove title attributes to prevent native tooltips
65+
this.element.find( this.options.items ).andSelf().each(function() {
66+
var element = $( this );
67+
if ( element.is( "[title]" ) ) {
68+
element
69+
.data( "tooltip-title", element.attr( "title" ) )
70+
.attr( "title", "" );
71+
}
72+
});
73+
},
74+
75+
_enable: function() {
76+
// restore title attributes
77+
this.element.find( this.options.items ).andSelf().each(function() {
78+
var element = $( this );
79+
if ( element.data( "tooltip-title" ) ) {
80+
element.attr( "title", element.data( "tooltip-title" ) );
81+
}
82+
});
83+
},
84+
5385
open: function( event ) {
5486
var content,
5587
that = this,
@@ -83,24 +115,17 @@ $.widget( "ui.tooltip", {
83115
}
84116

85117
// if we have a title, clear it to prevent the native tooltip
86-
// we do this before the disabled check to prevent native tooltips
87-
// even when disabled
88-
// TODO: the above doesn't work since ._bind() does a disabled check
89118
// we have to check first to avoid defining a title if none exists
90119
// (we don't want to cause an element to start matching [title])
91120
// TODO: document why we don't use .removeAttr()
92121
if ( target.is( "[title]" ) ) {
93122
target.attr( "title", "" );
94123
}
95124

96-
if ( this.options.disabled ) {
97-
return;
98-
}
99-
100125
// ajaxy tooltip can update an existing one
101126
var tooltip = this._find( target );
102127
if ( !tooltip.length ) {
103-
tooltip = this._tooltip();
128+
tooltip = this._tooltip( target );
104129
target.attr( "aria-describedby", tooltip.attr( "id" ) );
105130
}
106131
tooltip.find( ".ui-tooltip-content" ).html( content );
@@ -124,22 +149,22 @@ $.widget( "ui.tooltip", {
124149
});
125150
},
126151

127-
close: function( event ) {
152+
close: function( event, force ) {
128153
var that = this,
129154
target = $( event ? event.currentTarget : this.element ),
130155
tooltip = this._find( target );
131156

132-
// only set title if we had one before (see comment in _open())
133-
if ( target.data( "tooltip-title" ) ) {
134-
target.attr( "title", target.data( "tooltip-title" ) );
135-
}
136-
137157
// don't close if the element has focus
138158
// this prevents the tooltip from closing if you hover while focused
139-
if ( this.options.disabled || document.activeElement === target[0] ) {
159+
if ( !force && document.activeElement === target[0] ) {
140160
return;
141161
}
142162

163+
// only set title if we had one before (see comment in _open())
164+
if ( target.data( "tooltip-title" ) ) {
165+
target.attr( "title", target.data( "tooltip-title" ) );
166+
}
167+
143168
target.removeAttr( "aria-describedby" );
144169

145170
tooltip.stop( true );
@@ -153,7 +178,7 @@ $.widget( "ui.tooltip", {
153178
this._trigger( "close", event );
154179
},
155180

156-
_tooltip: function() {
181+
_tooltip: function( element ) {
157182
var id = "ui-tooltip-" + increments++,
158183
tooltip = $( "<div>" )
159184
.attr({
@@ -169,7 +194,7 @@ $.widget( "ui.tooltip", {
169194
if ( $.fn.bgiframe ) {
170195
tooltip.bgiframe();
171196
}
172-
this.tooltips[ id ] = true;
197+
this.tooltips[ id ] = element;
173198
return tooltip;
174199
},
175200

0 commit comments

Comments
 (0)