jquery.tipsy.js 9.6 KB

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