1// tipsy, facebook style tooltips for jquery 2// version 1.0.0a 3// (c) 2008-2010 jason frame [[email protected]] 4// releated under the MIT license 5 6(function($) { 7 8 function fixTitle($ele) { 9 if ($ele.attr('title') || typeof($ele.attr('original-title')) != 'string') { 10 $ele.attr('original-title', $ele.attr('title') || '').removeAttr('title'); 11 } 12 } 13 14 function Tipsy(element, options) { 15 this.$element = $(element); 16 this.options = options; 17 this.enabled = true; 18 fixTitle(this.$element); 19 } 20 21 Tipsy.prototype = { 22 show: function() { 23 var title = this.getTitle(); 24 if (title && this.enabled) { 25 var $tip = this.tip(); 26 27 $tip.find('.tipsy-inner')[this.options.html ? 'html' : 'text'](title); 28 $tip[0].className = 'tipsy'; // reset classname in case of dynamic gravity 29 $tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body); 30 31 var pos = $.extend({}, this.$element.offset(), { 32 width: this.$element[0].offsetWidth, 33 height: this.$element[0].offsetHeight 34 }); 35 36 var actualWidth = $tip[0].offsetWidth, actualHeight = $tip[0].offsetHeight; 37 var gravity = (typeof this.options.gravity == 'function') 38 ? this.options.gravity.call(this.$element[0]) 39 : this.options.gravity; 40 41 var tp; 42 switch (gravity.charAt(0)) { 43 case 'n': 44 tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2}; 45 break; 46 case 's': 47 tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2}; 48 break; 49 case 'e': 50 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset}; 51 break; 52 case 'w': 53 tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset}; 54 break; 55 } 56 57 if (gravity.length == 2) { 58 if (gravity.charAt(1) == 'w') { 59 tp.left = pos.left + pos.width / 2 - 15; 60 } else { 61 tp.left = pos.left + pos.width / 2 - actualWidth + 15; 62 } 63 } 64 65 $tip.css(tp).addClass('tipsy-' + gravity); 66 67 if (this.options.fade) { 68 $tip.stop().css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: this.options.opacity}); 69 } else { 70 $tip.css({visibility: 'visible', opacity: this.options.opacity}); 71 } 72 } 73 }, 74 75 hide: function() { 76 if (this.options.fade) { 77 this.tip().stop().fadeOut(function() { $(this).remove(); }); 78 } else { 79 this.tip().remove(); 80 } 81 }, 82 83 getTitle: function() { 84 var title, $e = this.$element, o = this.options; 85 fixTitle($e); 86 var title, o = this.options; 87 if (typeof o.title == 'string') { 88 title = $e.attr(o.title == 'title' ? 'original-title' : o.title); 89 } else if (typeof o.title == 'function') { 90 title = o.title.call($e[0]); 91 } 92 title = ('' + title).replace(/(^\s*|\s*$)/, ""); 93 return title || o.fallback; 94 }, 95 96 tip: function() { 97 if (!this.$tip) { 98 this.$tip = $('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"/></div>'); 99 } 100 return this.$tip; 101 }, 102 103 validate: function() { 104 if (!this.$element[0].parentNode) this.hide(); 105 }, 106 107 enable: function() { this.enabled = true; }, 108 disable: function() { this.enabled = false; }, 109 toggleEnabled: function() { this.enabled = !this.enabled; } 110 }; 111 112 $.fn.tipsy = function(options) { 113 114 if (options === true) { 115 return this.data('tipsy'); 116 } else if (typeof options == 'string') { 117 return this.data('tipsy')[options](); 118 } 119 120 options = $.extend({}, $.fn.tipsy.defaults, options); 121 122 function get(ele) { 123 var tipsy = $.data(ele, 'tipsy'); 124 if (!tipsy) { 125 tipsy = new Tipsy(ele, $.fn.tipsy.elementOptions(ele, options)); 126 $.data(ele, 'tipsy', tipsy); 127 } 128 return tipsy; 129 } 130 131 function enter() { 132 var tipsy = get(this); 133 tipsy.hoverState = 'in'; 134 if (options.delayIn == 0) { 135 tipsy.show(); 136 } else { 137 setTimeout(function() { if (tipsy.hoverState == 'in') tipsy.show(); }, options.delayIn); 138 } 139 }; 140 141 function leave() { 142 var tipsy = get(this); 143 tipsy.hoverState = 'out'; 144 if (options.delayOut == 0) { 145 tipsy.hide(); 146 } else { 147 setTimeout(function() { if (tipsy.hoverState == 'out') tipsy.hide(); }, options.delayOut); 148 } 149 }; 150 151 if (!options.live) this.each(function() { get(this); }); 152 153 if (options.trigger != 'manual') { 154 var binder = options.live ? 'live' : 'bind', 155 eventIn = options.trigger == 'hover' ? 'mouseenter' : 'focus', 156 eventOut = options.trigger == 'hover' ? 'mouseleave' : 'blur'; 157 this[binder](eventIn, enter)[binder](eventOut, leave); 158 } 159 160 return this; 161 162 }; 163 164 $.fn.tipsy.defaults = { 165 delayIn: 0, 166 delayOut: 0, 167 fade: false, 168 fallback: '', 169 gravity: 'n', 170 html: false, 171 live: false, 172 offset: 0, 173 opacity: 0.8, 174 title: 'title', 175 trigger: 'hover' 176 }; 177 178 // Overwrite this method to provide options on a per-element basis. 179 // For example, you could store the gravity in a 'tipsy-gravity' attribute: 180 // return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' }); 181 // (remember - do not modify 'options' in place!) 182 $.fn.tipsy.elementOptions = function(ele, options) { 183 return $.metadata ? $.extend({}, options, $(ele).metadata()) : options; 184 }; 185 186 $.fn.tipsy.autoNS = function() { 187 return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n'; 188 }; 189 190 $.fn.tipsy.autoWE = function() { 191 return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w'; 192 }; 193 194})(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.