PageSourceSearch

https://sbschool.org/js/jquery/jquery.tipsy/jquery.tipsy.js

js sbschool.org collected 2026-10-03 21:38:38 UTC 9,302 bytes, 243 lines download raw bytes

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

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.