tipsy.js 11 KB

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