-
Notifications
You must be signed in to change notification settings - Fork 97
/
tooltipsy.source.js
232 lines (212 loc) · 8.75 KB
/
tooltipsy.source.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* tooltipsy by Brian Cray
* Licensed under GPL2 - http://www.gnu.org/licenses/gpl-2.0.html
* Option quick reference:
* - alignTo: "element" or "cursor" (Defaults to "element")
* - offset: Tooltipsy distance from element or mouse cursor, dependent on alignTo setting. Set as array [x, y] (Defaults to [0, -1])
* - content: HTML or text content of tooltip. Defaults to "" (empty string), which pulls content from target element's title attribute
* - show: function(event, tooltip) to show the tooltip. Defaults to a show(100) effect
* - hide: function(event, tooltip) to hide the tooltip. Defaults to a fadeOut(100) effect
* - delay: A delay in milliseconds before showing a tooltip. Set to 0 for no delay. Defaults to 200
* - css: object containing CSS properties and values. Defaults to {} to use stylesheet for styles
* - className: DOM class for styling tooltips with CSS. Defaults to "tooltipsy"
* - showEvent: Set a custom event to bind the show function. Defaults to mouseenter
* - hideEvent: Set a custom event to bind the show function. Defaults to mouseleave
* Method quick reference:
* - $('element').data('tooltipsy').show(): Force the tooltip to show
* - $('element').data('tooltipsy').hide(): Force the tooltip to hide
* - $('element').data('tooltipsy').destroy(): Remove tooltip from DOM
* More information visit http://tooltipsy.com/
*/
(function($){
$.tooltipsy = function (el, options) {
this.options = options;
this.$el = $(el);
this.title = this.$el.attr('title') || '';
this.$el.attr('title', '');
this.random = parseInt(Math.random()*10000);
this.ready = false;
this.shown = false;
this.width = 0;
this.height = 0;
this.delaytimer = null;
this.$el.data("tooltipsy", this);
this.init();
};
$.tooltipsy.prototype = {
init: function () {
var base = this,
settings,
$el = base.$el,
el = $el[0];
base.settings = settings = $.extend({}, base.defaults, base.options);
settings.delay = +settings.delay;
if (typeof settings.content === 'function') {
base.readify();
}
if (settings.showEvent === settings.hideEvent && settings.showEvent === 'click') {
$el.toggle(function (e) {
if (settings.showEvent === 'click' && el.tagName == 'A') {
e.preventDefault();
}
if (settings.delay > 0) {
base.delaytimer = window.setTimeout(function () {
base.show(e);
}, settings.delay);
}
else {
base.show(e);
}
}, function (e) {
if (settings.showEvent === 'click' && el.tagName == 'A') {
e.preventDefault();
}
window.clearTimeout(base.delaytimer);
base.delaytimer = null;
base.hide(e);
});
}
else {
$el.bind(settings.showEvent, function (e) {
if (settings.showEvent === 'click' && el.tagName == 'A') {
e.preventDefault();
}
base.delaytimer = window.setTimeout(function () {
base.show(e);
}, settings.delay || 0);
}).bind(settings.hideEvent, function (e) {
if (settings.showEvent === 'click' && el.tagName == 'A') {
e.preventDefault();
}
window.clearTimeout(base.delaytimer);
base.delaytimer = null;
base.hide(e);
});
}
},
show: function (e) {
if (this.ready === false) {
this.readify();
}
var base = this,
settings = base.settings,
$tipsy = base.$tipsy,
$el = base.$el,
el = $el[0],
offset = base.offset(el);
if (base.shown === false) {
if ((function (o) {
var s = 0, k;
for (k in o) {
if (o.hasOwnProperty(k)) {
s++;
}
}
return s;
})(settings.css) > 0) {
base.$tip.css(settings.css);
}
base.width = $tipsy.outerWidth();
base.height = $tipsy.outerHeight();
}
if (settings.alignTo === 'cursor' && e) {
var tip_position = [e.clientX + settings.offset[0], e.clientY + settings.offset[1]];
if (tip_position[0] + base.width > $(window).width()) {
var tip_css = {top: tip_position[1] + 'px', right: tip_position[0] + 'px', left: 'auto'};
}
else {
var tip_css = {top: tip_position[1] + 'px', left: tip_position[0] + 'px', right: 'auto'};
}
}
else {
var tip_position = [
(function () {
if (settings.offset[0] < 0) {
return offset.left - Math.abs(settings.offset[0]) - base.width;
}
else if (settings.offset[0] === 0) {
return offset.left - ((base.width - $el.outerWidth()) / 2);
}
else {
return offset.left + $el.outerWidth() + settings.offset[0];
}
})(),
(function () {
if (settings.offset[1] < 0) {
return offset.top - Math.abs(settings.offset[1]) - base.height;
}
else if (settings.offset[1] === 0) {
return offset.top - ((base.height - base.$el.outerHeight()) / 2);
}
else {
return offset.top + base.$el.outerHeight() + settings.offset[1];
}
})()
];
}
$tipsy.css({top: tip_position[1] + 'px', left: tip_position[0] + 'px'});
base.settings.show(e, $tipsy.stop(true, true));
},
hide: function (e) {
var base = this;
if (base.ready === false) {
return;
}
if (e && e.relatedTarget === base.$tip[0]) {
base.$tip.bind('mouseleave', function (e) {
if (e.relatedTarget === base.$el[0]) {
return;
}
base.settings.hide(e, base.$tipsy.stop(true, true));
});
return;
}
base.settings.hide(e, base.$tipsy.stop(true, true));
},
readify: function () {
this.ready = true;
this.$tipsy = $('<div id="tooltipsy' + this.random + '" style="position:fixed;z-index:2147483647;display:none">').appendTo('body');
this.$tip = $('<div class="' + this.settings.className + '">').appendTo(this.$tipsy);
this.$tip.data('rootel', this.$el);
var e = this.$el;
var t = this.$tip;
this.$tip.html(this.settings.content != '' ? (typeof this.settings.content == 'string' ? this.settings.content : this.settings.content(e, t)) : this.title);
},
offset: function (el) {
return this.$el[0].getBoundingClientRect();
},
destroy: function () {
if (this.$tipsy) {
this.$tipsy.remove();
$.removeData(this.$el, 'tooltipsy');
}
},
update: function() {
this.title = this.$el.attr('title');
if(this.$tipsy) {
this.$tipsy.remove();
}
this.ready = false;
},
defaults: {
alignTo: 'element',
offset: [0, -1],
content: '',
show: function (e, $el) {
$el.fadeIn(100);
},
hide: function (e, $el) {
$el.fadeOut(100);
},
css: {},
className: 'tooltipsy',
delay: 200,
showEvent: 'mouseenter',
hideEvent: 'mouseleave'
}
};
$.fn.tooltipsy = function(options) {
return this.each(function() {
new $.tooltipsy(this, options);
});
};
})(jQuery);