/modules/mod_base/lib/js/modules/z.tooltip.js

https://code.google.com/p/zotonic/ · JavaScript · 100 lines · 65 code · 14 blank · 21 comment · 3 complexity · 15d8b4581d8007589aed65c40b7851e0 MD5 · raw file

  1. /* tooltip js
  2. ----------------------------------------------------------
  3. @package: Zotonic 2009
  4. @Author: Tim Benniks <tim@timbenniks.nl>
  5. Copyright 2009 Tim Benniks
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. ---------------------------------------------------------- */
  16. $.widget("ui.tooltip",
  17. {
  18. _init: function()
  19. {
  20. var self = this;
  21. var obj = this.element;
  22. var tip = undefined;
  23. if(this.title == '')
  24. {
  25. obj.unbind(self.options.inevent, self.options.outevent);
  26. return false;
  27. }
  28. obj.bind(self.options.inevent, function(e)
  29. {
  30. this.tip = this.title;
  31. var tip_content = this.tip;
  32. this.title = "";
  33. tip = $('<div></div>')
  34. .addClass('tooltip')
  35. .html(tip_content)
  36. .css({top: e.pageY + self.options.offsetY, left: e.pageX + self.options.offsetX, width: self.options.width, maxWidth: self.options.maxwidth });
  37. $(document.body).append(tip);
  38. var left = $(this).position().left;
  39. var top = $(this).position().top;
  40. tip.css({top: top - 30});
  41. if(left + tip.width() > $(window).width())
  42. {
  43. tip.css({left: Math.ceil(left) - Math.ceil((obj.width() / 2)) - Math.ceil((tip.width() / 2 ))});
  44. }
  45. else
  46. {
  47. tip.css({left: Math.ceil(left) + Math.ceil((obj.width() / 2)) - Math.ceil((tip.width() / 2 ))});
  48. }
  49. tip.stop().animate({opacity: 'show'}, 200);
  50. });
  51. obj.bind(self.options.outevent, function(e)
  52. {
  53. tip.stop().animate({opacity: 'show'}, 200, function()
  54. {
  55. $(this).remove();
  56. });
  57. self.destroy();
  58. this.title = this.tip;
  59. });
  60. },
  61. destroy: function()
  62. {
  63. var self = this;
  64. var obj = this.element;
  65. obj.unbind(self.options.inevent, self.options.outevent);
  66. $(document).unbind('mousemove');
  67. }
  68. });
  69. $.fn.tooltip.destroy = function()
  70. {
  71. $('.tooltip').tooltip('destroy');
  72. }
  73. $.ui.tooltip.defaults = {
  74. offsetY: 0,
  75. offsetX: 0,
  76. inevent: 'mouseover',
  77. outevent: 'mouseout',
  78. width: 'auto',
  79. maxwidth: '330px'
  80. }