@@ -18,9 +18,10 @@ var increments = 0;
18
18
19
19
$ . widget ( "ui.tooltip" , {
20
20
options : {
21
+ tooltipClass : null ,
21
22
items : "[title]" ,
22
23
content : function ( ) {
23
- return $ ( this ) . attr ( "title" ) ;
24
+ return $ ( this ) . attr ( "title" ) ;
24
25
} ,
25
26
position : {
26
27
my : "left center" ,
@@ -29,109 +30,114 @@ $.widget("ui.tooltip", {
29
30
}
30
31
} ,
31
32
_create : function ( ) {
32
- var self = this ;
33
- this . tooltip = $ ( "<div></div>" )
34
- . attr ( "id" , "ui-tooltip-" + increments ++ )
35
- . attr ( "role" , "tooltip" )
36
- . attr ( "aria-hidden" , "true" )
37
- . addClass ( "ui-tooltip ui-widget ui-corner-all ui-widget-content" )
38
- . appendTo ( document . body )
39
- . hide ( ) ;
40
- this . tooltipContent = $ ( "<div></div>" )
41
- . addClass ( "ui-tooltip-content" )
42
- . appendTo ( this . tooltip ) ;
43
- this . opacity = this . tooltip . css ( "opacity" ) ;
44
- this . element
45
- . bind ( "focus.tooltip mouseover.tooltip" , function ( event ) {
46
- self . open ( event ) ;
47
- } )
48
- . bind ( "blur.tooltip mouseout.tooltip" , function ( event ) {
49
- self . close ( event ) ;
50
- } ) ;
33
+ this . _bind ( {
34
+ mouseover : "open" ,
35
+ focusin : "open"
36
+ } ) ;
51
37
} ,
52
38
53
39
enable : function ( ) {
54
40
this . options . disabled = false ;
55
41
} ,
56
42
57
43
disable : function ( ) {
44
+ // only set option, disable element style changes
58
45
this . options . disabled = true ;
59
46
} ,
60
47
61
- _destroy : function ( ) {
62
- this . tooltip . remove ( ) ;
63
- } ,
64
-
65
- widget : function ( ) {
66
- return this . element . pushStack ( this . tooltip . get ( ) ) ;
67
- } ,
68
-
69
48
open : function ( event ) {
70
49
var target = $ ( event && event . target || this . element ) . closest ( this . options . items ) ;
71
50
if ( ! target . length ) {
72
51
return ;
73
52
}
74
- // already visible? possible when both focus and mouseover events occur
75
- if ( this . current && this . current [ 0 ] == target [ 0 ] )
76
- return ;
77
53
var self = this ;
78
- this . current = target ;
79
- this . currentTitle = target . attr ( "title" ) ;
54
+ if ( ! target . data ( "tooltip-title" ) ) {
55
+ target . data ( "tooltip-title" , target . attr ( "title" ) ) ;
56
+ }
80
57
var content = this . options . content . call ( target [ 0 ] , function ( response ) {
81
58
// IE may instantly serve a cached response, need to give it a chance to finish with _open before that
82
59
setTimeout ( function ( ) {
83
- // ignore async responses that come in after the tooltip is already hidden
84
- if ( self . current == target )
60
+ // when undefined, it got removeAttr, then ignore (ajax response)
61
+ // intially its an empty string, so not undefined
62
+ // TODO is there a better approach to enable ajax tooltips to have two updates?
63
+ if ( target . attr ( "aria-describedby" ) !== undefined ) {
85
64
self . _open ( event , target , response ) ;
65
+ }
86
66
} , 13 ) ;
87
67
} ) ;
88
68
if ( content ) {
89
69
self . _open ( event , target , content ) ;
90
70
}
91
71
} ,
92
72
93
- _open : function ( event , target , content ) {
94
- if ( ! content )
73
+ _open : function ( event , target , content ) {
74
+ if ( ! content )
95
75
return ;
96
-
76
+
97
77
target . attr ( "title" , "" ) ;
98
-
99
- if ( this . options . disabled )
78
+
79
+ if ( this . options . disabled )
100
80
return ;
101
-
102
- this . tooltipContent . html ( content ) ;
103
- this . tooltip . css ( {
104
- top : 0 ,
105
- left : 0
106
- } ) . show ( ) . position ( $ . extend ( {
81
+
82
+ // ajaxy tooltip can update an existing one
83
+ var tooltip = this . _find ( target ) ;
84
+ if ( ! tooltip . length ) {
85
+ tooltip = this . _tooltip ( ) ;
86
+ target . attr ( "aria-describedby" , tooltip . attr ( "id" ) ) ;
87
+ }
88
+ tooltip . find ( ".ui-tooltip-content" ) . html ( content ) ;
89
+ tooltip . position ( $ . extend ( {
107
90
of : target
108
- } , this . options . position ) ) . hide ( ) ;
109
-
110
- this . tooltip . attr ( "aria-hidden" , "false" ) ;
111
- target . attr ( "aria-describedby" , this . tooltip . attr ( "id" ) ) ;
91
+ } , this . options . position ) ) . hide ( ) ;
92
+
112
93
113
- this . tooltip . stop ( false , true ) . fadeIn ( ) ;
94
+ tooltip . fadeIn ( ) ;
114
95
115
96
this . _trigger ( "open" , event ) ;
97
+
98
+ this . _bind ( target , {
99
+ mouseleave : "close" ,
100
+ blur : "close"
101
+ } ) ;
116
102
} ,
117
103
118
- close : function ( event ) {
119
- if ( ! this . current )
120
- return ;
121
-
122
- var current = this . current ;
123
- this . current = null ;
124
- current . attr ( "title" , this . currentTitle ) ;
104
+ close : function ( event ) {
105
+ var target = $ ( event && event . currentTarget || this . element ) ;
106
+ target . attr ( "title" , target . data ( "tooltip-title" ) ) ;
125
107
126
- if ( this . options . disabled )
108
+ if ( this . options . disabled )
127
109
return ;
110
+
111
+ var tooltip = this . _find ( target ) ;
112
+ target . removeAttr ( "aria-describedby" ) ;
128
113
129
- current . removeAttr ( "aria-describedby" ) ;
130
- this . tooltip . attr ( "aria-hidden" , "true" ) ;
114
+ tooltip . fadeOut ( function ( ) {
115
+ $ ( this ) . remove ( ) ;
116
+ } ) ;
131
117
132
- this . tooltip . stop ( false , true ) . fadeOut ( ) ;
118
+ target . unbind ( "mouseleave.tooltip blur.tooltip" ) ;
133
119
134
120
this . _trigger ( "close" , event ) ;
121
+ } ,
122
+
123
+ _tooltip : function ( ) {
124
+ var tooltip = $ ( "<div></div>" )
125
+ . attr ( "id" , "ui-tooltip-" + increments ++ )
126
+ . attr ( "role" , "tooltip" )
127
+ . addClass ( "ui-tooltip ui-widget ui-corner-all ui-widget-content" ) ;
128
+ if ( this . options . tooltipClass ) {
129
+ tooltip . addClass ( this . options . tooltipClass ) ;
130
+ }
131
+ $ ( "<div></div>" )
132
+ . addClass ( "ui-tooltip-content" )
133
+ . appendTo ( tooltip ) ;
134
+ tooltip . appendTo ( document . body ) ;
135
+ return tooltip ;
136
+ } ,
137
+
138
+ _find : function ( target ) {
139
+ var id = target . attr ( "aria-describedby" ) ;
140
+ return id ? $ ( document . getElementById ( id ) ) : $ ( ) ;
135
141
}
136
142
} ) ;
137
143
0 commit comments