tooltip.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: cogadmin
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2013
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. //
  9. //
  10. // Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  11. // Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  12. //----------------------------------------------------------
  13. // Tooltip
  14. var ui_current_tooltip = null;
  15. function ui_tooltip(tooltipdef) {
  16. this.currentTooltip = null;
  17. this.tooltipdef = tooltipdef;
  18. this.updateableRenderers = new Array();
  19. // show and hide id's for the settimeouts. Allows us to cancel them if needed
  20. this.showTimeoutID = null;
  21. // mouse coordinates of the event
  22. this.x = null;
  23. this.y = null;
  24. if (this.tooltipdef.items) {
  25. this.updateRenderers(this.tooltipdef.items);
  26. }
  27. if (this.tooltipdef.expandedItems) {
  28. this.updateRenderers(this.tooltipdef.expandedItems);
  29. }
  30. }
  31. ui_tooltip.prototype = {
  32. // handles the mouse moving over the tooltip
  33. onmouseover: function(evt) {
  34. // cancel the timeout that would hide the tooltip
  35. if (this.hideTimeoutID != null) {
  36. clearTimeout(this.hideTimeoutID);
  37. this.hideTimeoutID = null;
  38. }
  39. },
  40. // handles the mouse leaving the tooltip
  41. onmouseout: function(evt) {
  42. var self = this;
  43. this.hideTimeoutID = setTimeout(function(){self.hideTooltip();}, 200);
  44. },
  45. // build the tooltip
  46. buildTooltip: function(expand) {
  47. tbody = document.createElement('tbody');
  48. var items = this.tooltipdef.items;
  49. for (var i=0; i < items.length; i++) {
  50. if (tbody.childNodes.length > 0) {
  51. var bufferTR = document.createElement('tr');
  52. tbody.appendChild(bufferTR);
  53. }
  54. if (items[i].render) {
  55. var tr = items[i].render(tbody);
  56. if (tr) {
  57. // add an extra column for the expand link and icon
  58. if (!expand) {
  59. var hasMore = i < items.length-1;
  60. if (hasMore && (i == this.tooltipdef.expandIdx)) {
  61. tr.appendChild(this.createExpandLink(expand));
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. if (this.currentTooltip == null) {
  69. var divTable = document.createElement('div');
  70. var table = document.createElement('table');
  71. var self = this;
  72. EventUtils.addEventListener(divTable, 'mouseover', function(evt) {self.onmouseover(evt);});
  73. EventUtils.addEventListener(divTable, 'mouseout', function(evt) {self.onmouseout(evt);});
  74. divTable.style.display="none";
  75. divTable.className="popup-tooltip";
  76. divTable.border="0";
  77. divTable.appendChild(table);
  78. table.appendChild(tbody);
  79. table.setAttribute("role","presentation");
  80. document.body.appendChild(divTable);
  81. }
  82. else {
  83. divTable = this.currentTooltip;
  84. table = divTable.firstChild;
  85. table.replaceChild(tbody, table.firstChild);
  86. }
  87. return divTable;
  88. },
  89. // create the column that contains the expand or collapse link and icon
  90. createExpandLink: function(hidden) {
  91. var tooltip = '';
  92. var iconSrc = '';
  93. var self = this;
  94. if (this.tooltipdef.expandText) {
  95. tooltip = this.tooltipdef.expandText;
  96. }
  97. td = document.createElement('td');
  98. td.style.whiteSpace = 'nowrap';
  99. if (hidden) {
  100. td.style.visibility="hidden";
  101. }
  102. if (this.tooltipdef.webcontent) {
  103. var a = document.createElement('a');
  104. a.innerHTML=this.tooltipdef.expandText;
  105. a.className="popup-tooltip-a";
  106. EventUtils.addEventListener(a, "click",
  107. function() {
  108. ui_current_tooltip.showTooltip(true);
  109. }
  110. );
  111. a.style.cursor = 'pointer';
  112. if (tooltip != '') {
  113. a.title = tooltip;
  114. }
  115. td.appendChild(a);
  116. }
  117. return td;
  118. },
  119. // gets called from an onmousemove event. Used to update the current mouse position
  120. mousemove: function(e) {
  121. var alreadyVisible = this.currentTooltip ? this.currentTooltip.style.display=="block" : false;
  122. // save the current mouse coord if the tooltip isn't already visible
  123. if (!alreadyVisible) {
  124. this.updateMouseCoord(e);
  125. }
  126. },
  127. // sets a timeout to show the tooltip. Should be called by an onmouseover event
  128. show: function(e) {
  129. // cancel the timeout that would hide the tooltip
  130. if (this.hideTimeoutID != null) {
  131. clearTimeout(this.hideTimeoutID);
  132. this.hideTimeoutID = null;
  133. }
  134. if (!this.currentTooltip || (this.currentTooltip && this.currentTooltip.style.display=="none")) {
  135. // save the current mouse coord
  136. this.updateMouseCoord(e);
  137. if (this.showTimeoutID == null) {
  138. var self = this;
  139. this.showTimeoutID = setTimeout(function(){self.showTooltip(false);}, 1000);
  140. }
  141. cancelBubble(e);
  142. }
  143. },
  144. // display the tooltip
  145. showTooltip: function(expanded) {
  146. // blank out the showTimeoutID since the event already happened
  147. this.showTimeoutID = null;
  148. // build the tooltip
  149. this.currentTooltip = this.buildTooltip(expanded);
  150. // make sure the old tooltip is in fact hidden before showing the new one
  151. if (ui_current_tooltip != null && ui_current_tooltip.currentTooltip.style.display == 'block') {
  152. ui_current_tooltip.currentTooltip.style.display = "none";
  153. }
  154. this.position();
  155. ui_current_tooltip = this;
  156. },
  157. position: function() {
  158. // position the tooltip
  159. this.currentTooltip.style.left=this.x +"px";
  160. this.currentTooltip.style.top=this.y+"px";
  161. this.currentTooltip.style.display="block";
  162. if ((this.x + this.currentTooltip.offsetWidth) > document.body.offsetWidth) {
  163. this.x = xClientWidth() - xWidth(this.currentTooltip);
  164. this.currentTooltip.style.left=this.x + "px"; // make some adjustments
  165. }
  166. if ((this.y + xHeight(this.currentTooltip)) > document.body.offsetHeight) {
  167. this.y = document.body.offsetHeight - xHeight(this.currentTooltip) - xScrollTop();
  168. this.currentTooltip.style.top=this.y + "px"; // make some adjustments
  169. }
  170. },
  171. // Either clears the show settimeout or sets a timeout to hide the tooltip.
  172. // Usually called by an onmouseout event
  173. hide: function() {
  174. if (this.showTimeoutID != null) {
  175. clearTimeout(this.showTimeoutID);
  176. this.showTimeoutID = null;
  177. }
  178. else {
  179. var self = this;
  180. this.hideTimeoutID = setTimeout(function(){self.hideTooltip();}, 200);
  181. }
  182. },
  183. hideTooltip: function() {
  184. if (this.currentTooltip) {
  185. this.currentTooltip.style.display="none";
  186. }
  187. },
  188. updateMouseCoord: function(e) {
  189. var evt = new xEvent(e);
  190. // save all the event information we need
  191. if (!e) {
  192. evt = new xEvent(window.event);
  193. }
  194. var target = evt.target;
  195. if (target) {
  196. this.x = evt.pageX;
  197. this.y = evt.pageY + xHeight(target);
  198. }
  199. },
  200. updateRenderers: function(renderers) {
  201. for (var i = 0; i < renderers.length; i++) {
  202. var renderer = renderers[i];
  203. if (renderer.update) {
  204. this.updateableRenderers[renderer.name] = renderer;
  205. }
  206. }
  207. }
  208. }
  209. function attrValueTooltipRenderer(name, values) {
  210. this.name = name;
  211. this.values = (values instanceof Array) ? values : [values];
  212. this.valueAsNode = null;
  213. }
  214. attrValueTooltipRenderer.prototype = {
  215. // handles the mouse moving over the tooltip
  216. render: function(tbody) {
  217. var nameTR = document.createElement('tr');
  218. var nameTD = document.createElement('td');
  219. nameTD.style.whiteSpace = 'nowrap';
  220. nameTD.className = 'popup-tooltip-label';
  221. nameTD.appendChild(document.createTextNode(this.name));
  222. nameTR.appendChild(nameTD);
  223. var valueTR = null;
  224. for (var i=0; i < this.values.length ;i++) {
  225. valueTR = document.createElement('tr');
  226. var valueTD = document.createElement('td');
  227. valueTD.style.whiteSpace = 'nowrap';
  228. this.valueAsNode = document.createTextNode(this.values[i]);
  229. valueTD.appendChild(this.valueAsNode);
  230. valueTD.style.paddingRight = '10px';
  231. valueTR.appendChild(valueTD);
  232. if (i==0) {
  233. tbody.appendChild(nameTR);
  234. }
  235. tbody.appendChild(valueTR);
  236. }
  237. return valueTR;
  238. },
  239. update: function(value) {
  240. this.value = value;
  241. if (this.valueAsNode) {
  242. this.valueAsNode.nodeValue = this.value;
  243. }
  244. }
  245. }
  246. function captionTooltipRenderer(name, value) {
  247. this.name = name;
  248. this.value = value;
  249. this.valueAsNode = null;
  250. }
  251. captionTooltipRenderer.prototype = {
  252. // handles the mouse moving over the tooltip
  253. render: function(tbody) {
  254. var nameTR = document.createElement('tr');
  255. var nameTD = document.createElement('td');
  256. nameTD.className = 'popup-tooltip-label';
  257. nameTD.appendChild(document.createTextNode(this.name));
  258. nameTR.appendChild(nameTD);
  259. var valueTR = document.createElement('tr');
  260. var valueTD = document.createElement('td');
  261. this.valueAsNode = document.createTextNode(this.value);
  262. valueTD.appendChild(this.valueAsNode);
  263. valueTR.appendChild(valueTD);
  264. tbody.appendChild(nameTR);
  265. tbody.appendChild(valueTR);
  266. return valueTR;
  267. },
  268. update: function(value) {
  269. this.value = value;
  270. if (this.valueAsNode) {
  271. this.valueAsNode.nodeValue = this.value;
  272. }
  273. }
  274. }
  275. function thresholdsTooltipRenderer(fragId, name, resourceID, metricName, value, propertyType, enabled) {
  276. this.name = name;
  277. this.resourceID = resourceID;
  278. this.metricName = metricName;
  279. this.metricValue = value;
  280. this.fragId = fragId;
  281. this.propertyType = propertyType;
  282. this.enabled = enabled;
  283. }
  284. thresholdsTooltipRenderer.prototype = {
  285. // handles the mouse moving over the tooltip
  286. render: function(tbody) {
  287. if (this.enabled) {
  288. var div = document.getElementById("threshDiv");
  289. if (!div) {
  290. div = document.createElement('div');
  291. div.id = "threshDiv";
  292. } else {
  293. while (div.childNodes.length > 0) {
  294. div.removeChild(div.firstChild);
  295. }
  296. }
  297. if (!fragments[this.fragId]) {
  298. createFragment(this.fragId, 'threshDiv', '/cogadmin/controls/threshold.xts');
  299. }
  300. var nameTR = document.createElement('tr');
  301. var nameTD = document.createElement('td');
  302. nameTD.style.whiteSpace = 'nowrap';
  303. nameTD.className = 'popup-tooltip-label';
  304. nameTD.appendChild(document.createTextNode(this.name + ':'));
  305. nameTR.appendChild(nameTD);
  306. var valueTR = document.createElement('tr');
  307. var valueTD = document.createElement('td');
  308. valueTD.style.whiteSpace = 'nowrap';
  309. var params = 'thresholdGroupName=na&thresholdPropertyCaption=na&thresholdPropertyName=' + this.metricName + '&thresholdResourceID=' + this.resourceID + '&metricValue=' + this.metricValue + '&propertyType=' + this.propertyType + '&style=tooltip';
  310. fragments[this.fragId].retrieve(params);
  311. valueTD.appendChild(div);
  312. valueTD.style.paddingRight = '10px';
  313. valueTR.appendChild(valueTD);
  314. tbody.appendChild(nameTR);
  315. tbody.appendChild(valueTR);
  316. return valueTR;
  317. }
  318. },
  319. update: function(enabled) {
  320. this.enabled = enabled;
  321. }
  322. }