PageSourceSearch

https://www.cpagrip.com/plugins/tipsy/jquery.tipsy.js

js cpagrip.com collected 2026-10-02 05:32:10 UTC 16,990 bytes, 423 lines download raw bytes

1// tipsy, facebook style tooltips for jquery
2// version 1.0.2
3// (c) 2008-2010 jason frame [[email protected]]
4// released under the MIT license
5(function ($) {
6    $.fn.extend({
7        size: function () {
8            return $(this).length;
9        }
10    });
11})(jQuery);
12(function($, window, undefined) {
13
14    function maybeCall(thing, ctx) {
15        return (typeof thing == 'function') ? (thing.call(ctx)) : thing;
16    }
17
18    function isElementInDOM(ele) {
19        while (ele = ele.parentNode) {
20            if (ele == document) return true;
21        }
22        return false;
23    }
24
25	// Returns true if it is a DOM element
26	// http://stackoverflow.com/a/384380/999
27	function isElement(o){
28		return (
29			typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
30			o && typeof o === "object" && o.nodeType === 1 && typeof o.nodeName==="string"
31		);	
32	}
33
34    var tipsyIDcounter = 0;
35    function tipsyID() {
36        return "tipsyuid" + (tipsyIDcounter++);
37    }
38
39    function Tipsy(element, options) {
40        this.$element = $(element);
41        this.options = options;
42        this.enabled = true;
43        this.fixTitle();
44    }
45
46    Tipsy.prototype = {
47        show: function() {
48            if (!isElementInDOM(this.$element[0])) {
49                return;
50            }
51
52            if (isElement(this.$element) && !this.$element.is(':visible')) { 
53                return; 
54            }
55            
56            var title;
57            if (this.enabled && (title = this.getTitle())) {
58                var $tip = this.tip();
59
60                $tip.find('.tipsy-inner' + this.options.theme)[this.options.html ? 'html' : 'text'](title);
61
62                $tip[0].className = 'tipsy' + this.options.theme; // reset classname in case of dynamic gravity
63                if (this.options.className) {
64                    $tip.addClass(maybeCall(this.options.className, this.$element[0]));
65                }
66
67                $tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).prependTo(document.body);
68
69                var pos = $.extend({}, this.$element.offset());
70
71                // If the element is contained in a SVG object, use getBBox
72                if (this.$element.parents('svg').size() > 0) {
73                    pos = $.extend(pos, this.$element[0].getBBox());
74                } else {
75                    pos = $.extend(pos, {
76                        width: this.$element[0].offsetWidth || 0,
77                        height: this.$element[0].offsetHeight || 0
78                    });
79                }
80
81                var actualWidth = $tip[0].offsetWidth,
82                    actualHeight = $tip[0].offsetHeight,
83                    gravity = maybeCall(this.options.gravity, this.$element[0]);
84
85                var tp;
86                switch (gravity.charAt(0)) {
87                    case 'n':
88                        tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
89                        break;
90                    case 's':
91                        tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
92                        break;
93                    case 'e':
94                        tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset};
95                        break;
96                    case 'w':
97                        tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset};
98                        break;
99                    default:
100                        break;
101                }
102
103                if (gravity.length == 2) {
104                    if (gravity.charAt(1) == 'w') {
105                        tp.left = pos.left + pos.width / 2 - 15;
106                    } else {
107                        tp.left = pos.left + pos.width / 2 - actualWidth + 15;
108                    }
109                }
110
111                $tip.css(tp).addClass('tipsy-' + gravity + this.options.theme);
112                $tip.find('.tipsy-arrow' + this.options.theme)[0].className = 'tipsy-arrow' + this.options.theme + ' tipsy-arrow-' + gravity.charAt(0) + this.options.theme;
113
114                if (this.options.fade) {
115                    if(this.options.shadow)
116                        $(".tipsy-inner").css({'box-shadow': '0px 0px '+this.options.shadowBlur+'px '+this.options.shadowSpread+'px rgba(0, 0, 0, '+this.options.shadowOpacity+')', '-webkit-box-shadow': '0px 0px '+this.options.shadowBlur+'px '+this.options.shadowSpread+'px rgba(0, 0, 0, '+this.options.shadowOpacity+')'});
117                    $tip.stop().css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: this.options.opacity}, this.options.fadeInTime);
118                } else {
119                    $tip.css({visibility: 'visible', opacity: this.options.opacity});
120                }
121
122                if (this.options.aria) {
123                    var $tipID = tipsyID();
124                    $tip.attr("id", $tipID);
125                    this.$element.attr("aria-describedby", $tipID);
126                }
127            }
128        },
129
130        hide: function() {
131            if (this.options.fade) {
132                this.tip().stop().fadeOut(this.options.fadeOutTime, function() { $(this).remove(); });
133            } else {
134                this.tip().remove();
135            }
136            if (this.options.aria) {
137                this.$element.removeAttr("aria-describedby");
138            }
139        },
140
141        fixTitle: function() {
142            var $e = this.$element,
143                id = maybeCall(this.options.id, this.$element[0]);
144            if ($e.prop('title') || typeof($e.prop('original-title')) != 'string') {
145                $e.prop('original-title', $e.prop('title') || '').removeAttr('title');
146                // add aria-describedby pointing to the tooltip's id
147                $e.attr('aria-describedby', id);
148                // if it doesn't already have a tabindex, force the trigger element into the tab cycle
149                // to make it keyboard accessible with tabindex=0. this automatically makes elements
150                // that are not normally keyboard accessible (div or span) that have been tipsy-fied
151                // also operable with the keyboard.
152                if ($e.attr('tabindex') === undefined) {
153                    $e.attr('tabindex', 0);
154                }
155            }
156        },
157
158        getTitle: function() {
159            var title, $e = this.$element, o = this.options;
160            this.fixTitle();
161            if (typeof o.title == 'string') {
162                title = $e.prop(o.title == 'title' ? 'original-title' : o.title);
163            } else if (typeof o.title == 'function') {
164                title = o.title.call($e[0]);
165            }
166            title = ('' + title).replace(/(^\s*|\s*$)/, "");
167            return title || o.fallback;
168        },
169
170        tip: function() {
171            var id = maybeCall(this.options.id, this.$element[0]);
172
173            if (!this.$tip) {
174                // generate tooltip, with appropriate ARIA role and an 'id' (can be set in options),
175                // so it can be targetted by aria-describedby in the trigger element
176                this.$tip = $('<div class="tipsy' + this.options.theme + '" id="'+id+'" role="tooltip"></div>').html('<div class="tipsy-arrow' + this.options.theme + '"></div><div class="tipsy-inner' + this.options.theme + '"></div>').attr("role","tooltip");
177                this.$tip.data('tipsy-pointee', this.$element[0]);
178            }
179            return this.$tip;
180        },
181
182        validate: function() {
183            if (!this.$element[0].parentNode) {
184                this.hide();
185                this.$element = null;
186                this.options = null;
187            }
188        },
189
190        enable: function() { this.enabled = true; },
191        disable: function() { this.enabled = false; },
192        toggleEnabled: function() { this.enabled = !this.enabled; }
193    };
194
195    $.fn.tipsy = function(options) {
196
197        $.fn.tipsy.enable();
198
199        if (options === true) {
200            return this.data('tipsy');
201        } else if (typeof options == 'string') {
202            var tipsy = this.data('tipsy');
203            if (tipsy) tipsy[options]();
204            return this;
205        }
206
207        options = $.extend({}, $.fn.tipsy.defaults, options);
208
209        // Establish theme
210        options.theme = (options.theme && options.theme !== '') ? '-' + options.theme : '';
211
212        function get(ele) {
213            var tipsy = $.data(ele, 'tipsy');
214            if (!tipsy) {
215                tipsy = new Tipsy(ele, $.fn.tipsy.elementOptions(ele, options));
216                $.data(ele, 'tipsy', tipsy);
217            }
218            return tipsy;
219        }
220
221        function enter() {
222            if ($.fn.tipsy.enabled !== true) {
223                return;
224            }
225            var tipsy = get(this);
226            tipsy.hoverState = 'in';
227            if (options.delayIn === 0) {
228                tipsy.show();
229            } else {
230                tipsy.fixTitle();
231                setTimeout(function() {
232                    if (tipsy.hoverState == 'in' && isElementInDOM(tipsy.$element)) {
233                        tipsy.show();
234                    }
235                }, options.delayIn);
236            }
237        }
238
239        function leave() {
240            var tipsy = get(this);
241            tipsy.hoverState = 'out';
242            if (options.delayOut === 0) {
243                tipsy.hide();
244            } else {
245                setTimeout(function() { if (tipsy.hoverState == 'out' || !tipsy.$element || !tipsy.$element.is(':visible')) tipsy.hide(); }, options.delayOut);
246            }
247        }
248
249        if (!options.live) this.each(function() { get(this); });
250
251        if (options.trigger != 'manual') {
252            // one of the biggest changes from 1.0.0a: by default, bind to BOTH mouseenter/mouseleave AND focus/blur
253            // this ensures out-of-the-box keyboard accessibility, showing tooltips when users TAB to a (focusable) element
254            // sites that still use 'hover' will also get this new interactive behavior automatically, only those who
255            // explicitly set 'focus' will only get focus/blur (for forms, for instance)
256
257            if (options.live && options.live !== true) {
258                if (options.trigger != 'focus') {
259                    $(this).on('mouseenter', options.live, enter);
260                    $(this).on('mouseleave', options.live, leave);
261                }
262                if (options.trigger != 'blur') {
263                    $(this).on('focus', options.live, enter);
264                    $(this).on('blur', options.live, leave);
265                }
266            } else {
267                if (options.live && !$.live) {
268                    //live === true and using jQuery >= 1.9
269                    throw "Since jQuery 1.9, pass selector as live argument. eg. $(document).tipsy({live: 'a.live'});";
270                }
271                var binder = options.live ? 'live' : 'bind';
272                if (options.trigger != 'focus') {
273                    this[binder]('mouseenter', enter)[binder]('mouseleave', leave);
274                }
275                if (options.trigger != 'blur') {
276                    this[binder]('focus', enter)[binder]('blur', leave);
277                }
278            }
279        }
280
281        return this;
282
283    };
284
285    $.fn.tipsy.defaults = {
286        aria: false,
287        className: null,
288        id: 'tipsy',
289        delayIn: 0,
290        delayOut: 0,
291        fade: false,
292        fadeInTime: 400,
293        fadeOutTime: 400, 
294        shadow: false,
295        shadowBlur: 8,
296        shadowOpacity: 1,
297        shadowSpread: 0,
298        fallback: '',
299        gravity: 'n',
300        html: false,
301        live: false,
302        offset: 0,
303        opacity: 0.8,
304        title: 'title',
305        trigger: 'interactive',
306        theme: ''
307    };
308
309    $.fn.tipsy.revalidate = function() {
310      $('.tipsy').each(function() {
311        var pointee = $.data(this, 'tipsy-pointee');
312        if (!pointee || !isElementInDOM(pointee)) {
313          $(this).remove();
314        }
315      });
316    };
317
318    $.fn.tipsy.enable = function() {
319        $.fn.tipsy.enabled = true;
320    };
321
322    $.fn.tipsy.disable = function() {
323        $.fn.tipsy.enabled = false;
324    };
325
326    // Overwrite this method to provide options on a per-element basis.
327    // For example, you could store the gravity in a 'tipsy-gravity' attribute:
328    // return $.extend({}, options, {gravity: $(ele).prop('tipsy-gravity') || 'n' });
329    // (remember - do not modify 'options' in place!)
330    $.fn.tipsy.elementOptions = function(ele, options) {
331        return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
332    };
333
334    $.fn.tipsy.autoNS = function() {
335        return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
336    };
337
338    $.fn.tipsy.autoWE = function() {
339        return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
340    };
341
342    $.fn.tipsy.autoNWNE = function() {
343        return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'ne' : 'nw';
344    };
345
346    $.fn.tipsy.autoSWSE = function() {
347        return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'se' : 'sw';
348    };
349
350    /**
351     * yields a closure of the supplied parameters, producing a function that takes
352     * no arguments and is suitable for use as an autogravity function like so:
353     *
354     * @param marginNorth (int) - distance from the viewable region top edge that an
355     *        element should be before setting its tooltip's gravity to be away
356     *        from that edge.
357     * @param marginEast (int) - distance from the viewable region right edge that an
358     *        element should be before setting its tooltip's gravity to be away
359     *        from that edge.
360     * @param prefer (string, e.g. 'n', 'sw', 'w') - the direction to prefer
361     *        if there are no viewable region edges effecting the tooltip's
362     *        gravity. It will try to vary from this minimally, for example,
363     *        if 'sw' is preferred and an element is near the right viewable
364     *        region edge, but not the top edge, it will set the gravity for
365     *        that element's tooltip to be 'se', preserving the southern
366     *        component.
367     */
368    $.fn.tipsy.autoBounds = function(marginNorth, marginEast, prefer) {
369        return function() {
370            var dir = {ns: prefer[0], ew: (prefer.length > 1 ? prefer[1] : false)},
371                boundTop = $(document).scrollTop() + marginNorth,
372                boundLeft = $(document).scrollLeft() + marginEast,
373                $this = $(this);
374
375            if ($this.offset().top < boundTop) dir.ns = 'n';
376            if ($this.offset().left < boundLeft) dir.ew = 'w';
377            if ($(window).width() + $(document).scrollLeft() - $this.offset().left < marginEast) dir.ew = 'e';
378            if ($(window).height() + $(document).scrollTop() - $this.offset().top < marginNorth) dir.ns = 's';
379
380            return dir.ns + (dir.ew ? dir.ew : '');
381        };
382    };
383
384    /**
385     * Improved version of autoBounds for automatic placement of chunky tips
386     * The original autoBounds failed in two regards: 1. it would never return a 'w' or 'e', gravity even if they
387     * were preferred and/or optimal, 2. it only respected the margin between the left hand side of an element and
388     * left hand side of the viewport, and the top of an element and the top of the viewport. This version checks
389     * to see if the bottom of an element is too close to the bottom of the screen, similarly for the right hand side
390     */
391    $.fn.tipsy.autoBounds2 = function(margin, prefer) {
392        return function() {
393            var dir = {},
394                boundTop = $(document).scrollTop() + margin,
395                boundLeft = $(document).scrollLeft() + margin,
396                $this = $(this);
397
398            // bi-directional string (ne, se, sw, etc...)
399            if (prefer.length > 1) {
400                dir.ns = prefer[0];
401                dir.ew = prefer[1];
402            } else {
403                // single direction string (e, w, n or s)
404                if (prefer[0] == 'e' || prefer[0] == 'w') {
405                    dir.ew = prefer[0];
406                } else {
407                    dir.ns = prefer[0];
408                }
409            }
410
411            if ($this.offset().top < boundTop) dir.ns = 'n';
412            if ($this.offset().left < boundLeft) dir.ew = 'w';
413            if ($(window).width() + $(document).scrollLeft() - ($this.offset().left + $this.width()) < margin) dir.ew = 'e';
414            if ($(window).height() + $(document).scrollTop() - ($this.offset().top + $this.height()) < margin) dir.ns = 's';
415
416            if (dir.ns) {
417                return dir.ns + (dir.ew ? dir.ew : '');
418            }
419            return dir.ew;
420        }
421    };
422    
423})(jQuery, window);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.