Neeve

ash - java 3

Aug 28th, 2015
2,750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. // tipsy, facebook style tooltips for jquery
  2. // version 1.0.0a
  3. // (c) 2008-2010 jason frame [[email protected]]
  4. // releated under the MIT license
  5. (function($) {
  6.  
  7. function fixTitle($ele) {
  8. if ($ele.attr('title') || typeof($ele.attr('original-title')) != 'string') {
  9. $ele.attr('original-title', $ele.attr('title') || '').removeAttr('title');
  10. }
  11. }
  12.  
  13. function Tipsy(element, options) {
  14. this.$element = $(element);
  15. this.options = options;
  16. this.enabled = true;
  17. fixTitle(this.$element);
  18. }
  19.  
  20. Tipsy.prototype = {
  21. show: function() {
  22. var title = this.getTitle();
  23. if (title && this.enabled) {
  24. var $tip = this.tip();
  25.  
  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'}).appendTo(document.body);
  29.  
  30. var pos = $.extend({}, this.$element.offset(), {
  31. width: this.$element[0].offsetWidth,
  32. height: this.$element[0].offsetHeight
  33. });
  34.  
  35. var actualWidth = $tip[0].offsetWidth, actualHeight = $tip[0].offsetHeight;
  36. var gravity = (typeof this.options.gravity == 'function')
  37. ? this.options.gravity.call(this.$element[0])
  38. : this.options.gravity;
  39.  
  40. var tp;
  41. switch (gravity.charAt(0)) {
  42. case 'n':
  43. tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
  44. break;
  45. case 's':
  46. tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2};
  47. break;
  48. case 'e':
  49. tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset};
  50. break;
  51. case 'w':
  52. tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset};
  53. break;
  54. }
  55.  
  56. if (gravity.length == 2) {
  57. if (gravity.charAt(1) == 'w') {
  58. tp.left = pos.left + pos.width / 2 - 15;
  59. } else {
  60. tp.left = pos.left + pos.width / 2 - actualWidth + 15;
  61. }
  62. }
  63.  
  64. $tip.css(tp).addClass('tipsy-' + gravity);
  65.  
  66. if (this.options.fade) {
  67. $tip.stop().css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: this.options.opacity});
  68. } else {
  69. $tip.css({visibility: 'visible', opacity: this.options.opacity});
  70. }
  71. }
  72. },
  73.  
  74. hide: function() {
  75. if (this.options.fade) {
  76. this.tip().stop().fadeOut(function() { $(this).remove(); });
  77. } else {
  78. this.tip().remove();
  79. }
  80. },
  81.  
  82. getTitle: function() {
  83. var title, $e = this.$element, o = this.options;
  84. fixTitle($e);
  85. var title, o = this.options;
  86. if (typeof o.title == 'string') {
  87. title = $e.attr(o.title == 'title' ? 'original-title' : o.title);
  88. } else if (typeof o.title == 'function') {
  89. title = o.title.call($e[0]);
  90. }
  91. title = ('' + title).replace(/(^\s*|\s*$)/, "");
  92. return title || o.fallback;
  93. },
  94.  
  95. tip: function() {
  96. if (!this.$tip) {
  97. this.$tip = $('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"/></div>');
  98. }
  99. return this.$tip;
  100. },
  101.  
  102. validate: function() {
  103. if (!this.$element[0].parentNode) {
  104. this.hide();
  105. this.$element = null;
  106. this.options = null;
  107. }
  108. },
  109.  
  110. enable: function() { this.enabled = true; },
  111. disable: function() { this.enabled = false; },
  112. toggleEnabled: function() { this.enabled = !this.enabled; }
  113. };
  114.  
  115. $.fn.tipsy = function(options) {
  116.  
  117. if (options === true) {
  118. return this.data('tipsy');
  119. } else if (typeof options == 'string') {
  120. return this.data('tipsy')[options]();
  121. }
  122.  
  123. options = $.extend({}, $.fn.tipsy.defaults, options);
  124.  
  125. function get(ele) {
  126. var tipsy = $.data(ele, 'tipsy');
  127. if (!tipsy) {
  128. tipsy = new Tipsy(ele, $.fn.tipsy.elementOptions(ele, options));
  129. $.data(ele, 'tipsy', tipsy);
  130. }
  131. return tipsy;
  132. }
  133.  
  134. function enter() {
  135. var tipsy = get(this);
  136. tipsy.hoverState = 'in';
  137. if (options.delayIn == 0) {
  138. tipsy.show();
  139. } else {
  140. setTimeout(function() { if (tipsy.hoverState == 'in') tipsy.show(); }, options.delayIn);
  141. }
  142. };
  143.  
  144. function leave() {
  145. var tipsy = get(this);
  146. tipsy.hoverState = 'out';
  147. if (options.delayOut == 0) {
  148. tipsy.hide();
  149. } else {
  150. setTimeout(function() { if (tipsy.hoverState == 'out') tipsy.hide(); }, options.delayOut);
  151. }
  152. };
  153.  
  154. if (!options.live) this.each(function() { get(this); });
  155.  
  156. if (options.trigger != 'manual') {
  157. var binder = options.live ? 'live' : 'bind',
  158. eventIn = options.trigger == 'hover' ? 'mouseenter' : 'focus',
  159. eventOut = options.trigger == 'hover' ? 'mouseleave' : 'blur';
  160. this[binder](eventIn, enter)[binder](eventOut, leave);
  161. }
  162.  
  163. return this;
  164.  
  165. };
  166.  
  167. $.fn.tipsy.defaults = {
  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.  
  181. // Overwrite this method to provide options on a per-element basis.
  182. // For example, you could store the gravity in a 'tipsy-gravity' attribute:
  183. // return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' });
  184. // (remember - do not modify 'options' in place!)
  185. $.fn.tipsy.elementOptions = function(ele, options) {
  186. return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
  187. };
  188.  
  189. $.fn.tipsy.autoNS = function() {
  190. return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
  191. };
  192.  
  193. $.fn.tipsy.autoWE = function() {
  194. return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
  195. };
  196.  
  197. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment