foundation.tooltip.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ;(function ($, window, document, undefined) {
  2. 'use strict';
  3. Foundation.libs.tooltip = {
  4. name : 'tooltip',
  5. version : '5.1.1',
  6. settings : {
  7. additional_inheritable_classes : [],
  8. tooltip_class : '.tooltip',
  9. append_to: 'body',
  10. touch_close_text: 'Tap To Close',
  11. disable_for_touch: false,
  12. hover_delay: 200,
  13. tip_template : function (selector, content) {
  14. return '<span data-selector="' + selector + '" class="'
  15. + Foundation.libs.tooltip.settings.tooltip_class.substring(1)
  16. + '">' + content + '<span class="nub"></span></span>';
  17. }
  18. },
  19. cache : {},
  20. init : function (scope, method, options) {
  21. Foundation.inherit(this, 'random_str');
  22. this.bindings(method, options);
  23. },
  24. events : function () {
  25. var self = this,
  26. S = self.S;
  27. if (Modernizr.touch) {
  28. S(document)
  29. .off('.tooltip')
  30. .on('click.fndtn.tooltip touchstart.fndtn.tooltip touchend.fndtn.tooltip',
  31. '[' + this.attr_name() + ']:not(a)', function (e) {
  32. var settings = $.extend({}, self.settings, self.data_options(S(this)));
  33. if (!settings.disable_for_touch) {
  34. e.preventDefault();
  35. S(settings.tooltip_class).hide();
  36. self.showOrCreateTip(S(this));
  37. }
  38. })
  39. .on('click.fndtn.tooltip touchstart.fndtn.tooltip touchend.fndtn.tooltip',
  40. this.settings.tooltip_class, function (e) {
  41. e.preventDefault();
  42. S(this).fadeOut(150);
  43. });
  44. } else {
  45. S(document)
  46. .off('.tooltip')
  47. .on('mouseenter.fndtn.tooltip mouseleave.fndtn.tooltip',
  48. '[' + this.attr_name() + ']', function (e) {
  49. var $this = S(this);
  50. if (/enter|over/i.test(e.type)) {
  51. this.timer = setTimeout(function () {
  52. var tip = self.showOrCreateTip($this);
  53. }.bind(this), self.settings.hover_delay);
  54. } else if (e.type === 'mouseout' || e.type === 'mouseleave') {
  55. clearTimeout(this.timer);
  56. self.hide($this);
  57. }
  58. });
  59. }
  60. },
  61. showOrCreateTip : function ($target) {
  62. var $tip = this.getTip($target);
  63. if ($tip && $tip.length > 0) {
  64. return this.show($target);
  65. }
  66. return this.create($target);
  67. },
  68. getTip : function ($target) {
  69. var selector = this.selector($target),
  70. tip = null;
  71. if (selector) {
  72. tip = this.S('span[data-selector="' + selector + '"]' + this.settings.tooltip_class);
  73. }
  74. return (typeof tip === 'object') ? tip : false;
  75. },
  76. selector : function ($target) {
  77. var id = $target.attr('id'),
  78. dataSelector = $target.attr(this.attr_name()) || $target.attr('data-selector');
  79. if ((id && id.length < 1 || !id) && typeof dataSelector != 'string') {
  80. dataSelector = 'tooltip' + this.random_str(6);
  81. $target.attr('data-selector', dataSelector);
  82. }
  83. return (id && id.length > 0) ? id : dataSelector;
  84. },
  85. create : function ($target) {
  86. var $tip = $(this.settings.tip_template(this.selector($target), $('<div></div>').html($target.attr('title')).html())),
  87. classes = this.inheritable_classes($target);
  88. $tip.addClass(classes).appendTo(this.settings.append_to);
  89. if (Modernizr.touch) {
  90. $tip.append('<span class="tap-to-close">'+this.settings.touch_close_text+'</span>');
  91. }
  92. $target.removeAttr('title').attr('title','');
  93. this.show($target);
  94. },
  95. reposition : function (target, tip, classes) {
  96. var width, nub, nubHeight, nubWidth, column, objPos;
  97. tip.css('visibility', 'hidden').show();
  98. width = target.data('width');
  99. nub = tip.children('.nub');
  100. nubHeight = nub.outerHeight();
  101. nubWidth = nub.outerHeight();
  102. if(this.small()) {
  103. tip.css({'width' : '100%' });
  104. } else {
  105. tip.css({'width' : (width) ? width : 'auto'});
  106. }
  107. objPos = function (obj, top, right, bottom, left, width) {
  108. return obj.css({
  109. 'top' : (top) ? top : 'auto',
  110. 'bottom' : (bottom) ? bottom : 'auto',
  111. 'left' : (left) ? left : 'auto',
  112. 'right' : (right) ? right : 'auto'
  113. }).end();
  114. };
  115. objPos(tip, (target.offset().top + target.outerHeight() + 10), 'auto', 'auto', target.offset().left);
  116. if (this.small()) {
  117. objPos(tip, (target.offset().top + target.outerHeight() + 10), 'auto', 'auto', 12.5, this.S(this.scope).width());
  118. tip.addClass('tip-override');
  119. objPos(nub, -nubHeight, 'auto', 'auto', target.offset().left + 10);
  120. } else {
  121. var left = target.offset().left;
  122. if (Foundation.rtl) {
  123. left = target.offset().left + target.outerWidth() - tip.outerWidth();
  124. }
  125. objPos(tip, (target.offset().top + target.outerHeight() + 10), 'auto', 'auto', left);
  126. tip.removeClass('tip-override');
  127. nub.removeAttr( 'style' );
  128. if (classes && classes.indexOf('tip-top') > -1) {
  129. objPos(tip, (target.offset().top - tip.outerHeight() - 10), 'auto', 'auto', left)
  130. .removeClass('tip-override');
  131. } else if (classes && classes.indexOf('tip-left') > -1) {
  132. objPos(tip, (target.offset().top + (target.outerHeight() / 2) - (tip.outerHeight() / 2)), 'auto', 'auto', (target.offset().left - tip.outerWidth() - nubHeight))
  133. .removeClass('tip-override');
  134. } else if (classes && classes.indexOf('tip-right') > -1) {
  135. objPos(tip, (target.offset().top + (target.outerHeight() / 2) - (tip.outerHeight() / 2)), 'auto', 'auto', (target.offset().left + target.outerWidth() + nubHeight))
  136. .removeClass('tip-override');
  137. }
  138. }
  139. tip.css('visibility', 'visible').hide();
  140. },
  141. small : function () {
  142. return matchMedia(Foundation.media_queries.small).matches;
  143. },
  144. inheritable_classes : function (target) {
  145. var inheritables = ['tip-top', 'tip-left', 'tip-bottom', 'tip-right', 'radius', 'round'].concat(this.settings.additional_inheritable_classes),
  146. classes = target.attr('class'),
  147. filtered = classes ? $.map(classes.split(' '), function (el, i) {
  148. if ($.inArray(el, inheritables) !== -1) {
  149. return el;
  150. }
  151. }).join(' ') : '';
  152. return $.trim(filtered);
  153. },
  154. show : function ($target) {
  155. var $tip = this.getTip($target);
  156. this.reposition($target, $tip, $target.attr('class'));
  157. return $tip.fadeIn(150);
  158. },
  159. hide : function ($target) {
  160. var $tip = this.getTip($target);
  161. return $tip.fadeOut(150);
  162. },
  163. // deprecate reload
  164. reload : function () {
  165. var $self = $(this);
  166. return ($self.data('fndtn-tooltips')) ? $self.foundationTooltips('destroy').foundationTooltips('init') : $self.foundationTooltips('init');
  167. },
  168. off : function () {
  169. this.S(this.scope).off('.fndtn.tooltip');
  170. this.S(this.settings.tooltip_class).each(function (i) {
  171. $('[' + this.attr_name() + ']').get(i).attr('title', $(this).text());
  172. }).remove();
  173. },
  174. reflow : function () {}
  175. };
  176. }(jQuery, this, this.document));