jquery.tipsy: Unbreak the 'live' option for now
[mediawiki.git] / resources / src / jquery.tipsy / jquery.tipsy.js
blob2a37fa86cedca12309d076faed38db5bc7ce01a9
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
6 // * This installation of tipsy includes several local modifications to both Javascript and CSS.
7 //   Please be careful when upgrading.
9 ( function ( mw, $ ) {
11     function maybeCall(thing, ctx) {
12         return (typeof thing == 'function') ? (thing.call(ctx)) : thing;
13     }
15     function Tipsy(element, options) {
16         this.$element = $(element);
17         this.options = options;
18         this.enabled = true;
19         this.fixTitle();
20     }
22     Tipsy.prototype = {
23         show: function() {
24             var title = this.getTitle();
25             if (title && this.enabled) {
26                 var $tip = this.tip();
28                 $tip.find('.tipsy-inner')[this.options.html ? 'html' : 'text'](title);
29                 $tip[0].className = 'tipsy'; // reset classname in case of dynamic gravity
30                 if (this.options.className) {
31                     $tip.addClass(maybeCall(this.options.className, this.$element[0]));
32                 }
33                 $tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
35                 var pos = $.extend({}, this.$element.offset(), {
36                     width: this.$element[0].offsetWidth,
37                     height: this.$element[0].offsetHeight
38                 });
40                 var gravity = (typeof this.options.gravity == 'function')
41                                 ? this.options.gravity.call(this.$element[0])
42                                 : this.options.gravity;
44                 // Attach css classes before checking height/width so they
45                 // can be applied.
46                 $tip.addClass('tipsy-' + gravity);
47                 if (this.options.className) {
48                     $tip.addClass(maybeCall(this.options.className, this.$element[0]));
49                 }
51                 var actualWidth = $tip[0].offsetWidth, actualHeight = $tip[0].offsetHeight;
52                 var tp;
53                 switch (gravity.charAt(0)) {
54                     case 'n':
55                         tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
56                         break;
57                     case 's':
58                         tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
59                         break;
60                     case 'e':
61                         tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset};
62                         break;
63                     case 'w':
64                         tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset};
65                         break;
66                 }
68                 if (gravity.length == 2) {
69                     if (gravity.charAt(1) == 'w') {
70                         if (this.options.center) {
71                             tp.left = pos.left + pos.width / 2 - 15;
72                         } else {
73                             tp.left = pos.left;
74                         }
75                     } else {
76                         if (this.options.center) {
77                             tp.left = pos.left + pos.width / 2 - actualWidth + 15;
78                         } else {
79                             tp.left = pos.left + pos.width;
80                         }
81                     }
82                 }
83                 $tip.css(tp);
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         },
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         },
102         fixTitle: function() {
103             var $e = this.$element;
104             if ($e.attr('title') || typeof($e.attr('original-title')) != 'string') {
105                 $e.attr('original-title', $e.attr('title') || '').removeAttr('title');
106             }
107         },
109         getTitle: function() {
110             var title, $e = this.$element, o = this.options;
111             this.fixTitle();
112             if (typeof o.title == 'string') {
113                 title = $e.attr(o.title == 'title' ? 'original-title' : o.title);
114             } else if (typeof o.title == 'function') {
115                 title = o.title.call($e[0]);
116             }
117             title = ('' + title).replace(/(^\s*|\s*$)/, "");
118             return title || o.fallback;
119         },
121         tip: function() {
122             if (!this.$tip) {
123                 this.$tip = $('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"></div>');
124             }
125             return this.$tip;
126         },
128         validate: function() {
129             if (!this.$element[0].parentNode) {
130                 this.hide();
131                 this.$element = null;
132                 this.options = null;
133             }
134         },
136         enable: function() { this.enabled = true; },
137         disable: function() { this.enabled = false; },
138         toggleEnabled: function() { this.enabled = !this.enabled; }
139     };
141     $.fn.tipsy = function(options) {
143         if (options === true) {
144             return this.data('tipsy');
145         } else if (typeof options == 'string') {
146             var tipsy = this.data('tipsy');
147             if (tipsy) tipsy[options]();
148             return this;
149         }
151         options = $.extend({}, $.fn.tipsy.defaults, options);
153         function get(ele) {
154             var tipsy = $.data(ele, 'tipsy');
155             if (!tipsy) {
156                 tipsy = new Tipsy(ele, $.fn.tipsy.elementOptions(ele, options));
157                 $.data(ele, 'tipsy', tipsy);
158             }
159             return tipsy;
160         }
162         function enter() {
163             var tipsy = get(this);
164             tipsy.hoverState = 'in';
165             if (options.delayIn == 0) {
166                 tipsy.show();
167             } else {
168                 tipsy.fixTitle();
169                 setTimeout(function() { if (tipsy.hoverState == 'in') tipsy.show(); }, options.delayIn);
170             }
171         };
173         function leave() {
174             var tipsy = get(this);
175             tipsy.hoverState = 'out';
176             if (options.delayOut == 0) {
177                 tipsy.hide();
178             } else {
179                 setTimeout(function() { if (tipsy.hoverState == 'out') tipsy.hide(); }, options.delayOut);
180             }
181         };
183         if (!options.live) this.each(function() { get(this); });
185         if ( options.trigger != 'manual' ) {
186             var eventIn  = options.trigger == 'hover' ? 'mouseenter' : 'focus',
187                 eventOut = options.trigger == 'hover' ? 'mouseleave' : 'blur';
188             if ( options.live ) {
189                 mw.track( 'mw.deprecate', 'tipsy-live' );
190                 mw.log.warn( 'Use of the "live" option of jquery.tipsy is deprecated.' );
191                 // XXX: The official status of 'context' is deprecated, and the official status of
192                 // 'selector' is removed, so this really needs to go.
193                 $( this.context )
194                     .on( eventIn, this.selector, enter )
195                     .on( eventOut, this.selector, leave );
196             } else {
197                 this
198                     .on( eventIn, enter )
199                     .on( eventOut, leave );
200             }
201         }
203         return this;
205     };
207     $.fn.tipsy.defaults = {
208         className: null,
209         delayIn: 0,
210         delayOut: 0,
211         fade: true,
212         fallback: '',
213         gravity: 'n',
214         center: true,
215         html: false,
216         live: false,
217         offset: 0,
218         opacity: 1.0,
219         title: 'title',
220         trigger: 'hover'
221     };
223     // Overwrite this method to provide options on a per-element basis.
224     // For example, you could store the gravity in a 'tipsy-gravity' attribute:
225     // return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' });
226     // (remember - do not modify 'options' in place!)
227     $.fn.tipsy.elementOptions = function(ele, options) {
228         return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
229     };
231     $.fn.tipsy.autoNS = function() {
232         return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
233     };
235     $.fn.tipsy.autoWE = function() {
236         return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
237     };
239     /**
240      * yields a closure of the supplied parameters, producing a function that takes
241      * no arguments and is suitable for use as an autogravity function like so:
242      *
243      * @param margin (int) - distance from the viewable region edge that an
244      *        element should be before setting its tooltip's gravity to be away
245      *        from that edge.
246      * @param prefer (string, e.g. 'n', 'sw', 'w') - the direction to prefer
247      *        if there are no viewable region edges effecting the tooltip's
248      *        gravity. It will try to vary from this minimally, for example,
249      *        if 'sw' is preferred and an element is near the right viewable 
250      *        region edge, but not the top edge, it will set the gravity for
251      *        that element's tooltip to be 'se', preserving the southern
252      *        component.
253      */
254      $.fn.tipsy.autoBounds = function(margin, prefer) {
255         return function() {
256             var dir = {ns: prefer[0], ew: (prefer.length > 1 ? prefer[1] : false)},
257                 boundTop = $(document).scrollTop() + margin,
258                 boundLeft = $(document).scrollLeft() + margin,
259                 $this = $(this);
261             if ($this.offset().top < boundTop) dir.ns = 'n';
262             if ($this.offset().left < boundLeft) dir.ew = 'w';
263             if ($(window).width() + $(document).scrollLeft() - $this.offset().left < margin) dir.ew = 'e';
264             if ($(window).height() + $(document).scrollTop() - $this.offset().top < margin) dir.ns = 's';
266             return dir.ns + (dir.ew ? dir.ew : '');
267         }
268     };
270 }( mediaWiki, jQuery ) );