Nenhuma Descrição

um-tipsy.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. extra_gap = 0;
  49. if ( $('body').css('position').length > 0 && $('body').css('position') == 'relative' ) {
  50. if ( $('#wpadminbar').length ) {
  51. extra_gap = $('#wpadminbar').height();
  52. }
  53. }
  54. tp = {top: pos.top + pos.height / 2 - actualHeight / 2 - extra_gap, left: pos.left + pos.width + this.options.offset};
  55. break;
  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. $tip.css(tp).addClass('tipsy-' + gravity);
  65. $tip.find('.tipsy-arrow')[0].className = 'tipsy-arrow tipsy-arrow-' + gravity.charAt(0);
  66. if (this.options.className) {
  67. $tip.addClass(maybeCall(this.options.className, this.$element[0]));
  68. }
  69. if (this.options.fade) {
  70. $tip.stop().css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: this.options.opacity});
  71. } else {
  72. $tip.css({visibility: 'visible', opacity: this.options.opacity});
  73. }
  74. }
  75. },
  76. hide: function() {
  77. if (this.options.fade) {
  78. this.tip().stop().fadeOut(function() { $(this).remove(); });
  79. } else {
  80. this.tip().remove();
  81. }
  82. },
  83. fixTitle: function() {
  84. var $e = this.$element;
  85. if ($e.attr('title') || typeof($e.attr('original-title')) != 'string') {
  86. $e.attr('original-title', $e.attr('title') || '').removeAttr('title');
  87. }
  88. },
  89. getTitle: function() {
  90. var title, $e = this.$element, o = this.options;
  91. this.fixTitle();
  92. var title, o = this.options;
  93. if (typeof o.title == 'string') {
  94. title = $e.attr(o.title == 'title' ? 'original-title' : o.title);
  95. } else if (typeof o.title == 'function') {
  96. title = o.title.call($e[0]);
  97. }
  98. title = ('' + title).replace(/(^\s*|\s*$)/, "");
  99. return title || o.fallback;
  100. },
  101. tip: function() {
  102. if (!this.$tip) {
  103. this.$tip = $('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"></div>');
  104. this.$tip.data('tipsy-pointee', this.$element[0]);
  105. }
  106. return this.$tip;
  107. },
  108. validate: function() {
  109. if (!this.$element[0].parentNode) {
  110. this.hide();
  111. this.$element = null;
  112. this.options = null;
  113. }
  114. },
  115. enable: function() { this.enabled = true; },
  116. disable: function() { this.enabled = false; },
  117. toggleEnabled: function() { this.enabled = !this.enabled; }
  118. };
  119. $.fn.tipsy = function(options) {
  120. if (options === true) {
  121. return this.data('tipsy');
  122. } else if (typeof options == 'string') {
  123. var tipsy = this.data('tipsy');
  124. if (tipsy) tipsy[options]();
  125. return this;
  126. }
  127. options = $.extend({}, $.fn.tipsy.defaults, options);
  128. function get(ele) {
  129. var tipsy = $.data(ele, 'tipsy');
  130. if (!tipsy) {
  131. tipsy = new Tipsy(ele, $.fn.tipsy.elementOptions(ele, options));
  132. $.data(ele, 'tipsy', tipsy);
  133. }
  134. return tipsy;
  135. }
  136. function enter() {
  137. var tipsy = get(this);
  138. tipsy.hoverState = 'in';
  139. if (options.delayIn == 0) {
  140. tipsy.show();
  141. } else {
  142. tipsy.fixTitle();
  143. setTimeout(function() { if (tipsy.hoverState == 'in') tipsy.show(); }, options.delayIn);
  144. }
  145. };
  146. function leave() {
  147. var tipsy = get(this);
  148. tipsy.hoverState = 'out';
  149. if (options.delayOut == 0) {
  150. tipsy.hide();
  151. } else {
  152. setTimeout(function() { if (tipsy.hoverState == 'out') tipsy.hide(); }, options.delayOut);
  153. }
  154. };
  155. if (!options.on) this.each(function() { get(this); });
  156. if (options.trigger != 'manual') {
  157. var eventIn = options.trigger == 'hover' ? 'mouseenter' : 'focus',
  158. eventOut = options.trigger == 'hover' ? 'mouseleave' : 'blur';
  159. if (options.on)
  160. $(document).on(eventIn, this.selector, enter).on(eventOut, this.selector, leave);
  161. else
  162. this.on(eventIn, enter).on(eventOut, leave);
  163. }
  164. return this;
  165. };
  166. $.fn.tipsy.defaults = {
  167. className: null,
  168. delayIn: 0,
  169. delayOut: 0,
  170. fade: false,
  171. fallback: '',
  172. gravity: 'n',
  173. html: false,
  174. live: false,
  175. offset: 0,
  176. opacity: 0.8,
  177. title: 'title',
  178. trigger: 'hover'
  179. };
  180. $.fn.tipsy.revalidate = function() {
  181. $('.tipsy').each(function() {
  182. var pointee = $.data(this, 'tipsy-pointee');
  183. if (!pointee || !isElementInDOM(pointee)) {
  184. $(this).remove();
  185. }
  186. });
  187. };
  188. // Overwrite this method to provide options on a per-element basis.
  189. // For example, you could store the gravity in a 'tipsy-gravity' attribute:
  190. // return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' });
  191. // (remember - do not modify 'options' in place!)
  192. $.fn.tipsy.elementOptions = function(ele, options) {
  193. return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
  194. };
  195. $.fn.tipsy.autoNS = function() {
  196. return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
  197. };
  198. $.fn.tipsy.autoWE = function() {
  199. return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
  200. };
  201. /**
  202. * yields a closure of the supplied parameters, producing a function that takes
  203. * no arguments and is suitable for use as an autogravity function like so:
  204. *
  205. * @param margin (int) - distance from the viewable region edge that an
  206. * element should be before setting its tooltip's gravity to be away
  207. * from that edge.
  208. * @param prefer (string, e.g. 'n', 'sw', 'w') - the direction to prefer
  209. * if there are no viewable region edges effecting the tooltip's
  210. * gravity. It will try to vary from this minimally, for example,
  211. * if 'sw' is preferred and an element is near the right viewable
  212. * region edge, but not the top edge, it will set the gravity for
  213. * that element's tooltip to be 'se', preserving the southern
  214. * component.
  215. */
  216. $.fn.tipsy.autoBounds = function(margin, prefer) {
  217. return function() {
  218. var dir = {ns: prefer[0], ew: (prefer.length > 1 ? prefer[1] : false)},
  219. boundTop = $(document).scrollTop() + margin,
  220. boundLeft = $(document).scrollLeft() + margin,
  221. $this = $(this);
  222. if ($this.offset().top < boundTop) dir.ns = 'n';
  223. if ($this.offset().left < boundLeft) dir.ew = 'w';
  224. if ($(window).width() + $(document).scrollLeft() - $this.offset().left < margin) dir.ew = 'e';
  225. if ($(window).height() + $(document).scrollTop() - $this.offset().top < margin) dir.ns = 's';
  226. return dir.ns + (dir.ew ? dir.ew : '');
  227. }
  228. };
  229. })(jQuery);