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