tooltip.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*##############################################################################
  2. # Copyright (C) 2011 HPCC Systems.
  3. #
  4. # All rights reserved. This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ############################################################################## */
  17. var tooltipDiv = null;
  18. var tooltipPopup = null;
  19. var tooltipX = 0;
  20. var tooltipY = 0;
  21. var tooltipSrcObj = null;
  22. var tooltipCaptionColor = "#6699FF"; //"#808080";
  23. var tooltipBodyColor = "#FFFF99"; //"#C0C0C0";
  24. var tooltipBodyTextColor = "black";
  25. var tooltipMaxWidth = screen.width;
  26. function EnterContent(tooltipDivId, TTitle, TContent, nowrap, align)
  27. {
  28. var div = document.all[tooltipDivId];
  29. if (!div)
  30. return;
  31. var s = [];
  32. s.push('<table border="0" cellspacing="0" cellpadding="2px 0px 2px 0px" style="font:10pt serif; text-align:center; vertical-align:center"');
  33. if (nowrap)
  34. s.push(' width="100%"');
  35. s.push('>');
  36. if (TTitle)
  37. s.push('<tr><td ' + (nowrap ? 'nowrap="true"' : '') +
  38. ' bgcolor="'+tooltipCaptionColor+'">'+ TTitle+'</td></tr>');
  39. if (!align)
  40. align = 'left';
  41. s.push( '<tr><td align="' + align + '" ' + (nowrap ? 'nowrap="true"' : '') +
  42. ' bgcolor="'+tooltipBodyColor+'" style="color:' + tooltipBodyTextColor + '">'+
  43. TContent.replace(new RegExp('\n', 'g'), '<br/>')+
  44. '</td></tr></table>' );
  45. tooltipDiv = div;
  46. div.style.width = 0;//reset width
  47. div.innerHTML = s.join('');
  48. //if the tooltip is too wide then limit it to width defined by tooltipMaxWidth
  49. //
  50. if (tooltipDiv.offsetWidth > tooltipMaxWidth && nowrap)
  51. EnterContent(tooltipDivId, TTitle, TContent, false);
  52. }
  53. function tooltipContextMenu()
  54. {
  55. var srcObj = tooltipSrcObj;
  56. deActivate();
  57. return srcObj ? srcObj.oncontextmenu() : true;
  58. }
  59. function tooltipClick()
  60. {
  61. var srcObj = tooltipSrcObj;
  62. deActivate();
  63. return srcObj ? srcObj.onclick() : true;
  64. }
  65. function Activate(object, x, y)
  66. {
  67. if (tooltipDiv)
  68. {
  69. tooltipPopup=window.createPopup();
  70. var tooltipBody=tooltipPopup.document.body;
  71. //tooltipBody.style.backgroundColor = "yellow";
  72. tooltipBody.style.border = "outset black 1px";
  73. tooltipBody.innerHTML=tooltipDiv.innerHTML;
  74. if (object)
  75. {
  76. tooltipBody.oncontextmenu = tooltipContextMenu;
  77. tooltipBody.onclick = tooltipClick;
  78. }
  79. overhere(object, x, y);
  80. }
  81. }
  82. function overhere(object, x, y)
  83. {
  84. if (tooltipPopup && tooltipDiv)
  85. {
  86. if (object == null)
  87. {
  88. var e = window.event;
  89. if (e == null)
  90. return;
  91. x = e.screenX;
  92. y = e.screenY;
  93. object = tooltipSrcObj;
  94. }
  95. if (x != tooltipX || y != tooltipY || object != tooltipSrcObj)
  96. {
  97. tooltipX = x;
  98. tooltipY = y;
  99. tooltipSrcObj = object;
  100. tooltipPopup.show(tooltipX+5, tooltipY+5, tooltipDiv.offsetWidth+2, tooltipDiv.offsetHeight, object);
  101. }
  102. }
  103. }
  104. function deActivate()
  105. {
  106. if (tooltipPopup)
  107. {
  108. tooltipPopup.hide();
  109. tooltipPopup=null;
  110. tooltipX = tooltipY = 0;
  111. tooltipSrcObj = null;
  112. tooltipDiv = null;
  113. }
  114. }