|
@@ -0,0 +1,260 @@
|
|
1
|
+// tipsy, facebook style tooltips for jquery
|
|
2
|
+// version 1.0.0a
|
|
3
|
+// (c) 2008-2010 jason frame [jason@onehackoranother.com]
|
|
4
|
+// released under the MIT license
|
|
5
|
+
|
|
6
|
+(function($) {
|
|
7
|
+
|
|
8
|
+ function maybeCall(thing, ctx) {
|
|
9
|
+ return (typeof thing == 'function') ? (thing.call(ctx)) : thing;
|
|
10
|
+ };
|
|
11
|
+
|
|
12
|
+ function isElementInDOM(ele) {
|
|
13
|
+ while (ele = ele.parentNode) {
|
|
14
|
+ if (ele == document) return true;
|
|
15
|
+ }
|
|
16
|
+ return false;
|
|
17
|
+ };
|
|
18
|
+
|
|
19
|
+ function Tipsy(element, options) {
|
|
20
|
+ this.$element = $(element);
|
|
21
|
+ this.options = options;
|
|
22
|
+ this.enabled = true;
|
|
23
|
+ this.fixTitle();
|
|
24
|
+ };
|
|
25
|
+
|
|
26
|
+ Tipsy.prototype = {
|
|
27
|
+ show: function() {
|
|
28
|
+ var title = this.getTitle();
|
|
29
|
+ if (title && this.enabled) {
|
|
30
|
+ var $tip = this.tip();
|
|
31
|
+
|
|
32
|
+ $tip.find('.tipsy-inner')[this.options.html ? 'html' : 'text'](title);
|
|
33
|
+ $tip[0].className = 'tipsy'; // reset classname in case of dynamic gravity
|
|
34
|
+ $tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).prependTo(document.body);
|
|
35
|
+
|
|
36
|
+ var pos = $.extend({}, this.$element.offset(), {
|
|
37
|
+ width: this.$element[0].offsetWidth,
|
|
38
|
+ height: this.$element[0].offsetHeight
|
|
39
|
+ });
|
|
40
|
+
|
|
41
|
+ var actualWidth = $tip[0].offsetWidth,
|
|
42
|
+ actualHeight = $tip[0].offsetHeight,
|
|
43
|
+ gravity = maybeCall(this.options.gravity, this.$element[0]);
|
|
44
|
+
|
|
45
|
+ var tp;
|
|
46
|
+ switch (gravity.charAt(0)) {
|
|
47
|
+ case 'n':
|
|
48
|
+ tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
|
|
49
|
+ break;
|
|
50
|
+ case 's':
|
|
51
|
+ tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
|
|
52
|
+ break;
|
|
53
|
+ case 'e':
|
|
54
|
+ tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset};
|
|
55
|
+ // XXX HACK: i want to set another offset so apparently I have to hardcode this into the code. obviously.
|
|
56
|
+ tp = {top: pos.top + pos.height / 2 - actualHeight / 2 + this.options.offset, left: pos.left - actualWidth};
|
|
57
|
+ break;
|
|
58
|
+ case 'w':
|
|
59
|
+ tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset};
|
|
60
|
+ break;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ if (gravity.length == 2) {
|
|
64
|
+ if (gravity.charAt(1) == 'w') {
|
|
65
|
+ tp.left = pos.left + pos.width / 2 - 15;
|
|
66
|
+ } else {
|
|
67
|
+ tp.left = pos.left + pos.width / 2 - actualWidth + 15;
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ $tip.css(tp).addClass('tipsy-' + gravity);
|
|
72
|
+ $tip.find('.tipsy-arrow')[0].className = 'tipsy-arrow tipsy-arrow-' + gravity.charAt(0);
|
|
73
|
+ if (this.options.className) {
|
|
74
|
+ $tip.addClass(maybeCall(this.options.className, this.$element[0]));
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ if (this.options.fade) {
|
|
78
|
+ $tip.stop().css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: this.options.opacity});
|
|
79
|
+ } else {
|
|
80
|
+ $tip.css({visibility: 'visible', opacity: this.options.opacity});
|
|
81
|
+ }
|
|
82
|
+ }
|
|
83
|
+ },
|
|
84
|
+
|
|
85
|
+ hide: function() {
|
|
86
|
+ if (this.options.fade) {
|
|
87
|
+ this.tip().stop().fadeOut(function() { $(this).remove(); });
|
|
88
|
+ } else {
|
|
89
|
+ this.tip().remove();
|
|
90
|
+ }
|
|
91
|
+ },
|
|
92
|
+
|
|
93
|
+ fixTitle: function() {
|
|
94
|
+ var $e = this.$element;
|
|
95
|
+ if ($e.attr('title') || typeof($e.attr('original-title')) != 'string') {
|
|
96
|
+ $e.attr('original-title', $e.attr('title') || '').removeAttr('title');
|
|
97
|
+ }
|
|
98
|
+ },
|
|
99
|
+
|
|
100
|
+ getTitle: function() {
|
|
101
|
+ var title, $e = this.$element, o = this.options;
|
|
102
|
+ this.fixTitle();
|
|
103
|
+ var title, o = this.options;
|
|
104
|
+ if (typeof o.title == 'string') {
|
|
105
|
+ title = $e.attr(o.title == 'title' ? 'original-title' : o.title);
|
|
106
|
+ } else if (typeof o.title == 'function') {
|
|
107
|
+ title = o.title.call($e[0]);
|
|
108
|
+ }
|
|
109
|
+ title = ('' + title).replace(/(^\s*|\s*$)/, "");
|
|
110
|
+ return title || o.fallback;
|
|
111
|
+ },
|
|
112
|
+
|
|
113
|
+ tip: function() {
|
|
114
|
+ if (!this.$tip) {
|
|
115
|
+ this.$tip = $('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"></div>');
|
|
116
|
+ this.$tip.data('tipsy-pointee', this.$element[0]);
|
|
117
|
+ }
|
|
118
|
+ return this.$tip;
|
|
119
|
+ },
|
|
120
|
+
|
|
121
|
+ validate: function() {
|
|
122
|
+ if (!this.$element[0].parentNode) {
|
|
123
|
+ this.hide();
|
|
124
|
+ this.$element = null;
|
|
125
|
+ this.options = null;
|
|
126
|
+ }
|
|
127
|
+ },
|
|
128
|
+
|
|
129
|
+ enable: function() { this.enabled = true; },
|
|
130
|
+ disable: function() { this.enabled = false; },
|
|
131
|
+ toggleEnabled: function() { this.enabled = !this.enabled; }
|
|
132
|
+ };
|
|
133
|
+
|
|
134
|
+ $.fn.tipsy = function(options) {
|
|
135
|
+
|
|
136
|
+ if (options === true) {
|
|
137
|
+ return this.data('tipsy');
|
|
138
|
+ } else if (typeof options == 'string') {
|
|
139
|
+ var tipsy = this.data('tipsy');
|
|
140
|
+ if (tipsy) tipsy[options]();
|
|
141
|
+ return this;
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ options = $.extend({}, $.fn.tipsy.defaults, options);
|
|
145
|
+
|
|
146
|
+ function get(ele) {
|
|
147
|
+ var tipsy = $.data(ele, 'tipsy');
|
|
148
|
+ if (!tipsy) {
|
|
149
|
+ tipsy = new Tipsy(ele, $.fn.tipsy.elementOptions(ele, options));
|
|
150
|
+ $.data(ele, 'tipsy', tipsy);
|
|
151
|
+ }
|
|
152
|
+ return tipsy;
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ function enter() {
|
|
156
|
+ var tipsy = get(this);
|
|
157
|
+ tipsy.hoverState = 'in';
|
|
158
|
+ if (options.delayIn == 0) {
|
|
159
|
+ tipsy.show();
|
|
160
|
+ } else {
|
|
161
|
+ tipsy.fixTitle();
|
|
162
|
+ setTimeout(function() { if (tipsy.hoverState == 'in') tipsy.show(); }, options.delayIn);
|
|
163
|
+ }
|
|
164
|
+ };
|
|
165
|
+
|
|
166
|
+ function leave() {
|
|
167
|
+ var tipsy = get(this);
|
|
168
|
+ tipsy.hoverState = 'out';
|
|
169
|
+ if (options.delayOut == 0) {
|
|
170
|
+ tipsy.hide();
|
|
171
|
+ } else {
|
|
172
|
+ setTimeout(function() { if (tipsy.hoverState == 'out') tipsy.hide(); }, options.delayOut);
|
|
173
|
+ }
|
|
174
|
+ };
|
|
175
|
+
|
|
176
|
+ if (!options.live) this.each(function() { get(this); });
|
|
177
|
+
|
|
178
|
+ if (options.trigger != 'manual') {
|
|
179
|
+ var binder = options.live ? 'live' : 'bind',
|
|
180
|
+ eventIn = options.trigger == 'hover' ? 'mouseenter' : 'focus',
|
|
181
|
+ eventOut = options.trigger == 'hover' ? 'mouseleave' : 'blur';
|
|
182
|
+ this[binder](eventIn, enter)[binder](eventOut, leave);
|
|
183
|
+ }
|
|
184
|
+
|
|
185
|
+ return this;
|
|
186
|
+
|
|
187
|
+ };
|
|
188
|
+
|
|
189
|
+ $.fn.tipsy.defaults = {
|
|
190
|
+ className: null,
|
|
191
|
+ delayIn: 0,
|
|
192
|
+ delayOut: 0,
|
|
193
|
+ fade: false,
|
|
194
|
+ fallback: '',
|
|
195
|
+ gravity: 'n',
|
|
196
|
+ html: false,
|
|
197
|
+ live: false,
|
|
198
|
+ offset: 0,
|
|
199
|
+ opacity: 0.8,
|
|
200
|
+ title: 'title',
|
|
201
|
+ trigger: 'hover'
|
|
202
|
+ };
|
|
203
|
+
|
|
204
|
+ $.fn.tipsy.revalidate = function() {
|
|
205
|
+ $('.tipsy').each(function() {
|
|
206
|
+ var pointee = $.data(this, 'tipsy-pointee');
|
|
207
|
+ if (!pointee || !isElementInDOM(pointee)) {
|
|
208
|
+ $(this).remove();
|
|
209
|
+ }
|
|
210
|
+ });
|
|
211
|
+ };
|
|
212
|
+
|
|
213
|
+ // Overwrite this method to provide options on a per-element basis.
|
|
214
|
+ // For example, you could store the gravity in a 'tipsy-gravity' attribute:
|
|
215
|
+ // return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' });
|
|
216
|
+ // (remember - do not modify 'options' in place!)
|
|
217
|
+ $.fn.tipsy.elementOptions = function(ele, options) {
|
|
218
|
+ return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
|
|
219
|
+ };
|
|
220
|
+
|
|
221
|
+ $.fn.tipsy.autoNS = function() {
|
|
222
|
+ return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
|
|
223
|
+ };
|
|
224
|
+
|
|
225
|
+ $.fn.tipsy.autoWE = function() {
|
|
226
|
+ return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
|
|
227
|
+ };
|
|
228
|
+
|
|
229
|
+ /**
|
|
230
|
+ * yields a closure of the supplied parameters, producing a function that takes
|
|
231
|
+ * no arguments and is suitable for use as an autogravity function like so:
|
|
232
|
+ *
|
|
233
|
+ * @param margin (int) - distance from the viewable region edge that an
|
|
234
|
+ * element should be before setting its tooltip's gravity to be away
|
|
235
|
+ * from that edge.
|
|
236
|
+ * @param prefer (string, e.g. 'n', 'sw', 'w') - the direction to prefer
|
|
237
|
+ * if there are no viewable region edges effecting the tooltip's
|
|
238
|
+ * gravity. It will try to vary from this minimally, for example,
|
|
239
|
+ * if 'sw' is preferred and an element is near the right viewable
|
|
240
|
+ * region edge, but not the top edge, it will set the gravity for
|
|
241
|
+ * that element's tooltip to be 'se', preserving the southern
|
|
242
|
+ * component.
|
|
243
|
+ */
|
|
244
|
+ $.fn.tipsy.autoBounds = function(margin, prefer) {
|
|
245
|
+ return function() {
|
|
246
|
+ var dir = {ns: prefer[0], ew: (prefer.length > 1 ? prefer[1] : false)},
|
|
247
|
+ boundTop = $(document).scrollTop() + margin,
|
|
248
|
+ boundLeft = $(document).scrollLeft() + margin,
|
|
249
|
+ $this = $(this);
|
|
250
|
+
|
|
251
|
+ if ($this.offset().top < boundTop) dir.ns = 'n';
|
|
252
|
+ if ($this.offset().left < boundLeft) dir.ew = 'w';
|
|
253
|
+ if ($(window).width() + $(document).scrollLeft() - $this.offset().left < margin) dir.ew = 'e';
|
|
254
|
+ if ($(window).height() + $(document).scrollTop() - $this.offset().top < margin) dir.ns = 's';
|
|
255
|
+
|
|
256
|
+ return dir.ns + (dir.ew ? dir.ew : '');
|
|
257
|
+ }
|
|
258
|
+ };
|
|
259
|
+
|
|
260
|
+})(jQuery);
|