DrillContextMenuHelper.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2013
  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 DrillContextMenuHelper() {}
  13. /**
  14. * Visualization support: (also can be used for charts if VIEWER_JS_ENABLE_DRILL_SUBMENU is set to "charts")
  15. * This function either populates a drill submenu (see needsDrillSubMenu) or ensures jsonSpec is
  16. * set properly for a simple Drill Down or Drill Up menu item. The submenu contains a "default"
  17. * and a set of one or more individual pieces that a user can decide to drill on.
  18. */
  19. DrillContextMenuHelper.updateDrillMenuItems = function(jsonSpec, oCV, sAction)
  20. {
  21. //There will be a submenu only if conditions are met....
  22. var subMenuItems = [];
  23. if (DrillContextMenuHelper.needsDrillSubMenu(oCV)) {
  24. var selectionController = oCV.getSelectionController();
  25. var selectedObjects = selectionController.getAllSelectedObjects();
  26. var selObj = selectedObjects[0];
  27. //For intersections, add the "Default menu item"
  28. if (selObj.getUseValues().length > 1 && typeof RV_RES != "undefined") {
  29. var oDrillOnMenuItem = { name: sAction, label: RV_RES.RV_DRILL_DEFAULT, action: { name: sAction, payload: {} } };
  30. subMenuItems.push(oDrillOnMenuItem);
  31. }
  32. //Add the innermost item. For intersections, add the innermost level of dim1 and dim2
  33. var firstDim=(selObj.getUseValues().length>1) ? 1 : 0;
  34. var lastDim=selObj.getUseValues().length-1;
  35. lastDim=(lastDim>2) ? 2 : lastDim; //Never allow the last dim to process more than rows/columns
  36. for (var iDim=firstDim; iDim<=lastDim; ++iDim) {
  37. DrillContextMenuHelper.addSubMenuItem(sAction, subMenuItems, selObj, iDim, 0);
  38. }
  39. //Do nested levels (either dim0 for edges or dim1 and dim2 for intersections)
  40. var bRenderedSeparator=false;
  41. for (var iDim=firstDim; iDim<=lastDim; ++iDim) {
  42. for (var iLevel=1; iLevel<selObj.getUseValues()[iDim].length; ++iLevel) {
  43. if (bRenderedSeparator==false) {
  44. subMenuItems.push({separator: true});
  45. bRenderedSeparator=true; //If upper levels exist, render a separator.
  46. }
  47. DrillContextMenuHelper.addSubMenuItem(sAction, subMenuItems, selObj, iDim, iLevel);
  48. }
  49. }
  50. }
  51. DrillContextMenuHelper.completeDrillMenu(sAction, subMenuItems, jsonSpec);
  52. };
  53. /**
  54. * Visualization support:
  55. * Return true if a drill submenu needs to be shown under the Drill Up or Drill Down menu item.
  56. *
  57. * Rules: Show the submenu:
  58. * IF the number of dimensions OR the number of levels in the first dimension are > 1
  59. * AND its a visualization OR its a chart and the VIEWER_JS_ENABLE_DRILL_SUBMENU advanced server property is set to "charts".
  60. *
  61. * NOTE: The Drill Up/Drill Down menu item won't be shown at all if the net drillability is determined to be 0.
  62. *
  63. * @return true if this is the case.
  64. */
  65. DrillContextMenuHelper.needsDrillSubMenu = function(oCV)
  66. {
  67. var selectionController = (oCV && oCV.getSelectionController());
  68. if (selectionController) {
  69. var selectedObjects = selectionController.getAllSelectedObjects();
  70. if(selectedObjects.length == 1 && selectedObjects[0].isHomeCell && selectedObjects[0].isHomeCell() == false) {
  71. var bDrillSubmenu = selectedObjects[0].isSelectionOnVizChart();
  72. if (!bDrillSubmenu) {
  73. var drillSubMenuType = oCV.getAdvancedServerProperty("VIEWER_JS_ENABLE_DRILL_SUBMENU");
  74. bDrillSubmenu = (drillSubMenuType=="charts" && selectionController.hasSelectedChartNodes());
  75. }
  76. if (bDrillSubmenu) {
  77. var selObj = selectedObjects[0];
  78. return (bDrillSubmenu && selObj.getUseValues() && (selObj.getUseValues().length > 1 || selObj.getUseValues()[0].length > 1));
  79. }
  80. }
  81. }
  82. return false;
  83. };
  84. /**
  85. * For the selected object at position iDim and iLevel, if that component of the selection is drillable,
  86. * add an item to the submenu.
  87. */
  88. DrillContextMenuHelper.addSubMenuItem = function(sAction, subMenuItems, selObj, iDim, iLevel)
  89. {
  90. var drillOption = selObj.getDrillOptions()[iDim][iLevel];
  91. if (DrillContextMenuHelper.isOptionDrillable(sAction, drillOption)) {
  92. var sItemLabel = DrillContextMenuHelper.getItemValue(selObj, iDim, iLevel);
  93. if (sItemLabel) {
  94. var sDataItem = selObj.getDataItems()[iDim][iLevel];
  95. var oDrillOnMenuItem = { name: sAction, label: sItemLabel, action: { name: sAction, payload: { userSelectedDrillItem: sDataItem } } };
  96. subMenuItems.push(oDrillOnMenuItem);
  97. }
  98. }
  99. };
  100. /**
  101. * If a submenu is required, add the items, otherwise ensure the basic action is defined.
  102. */
  103. DrillContextMenuHelper.completeDrillMenu = function(sAction, subMenuItems, jsonSpec)
  104. {
  105. if (subMenuItems.length > 0) {
  106. jsonSpec.items = subMenuItems;
  107. } else {
  108. jsonSpec.items = null;
  109. if (jsonSpec.action==null) {
  110. jsonSpec.action = { name: sAction, action: { name: sAction } };
  111. }
  112. }
  113. };
  114. /**
  115. * Return true if the drillFlag value is drillable for the current action (eg: DrillDown and 2,3,4; DrillUp and 1,3,4)
  116. */
  117. DrillContextMenuHelper.isOptionDrillable = function(sAction, drillFlag)
  118. {
  119. //0=none, 1=up, 2=down, 3=downorup, 4=upordown
  120. return (drillFlag>=3 || (sAction=="DrillDown" && drillFlag==2) || (sAction=="DrillUp" && drillFlag==1));
  121. };
  122. /**
  123. * Return the item value for the selected object...(usually the useValue of a label like "Camping Equipment")
  124. */
  125. DrillContextMenuHelper.getItemValue = function(selObj, iDim, iLevel)
  126. {
  127. var itemsLabel = (iLevel==0) ? selObj.getDisplayValues()[iDim] : null;
  128. return ((itemsLabel) ? itemsLabel : selObj.getUseValues()[iDim][iLevel]);
  129. };