code readability is everything
[lhc/web/wiklou.git] / resources / jquery.tipsy / jquery.tipsy.js
1 // tipsy, facebook style tooltips for jquery
2 // version 1.0.0a
3 // (c) 2008-2010 jason frame [jason@onehackoranother.com]
4 // releated under the MIT license
5
6 (function($) {
7
8 function fixTitle($ele) {
9 if ($ele.attr('title') || typeof($ele.attr('original-title')) != 'string') {
10 $ele.attr('original-title', $ele.attr('title') || '').removeAttr('title');
11 }
12 }
13
14 function Tipsy(element, options) {
15 this.$element = $(element);
16 this.options = options;
17 this.enabled = true;
18 fixTitle(this.$element);
19 }
20
21 Tipsy.prototype = {
22 show: function() {
23 var title = this.getTitle();
24 if (title && this.enabled) {
25 var $tip = this.tip();
26
27 $tip.find('.tipsy-inner')[this.options.html ? 'html' : 'text'](title);
28 $tip[0].className = 'tipsy'; // reset classname in case of dynamic gravity
29 $tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
30
31 var pos = $.extend({}, this.$element.offset(), {
32 width: this.$element[0].offsetWidth,
33 height: this.$element[0].offsetHeight
34 });
35
36 var actualWidth = $tip[0].offsetWidth, actualHeight = $tip[0].offsetHeight;
37 var gravity = (typeof this.options.gravity == 'function')
38 ? this.options.gravity.call(this.$element[0])
39 : this.options.gravity;
40
41 var tp;
42 switch (gravity.charAt(0)) {
43 case 'n':
44 tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
45 break;
46 case 's':
47 tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
48 break;
49 case 'e':
50 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset};
51 break;
52 case 'w':
53 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset};
54 break;
55 }
56
57 if (gravity.length == 2) {
58 if (gravity.charAt(1) == 'w') {
59 if (this.options.center) {
60 tp.left = pos.left + pos.width / 2 - 15;
61 } else {
62 tp.left = pos.left;
63 }
64 } else {
65 if (this.options.center) {
66 tp.left = pos.left + pos.width / 2 - actualWidth + 15;
67 } else {
68 tp.left = pos.left + pos.width;
69 }
70 }
71 }
72
73 $tip.css(tp).addClass('tipsy-' + gravity);
74
75 if (this.options.fade) {
76 $tip.stop().css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: this.options.opacity}, 100);
77 } else {
78 $tip.css({visibility: 'visible', opacity: this.options.opacity});
79 }
80 }
81 },
82
83 hide: function() {
84 if (this.options.fade) {
85 this.tip().stop().fadeOut(100, function() { $(this).remove(); });
86 } else {
87 this.tip().remove();
88 }
89 },
90
91 getTitle: function() {
92 var title, $e = this.$element, o = this.options;
93 fixTitle($e);
94 var title, o = this.options;
95 if (typeof o.title == 'string') {
96 title = $e.attr(o.title == 'title' ? 'original-title' : o.title);
97 } else if (typeof o.title == 'function') {
98 title = o.title.call($e[0]);
99 }
100 title = ('' + title).replace(/(^\s*|\s*$)/, "");
101 return title || o.fallback;
102 },
103
104 tip: function() {
105 if (!this.$tip) {
106 this.$tip = $('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"/></div>');
107 }
108 return this.$tip;
109 },
110
111 validate: function() {
112 if (!this.$element[0].parentNode) {
113 this.hide();
114 this.$element = null;
115 this.options = null;
116 }
117 },
118
119 enable: function() { this.enabled = true; },
120 disable: function() { this.enabled = false; },
121 toggleEnabled: function() { this.enabled = !this.enabled; }
122 };
123
124 $.fn.tipsy = function(options) {
125
126 if (options === true) {
127 return this.data('tipsy');
128 } else if (typeof options == 'string') {
129 return this.data('tipsy')[options]();
130 }
131
132 options = $.extend({}, $.fn.tipsy.defaults, options);
133
134 function get(ele) {
135 var tipsy = $.data(ele, 'tipsy');
136 if (!tipsy) {
137 tipsy = new Tipsy(ele, $.fn.tipsy.elementOptions(ele, options));
138 $.data(ele, 'tipsy', tipsy);
139 }
140 return tipsy;
141 }
142
143 function enter() {
144 var tipsy = get(this);
145 tipsy.hoverState = 'in';
146 if (options.delayIn == 0) {
147 tipsy.show();
148 } else {
149 setTimeout(function() { if (tipsy.hoverState == 'in') tipsy.show(); }, options.delayIn);
150 }
151 };
152
153 function leave() {
154 var tipsy = get(this);
155 tipsy.hoverState = 'out';
156 if (options.delayOut == 0) {
157 tipsy.hide();
158 } else {
159 setTimeout(function() { if (tipsy.hoverState == 'out') tipsy.hide(); }, options.delayOut);
160 }
161 };
162
163 if (!options.live) this.each(function() { get(this); });
164
165 if (options.trigger != 'manual') {
166 var binder = options.live ? 'live' : 'bind',
167 eventIn = options.trigger == 'hover' ? 'mouseenter' : 'focus',
168 eventOut = options.trigger == 'hover' ? 'mouseleave' : 'blur';
169 this[binder](eventIn, enter)[binder](eventOut, leave);
170 }
171
172 return this;
173
174 };
175
176 $.fn.tipsy.defaults = {
177 delayIn: 0,
178 delayOut: 0,
179 fade: true,
180 fallback: '',
181 gravity: 'n',
182 center: true,
183 html: false,
184 live: false,
185 offset: 0,
186 opacity: 1.0,
187 title: 'title',
188 trigger: 'hover'
189 };
190
191 // Overwrite this method to provide options on a per-element basis.
192 // For example, you could store the gravity in a 'tipsy-gravity' attribute:
193 // return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' });
194 // (remember - do not modify 'options' in place!)
195 $.fn.tipsy.elementOptions = function(ele, options) {
196 return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
197 };
198
199 $.fn.tipsy.autoNS = function() {
200 return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
201 };
202
203 $.fn.tipsy.autoWE = function() {
204 return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
205 };
206
207 })(jQuery);