CCognosViewerToolbarHelper.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2017
  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 CCognosViewerToolbarHelper() {}
  13. CCognosViewerToolbarHelper.updateToolbarForCurrentSelection = function(oCV, oToolbar) {
  14. if (oToolbar) {
  15. var actionFactory = oCV.getActionFactory();
  16. for(var toolbarItem = 0; toolbarItem < oToolbar.length; ++toolbarItem)
  17. {
  18. var name = oToolbar[toolbarItem]["name"];
  19. if (typeof name != "undefined" && name != null) {
  20. var action = actionFactory.load(name);
  21. if (action != null && typeof action != "undefined") {
  22. var updatedToolbarItem = action.updateMenu(oToolbar[toolbarItem]);
  23. if (updatedToolbarItem.visible == false) {
  24. if (updatedToolbarItem.save) {
  25. // save the button and location in case we need to add it back later
  26. oCV.getViewerWidget().addButtonToSavedToolbarButtons(name,oToolbar[toolbarItem],toolbarItem);
  27. }
  28. oToolbar.splice(toolbarItem, 1);
  29. --toolbarItem;
  30. } else {
  31. oToolbar[toolbarItem] = updatedToolbarItem;
  32. }
  33. }
  34. } else if (typeof oToolbar[toolbarItem]._root != "undefined") {
  35. CCognosViewerToolbarHelper.updateToolbarForCurrentSelection(oCV, oToolbar[toolbarItem]._root);
  36. } else if (oToolbar[toolbarItem].separator) {
  37. if (toolbarItem == 0 || (toolbarItem > 0 && oToolbar[toolbarItem - 1].separator) || toolbarItem == oToolbar.length) {
  38. oToolbar.splice(toolbarItem, 1);
  39. --toolbarItem;
  40. }
  41. }
  42. }
  43. }
  44. };
  45. CCognosViewerToolbarHelper.updateContextMenuForCurrentSelection = function(oCV, oContextMenu) {
  46. // we don't want to modify the original context menu spec, so create a temp object
  47. // TODO: Get a way to simply hide a menu item instead of setting the json for it to empty.
  48. var tempContextMenu = [];
  49. if (oContextMenu) {
  50. var actionFactory = oCV.getActionFactory();
  51. for (var contextMenuItem = 0; contextMenuItem < oContextMenu.length; ++contextMenuItem) {
  52. var menuItem = oContextMenu[contextMenuItem];
  53. var name = oContextMenu[contextMenuItem]["name"];
  54. var isItemValidInArea = true;
  55. if (typeof name != "undefined") {
  56. var action = actionFactory.load(name);
  57. // For Mobile app purposes, there are two actions, "drillDown" and "drillUp" that need to be changed to
  58. // "DrillDown" and "DrillUp" in order for the Action Factory to properly recognize the actions.
  59. // If the Action Factory previously returned null, there is a high chance that it's because of "drillDown"
  60. // and "drillUp" so let's double check to be sure.
  61. if(!action) {
  62. if(name == "drillDown")
  63. {
  64. action = actionFactory.load("DrillDown");
  65. }
  66. else if(name == "drillUp")
  67. {
  68. action = actionFactory.load("DrillUp");
  69. }
  70. }
  71. if (action != null && typeof action != "undefined") {
  72. if (typeof action.buildMenu == "function") {
  73. //TODO context menus don't support callbacks, once the support is in, we can call updateMenu as we normally do, for now
  74. menuItem = action.buildMenu(oContextMenu[contextMenuItem]);
  75. }
  76. else {
  77. menuItem = action.updateMenu(oContextMenu[contextMenuItem]);
  78. }
  79. isItemValidInArea = action.isValidMenuItem();
  80. } else if(typeof menuItem.items != "undefined") {
  81. menuItem.items = CCognosViewerToolbarHelper.updateContextMenuForCurrentSelection(oCV, menuItem.items);
  82. //We don't want empty or nested menu with one item
  83. isItemValidInArea = (menuItem.items && menuItem.items.length>0)? true:false;
  84. if (isItemValidInArea && menuItem.items.length ==1) {
  85. menuItem = menuItem.items[0];
  86. }
  87. }
  88. }
  89. if (menuItem && menuItem.visible !== false && isItemValidInArea) {
  90. // only add the separator if the previous menuItem wasn't a separator
  91. if (menuItem.separator === true) {
  92. if (tempContextMenu.length > 0 && typeof tempContextMenu[tempContextMenu.length - 1].separator == "undefined") {
  93. tempContextMenu[tempContextMenu.length] = menuItem;
  94. }
  95. }
  96. else if (menuItem.useChildrenItems == true && menuItem.items && menuItem.items.length > 0) {
  97. if (!menuItem.disabled) {
  98. for (var subItems = 0; subItems < menuItem.items.length; subItems++) {
  99. tempContextMenu[tempContextMenu.length] = menuItem.items[subItems];
  100. }
  101. }
  102. }
  103. else if (typeof menuItem._root != "undefined") {
  104. tempContextMenu[tempContextMenu.length] = { "_root" : CCognosViewerToolbarHelper.updateContextMenuForCurrentSelection(oCV, menuItem._root)};
  105. }
  106. else {
  107. tempContextMenu[tempContextMenu.length] = menuItem;
  108. }
  109. }
  110. }
  111. if (tempContextMenu.length>1) {
  112. //remove the separator if it is at the end.
  113. if (tempContextMenu[tempContextMenu.length - 1].separator) {
  114. tempContextMenu = tempContextMenu.splice(0, tempContextMenu.length - 1);
  115. }
  116. }
  117. }
  118. return tempContextMenu;
  119. };