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