Merge "ApiQueryInfo: fix query limits for testactions"
[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 ( mw, $ ) {
10
11 function maybeCall(thing, ctx) {
12 return (typeof thing == 'function') ? (thing.call(ctx)) : thing;
13 }
14
15 function Tipsy(element, options) {
16 this.$element = $(element);
17 this.options = options;
18 this.enabled = true;
19 this.keyHandler = $.proxy( this.closeOnEsc, this );
20 this.fixTitle();
21 }
22
23 Tipsy.prototype = {
24 show: function() {
25 var title = this.getTitle();
26 if (title && this.enabled) {
27 var $tip = this.tip();
28
29 $tip.find('.tipsy-inner')[this.options.html ? 'html' : 'text'](title);
30 $tip[0].className = 'tipsy'; // reset classname in case of dynamic gravity
31 if (this.options.className) {
32 $tip.addClass(maybeCall(this.options.className, this.$element[0]));
33 }
34 $tip.remove()
35 .css({top: 0, left: 0, visibility: 'hidden', display: 'block'})
36 .attr( 'aria-hidden', 'true' )
37 .appendTo(document.body);
38
39 var pos = $.extend({}, this.$element.offset(), {
40 width: this.$element[0].offsetWidth,
41 height: this.$element[0].offsetHeight
42 });
43
44 var gravity = (typeof this.options.gravity == 'function')
45 ? this.options.gravity.call(this.$element[0])
46 : this.options.gravity;
47
48 // Attach css classes before checking height/width so they
49 // can be applied.
50 $tip.addClass('tipsy-' + gravity);
51 if (this.options.className) {
52 $tip.addClass(maybeCall(this.options.className, this.$element[0]));
53 }
54
55 var actualWidth = $tip[0].offsetWidth, actualHeight = $tip[0].offsetHeight;
56 var tp;
57 switch (gravity.charAt(0)) {
58 case 'n':
59 tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
60 break;
61 case 's':
62 tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
63 break;
64 case 'e':
65 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset};
66 break;
67 case 'w':
68 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset};
69 break;
70 }
71
72 if (gravity.length == 2) {
73 if (gravity.charAt(1) == 'w') {
74 if (this.options.center) {
75 tp.left = pos.left + pos.width / 2 - 15;
76 } else {
77 tp.left = pos.left;
78 }
79 } else {
80 if (this.options.center) {
81 tp.left = pos.left + pos.width / 2 - actualWidth + 15;
82 } else {
83 tp.left = pos.left + pos.width;
84 }
85 }
86 }
87 $tip.css(tp);
88
89 $( document ).on( 'keydown', this.keyHandler );
90 if (this.options.fade) {
91 $tip.stop()
92 .css({opacity: 0, display: 'block', visibility: 'visible'})
93 .attr( 'aria-hidden', 'false' )
94 .animate({opacity: this.options.opacity}, 100);
95 } else {
96 $tip
97 .css({visibility: 'visible', opacity: this.options.opacity})
98 .attr( 'aria-hidden', 'false' );
99 }
100 }
101 },
102
103 hide: function() {
104 $( document ).off( 'keydown', this.keyHandler );
105 if (this.options.fade) {
106 this.tip().stop().fadeOut(100, function() { $(this).remove(); });
107 } else {
108 this.tip().remove();
109 }
110 },
111
112 fixTitle: function() {
113 var $e = this.$element;
114 if ($e.attr('title') || typeof($e.attr('original-title')) != 'string') {
115 $e.attr('original-title', $e.attr('title') || '').removeAttr('title');
116 }
117 },
118
119 getTitle: function() {
120 var title, $e = this.$element, o = this.options;
121 this.fixTitle();
122 if (typeof o.title == 'string') {
123 title = $e.attr(o.title == 'title' ? 'original-title' : o.title);
124 } else if (typeof o.title == 'function') {
125 title = o.title.call($e[0]);
126 }
127 title = ('' + title).replace(/(^\s*|\s*$)/, "");
128 return title || o.fallback;
129 },
130
131 tip: function() {
132 if (!this.$tip) {
133 this.$tip = $('<div class="tipsy" role="tooltip"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"></div>');
134 }
135 return this.$tip;
136 },
137
138 validate: function() {
139 if (!this.$element[0].parentNode) {
140 this.hide();
141 this.$element = null;
142 this.options = null;
143 }
144 },
145
146 // $.proxy event handler
147 closeOnEsc: function ( e ) {
148 if ( e.keyCode === 27 ) {
149 this.hide();
150 }
151 },
152
153 enable: function() { this.enabled = true; },
154 disable: function() { this.enabled = false; },
155 toggleEnabled: function() { this.enabled = !this.enabled; }
156 };
157
158 $.fn.tipsy = function(options) {
159
160 if (options === true) {
161 return this.data('tipsy');
162 } else if (typeof options == 'string') {
163 var tipsy = this.data('tipsy');
164 if (tipsy) tipsy[options]();
165 return this;
166 }
167
168 options = $.extend({}, $.fn.tipsy.defaults, options);
169
170 function get(ele) {
171 var tipsy = $.data(ele, 'tipsy');
172 if (!tipsy) {
173 tipsy = new Tipsy(ele, $.fn.tipsy.elementOptions(ele, options));
174 $.data(ele, 'tipsy', tipsy);
175 }
176 return tipsy;
177 }
178
179 function enter() {
180 var tipsy = get(this);
181 tipsy.hoverState = 'in';
182 if (options.delayIn == 0) {
183 tipsy.show();
184 } else {
185 tipsy.fixTitle();
186 setTimeout(function() { if (tipsy.hoverState == 'in') tipsy.show(); }, options.delayIn);
187 }
188 };
189
190 function leave() {
191 var tipsy = get(this);
192 tipsy.hoverState = 'out';
193 if (options.delayOut == 0) {
194 tipsy.hide();
195 } else {
196 setTimeout(function() { if (tipsy.hoverState == 'out') tipsy.hide(); }, options.delayOut);
197 }
198 };
199
200 this.each(function() { get(this); });
201
202 if ( options.trigger != 'manual' ) {
203 var eventIn = options.trigger == 'hover' ? 'mouseenter focus' : 'focus',
204 eventOut = options.trigger == 'hover' ? 'mouseleave blur' : 'blur';
205 if ( options.live ) {
206 mw.track( 'mw.deprecate', 'tipsy-live' );
207 mw.log.warn( 'Use of the "live" option of jquery.tipsy is no longer supported.' );
208 }
209 this
210 .on( eventIn, enter )
211 .on( eventOut, leave );
212 }
213
214 return this;
215
216 };
217
218 $.fn.tipsy.defaults = {
219 className: null,
220 delayIn: 0,
221 delayOut: 0,
222 fade: true,
223 fallback: '',
224 gravity: 'n',
225 center: true,
226 html: false,
227 live: false,
228 offset: 0,
229 opacity: 1.0,
230 title: 'title',
231 trigger: 'hover'
232 };
233
234 // Overwrite this method to provide options on a per-element basis.
235 // For example, you could store the gravity in a 'tipsy-gravity' attribute:
236 // return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' });
237 // (remember - do not modify 'options' in place!)
238 $.fn.tipsy.elementOptions = function(ele, options) {
239 return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
240 };
241
242 $.fn.tipsy.autoNS = function() {
243 return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
244 };
245
246 $.fn.tipsy.autoWE = function() {
247 return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
248 };
249
250 /**
251 * yields a closure of the supplied parameters, producing a function that takes
252 * no arguments and is suitable for use as an autogravity function like so:
253 *
254 * @param margin (int) - distance from the viewable region edge that an
255 * element should be before setting its tooltip's gravity to be away
256 * from that edge.
257 * @param prefer (string, e.g. 'n', 'sw', 'w') - the direction to prefer
258 * if there are no viewable region edges effecting the tooltip's
259 * gravity. It will try to vary from this minimally, for example,
260 * if 'sw' is preferred and an element is near the right viewable
261 * region edge, but not the top edge, it will set the gravity for
262 * that element's tooltip to be 'se', preserving the southern
263 * component.
264 */
265 $.fn.tipsy.autoBounds = function(margin, prefer) {
266 return function() {
267 var dir = {ns: prefer[0], ew: (prefer.length > 1 ? prefer[1] : false)},
268 boundTop = $(document).scrollTop() + margin,
269 boundLeft = $(document).scrollLeft() + margin,
270 $this = $(this);
271
272 if ($this.offset().top < boundTop) dir.ns = 'n';
273 if ($this.offset().left < boundLeft) dir.ew = 'w';
274 if ($(window).width() + $(document).scrollLeft() - $this.offset().left < margin) dir.ew = 'e';
275 if ($(window).height() + $(document).scrollTop() - $this.offset().top < margin) dir.ns = 's';
276
277 return dir.ns + (dir.ew ? dir.ew : '');
278 }
279 };
280
281 }( mediaWiki, jQuery ) );