cvImageHighlight.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2011, 2015
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. /**
  13. * Class responsibility: Add or remove Highlight image map areas
  14. */
  15. function CImageMapHighlight(map, webContentRoot)
  16. {
  17. this.m_webContentRoot = webContentRoot;
  18. this.createHighlight = CImageMapHighlight.prototype.createHighlightElement;
  19. this.initialize(map);
  20. }
  21. CImageMapHighlight.prototype.initialize = function(map) {
  22. this.m_map = map;
  23. this.m_areas = {};
  24. this.m_areaNodes = {};
  25. this.m_visibleAreas = [];
  26. this.initImageBlank();
  27. this.m_divCanvas = null;
  28. this.m_creationNode = null;
  29. this._setMapAreasId();
  30. this.m_sDefaultFillColour = "#F7E1BC";
  31. this.m_sDefaultStrokeColour = "#F0A630";
  32. this.m_sFillColour = this.m_sDefaultFillColour;
  33. this.m_sStrokeColour = this.m_sDefaultStrokeColour;
  34. };
  35. CImageMapHighlight.prototype.setFillColour = function( sFillColour ){
  36. this.m_sFillColour = ( !sFillColour ) ? this.m_sDefaultFillColour : sFillColour;
  37. };
  38. CImageMapHighlight.prototype.getFillColour = function(){
  39. return this.m_sFillColour;
  40. };
  41. CImageMapHighlight.prototype.setStrokeColour = function( sStrokeColour ){
  42. this.m_sStrokeColour = (!sStrokeColour ) ? this.m_sDefaultStrokeColour : sStrokeColour ;
  43. };
  44. CImageMapHighlight.prototype.getStrokeColour = function(){
  45. return this.m_sStrokeColour;
  46. };
  47. CImageMapHighlight.prototype.resetColours = function(){
  48. this.m_sStrokeColour = this.m_sDefaultStrokeColour;
  49. this.m_sFillColour = this.m_sDefaultFillColour;
  50. };
  51. CImageMapHighlight.prototype.initImageBlank = function() {
  52. var img = this._getChartImageFromMap();
  53. // this test is to avoid a javascript error, but it won't work
  54. if (img === null) {
  55. return;
  56. }
  57. this.m_img = img;
  58. this.m_sImageHeight = img.offsetHeight + "px";
  59. this.m_sImageWidth = img.offsetWidth + "px";
  60. this.m_sUseMap = img.getAttribute( "usemap" );
  61. this.m_imgLid = img.getAttribute( "lid" );
  62. this.m_imgBlank = img.ownerDocument.createElement( "IMG" );
  63. this.m_imgBlank.src = this.m_webContentRoot + "/rv/images/blank.gif";
  64. this.m_imgBlank.style.height = this.m_sImageHeight;
  65. this.m_imgBlank.style.width = this.m_sImageWidth;
  66. this.m_imgBlank.style.position = "absolute";
  67. this.m_imgBlank.border = "0";
  68. this.m_imgBlank.useMap = this.m_sUseMap;
  69. this.m_imgBlank.setAttribute("lid", this.m_imgLid);
  70. this.m_imgBlank.setAttribute("rsvpchart", img.getAttribute( "rsvpchart"));
  71. this.m_imgBlank.alt = img.alt;
  72. if ( this.m_bShowPointer )
  73. {
  74. this.m_imgBlank.style.cursor = "auto";
  75. }
  76. this.m_imgBlank.v_bIsBlankImageMapImg = true;
  77. img.parentNode.insertBefore( this.m_imgBlank, img );
  78. this.f_copyStyle( img, this.m_imgBlank );
  79. this.m_imgBlank.style.borderColor = "transparent";
  80. };
  81. /**
  82. * Look in the DOM for the chart container image
  83. * <map/> [<div/>] <div><span></div> [<svg|div/> <img-blank/>] <img-rsvpchart=true>
  84. */
  85. CImageMapHighlight.prototype._getChartImageFromMap = function() {
  86. var map = this.m_map;
  87. var result = null;
  88. // 1. find the span which is the chart container (it should be in the next
  89. var chartContainerSpan = null;
  90. var mapNextSibling = map.nextSibling;
  91. while (mapNextSibling) {
  92. if(mapNextSibling.tagName == "DIV") {
  93. var nDivChild = mapNextSibling.firstChild;
  94. while (nDivChild) {
  95. if ((nDivChild.tagName == "SPAN" || nDivChild.tagName == "DIV") && nDivChild.getAttribute("chartcontainer") == "true") {
  96. chartContainerSpan = nDivChild;
  97. break;
  98. }
  99. nDivChild = nDivChild.nextSibling;
  100. }
  101. }
  102. if(chartContainerSpan) {
  103. break;
  104. }
  105. mapNextSibling = mapNextSibling.nextSibling;
  106. }
  107. // 2. Find the image within the chart container Span
  108. if (chartContainerSpan) {
  109. var chartContainerChildren = chartContainerSpan.children;
  110. var chartContainerChildrenCount = chartContainerChildren.length;
  111. for (var i=0; i<chartContainerChildrenCount; i++) {
  112. var el = chartContainerChildren[i];
  113. if (el.tagName == "IMG" && el.getAttribute("rsvpchart") == "true" && el.getAttribute("usemap") == "#" + map.name) {
  114. result = el;
  115. break;
  116. }
  117. }
  118. }
  119. return result;
  120. };
  121. CImageMapHighlight.prototype._AREA_ID = "aid";
  122. CImageMapHighlight.prototype._setMapAreasId = function() {
  123. var mapLid_ = this.m_map.getAttribute( "lid" ) + "_";
  124. var areas = this.m_map.childNodes;
  125. var areaCount = areas.length;
  126. for (var i=0; i<areaCount; i++ ) {
  127. var a = areas[i];
  128. var id = mapLid_ + i;
  129. a.setAttribute(this._AREA_ID,id);
  130. this.m_areaNodes[id]=a;
  131. }
  132. };
  133. CImageMapHighlight.prototype.isAreaInitialized = function(area) {
  134. return (area.getAttribute(this._AREA_ID) === null? false : true);
  135. };
  136. CImageMapHighlight.prototype.getAreaId = function(area) {
  137. var areaId = area.getAttribute(this._AREA_ID);
  138. if (areaId === null) {
  139. // re-initialize
  140. this.initialize(area.parentNode);
  141. areaId = area.getAttribute(this._AREA_ID);
  142. }
  143. return areaId + this.getFillColour();
  144. };
  145. CImageMapHighlight.prototype.getAreaFromId = function(areaid) {
  146. return this.m_areaNodes[areaid];
  147. };
  148. CImageMapHighlight.prototype.highlightArea = function(area, append) {
  149. var areaId = this.getAreaId(area); //
  150. // hide all but the one to highlight if not append
  151. if (!append) {
  152. var visibleAreas = this.m_visibleAreas;
  153. var visiblesAreaCount = visibleAreas.length;
  154. for (var i = 0; i < visiblesAreaCount; i++) {
  155. if (areaId != visibleAreas[i]) {
  156. this.hideAreaById(visibleAreas[i]);
  157. }
  158. }
  159. this.m_visibleAreas = [];
  160. }
  161. this._highlightArea(area);
  162. };
  163. CImageMapHighlight.prototype.highlightAreas = function(areas, append) {
  164. if (!append) {
  165. this.hideAllAreas();
  166. }
  167. this._highlightAreas(areas);
  168. };
  169. /**
  170. * highlight multiple areas
  171. */
  172. CImageMapHighlight.prototype._highlightAreas = function(areas) {
  173. var areasCount = areas.length;
  174. for (var i = 0; i < areasCount; i++) {
  175. this._highlightArea(areas[i]);
  176. }
  177. };
  178. CImageMapHighlight.prototype._highlightArea = function(area) {
  179. var areaId = this.getAreaId(area);
  180. if (!this.highlightAreaExists(areaId)) {
  181. var highLight = this.createHighlight(area);
  182. if (highLight) {
  183. this.m_areas[areaId] = highLight;
  184. highLight.style.visibility = "visible";
  185. area.setAttribute("highlighted", "true");
  186. }
  187. } else {
  188. // if not visible make it so
  189. if (this.m_areas[areaId].style.visibility == "hidden") {
  190. this.m_areas[areaId].style.visibility = "visible";
  191. area.setAttribute("highlighted", "true");
  192. }
  193. }
  194. this.m_visibleAreas.push(areaId);
  195. };
  196. CImageMapHighlight.prototype.highlightAreaExists = function(areaId){
  197. return this.m_areas[areaId] ? true : false;
  198. };
  199. CImageMapHighlight.prototype.hideAreaById = function(areaId) {
  200. if ( this.m_areas[areaId] && this.m_areas[areaId].style.visibility ) {
  201. this.m_areas[areaId].style.visibility = "hidden";
  202. }
  203. };
  204. CImageMapHighlight.prototype.hideAreas = function(areas) {
  205. var areasCount = areas.length;
  206. for (var i = 0; i < areasCount; i++) {
  207. this.hideArea(areas[i]);
  208. }
  209. };
  210. CImageMapHighlight.prototype.hideArea = function(area) {
  211. this.hideAreaById(this.getAreaId(area));
  212. area.setAttribute("highlighted", "false");
  213. };
  214. CImageMapHighlight.prototype.hideAllAreas = function() {
  215. var visibleAreas = this.m_visibleAreas;
  216. var visiblesAreaCount = visibleAreas.length;
  217. for (var i = 0; i < visiblesAreaCount; i++) {
  218. this.hideAreaById(visibleAreas[i]);
  219. var areaNode = this.getAreaFromId(visibleAreas[i]);
  220. if(areaNode) {
  221. areaNode.setAttribute("highlighted", "false");
  222. }
  223. }
  224. this.m_visibleAreas = [];
  225. };
  226. CImageMapHighlight.prototype.isAreaHighlighted = function(area) {
  227. var areaId = this.getAreaId(area);
  228. return this.m_areas[areaId] && this.m_areas[areaId].style.visibility == "visible";
  229. };
  230. CImageMapHighlight.prototype.removeAreaHighlights = function(areas) {
  231. };
  232. CImageMapHighlight.prototype.removeAllAreaHighlights = function() {
  233. };
  234. CImageMapHighlight.prototype.destroy = function(area) {
  235. this.removeAllAreaHighlights();
  236. };
  237. CImageMapHighlight.prototype.createHighlightElement = function( v_elArea, v_bIsHover )
  238. {
  239. var doc = v_elArea.ownerDocument;
  240. if ( !this.m_divCanvas )
  241. {
  242. for ( var v_elAncestor = this.m_img.parentNode; v_elAncestor; v_elAncestor = v_elAncestor.parentNode )
  243. {
  244. if ( ( v_elAncestor.nodeName == "DIV" ) && ( v_elAncestor.getAttribute( "sSpecName" ) == "block" ) )
  245. {
  246. var v_oStyle = doc.defaultView.getComputedStyle( v_elAncestor , null );
  247. var v_sOverflow = v_oStyle.overflow;
  248. if ( ( v_sOverflow == "auto" ) || ( v_sOverflow == "scroll" ) && ( v_oStyle.position != "relative" ) )
  249. {
  250. v_elAncestor.style.position = "relative";
  251. }
  252. }
  253. }
  254. this.m_divCanvas = doc.createElementNS( "http://www.w3.org/2000/svg", "svg" );
  255. this.m_divCanvas.style.height = this.m_sImageHeight;
  256. this.m_divCanvas.style.width = this.m_sImageWidth;
  257. this.m_divCanvas.style.position = "absolute";
  258. //this.m_divCanvas.style.border = "1px solid green";
  259. //this.m_divCanvas.style.backgroundColor = "red";
  260. this.m_img.parentNode.insertBefore( this.m_divCanvas, this.m_imgBlank );
  261. this.f_copyStyle( this.m_imgBlank, this.m_divCanvas );
  262. this.m_divCanvas.style.display = this.m_bHiddenCanvas ? "none" : "block";
  263. }
  264. var v_elPolyline = doc.createElementNS( "http://www.w3.org/2000/svg", "polyline" );
  265. var v_sCoords = v_elArea.getAttribute( "coords" );
  266. v_elPolyline.setAttribute( "points", v_elArea.getAttribute( "coords" ) + " " + v_sCoords.substr( 0, v_sCoords.indexOf( ",", v_sCoords.indexOf( "," ) + 1 ) ) );
  267. v_elPolyline.style.position = "absolute";
  268. v_elPolyline.style.top = "0px";
  269. v_elPolyline.style.left = "0px";
  270. v_elPolyline.style.visibility = "hidden";
  271. v_elPolyline.setAttribute( "stroke", v_bIsHover ? "#F7CB83" : this.getStrokeColour() );
  272. v_elPolyline.setAttribute( "stroke-width", ( v_elArea.getAttribute( "type" ) == "legendLabel" ) ? "1pt" : "1.75pt" );
  273. v_elPolyline.setAttribute( "fill", v_bIsHover ? "#F7E1BC" : this.getFillColour() );
  274. v_elPolyline.setAttribute( "fill-opacity", "0.4" );
  275. this.m_divCanvas.appendChild( v_elPolyline );
  276. return v_elPolyline;
  277. };
  278. CImageMapHighlight.prototype.f_copyStyle = function( v_elFrom, v_elTo )
  279. {
  280. var a = ["margin", "marginTop", "marginRight", "marginBottom", "marginLeft", "border", "borderTop", "borderRight", "borderBottom", "borderLeft"];
  281. var v_iLength = a.length;
  282. for ( var i = 0; i < v_iLength; i++ )
  283. {
  284. var v_sName = a[i];
  285. var v_sValue = v_elFrom.style[v_sName];
  286. if ( v_sValue )
  287. {
  288. v_elTo.style[v_sName] = v_sValue;
  289. }
  290. }
  291. };