ButtonHideHelper.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Content Explorer
  6. *| (C) Copyright IBM Corp. 2015, 2019
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['../../lib/@waca/core-client/js/core-client/ui/core/Class', 'underscore', '../../app/util/ErrorUtils'], function (BaseClass, _, ErrorUtils) {
  13. var defaultEmbeddedUIConfiguration = {
  14. buttons: {
  15. 'com.ibm.bi.dashboard.pin': false,
  16. 'com.ibm.bi.dashboard.customWidget': false,
  17. 'com.ibm.bi.dashboard.saveMenu': false
  18. }
  19. };
  20. var uiParts = [{
  21. id: 'navbar'
  22. }, {
  23. id: 'appbar'
  24. }];
  25. var ButtonHideHelper = BaseClass.extend({
  26. /**
  27. Used when changing the mode in which the dashboard
  28. @param glassContext {object} - the glass context
  29. @param mode {string} - one of three possible modes: 'authoring', 'consume', 'eventGroups', 'translation'
  30. **/
  31. changeMode: function changeMode(glassContext, mode, options) {
  32. if (this._buttonCollection) {
  33. this._changeMode(glassContext, mode);
  34. } else {
  35. glassContext.appController.findCollection(this._getButtonsCollection(glassContext)).then(function (buttonCollection) {
  36. this._buttonCollection = buttonCollection;
  37. this._applyContainerAppButtonConfig(options && options.containerAppOptions);
  38. this._applyContainerUIPartConfig(options && options.containerAppOptions);
  39. this._changeMode(glassContext, mode);
  40. }.bind(this));
  41. }
  42. },
  43. _applyContainerAppButtonConfig: function _applyContainerAppButtonConfig(options) {
  44. var uiOptions = ((options || {}).configuration || {}).ui || null;
  45. if (uiOptions) {
  46. // default non-dashboard buttons added to the dashboard
  47. var nonDashboardButtons = ['com.ibm.bi.glass.common.home', 'com.ibm.bi.admin.admin', 'com.ibm.bi.glass.common.createMenu', 'com.ibm.bi.share.notifications', 'com.ibm.bi.glass.common.viewSwitcher', 'com.ibm.bi.glass.common.personalMenu', 'com.ibm.bi.dashboard.saveMenu', 'com.ibm.bi.contentApps.myContentFoldersSlideout', 'com.ibm.bi.search.search', 'com.ibm.bi.contentApps.teamFoldersSlideout', 'com.ibm.bi.contentApps.mruSlideout', 'com.ibm.bi.contentApps.myPortalPages'];
  48. nonDashboardButtons.forEach(function (id) {
  49. this._buttonCollection.push({
  50. id: id
  51. });
  52. }.bind(this));
  53. _.each(this._buttonCollection, function (button) {
  54. // extend any buttons that are configured or needs to hidden by default
  55. _.extend(button, {
  56. modes: {
  57. authoring: {
  58. visible: this._getButtonVisibility(uiOptions, button, 'authoring', true)
  59. },
  60. consume: {
  61. visible: this._getButtonVisibility(uiOptions, button, 'consume', true)
  62. },
  63. eventGroups: {
  64. visible: this._getButtonVisibility(uiOptions, button, 'eventGroups', true)
  65. },
  66. widgetMaximized: {
  67. visible: this._getButtonVisibility(uiOptions, button, 'widgetMaximized', false)
  68. },
  69. translation: {
  70. visible: this._getButtonVisibility(uiOptions, button, 'translation', true)
  71. }
  72. }
  73. });
  74. }.bind(this));
  75. }
  76. },
  77. _applyContainerUIPartConfig: function _applyContainerUIPartConfig(options) {
  78. var uiOptions = ((options || {}).configuration || {}).ui || null;
  79. if (uiOptions) {
  80. uiParts.forEach(function (part) {
  81. _.extend(part, {
  82. modes: {
  83. authoring: {
  84. visible: this._getUISectionVisibility(uiOptions, part.id, 'authoring', true)
  85. },
  86. consume: {
  87. visible: this._getUISectionVisibility(uiOptions, part.id, 'consume', part.id !== 'navbar')
  88. },
  89. eventGroups: {
  90. visible: this._getUISectionVisibility(uiOptions, part.id, 'eventGroups', true)
  91. },
  92. translation: {
  93. visible: this._getUISectionVisibility(uiOptions, part.id, 'translation', true)
  94. }
  95. }
  96. });
  97. }.bind(this));
  98. }
  99. },
  100. _getUISectionVisibility: function _getUISectionVisibility(uiOptions, id, mode, defaultValue) {
  101. if (!uiOptions) {
  102. return {};
  103. }
  104. var uiModeProperty = ((uiOptions.modes || {})[mode] || {})[id];
  105. if (uiModeProperty !== undefined) {
  106. return uiModeProperty;
  107. } else if (uiOptions[id] !== undefined) {
  108. return uiOptions[id];
  109. }
  110. return defaultValue;
  111. },
  112. _getButtonVisibility: function _getButtonVisibility(uiOptions, button, mode, hideByDefault) {
  113. var visible = this._getButtonVisibilityFromOptions(uiOptions, button.id, mode);
  114. if (visible !== null) {
  115. return visible;
  116. }
  117. visible = this._getButtonVisibilityFromOptions(defaultEmbeddedUIConfiguration, button.id, mode);
  118. if (visible !== null) {
  119. return visible;
  120. }
  121. // If we shouldn't hide by default then set it to undefined and our hide code will ignore this button
  122. var defaultValue = hideByDefault === true ? false : undefined;
  123. return ((button.modes || {})[mode] || {}).visible || defaultValue;
  124. },
  125. _getButtonVisibilityFromOptions: function _getButtonVisibilityFromOptions(uiOptions, id, mode) {
  126. if (!uiOptions) {
  127. return {};
  128. }
  129. var modeButtonConfig = (((uiOptions.modes || {})[mode] || {}).buttons || {})[id];
  130. if (modeButtonConfig !== undefined) {
  131. return modeButtonConfig;
  132. }
  133. var buttonConfig = (uiOptions.buttons || {})[id];
  134. if (buttonConfig !== undefined) {
  135. return buttonConfig;
  136. }
  137. return null;
  138. },
  139. _handleUIPartModeChange: function _handleUIPartModeChange(glassContext, mode) {
  140. uiParts.forEach(function (part) {
  141. if (part.modes && part.modes[mode]) {
  142. glassContext.appController.currentAppView.$('.' + part.id).css('display', part.modes[mode].visible ? '' : 'none');
  143. }
  144. });
  145. },
  146. _changeMode: function _changeMode(glassContext, mode) {
  147. if (!this._buttonCollection) {
  148. console.log('Could not fix the contributed button collection.');
  149. return;
  150. }
  151. if (!glassContext.appController.currentAppView.$('.navbar').hasClass('narrow')) {
  152. glassContext.appController.currentAppView.$('.navbar').addClass('narrow');
  153. }
  154. this._handleUIPartModeChange(glassContext, mode);
  155. this._buttonCollection.forEach(function (button) {
  156. var plugin = glassContext.appController.findPlugin(button.id);
  157. if (!plugin) {
  158. return;
  159. }
  160. if (!this._isDevMode(glassContext) && button.development === true) {
  161. this._hidePlugin(plugin);
  162. return;
  163. }
  164. if (this._isHideForConsumerMode(glassContext, button.id)) {
  165. this._hidePlugin(plugin);
  166. return;
  167. }
  168. if (this._isDisabledForConsumerMode(glassContext, button.id)) {
  169. this._disablePlugin(plugin);
  170. return;
  171. }
  172. var modeInfo = button.modes ? button.modes[mode] : null;
  173. if (modeInfo) {
  174. var visible = plugin.hideForever === true ? false : modeInfo.visible;
  175. this._handleVisibleProperty(visible, plugin);
  176. this._handleDisabledProperty(modeInfo.disabled, plugin);
  177. this._handlePressedProperty(modeInfo.pressed, plugin);
  178. }
  179. }.bind(this));
  180. },
  181. _isHideForConsumerMode: function _isHideForConsumerMode(glassContext, buttonId) {
  182. // explore and dashboard shares the save button, but dashboard capability should not affect explore user to save exploration
  183. // TODO: clean this up once explore has their own button contribution
  184. var appViewType = glassContext.appController.getCurrentContentView().getDashboardApi().getType().toUpperCase();
  185. var hiddenButtons = ['com.ibm.bi.dashboard.saveMenu', 'com.ibm.bi.dashboard.mode'];
  186. return hiddenButtons.indexOf(buttonId) !== -1 && appViewType !== 'EXPLORE' ? !ErrorUtils.hasCapability(glassContext, 'canAuthorDashboard') : false;
  187. },
  188. _isDisabledForConsumerMode: function _isDisabledForConsumerMode(glassContext, buttonId) {
  189. var disabledButtons = ['com.ibm.bi.dashboard.mode'];
  190. return disabledButtons.indexOf(buttonId) !== -1 ? ErrorUtils.hasCapability(glassContext, 'canAuthorDashboard') && !glassContext.appController.getCurrentContentView().canAuthor() && !glassContext.appController.getCurrentContentView().isNew() : false;
  191. },
  192. _isDevMode: function _isDevMode(glassContext) {
  193. if (glassContext.isDevInstall) {
  194. return glassContext.isDevInstall();
  195. }
  196. return false;
  197. },
  198. _handleVisibleProperty: function _handleVisibleProperty(visible, plugin) {
  199. if (visible === true) {
  200. this._showPlugin(plugin);
  201. } else if (visible === false) {
  202. this._hidePlugin(plugin);
  203. }
  204. },
  205. _handleDisabledProperty: function _handleDisabledProperty(disabled, plugin) {
  206. if (disabled === true) {
  207. this._disablePlugin(plugin);
  208. } else if (disabled === false) {
  209. plugin.enable();
  210. }
  211. },
  212. _handlePressedProperty: function _handlePressedProperty(pressed, plugin) {
  213. if (pressed === true) {
  214. plugin.setPressed();
  215. } else if (pressed === false) {
  216. plugin.setUnpressed();
  217. }
  218. },
  219. _disablePlugin: function _disablePlugin(plugin) {
  220. if (plugin) {
  221. // Make sure any slideouts for the common plugins are hidden when switching modes
  222. this._actionControllerOnHide(plugin);
  223. plugin.disable();
  224. }
  225. },
  226. _hidePlugin: function _hidePlugin(plugin) {
  227. if (plugin) {
  228. this._actionControllerOnHide(plugin);
  229. plugin.hide();
  230. }
  231. },
  232. _showPlugin: function _showPlugin(plugin) {
  233. if (plugin) {
  234. plugin.enable();
  235. plugin.show();
  236. }
  237. },
  238. _actionControllerOnHide: function _actionControllerOnHide(plugin) {
  239. // TODO : the glass should be able to notify the action controller when there is a hide/show/enable/disable
  240. // Currently there is not way to do that, so we manually call hide on the controller.
  241. if (plugin && plugin.actionController && plugin.actionController.onHide) {
  242. plugin.actionController.onHide();
  243. }
  244. },
  245. _getButtonsCollection: function _getButtonsCollection(glassContext) {
  246. var content = glassContext.appController.currentAppView.getContent();
  247. return content.options.collections.buttons.id;
  248. }
  249. });
  250. return new ButtonHideHelper();
  251. });
  252. //# sourceMappingURL=ButtonHideHelper.js.map