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