ReselectAction.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2014
  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. function ReselectAction(oCV, aSelectionFilterObjects) {
  13. this.m_aSelectionFilterObjects = aSelectionFilterObjects;
  14. this.m_oCV = oCV;
  15. this.m_oSC = oCV.getSelectionController();
  16. this.m_cd = this.m_oSC.getCCDManager().m_cd;
  17. this.m_md = this.m_oSC.getCCDManager().m_md;
  18. this.m_nReportDiv = oCV.getReportDiv();
  19. this.m_oRAPReportContainers = oCV.getRAPReportInfo().getContainers();
  20. }
  21. ReselectAction.prototype.executeAction = function()
  22. {
  23. this.mapCdMd();
  24. if (this.show()) {
  25. //Send selection payload to Canvas to cache so that filtering onDrop
  26. var oVW = this.m_oCV.getViewerWidget();
  27. if (oVW) {
  28. var selectionJson = oVW.m_oWidgetContextManager.genSelectionPayload(this.m_oCV, oVW.getWidgetId());
  29. oVW.setSelectionFilterSent(true);
  30. oVW.fireEvent("com.ibm.bux.data.filterCache.init", null, selectionJson);
  31. if (oVW.isPastedWidget() && !oVW.pastedSelectedBroadcasted) {
  32. oVW.pastedSelectedBroadcasted = true;
  33. oVW.onSelectionChange();
  34. }
  35. }
  36. }
  37. };
  38. ReselectAction.prototype.mapCdMd = function() {
  39. var aSerialzedSelections = this.m_aSelectionFilterObjects;
  40. for(var i in aSerialzedSelections) {
  41. var oSerializedJson = aSerialzedSelections[i];
  42. var oItem = oSerializedJson.selectedItem;
  43. var aCtx = [];
  44. var newCtx = null;
  45. var queryId = this._findMdIndex('q', oSerializedJson.query);
  46. newCtx = this._setNewCtx(oItem, queryId);
  47. if (newCtx) {
  48. aCtx.push(newCtx);
  49. } else {
  50. //stop process context of current item if first ctx is not found.
  51. continue;
  52. }
  53. for( var j in oSerializedJson.context) {
  54. var oContextItem = oSerializedJson.context[j];
  55. newCtx = this._setNewCtx(oContextItem, queryId);
  56. if (newCtx) {
  57. aCtx.push(newCtx);
  58. }
  59. }
  60. oSerializedJson.ctxArray = aCtx;
  61. }
  62. };
  63. ReselectAction.prototype._setNewCtx = function(oItem, queryId)
  64. {
  65. var mdIndex = null;
  66. var ctxId = null;
  67. if(oItem.isMeasure === 'true') {
  68. mdIndex = this._findMdIndex(oItem.mdProperty, oItem.mdValue, queryId);
  69. if (mdIndex) {
  70. oItem.ctxRef = {'property': oItem.mdProperty, 'mdIndex': mdIndex};
  71. return oItem.ctxRef;
  72. }
  73. } else {
  74. if(oItem.mun) {
  75. mdIndex = this._findMdIndex('m', oItem.mun, queryId);
  76. ctxId = this._getContextIdForCdProperty(queryId, 'm', mdIndex);
  77. } else {
  78. mdIndex = this._findMdIndex(oItem.mdProperty, oItem.mdValue, queryId);
  79. ctxId = this._getContextIdForCdPropertyAndMdProperty(queryId, 'u', oItem.use, oItem.mdProperty, mdIndex);
  80. }
  81. if (ctxId) {
  82. oItem.ctxId = ctxId;
  83. return oItem.ctxId;
  84. }
  85. }
  86. return null;
  87. };
  88. ReselectAction.prototype._getContextIdForCdProperty = function(queryId, cdProperty, cdValue) {
  89. var ctxid = null;
  90. for (var j in this.m_cd) {
  91. if (this.m_cd[j].q == queryId && this.m_cd[j][cdProperty] == cdValue) {
  92. ctxid = j;
  93. break;
  94. }
  95. }
  96. return ctxid;
  97. };
  98. ReselectAction.prototype._getContextIdForCdPropertyAndMdProperty = function(queryId, cdProperty, cdValue, mdProperty, mdIndex) {
  99. var ctxid = null;
  100. for (var j in this.m_cd) {
  101. if (this.m_cd[j].q == queryId &&
  102. this.m_cd[j][mdProperty] == mdIndex && this.m_cd[j][cdProperty] == cdValue) {
  103. ctxid = j;
  104. break;
  105. }
  106. }
  107. return ctxid;
  108. };
  109. /*
  110. * queryId optional
  111. */
  112. ReselectAction.prototype._findMdIndex = function(mdProperty, value, queryId)
  113. {
  114. var mdIndex = null;
  115. for(var j in this.m_md) {
  116. if(this.m_md[j][mdProperty] === value) {
  117. if (this.m_md[j]['q'] && queryId) { //if q exists and queryId is given
  118. if (this.m_md[j]['q'] == queryId) { //must be matching queryId
  119. mdIndex = j;
  120. break;
  121. }
  122. } else {
  123. mdIndex = j;
  124. break
  125. }
  126. }
  127. }
  128. return mdIndex;
  129. };
  130. /**
  131. * If the selection filter object is a measure, it does not get reselected.
  132. */
  133. ReselectAction.prototype.show = function()
  134. {
  135. var foundMatch = false;
  136. var oCV = this.m_oCV;
  137. var oVW = oCV.getViewerWidget();
  138. var nReportDiv = oCV.getReportDiv();
  139. var anContainers = null;
  140. var anCandidateCells = null; /* cell or chart element */
  141. var nContainerSearchRoot = null;
  142. oVW.preprocessPageClicked( false /*bInvokeContextMenu*/);
  143. if (oVW && nReportDiv) {
  144. for(var iSelectedObjectIdx in this.m_aSelectionFilterObjects) {
  145. var oSerializedJson = this.m_aSelectionFilterObjects[iSelectedObjectIdx];
  146. var selectedItemIsMeasure = (oSerializedJson.selectedItem.isMeasure === 'true') ;
  147. var bContainsOnlyMeasure = true;
  148. if( selectedItemIsMeasure ){
  149. var context = oSerializedJson.context;
  150. for( var idx in context ){
  151. if( context[idx].isMeasure !== 'true' ){
  152. bContainsOnlyMeasure = false;
  153. break;
  154. }
  155. }
  156. if( bContainsOnlyMeasure ){
  157. continue;
  158. }
  159. }
  160. var aCtx = oSerializedJson.ctxArray;
  161. if (aCtx && aCtx.length>0) {
  162. //get Layout node
  163. anContainers = this._getLayoutContainers(oSerializedJson.lid);
  164. var foundInContainer = false;
  165. for (var iContainerIdx = 0; !foundInContainer && iContainerIdx<anContainers.length ; iContainerIdx++) {
  166. if ( /img|IMG/.test(anContainers[iContainerIdx].tagName)) {
  167. //find map element
  168. var tmpElement = anContainers[iContainerIdx];
  169. for (nContainerSearchRoot = null; !nContainerSearchRoot && tmpElement.parentNode;) {
  170. tmpElement = tmpElement.parentNode;
  171. var childElements = tmpElement.getElementsByTagName('map');
  172. if (childElements && childElements.length>0) {
  173. nContainerSearchRoot = childElements[0];
  174. }
  175. }
  176. } else {
  177. nContainerSearchRoot = anContainers[iContainerIdx].parentNode;
  178. }
  179. if (nContainerSearchRoot) {
  180. var queryRegEx = this._buildCtxQueryRegExp(aCtx);
  181. anCandidateCells = getElementsByAttribute( nContainerSearchRoot, "*", "ctx", null, -1, queryRegEx);
  182. var selectedItem = oSerializedJson.selectedItem;
  183. //Go through candiate cells and compare ctx
  184. for(var idx in anCandidateCells) {
  185. if (anCandidateCells[idx].getAttribute) {
  186. if( selectedItemIsMeasure && selectedItem.isDataValueOrChartElement === 'false' &&
  187. (anCandidateCells[idx].getAttribute( 'type') === 'datavalue' ||
  188. anCandidateCells[idx].getAttribute( 'type') === 'chartElement') ){
  189. continue;
  190. }
  191. if (this.isMatchingCell(aCtx, anCandidateCells[idx])) {
  192. var foundCell = anCandidateCells[idx];
  193. if (foundCell.tagName === 'SPAN' || foundCell.tagName === 'span') {
  194. var parentTag = foundCell.parentNode.tagName;
  195. if (parentTag == 'td' || parentTag == 'TD' || parentTag == 'TH'|| parentTag == 'th') {
  196. if (foundCell.parentNode.getAttribute('ctx') == foundCell.getAttribute('ctx')) {
  197. continue;
  198. }
  199. }
  200. }
  201. var evt = {'target': foundCell, 'button': 1, 'ctrlKey': true};
  202. this.m_oSC.pageClicked(evt);
  203. foundInContainer = true; //continue process candidate cells in same container
  204. foundMatch = true;
  205. }
  206. }
  207. }//candidate cells loop
  208. }
  209. }//containers loop
  210. }
  211. }
  212. }
  213. return foundMatch;
  214. };
  215. ReselectAction.prototype.isMatchingCell = function(aCtx, nCandidateCell)
  216. {
  217. var cellCtx = nCandidateCell.getAttribute('ctx');
  218. if (!cellCtx || cellCtx.length == 0) {
  219. return false;
  220. }
  221. var aCandidateCellCtxIds = cellCtx.replace(/::/g, ":").split(":");
  222. var candidateCellCtxIdx = 0;
  223. var candidateCellCtxId = null;
  224. var sourceCtxIdx = 0;
  225. var sourceCtx = null;
  226. var iCandiateCtxMatchingCount = 0;
  227. var bMatched = false;
  228. //Get first ctx of Candidate cell
  229. for(; candidateCellCtxIdx < aCandidateCellCtxIds.length; candidateCellCtxIdx++) { //found non empty string
  230. if (aCandidateCellCtxIds[candidateCellCtxIdx].length >0) {
  231. candidateCellCtxId = aCandidateCellCtxIds[candidateCellCtxIdx++];
  232. break;
  233. }
  234. }
  235. //First ctx is selected item itselt. It must match or be a same kind
  236. sourceCtx = aCtx[sourceCtxIdx];
  237. if (typeof sourceCtx == 'string') {
  238. if (sourceCtx == candidateCellCtxId) {
  239. iCandiateCtxMatchingCount++;
  240. }
  241. } else {
  242. if (this.m_cd[candidateCellCtxId][sourceCtx.property] == sourceCtx.mdIndex) {
  243. iCandiateCtxMatchingCount++;
  244. }
  245. }
  246. if (iCandiateCtxMatchingCount != 1) { //First ctx must be found
  247. return false;
  248. }
  249. if (aCtx.length == 1) {
  250. bMatched = true;
  251. }
  252. //Match context
  253. for (sourceCtxIdx = 1; !bMatched && sourceCtxIdx< aCtx.length; sourceCtxIdx++) {
  254. sourceCtx = aCtx[sourceCtxIdx];
  255. if (typeof sourceCtx == 'string') {
  256. //Go through candidate ctx and find match
  257. for(candidateCellCtxIdx=1; candidateCellCtxIdx < aCandidateCellCtxIds.length; candidateCellCtxIdx++) { //found non empty string
  258. if (aCandidateCellCtxIds[candidateCellCtxIdx].length >0 && aCandidateCellCtxIds[candidateCellCtxIdx] != '0') {
  259. candidateCellCtxId = aCandidateCellCtxIds[candidateCellCtxIdx];
  260. if (sourceCtx == candidateCellCtxId) {
  261. iCandiateCtxMatchingCount++;
  262. aCandidateCellCtxIds[candidateCellCtxIdx] = '0'; //set '0' to matched ctx
  263. break;
  264. }
  265. }
  266. }
  267. } else {
  268. //Go through candidate ctx and find match
  269. for(candidateCellCtxIdx=1; candidateCellCtxIdx < aCandidateCellCtxIds.length; candidateCellCtxIdx++) { //found non empty string
  270. if (aCandidateCellCtxIds[candidateCellCtxIdx].length >0 && aCandidateCellCtxIds[candidateCellCtxIdx ] != '0') {
  271. candidateCellCtxId = aCandidateCellCtxIds[candidateCellCtxIdx];
  272. if (this.m_cd[candidateCellCtxId][sourceCtx.property] == sourceCtx.mdIndex) {
  273. iCandiateCtxMatchingCount++;
  274. aCandidateCellCtxIds[candidateCellCtxIdx] = '0'; //set '0' to matched ctx
  275. break;
  276. }
  277. }
  278. }
  279. }
  280. if (aCtx.length == iCandiateCtxMatchingCount) {
  281. bMatched = true;
  282. }
  283. }
  284. return bMatched;
  285. };
  286. ReselectAction.prototype._buildCtxQueryRegExp = function(aCtx)
  287. {
  288. var queryString = "";
  289. var bFoundStringContext = false;
  290. var i = 0; // First ctx is about itself
  291. if (typeof aCtx[i] == 'string') {
  292. queryString += '^' + aCtx[i]; //Start with: ^ctxId
  293. } else {
  294. queryString += '^[\\d:]+'; //Start with valid character
  295. }
  296. //Find first String context
  297. i++;
  298. var contextPart = "";
  299. for ( ; !bFoundStringContext && i < aCtx.length; i++) {
  300. if (typeof aCtx[i] == 'string') {
  301. if (contextPart.length>0) {
  302. contextPart += '+';
  303. }
  304. contextPart += aCtx[i] ;
  305. bFoundStringContext = true;
  306. } else {
  307. if (i < aCtx.length-1) {
  308. if (contextPart.length>0) {
  309. contextPart += '+';
  310. }
  311. contextPart += '[\\d:]';
  312. }
  313. }
  314. }
  315. if (contextPart.length>0) {
  316. queryString += ':{1,}' + contextPart ;
  317. }
  318. return new RegExp(queryString);
  319. };
  320. ReselectAction.prototype._getLayoutContainers = function(sLid)
  321. {
  322. var queryString = "";
  323. if (this.m_oRAPReportContainers && this.m_oRAPReportContainers[sLid]) {
  324. queryString = sLid + '.+';
  325. } else {
  326. for (var name in this.m_oRAPReportContainers) {
  327. if (queryString.length>0) {
  328. queryString += '|';
  329. }
  330. queryString += name + '.+';
  331. }
  332. }
  333. return getElementsByAttribute(this.m_nReportDiv, "*", "lid", null, -1, new RegExp(queryString) );
  334. };