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 Tipsy(element, options) {
16 this.$element = $(element);
17 this.options = options;
24 var title = this.getTitle();
25 if (title && this.enabled) {
26 var $tip = this.tip();
28 $tip.find('.tipsy-inner')[this.options.html ? 'html' : 'text'](title);
29 $tip[0].className = 'tipsy'; // reset classname in case of dynamic gravity
30 if (this.options.className) {
31 $tip.addClass(maybeCall(this.options.className, this.$element[0]));
33 $tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
35 var pos = $.extend({}, this.$element.offset(), {
36 width: this.$element[0].offsetWidth,
37 height: this.$element[0].offsetHeight
40 var gravity = (typeof this.options.gravity == 'function')
41 ? this.options.gravity.call(this.$element[0])
42 : this.options.gravity;
44 // Attach css classes before checking height/width so they
46 $tip.addClass('tipsy-' + gravity);
47 if (this.options.className) {
48 $tip.addClass(maybeCall(this.options.className, this.$element[0]));
51 var actualWidth = $tip[0].offsetWidth, actualHeight = $tip[0].offsetHeight;
53 switch (gravity.charAt(0)) {
55 tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
58 tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
61 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset};
64 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset};
68 if (gravity.length == 2) {
69 if (gravity.charAt(1) == 'w') {
70 if (this.options.center) {
71 tp.left = pos.left + pos.width / 2 - 15;
76 if (this.options.center) {
77 tp.left = pos.left + pos.width / 2 - actualWidth + 15;
79 tp.left = pos.left + pos.width;
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(); });
102 fixTitle: function() {
103 var $e = this.$element;
104 if ($e.attr('title') || typeof($e.attr('original-title')) != 'string') {
105 $e.attr('original-title', $e.attr('title') || '').removeAttr('title');
109 getTitle: function() {
110 var title, $e = this.$element, o = this.options;
112 if (typeof o.title == 'string') {
113 title = $e.attr(o.title == 'title' ? 'original-title' : o.title);
114 } else if (typeof o.title == 'function') {
115 title = o.title.call($e[0]);
117 title = ('' + title).replace(/(^\s*|\s*$)/, "");
118 return title || o.fallback;
123 this.$tip = $('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"></div>');
128 validate: function() {
129 if (!this.$element[0].parentNode) {
131 this.$element = null;
136 enable: function() { this.enabled = true; },
137 disable: function() { this.enabled = false; },
138 toggleEnabled: function() { this.enabled = !this.enabled; }
141 $.fn.tipsy = function(options) {
143 if (options === true) {
144 return this.data('tipsy');
145 } else if (typeof options == 'string') {
146 var tipsy = this.data('tipsy');
147 if (tipsy) tipsy[options]();
151 options = $.extend({}, $.fn.tipsy.defaults, options);
154 var tipsy = $.data(ele, 'tipsy');
156 tipsy = new Tipsy(ele, $.fn.tipsy.elementOptions(ele, options));
157 $.data(ele, 'tipsy', tipsy);
163 var tipsy = get(this);
164 tipsy.hoverState = 'in';
165 if (options.delayIn == 0) {
169 setTimeout(function() { if (tipsy.hoverState == 'in') tipsy.show(); }, options.delayIn);
174 var tipsy = get(this);
175 tipsy.hoverState = 'out';
176 if (options.delayOut == 0) {
179 setTimeout(function() { if (tipsy.hoverState == 'out') tipsy.hide(); }, options.delayOut);
183 if (!options.live) this.each(function() { get(this); });
185 if (options.trigger != 'manual') {
186 var binder = options.live ? 'live' : 'bind',
187 eventIn = options.trigger == 'hover' ? 'mouseenter' : 'focus',
188 eventOut = options.trigger == 'hover' ? 'mouseleave' : 'blur';
189 this[binder](eventIn, enter)[binder](eventOut, leave);
196 $.fn.tipsy.defaults = {
212 // Overwrite this method to provide options on a per-element basis.
213 // For example, you could store the gravity in a 'tipsy-gravity' attribute:
214 // return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' });
215 // (remember - do not modify 'options' in place!)
216 $.fn.tipsy.elementOptions = function(ele, options) {
217 return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
220 $.fn.tipsy.autoNS = function() {
221 return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
224 $.fn.tipsy.autoWE = function() {
225 return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
229 * yields a closure of the supplied parameters, producing a function that takes
230 * no arguments and is suitable for use as an autogravity function like so:
232 * @param margin (int) - distance from the viewable region edge that an
233 * element should be before setting its tooltip's gravity to be away
235 * @param prefer (string, e.g. 'n', 'sw', 'w') - the direction to prefer
236 * if there are no viewable region edges effecting the tooltip's
237 * gravity. It will try to vary from this minimally, for example,
238 * if 'sw' is preferred and an element is near the right viewable
239 * region edge, but not the top edge, it will set the gravity for
240 * that element's tooltip to be 'se', preserving the southern
243 $.fn.tipsy.autoBounds = function(margin, prefer) {
245 var dir = {ns: prefer[0], ew: (prefer.length > 1 ? prefer[1] : false)},
246 boundTop = $(document).scrollTop() + margin,
247 boundLeft = $(document).scrollLeft() + margin,
250 if ($this.offset().top < boundTop) dir.ns = 'n';
251 if ($this.offset().left < boundLeft) dir.ew = 'w';
252 if ($(window).width() + $(document).scrollLeft() - $this.offset().left < margin) dir.ew = 'e';
253 if ($(window).height() + $(document).scrollTop() - $this.offset().top < margin) dir.ns = 's';
255 return dir.ns + (dir.ew ? dir.ew : '');