@@ -8,20 +8,29 @@ define([
8
8
borderWidth : 2 ,
9
9
arrowSize : 10 ,
10
10
initialize : function ( options ) {
11
- this . options = options || { } ;
11
+ if ( options instanceof jQuery ) {
12
+ this . options = this . parseDataAttributes ( options ) ;
13
+ } else {
14
+ this . options = options || { } ;
15
+ }
16
+ if ( ! this . options . $el ) {
17
+ throw new Error ( 'Tooltip needs a target element' ) ;
18
+ return ;
19
+ }
20
+ this . options . speed = this . options . speed === undefined ? 200 : parseInt ( this . options . speed , 10 ) ;
12
21
var self = this ,
13
- currentTooltip = this . options . target . data ( 'activeTooltip' ) ;
22
+ currentTooltip = this . options . $el . data ( 'activeTooltip' ) ;
14
23
if ( currentTooltip ) {
15
24
if ( currentTooltip . options . interrupt ) {
16
25
return ;
17
26
}
18
27
currentTooltip . destroy ( ) ;
19
28
}
20
- this . options . target . data ( 'activeTooltip' , this ) ;
29
+ this . options . $el . data ( 'activeTooltip' , this ) ;
21
30
this . id = this . options . id || null ;
22
31
23
- this . elemWidth = this . options . target . outerWidth ( ) ;
24
- this . elemHeight = this . options . target . outerHeight ( ) ;
32
+ this . elemWidth = this . options . $el . outerWidth ( ) ;
33
+ this . elemHeight = this . options . $el . outerHeight ( ) ;
25
34
26
35
this . options . rootElem = this . options . rootElem || $ ( 'body' ) ;
27
36
this . options . moveUp = this . options . moveUp || 0 ;
@@ -32,9 +41,23 @@ define([
32
41
} else {
33
42
this . enterHandler = _ . bind ( this . show , this ) ;
34
43
this . exitHandler = _ . bind ( this . hide , this ) ;
35
- this . options . target . bind ( this . options . trigger , this . enterHandler ) ;
44
+ this . options . $el . bind ( this . options . trigger , this . enterHandler ) ;
36
45
}
37
46
} ,
47
+ parseDataAttributes : function ( $el ) {
48
+ var ops = { } ;
49
+ ops . $el = $el ;
50
+ ops . align = $el . attr ( 'data-align' ) ;
51
+ ops . context = $el . attr ( 'data-context' ) ;
52
+ ops . text = $el . attr ( 'data-tooltip' ) ;
53
+ ops . timeout = $el . attr ( 'data-timeout' ) ;
54
+ ops . interrupt = $el . attr ( 'data-interrupt' ) ;
55
+ ops . trigger = $el . attr ( 'data-trigger' ) ;
56
+ ops . exit = $el . attr ( 'data-exit' ) ;
57
+ ops . feedback = $el . attr ( 'data-feedback' ) ;
58
+ ops . speed = $el . attr ( 'data-speed' ) ;
59
+ return ops ;
60
+ } ,
38
61
events : {
39
62
'click button.tooltip-confirm' : 'confirmTooltip' ,
40
63
'click button.tooltip-deny' : 'exit'
@@ -44,19 +67,20 @@ define([
44
67
return false ;
45
68
} ,
46
69
show : function ( ) {
47
- this . $el . stop ( ) . fadeIn ( 200 ) ;
70
+ this . $el . stop ( ) . fadeIn ( this . options . speed ) ;
48
71
if ( this . options . trigger ) {
49
- this . options . target . unbind ( this . options . trigger , this . enterHandler ) ;
50
- this . options . target . bind ( this . options . exit , this . exitHandler ) ;
72
+ this . options . $el . unbind ( this . options . trigger , this . enterHandler ) ;
73
+ this . options . $el . bind ( this . options . exit , this . exitHandler ) ;
51
74
}
52
75
} ,
53
76
addEvents : function ( ) {
77
+ var self = this ;
54
78
this . clickHandler = _ . bind ( this . clicked , this ) ;
55
79
this . keypressHandler = _ . bind ( this . keypressed , this ) ;
56
80
$ ( window ) . bind ( 'mousedown' , this . clickHandler ) ;
57
81
$ ( window ) . bind ( 'keydown' , this . keypressHandler ) ;
58
82
if ( this . options . hoverTrigger ) {
59
- this . options . target . on ( 'mouseleave' , function ( e ) {
83
+ this . options . $el . on ( 'mouseleave' , function ( e ) {
60
84
self . mouseLeaveHandler ( e ) ;
61
85
} ) ;
62
86
this . $el . on ( 'mouseleave' , function ( e ) {
@@ -68,24 +92,31 @@ define([
68
92
return $ ( this ) . data ( 'activeTooltip' ) !== undefined ;
69
93
} ) ;
70
94
elems . each ( function ( i , item ) {
71
- if ( item !== self . options . target . get ( 0 ) ) {
72
- if ( $ ( item ) . data ( 'activeTooltip' ) ) {
73
- $ ( item ) . data ( 'activeTooltip' ) . exit ( ) ;
95
+ if ( item !== self . options . $el . get ( 0 ) ) {
96
+ var ttip ;
97
+ if ( ttip = $ ( item ) . data ( 'activeTooltip' ) ) {
98
+ if ( ttip . options . trigger ) {
99
+ if ( ttip . $el . is ( ':visible' ) ) {
100
+ ttip . hide ( ) ;
101
+ }
102
+ } else {
103
+ ttip . exit ( ) ;
104
+ }
74
105
}
75
106
}
76
107
} ) ;
77
108
}
78
109
} ,
79
110
clicked : function ( e ) {
80
- var isTargetElem = this . options . target . get ( 0 ) === $ ( e . target ) . get ( 0 ) || this . options . target . find ( $ ( e . target ) ) . length > 0 ,
111
+ var isTargetElem = this . options . $el . get ( 0 ) === $ ( e . target ) . get ( 0 ) || this . options . $el . find ( $ ( e . target ) ) . length > 0 ,
81
112
isTooltip = $ ( e . target ) . hasClass ( 'tooltip' ) || $ ( e . target ) . parents ( '.tooltip' ) . length > 0 || false ;
82
113
if ( ! isTargetElem && ! isTooltip ) {
83
114
this . exit ( ) ;
84
115
}
85
116
} ,
86
117
mouseLeaveHandler : function ( e ) {
87
118
var isTooltip = $ ( e . toElement ) . get ( 0 ) === this . $el . get ( 0 ) || this . $el . find ( $ ( e . toElement ) ) . length > 0 || false ,
88
- isTargetElem = this . options . target . get ( 0 ) === $ ( e . toElement ) . get ( 0 ) || this . options . target . find ( $ ( e . toElement ) ) . length > 0 || false ;
119
+ isTargetElem = this . options . $el . get ( 0 ) === $ ( e . toElement ) . get ( 0 ) || this . options . $el . find ( $ ( e . toElement ) ) . length > 0 || false ;
89
120
if ( ! isTargetElem && ! isTooltip ) {
90
121
this . exit ( ) ;
91
122
}
@@ -101,8 +132,8 @@ define([
101
132
<span><%= options.text %></span>\
102
133
<% if(options.feedback) { %>\
103
134
<div class="feedback-buttons">\
104
- <button class="btn btn-secondary tooltip-confirm">Yes</button>\
105
- <button class="btn btn-secondary tooltip-deny">No</button>\
135
+ <button class="btn btn-primary tooltip-confirm">Yes</button>\
136
+ <button class="btn btn-primary tooltip-deny">No</button>\
106
137
</div>\
107
138
<% } %>' ) ( this ) ) ;
108
139
this . options . rootElem . append ( this . el ) ;
@@ -125,7 +156,7 @@ define([
125
156
positionSelf : function ( ) {
126
157
var rootElemOffset = this . options . rootElem . offset ( ) ,
127
158
scrollTop = this . options . rootElem . prop ( 'tagName' ) === "BODY" ? 0 : this . options . rootElem . get ( 0 ) . scrollTop ,
128
- pos = this . options . target . offset ( ) ;
159
+ pos = this . options . $el . offset ( ) ;
129
160
pos . top = pos . top - rootElemOffset . top + scrollTop ;
130
161
pos . left = pos . left - rootElemOffset . left ;
131
162
switch ( this . options . align ) {
@@ -153,28 +184,27 @@ define([
153
184
} ,
154
185
exit : function ( ) {
155
186
var self = this ;
156
- this . $el . fadeOut ( 200 , function ( ) {
187
+ this . $el . fadeOut ( this . options . speed , function ( ) {
157
188
self . destroy ( ) ;
158
189
} ) ;
159
190
return false ;
160
191
} ,
161
192
hide : function ( ) {
162
- console . log ( 'bye' ) ;
163
- this . $el . stop ( ) . fadeOut ( 200 ) ;
193
+ this . $el . stop ( ) . fadeOut ( this . options . speed ) ;
164
194
if ( this . options . trigger ) {
165
- this . options . target . unbind ( this . options . exit , this . exitHandler ) ;
166
- this . options . target . bind ( this . options . trigger , this . enterHandler ) ;
195
+ this . options . $el . unbind ( this . options . exit , this . exitHandler ) ;
196
+ this . options . $el . bind ( this . options . trigger , this . enterHandler ) ;
167
197
}
168
198
} ,
169
199
destroy : function ( ) {
170
200
this . undelegateEvents ( ) ;
171
- this . options . target . data ( 'activeTooltip' , null ) ;
201
+ this . options . $el . data ( 'activeTooltip' , null ) ;
172
202
$ ( window ) . unbind ( 'click' , this . clickHandler ) ;
173
203
$ ( window ) . unbind ( 'keydown' , this . keypressHandler ) ;
174
- this . options . target . off ( 'mouseleave' ) ;
204
+ this . options . $el . off ( 'mouseleave' ) ;
175
205
if ( this . options . trigger ) {
176
- this . options . target . unbind ( this . options . exit , this . exitHandler ) ;
177
- this . options . target . unbind ( this . options . trigger , this . enterHandler ) ;
206
+ this . options . $el . unbind ( this . options . exit , this . exitHandler ) ;
207
+ this . options . $el . unbind ( this . options . trigger , this . enterHandler ) ;
178
208
}
179
209
this . $el . off ( 'mouseleave' ) ;
180
210
this . $el . removeData ( ) . unbind ( ) ;
@@ -183,7 +213,7 @@ define([
183
213
} ,
184
214
addClasses : function ( ) {
185
215
var type , align ;
186
- switch ( this . options . type ) {
216
+ switch ( this . options . context ) {
187
217
case 'info' :
188
218
type = 'info' ;
189
219
break ;
0 commit comments