(bug 31704) Allow selection of associated namespace on the watchlist
[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 // 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 $tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
37
38 var pos = $.extend({}, this.$element.offset(), {
39 width: this.$element[0].offsetWidth,
40 height: this.$element[0].offsetHeight
41 });
42
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;
47
48 var tp;
49 switch (gravity.charAt(0)) {
50 case 'n':
51 tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
52 break;
53 case 's':
54 tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
55 break;
56 case 'e':
57 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset};
58 break;
59 case 'w':
60 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset};
61 break;
62 }
63
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;
68 } else {
69 tp.left = pos.left;
70 }
71 } else {
72 if (this.options.center) {
73 tp.left = pos.left + pos.width / 2 - actualWidth + 15;
74 } else {
75 tp.left = pos.left + pos.width;
76 }
77 }
78 }
79
80 $tip.css(tp).addClass('tipsy-' + gravity);
81 if (this.options.className) {
82 $tip.addClass(maybeCall(this.options.className, this.$element[0]));
83 }
84
85 if (this.options.fade) {
86 $tip.stop().css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: this.options.opacity}, 100);
87 } else {
88 $tip.css({visibility: 'visible', opacity: this.options.opacity});
89 }
90 }
91 },
92
93 hide: function() {
94 if (this.options.fade) {
95 this.tip().stop().fadeOut(100, function() { $(this).remove(); });
96 } else {
97 this.tip().remove();
98 }
99 },
100
101 getTitle: function() {
102 var title, $e = this.$element, o = this.options;
103 fixTitle($e);
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]);
109 }
110 title = ('' + title).replace(/(^\s*|\s*$)/, "");
111 return title || o.fallback;
112 },
113
114 tip: function() {
115 if (!this.$tip) {
116 this.$tip = $('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"/></div>');
117 }
118 return this.$tip;
119 },
120
121 validate: function() {
122 if (!this.$element[0].parentNode) {
123 this.hide();
124 this.$element = null;
125 this.options = null;
126 }
127 },
128
129 enable: function() { this.enabled = true; },
130 disable: function() { this.enabled = false; },
131 toggleEnabled: function() { this.enabled = !this.enabled; }
132 };
133
134 $.fn.tipsy = function(options) {
135
136 if (options === true) {
137 return this.data('tipsy');
138 } else if (typeof options == 'string') {
139 return this.data('tipsy')[options]();
140 }
141
142 options = $.extend({}, $.fn.tipsy.defaults, options);
143
144 function get(ele) {
145 var tipsy = $.data(ele, 'tipsy');
146 if (!tipsy) {
147 tipsy = new Tipsy(ele, $.fn.tipsy.elementOptions(ele, options));
148 $.data(ele, 'tipsy', tipsy);
149 }
150 return tipsy;
151 }
152
153 function enter() {
154 var tipsy = get(this);
155 tipsy.hoverState = 'in';
156 if (options.delayIn == 0) {
157 tipsy.show();
158 } else {
159 setTimeout(function() { if (tipsy.hoverState == 'in') tipsy.show(); }, options.delayIn);
160 }
161 };
162
163 function leave() {
164 var tipsy = get(this);
165 tipsy.hoverState = 'out';
166 if (options.delayOut == 0) {
167 tipsy.hide();
168 } else {
169 setTimeout(function() { if (tipsy.hoverState == 'out') tipsy.hide(); }, options.delayOut);
170 }
171 };
172
173 if (!options.live) this.each(function() { get(this); });
174
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);
180 }
181
182 return this;
183
184 };
185
186 $.fn.tipsy.defaults = {
187 className: null,
188 delayIn: 0,
189 delayOut: 0,
190 fade: true,
191 fallback: '',
192 gravity: 'n',
193 center: true,
194 html: false,
195 live: false,
196 offset: 0,
197 opacity: 1.0,
198 title: 'title',
199 trigger: 'hover'
200 };
201
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;
208 };
209
210 $.fn.tipsy.autoNS = function() {
211 return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
212 };
213
214 $.fn.tipsy.autoWE = function() {
215 return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
216 };
217
218 })(jQuery);