1 // tipsy, facebook style tooltips for jquery
3 // (c) 2008-2010 jason frame [jason@onehackoranother.com]
4 // released under the MIT license
6 // * This installation of tipsy includes several local modifications to both Javascript and CSS.
7 // Please be careful when upgrading.
11 function maybeCall(thing, ctx) {
12 return (typeof thing == 'function') ? (thing.call(ctx)) : thing;
15 function Tipsy(element, options) {
16 this.$element = $(element);
17 this.options = options;
19 this.keyHandler = $.proxy( this.closeOnEsc, this );
25 var title = this.getTitle();
26 if (title && this.enabled) {
27 var $tip = this.tip();
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]));
35 .css({top: 0, left: 0, visibility: 'hidden', display: 'block'})
36 .attr( 'aria-hidden', 'true' )
37 .appendTo(document.body);
39 var pos = $.extend({}, this.$element.offset(), {
40 width: this.$element[0].offsetWidth,
41 height: this.$element[0].offsetHeight
44 var gravity = (typeof this.options.gravity == 'function')
45 ? this.options.gravity.call(this.$element[0])
46 : this.options.gravity;
48 // Attach css classes before checking height/width so they
50 $tip.addClass('tipsy-' + gravity);
51 if (this.options.className) {
52 $tip.addClass(maybeCall(this.options.className, this.$element[0]));
55 var actualWidth = $tip[0].offsetWidth, actualHeight = $tip[0].offsetHeight;
57 switch (gravity.charAt(0)) {
59 tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
62 tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
65 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset};
68 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset};
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;
80 if (this.options.center) {
81 tp.left = pos.left + pos.width / 2 - actualWidth + 15;
83 tp.left = pos.left + pos.width;
89 $( document ).on( 'keydown', this.keyHandler );
90 if (this.options.fade) {
92 .css({opacity: 0, display: 'block', visibility: 'visible'})
93 .attr( 'aria-hidden', 'false' )
94 .animate({opacity: this.options.opacity}, 100);
97 .css({visibility: 'visible', opacity: this.options.opacity})
98 .attr( 'aria-hidden', 'false' );
104 $( document ).off( 'keydown', this.keyHandler );
105 if (this.options.fade) {
106 this.tip().stop().fadeOut(100, function() { $(this).remove(); });
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');
119 getTitle: function() {
120 var title, $e = this.$element, o = this.options;
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]);
127 title = ('' + title).replace(/(^\s*|\s*$)/, "");
128 return title || o.fallback;
133 this.$tip = $('<div class="tipsy" role="tooltip"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"></div>');
138 validate: function() {
139 if (!this.$element[0].parentNode) {
141 this.$element = null;
146 // $.proxy event handler
147 closeOnEsc: function ( e ) {
148 if ( e.keyCode === 27 ) {
153 enable: function() { this.enabled = true; },
154 disable: function() { this.enabled = false; },
155 toggleEnabled: function() { this.enabled = !this.enabled; }
158 $.fn.tipsy = function(options) {
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]();
168 options = $.extend({}, $.fn.tipsy.defaults, options);
171 var tipsy = $.data(ele, 'tipsy');
173 tipsy = new Tipsy(ele, $.fn.tipsy.elementOptions(ele, options));
174 $.data(ele, 'tipsy', tipsy);
180 var tipsy = get(this);
181 tipsy.hoverState = 'in';
182 if (options.delayIn == 0) {
186 setTimeout(function() { if (tipsy.hoverState == 'in') tipsy.show(); }, options.delayIn);
191 var tipsy = get(this);
192 tipsy.hoverState = 'out';
193 if (options.delayOut == 0) {
196 setTimeout(function() { if (tipsy.hoverState == 'out') tipsy.hide(); }, options.delayOut);
200 this.each(function() { get(this); });
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.' );
210 .on( eventIn, enter )
211 .on( eventOut, leave );
218 $.fn.tipsy.defaults = {
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;
242 $.fn.tipsy.autoNS = function() {
243 return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
246 $.fn.tipsy.autoWE = function() {
247 return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
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:
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
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
265 $.fn.tipsy.autoBounds = function(margin, prefer) {
267 var dir = {ns: prefer[0], ew: (prefer.length > 1 ? prefer[1] : false)},
268 boundTop = $(document).scrollTop() + margin,
269 boundLeft = $(document).scrollLeft() + margin,
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';
277 return dir.ns + (dir.ew ? dir.ew : '');
281 }( mediaWiki, jQuery ) );