1 // tipsy, facebook style tooltips for jquery
3 // (c) 2008-2010 jason frame [jason@onehackoranother.com]
4 // released under the MIT license
6 // * This installation of tipsy includes several local modifications to both Javascript and CSS.
7 // Please be careful when upgrading.
11 function maybeCall(thing
, ctx
) {
12 return (typeof thing
== 'function') ? (thing
.call(ctx
)) : thing
;
15 function fixTitle($ele
) {
16 if ($ele
.attr('title') || typeof($ele
.attr('original-title')) != 'string') {
17 $ele
.attr('original-title', $ele
.attr('title') || '').removeAttr('title');
21 function Tipsy(element
, options
) {
22 this.$element
= $(element
);
23 this.options
= options
;
25 fixTitle(this.$element
);
30 var title
= this.getTitle();
31 if (title
&& this.enabled
) {
32 var $tip
= this.tip();
34 $tip
.find('.tipsy-inner')[this.options
.html
? 'html' : 'text'](title
);
35 $tip
[0].className
= 'tipsy'; // reset classname in case of dynamic gravity
36 $tip
.remove().css({top
: 0, left
: 0, visibility
: 'hidden', display
: 'block'}).appendTo(document
.body
);
38 var pos
= $.extend({}, this.$element
.offset(), {
39 width
: this.$element
[0].offsetWidth
,
40 height
: this.$element
[0].offsetHeight
43 var actualWidth
= $tip
[0].offsetWidth
, actualHeight
= $tip
[0].offsetHeight
;
44 var gravity
= (typeof this.options
.gravity
== 'function')
45 ? this.options
.gravity
.call(this.$element
[0])
46 : this.options
.gravity
;
49 switch (gravity
.charAt(0)) {
51 tp
= {top
: pos
.top
+ pos
.height
+ this.options
.offset
, left
: pos
.left
+ pos
.width
/ 2 - actualWidth
/ 2};
54 tp
= {top
: pos
.top
- actualHeight
- this.options
.offset
, left
: pos
.left
+ pos
.width
/ 2 - actualWidth
/ 2};
57 tp
= {top
: pos
.top
+ pos
.height
/ 2 - actualHeight
/ 2, left
: pos
.left
- actualWidth
- this.options
.offset
};
60 tp
= {top
: pos
.top
+ pos
.height
/ 2 - actualHeight
/ 2, left
: pos
.left
+ pos
.width
+ this.options
.offset
};
64 if (gravity
.length
== 2) {
65 if (gravity
.charAt(1) == 'w') {
66 if (this.options
.center
) {
67 tp
.left
= pos
.left
+ pos
.width
/ 2 - 15;
72 if (this.options
.center
) {
73 tp
.left
= pos
.left
+ pos
.width
/ 2 - actualWidth
+ 15;
75 tp
.left
= pos
.left
+ pos
.width
;
80 $tip
.css(tp
).addClass('tipsy-' + gravity
);
81 if (this.options
.className
) {
82 $tip
.addClass(maybeCall(this.options
.className
, this.$element
[0]));
85 if (this.options
.fade
) {
86 $tip
.stop().css({opacity
: 0, display
: 'block', visibility
: 'visible'}).animate({opacity
: this.options
.opacity
}, 100);
88 $tip
.css({visibility
: 'visible', opacity
: this.options
.opacity
});
94 if (this.options
.fade
) {
95 this.tip().stop().fadeOut(100, function() { $(this).remove(); });
101 getTitle: function() {
102 var title
, $e
= this.$element
, o
= this.options
;
104 var title
, o
= this.options
;
105 if (typeof o
.title
== 'string') {
106 title
= $e
.attr(o
.title
== 'title' ? 'original-title' : o
.title
);
107 } else if (typeof o
.title
== 'function') {
108 title
= o
.title
.call($e
[0]);
110 title
= ('' + title
).replace(/(^\s*|\s*$)/, "");
111 return title
|| o
.fallback
;
116 this.$tip
= $('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"/></div>');
121 validate: function() {
122 if (!this.$element
[0].parentNode
) {
124 this.$element
= null;
129 enable: function() { this.enabled
= true; },
130 disable: function() { this.enabled
= false; },
131 toggleEnabled: function() { this.enabled
= !this.enabled
; }
134 $.fn
.tipsy = function(options
) {
136 if (options
=== true) {
137 return this.data('tipsy');
138 } else if (typeof options
== 'string') {
139 return this.data('tipsy')[options
]();
142 options
= $.extend({}, $.fn
.tipsy
.defaults
, options
);
145 var tipsy
= $.data(ele
, 'tipsy');
147 tipsy
= new Tipsy(ele
, $.fn
.tipsy
.elementOptions(ele
, options
));
148 $.data(ele
, 'tipsy', tipsy
);
154 var tipsy
= get(this);
155 tipsy
.hoverState
= 'in';
156 if (options
.delayIn
== 0) {
159 setTimeout(function() { if (tipsy
.hoverState
== 'in') tipsy
.show(); }, options
.delayIn
);
164 var tipsy
= get(this);
165 tipsy
.hoverState
= 'out';
166 if (options
.delayOut
== 0) {
169 setTimeout(function() { if (tipsy
.hoverState
== 'out') tipsy
.hide(); }, options
.delayOut
);
173 if (!options
.live
) this.each(function() { get(this); });
175 if (options
.trigger
!= 'manual') {
176 var binder
= options
.live
? 'live' : 'bind',
177 eventIn
= options
.trigger
== 'hover' ? 'mouseenter' : 'focus',
178 eventOut
= options
.trigger
== 'hover' ? 'mouseleave' : 'blur';
179 this[binder
](eventIn
, enter
)[binder
](eventOut
, leave
);
186 $.fn
.tipsy
.defaults
= {
202 // Overwrite this method to provide options on a per-element basis.
203 // For example, you could store the gravity in a 'tipsy-gravity' attribute:
204 // return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' });
205 // (remember - do not modify 'options' in place!)
206 $.fn
.tipsy
.elementOptions = function(ele
, options
) {
207 return $.metadata
? $.extend({}, options
, $(ele
).metadata()) : options
;
210 $.fn
.tipsy
.autoNS = function() {
211 return $(this).offset().top
> ($(document
).scrollTop() + $(window
).height() / 2) ? 's' : 'n';
214 $.fn
.tipsy
.autoWE = function() {
215 return $(this).offset().left
> ($(document
).scrollLeft() + $(window
).width() / 2) ? 'e' : 'w';