jquery-tipsy.js 9.1 KB

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