jquery.tooltips.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * jQuery Foundation Tooltip Plugin 2.0.1
  3. * http://foundation.zurb.com
  4. * Copyright 2012, ZURB
  5. * Free to use under the MIT license.
  6. * http://www.opensource.org/licenses/mit-license.php
  7. */
  8. ;(function($) {
  9. 'use strict';
  10. var settings = {
  11. bodyHeight : 0,
  12. targetClass : '.has-tip',
  13. tooltipClass : '.tooltip',
  14. tipTemplate : function (selector, content) {
  15. return '<span data-selector="' + selector + '" class="' + settings.tooltipClass.substring(1) + '">' + content + '<span class="nub"></span></span>';
  16. }
  17. },
  18. methods = {
  19. init : function (options) {
  20. return this.each(function () {
  21. var $body = $('body'),
  22. self = this;
  23. if (Modernizr.touch) {
  24. $body.on('click.tooltip touchstart.tooltip touchend.tooltip', settings.targetClass, function (e) {
  25. e.preventDefault();
  26. var $this = $(this);
  27. $(settings.tooltipClass).hide();
  28. methods.showOrCreateTip($this);
  29. });
  30. $(settings.tooltipClass).on('click.tooltip touchstart.tooltip touchend.tooltip', function (e) {
  31. e.preventDefault();
  32. $(this).fadeOut(150);
  33. });
  34. } else {
  35. $body.on('mouseover.tooltip mouseout.tooltip', settings.targetClass, function (e) {
  36. var $this = $(this);
  37. if (e.type === 'mouseover') {
  38. methods.showOrCreateTip($this);
  39. } else if (e.type === 'mouseout') {
  40. methods.hide($this);
  41. }
  42. });
  43. }
  44. });
  45. },
  46. showOrCreateTip : function ($target) {
  47. var $tip = methods.getTip($target);
  48. if ($tip && $tip.length > 0) {
  49. methods.show($target);
  50. } else {
  51. methods.create($target);
  52. }
  53. },
  54. getTip : function ($target) {
  55. var selector = methods.selector($target),
  56. tip = null;
  57. if (selector) tip = $('span[data-selector=' + selector + ']' + settings.tooltipClass);
  58. return (tip) ? tip : false;
  59. },
  60. selector : function ($target) {
  61. var id = $target.attr('id'),
  62. dataSelector = $target.data('selector');
  63. if (id === undefined && dataSelector === undefined) {
  64. dataSelector = 'tooltip' + Math.random().toString(36).substring(7);
  65. $target.attr('data-selector', dataSelector);
  66. }
  67. return (id) ? id : dataSelector;
  68. },
  69. create : function ($target) {
  70. var $tip = $(settings.tipTemplate(methods.selector($target), $target.attr('title'))),
  71. classes = methods.inheritable_classes($target);
  72. $tip.addClass(classes).appendTo('body');
  73. if (Modernizr.touch) $tip.append('<span class="tap-to-close">tap to close </span>');
  74. $target.removeAttr('title');
  75. methods.show($target);
  76. },
  77. reposition : function (target, tip, classes) {
  78. var width, nub, nubHeight, nubWidth, row, objPos;
  79. tip.css('visibility', 'hidden').show();
  80. width = target.data('width');
  81. nub = tip.children('.nub');
  82. nubHeight = nub.outerHeight();
  83. nubWidth = nub.outerWidth();
  84. objPos = function (obj, top, right, bottom, left, width) {
  85. return obj.css({
  86. 'top' : top,
  87. 'bottom' : bottom,
  88. 'left' : left,
  89. 'right' : right,
  90. 'width' : (width) ? width : 'auto'
  91. }).end();
  92. };
  93. objPos(tip, (target.offset().top + target.outerHeight() + 10), 'auto', 'auto', target.offset().left, width);
  94. objPos(nub, -nubHeight, 'auto', 'auto', 10);
  95. if ($(window).width() < 767) {
  96. row = target.parents('.row');
  97. tip.width(row.outerWidth() - 20).css('left', row.offset().left).addClass('tip-override');
  98. objPos(nub, -nubHeight, 'auto', 'auto', target.offset().left);
  99. } else {
  100. if (classes.indexOf('tip-top') > -1) {
  101. objPos(tip, (target.offset().top - tip.outerHeight() - nubHeight), 'auto', 'auto', target.offset().left, width)
  102. .removeClass('tip-override');
  103. objPos(nub, 'auto', 'auto', -nubHeight, 'auto');
  104. } else if (classes.indexOf('tip-left') > -1) {
  105. objPos(tip, (target.offset().top + (target.outerHeight() / 2) - nubHeight), 'auto', 'auto', (target.offset().left - tip.outerWidth() - 10), width)
  106. .removeClass('tip-override');
  107. objPos(nub, (tip.outerHeight() / 2) - (nubHeight / 2), -nubHeight, 'auto', 'auto');
  108. } else if (classes.indexOf('tip-right') > -1) {
  109. objPos(tip, (target.offset().top + (target.outerHeight() / 2) - nubHeight), 'auto', 'auto', (target.offset().left + target.outerWidth() + 10), width)
  110. .removeClass('tip-override');
  111. objPos(nub, (tip.outerHeight() / 2) - (nubHeight / 2), 'auto', 'auto', -nubHeight);
  112. }
  113. }
  114. tip.css('visibility', 'visible').hide();
  115. },
  116. inheritable_classes : function (target) {
  117. var inheritables = ['tip-top', 'tip-left', 'tip-bottom', 'tip-right', 'noradius'],
  118. filtered = target.attr('class').split(' ').map(function (el, i) {
  119. if ($.inArray(el, inheritables) !== -1) {
  120. return el;
  121. }
  122. }).join(' ');
  123. return $.trim(filtered);
  124. },
  125. show : function ($target) {
  126. var $tip = methods.getTip($target);
  127. methods.reposition($target, $tip, $target.attr('class'));
  128. $tip.fadeIn(150);
  129. },
  130. hide : function ($target) {
  131. var $tip = methods.getTip($target);
  132. $tip.fadeOut(150);
  133. },
  134. reload : function () {
  135. var $self = $(this);
  136. return ($self.data('tooltips')) ? $self.tooltips('destroy').tooltips('init') : $self.tooltips('init');
  137. },
  138. destroy : function () {
  139. return this.each(function () {
  140. $(window).off('.tooltip');
  141. $(settings.targetClass).off('.tooltip');
  142. $(settings.tooltipClass).each(function(i) {
  143. $($(settings.targetClass).get(i)).attr('title', $(this).text());
  144. }).remove();
  145. });
  146. }
  147. };
  148. $.fn.tooltips = function (method) {
  149. if (methods[method]) {
  150. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  151. } else if (typeof method === 'object' || ! method) {
  152. return methods.init.apply(this, arguments);
  153. } else {
  154. $.error('Method ' + method + ' does not exist on jQuery.tooltips');
  155. }
  156. };
  157. })(jQuery);