forked from merbjedi/jquery-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtools.tooltip.js
390 lines (287 loc) · 9.46 KB
/
tools.tooltip.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* tools.tooltip 1.1.2 - Tooltips done right.
*
* Copyright (c) 2009 Tero Piirainen
* http://flowplayer.org/tools/tooltip.html
*
* Dual licensed under MIT and GPL 2+ licenses
* http://www.opensource.org/licenses
*
* Launch : November 2008
* Date: ${date}
* Revision: ${revision}
*/
(function($) {
var instances = [];
// static constructs
$.tools = $.tools || {};
$.tools.tooltip = {
version: '1.1.2',
conf: {
// default effect variables
effect: 'toggle',
fadeOutSpeed: "fast",
tip: null,
predelay: 0,
delay: 30,
opacity: 1,
lazy: undefined,
// 'top', 'bottom', 'right', 'left', 'center'
position: ['top', 'center'],
offset: [0, 0],
cancelDefault: true,
relative: false,
oneInstance: true,
// type to event mapping
events: {
def: "mouseover,mouseout",
input: "focus,blur",
widget: "focus mouseover,blur mouseout",
tooltip: "mouseover,mouseout"
},
api: false
},
addEffect: function(name, loadFn, hideFn) {
effects[name] = [loadFn, hideFn];
}
};
var effects = {
toggle: [
function(done) {
var conf = this.getConf(), tip = this.getTip(), o = conf.opacity;
if (o < 1) { tip.css({opacity: o}); }
tip.show();
done.call();
},
function(done) {
this.getTip().hide();
done.call();
}
],
fade: [
function(done) { this.getTip().fadeIn(this.getConf().fadeInSpeed, done); },
function(done) { this.getTip().fadeOut(this.getConf().fadeOutSpeed, done); }
]
};
function Tooltip(trigger, conf) {
var self = this, $self = $(this);
trigger.data("tooltip", self);
// find the tip
var tip = trigger.next();
if (conf.tip) {
tip = $(conf.tip);
// multiple tip elements
if (tip.length > 1) {
// find sibling
tip = trigger.nextAll(conf.tip).eq(0);
// find sibling from the parent element
if (!tip.length) {
tip = trigger.parent().nextAll(conf.tip).eq(0);
}
}
}
/* calculate tip position relative to the trigger */
function getPosition(e) {
// get origin top/left position
var top = conf.relative ? trigger.position().top : trigger.offset().top,
left = conf.relative ? trigger.position().left : trigger.offset().left,
pos = conf.position[0];
top -= tip.outerHeight() - conf.offset[0];
left += trigger.outerWidth() + conf.offset[1];
// adjust Y
var height = tip.outerHeight() + trigger.outerHeight();
if (pos == 'center') { top += height / 2; }
if (pos == 'bottom') { top += height; }
// adjust X
pos = conf.position[1];
var width = tip.outerWidth() + trigger.outerWidth();
if (pos == 'center') { left -= width / 2; }
if (pos == 'left') { left -= width; }
return {top: top, left: left};
}
// event management
var isInput = trigger.is(":input"),
isWidget = isInput && trigger.is(":checkbox, :radio, select, :button"),
type = trigger.attr("type");
var evt = "";
if(conf.events) {
evt = conf.events[type] || conf.events[isInput ? (isWidget ? 'widget' : 'input') : 'def'];
}
evt = evt.split(/,\s*/);
// bind the first event as the show
if (evt[0]) {
trigger.bind(evt[0], function(e) {
// close all instances
if (conf.oneInstance) {
$.each(instances, function() {
this.hide();
});
}
// see if the tip was launched by this trigger
var t = tip.data("trigger");
if (t && t[0] != this) { tip.hide().stop(true, true); }
e.target = this;
self.show(e);
// tooltip close events
evt = conf.events.tooltip.split(/,\s*/);
tip.bind(evt[0], function() { self.show(e); });
if (evt[1]) { tip.bind(evt[1], function() { self.hide(e); }); }
});
}
// bind the second event as the hide
if (evt[1]) {
trigger.bind(evt[1], function(e) {
self.hide(e);
});
}
// ensure that the tip really shows up. IE cannot catch up with this.
if (!$.browser.msie && !isInput && !conf.predelay) {
trigger.mousemove(function() {
if (!self.isShown()) {
trigger.triggerHandler("mouseover");
}
});
}
// avoid "black box" bug in IE with PNG background images
if (conf.opacity < 1) {
tip.css("opacity", conf.opacity);
}
var pretimer = 0, title = trigger.attr("title");
if (title && conf.cancelDefault) {
trigger.removeAttr("title");
trigger.data("title", title);
}
$.extend(self, {
show: function(e) {
if (e) { trigger = $(e.target); }
clearTimeout(tip.data("timer"));
if (tip.is(":animated") || tip.is(":visible")) { return self; }
function show() {
// remember the trigger element for this tip
tip.data("trigger", trigger);
// get position
var pos = getPosition(e);
// title attribute
if (conf.tip && title) {
tip.html(trigger.data("title"));
}
// onBeforeShow
e = e || $.Event();
e.type = "onBeforeShow";
$self.trigger(e, [pos]);
if (e.isDefaultPrevented()) { return self; }
// onBeforeShow may have altered the configuration
pos = getPosition(e);
// set position
tip.css({position:'absolute', top: pos.top, left: pos.left});
// invoke effect
var eff = effects[conf.effect];
if (!eff) { throw "Nonexistent effect \"" + conf.effect + "\""; }
eff[0].call(self, function() {
e.type = "onShow";
$self.trigger(e);
});
}
if (conf.predelay) {
clearTimeout(pretimer);
pretimer = setTimeout(show, conf.predelay);
} else {
show();
}
return self;
},
hide: function(e) {
clearTimeout(tip.data("timer"));
clearTimeout(pretimer);
if (!tip.is(":visible")) { return; }
function hide() {
// onBeforeHide
e = e || $.Event();
e.type = "onBeforeHide";
$self.trigger(e);
if (e.isDefaultPrevented()) { return; }
effects[conf.effect][1].call(self, function() {
e.type = "onHide";
$self.trigger(e);
});
}
if (conf.delay && e) {
tip.data("timer", setTimeout(hide, conf.delay));
} else {
hide();
}
return self;
},
isShown: function() {
return tip.is(":visible, :animated");
},
getConf: function() {
return conf;
},
getTip: function() {
return tip;
},
getTrigger: function() {
return trigger;
},
// callback functions
bind: function(name, fn) {
$self.bind(name, fn);
return self;
},
onHide: function(fn) {
return this.bind("onHide", fn);
},
onBeforeShow: function(fn) {
return this.bind("onBeforeShow", fn);
},
onShow: function(fn) {
return this.bind("onShow", fn);
},
onBeforeHide: function(fn) {
return this.bind("onBeforeHide", fn);
},
unbind: function(name) {
$self.unbind(name);
return self;
}
});
// bind all callbacks from configuration
$.each(conf, function(name, fn) {
if ($.isFunction(fn)) { self.bind(name, fn); }
});
}
// jQuery plugin implementation
$.prototype.tooltip = function(conf) {
// return existing instance
var api = this.eq(typeof conf == 'number' ? conf : 0).data("tooltip");
if (api) { return api; }
// setup options
var globals = $.extend(true, {}, $.tools.tooltip.conf);
if ($.isFunction(conf)) {
conf = {onBeforeShow: conf};
} else if (typeof conf == 'string') {
conf = {tip: conf};
}
conf = $.extend(true, globals, conf);
// can also be given as string
if (typeof conf.position == 'string') {
conf.position = conf.position.split(/,?\s/);
}
// assign tip's only when apiement is being mouseovered
if (conf.lazy !== false && (conf.lazy === true || this.length > 20)) {
this.one("mouseover", function(e) {
api = new Tooltip($(this), conf);
api.show(e);
instances.push(api);
});
} else {
// install tooltip for each entry in jQuery object
this.each(function() {
api = new Tooltip($(this), conf);
instances.push(api);
});
}
return conf.api ? api: this;
};
}) (jQuery);