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