ToolbarDock.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. /**
  6. * Licensed Materials - Property of IBM
  7. * IBM Business Analytics (C) Copyright IBM Corp. 2019, 2020
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. define(['underscore', '../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../../lib/@waca/dashboard-common/dist/api/impl/CallbackInvoker', '../../../lib/@waca/dashboard-common/dist/utils/ActionTypes', '../../../api/DashboardAPI', './ToolbarDockAPI'], function (_, APIFactory, CallbackInvoker, ActionTypes, DashboardAPI, ToolbarDockAPI) {
  11. var DOCK_PREFERENCE_KEY = 'isToolbarDocked';
  12. var ToolbarDock = function (_CallbackInvoker) {
  13. _inherits(ToolbarDock, _CallbackInvoker);
  14. function ToolbarDock(_ref) {
  15. var features = _ref.features;
  16. _classCallCheck(this, ToolbarDock);
  17. var dashboard = features.API;
  18. var logger = dashboard.getService(DashboardAPI.GLOBAL_SERVICES.LOGGER);
  19. var _this = _possibleConstructorReturn(this, _CallbackInvoker.call(this, { logger: logger }));
  20. _this._dashboard = dashboard;
  21. _this._contentActions = features.ContentActions;
  22. _this._dashboardState = features.DashboardState;
  23. _this._dashboardPreferences = features.DashboardPreferences;
  24. _this._dockedToolbarState = {
  25. actions: [],
  26. selectedIds: []
  27. };
  28. return _this;
  29. }
  30. ToolbarDock.prototype.initialize = function initialize() {
  31. var _this2 = this;
  32. var isExplore = this._dashboard.getType().toUpperCase() === 'EXPLORE';
  33. if (!isExplore) {
  34. this._api = APIFactory.createAPI(this, [ToolbarDockAPI, CallbackInvoker]);
  35. var dockedState = this._dashboardPreferences.getPreference(DOCK_PREFERENCE_KEY);
  36. if (dockedState === undefined) {
  37. this._dashboardPreferences.setPreference(DOCK_PREFERENCE_KEY, true);
  38. this._dashboardPreferences.savePreferences().catch(function (error) {
  39. _this2._logger.error('unable to save dashboard preferences', error.message);
  40. });
  41. }
  42. this._dashboard.getCanvasWhenReady().then(function (canvas) {
  43. _this2._canvas = canvas;
  44. canvas.on('change:selections:select', _this2.onContentSelectionChange, _this2);
  45. canvas.on('change:selections:deselect', _this2.onContentSelectionChange, _this2);
  46. _this2._dashboard.on('dashboard:show', _this2.onDashboardShow, _this2);
  47. _this2._updateCanvasToolbar();
  48. _this2._currentUIState = Object.assign({}, _this2._dashboardState.getUiState());
  49. _this2._dashboardState.onUiStateChange(_this2._uiStateChange.bind(_this2));
  50. });
  51. } else {
  52. this._api = undefined;
  53. }
  54. };
  55. ToolbarDock.prototype.getAPI = function getAPI() {
  56. return this._api;
  57. };
  58. ToolbarDock.prototype.destroy = function destroy() {
  59. if (this._canvas) {
  60. this._canvas.off('change:selections:select', this.onContentSelectionChange, this);
  61. this._canvas.off('change:selections:deselect', this.onContentSelectionChange, this);
  62. }
  63. if (this._dashboard) {
  64. this._dashboard.off('dashboard:show', this.onDashboardShow, this);
  65. }
  66. this._api = null;
  67. };
  68. ToolbarDock.prototype.getOwnerIDs = function getOwnerIDs() {
  69. return this._dockedToolbarState.selectedIds;
  70. };
  71. ToolbarDock.prototype.onDashboardShow = function onDashboardShow() {
  72. if (this.isContentToolbarDocked()) {
  73. //isContentToolbarDocked check is redundant with the check inside _renderContentToolbar.
  74. //Ideally _renderContentToolbar should either render empty or render the view
  75. this._renderContentToolbar();
  76. } else {
  77. this.updateODTState([], []);
  78. }
  79. };
  80. ToolbarDock.prototype.onContentSelectionChange = function onContentSelectionChange() {
  81. this._renderContentToolbar();
  82. };
  83. ToolbarDock.prototype._getSelectedIds = function _getSelectedIds() {
  84. var selectedIds = [];
  85. if (this._currentUIState.focus) {
  86. var widgets = this._canvas.getWidgets();
  87. for (var i = 0; i < widgets.length; i++) {
  88. if (widgets[i].isWidgetMaximized()) {
  89. selectedIds.push(widgets[i].getId());
  90. break;
  91. }
  92. }
  93. } else {
  94. var currentSelection = this._canvas.getSelectedContentList();
  95. selectedIds = currentSelection.map(function (content) {
  96. return content.getId();
  97. });
  98. selectedIds.sort();
  99. }
  100. return selectedIds;
  101. };
  102. ToolbarDock.prototype._renderContentToolbar = function _renderContentToolbar() {
  103. var focusToolbar = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  104. var forceRender = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  105. if (this.isContentToolbarDocked()) {
  106. var selectedIds = this._getSelectedIds();
  107. if (selectedIds.length === 0) {
  108. this.updateODTState([]);
  109. } else {
  110. if (forceRender || !_.isEqual(this._dockedToolbarState.selectedIds, selectedIds)) {
  111. this.updateODTState(this._contentActions.getContentActionList(selectedIds), selectedIds);
  112. if (focusToolbar && selectedIds.length) {
  113. this.focusODT();
  114. }
  115. }
  116. }
  117. }
  118. };
  119. /** API implementations */
  120. /**
  121. * @implements ToolbarDockAPI#toggle
  122. * @description will toggle the toolbar state to change from dock/undock. We do this currently by triggering a click on the
  123. * selection which will prompt the toolbar to re-render
  124. *
  125. * Note: We are triggering a click to show/hide the floating toolbar
  126. */
  127. ToolbarDock.prototype.toggle = function toggle() {
  128. var _this3 = this;
  129. var docked = !this.isContentToolbarDocked();
  130. this._dashboardPreferences.setPreference(DOCK_PREFERENCE_KEY, docked);
  131. this._dashboardPreferences.savePreferences().catch(function (error) {
  132. _this3._logger.error('Unable to save dashboard preferences', error.message);
  133. });
  134. var currentSelection = this._canvas.getSelectedContentList();
  135. if (docked === true) {
  136. this._renderContentToolbar();
  137. } else {
  138. this.updateODTState([], []);
  139. }
  140. if (currentSelection.length > 0) {
  141. document.getElementById(currentSelection[0].getId()).click();
  142. }
  143. };
  144. /**
  145. * @implements ToolbarDockAPI#disableDockedODT
  146. */
  147. ToolbarDock.prototype.disableDockedODT = function disableDockedODT() {
  148. this._dashboardPreferences.setPreference(DOCK_PREFERENCE_KEY, false);
  149. this._dashboardPreferences.savePreferences();
  150. this.updateODTState([], []);
  151. };
  152. /**
  153. * @implements ToolbarDockAPI#enableDockedODT
  154. */
  155. ToolbarDock.prototype.enableDockedODT = function enableDockedODT() {
  156. this._dashboardPreferences.setPreference(DOCK_PREFERENCE_KEY, true);
  157. this._dashboardPreferences.savePreferences();
  158. this.updateODTState([], []);
  159. };
  160. /**
  161. * @implements ToolbarDockAPI#updateODTState
  162. */
  163. ToolbarDock.prototype.updateODTState = function updateODTState(actions, selectedIds) {
  164. this._dockedToolbarState.actions = this._orderActions(actions);
  165. this._dockedToolbarState.selectedIds = selectedIds || undefined;
  166. this.invokeCallbacks(ToolbarDockAPI.UPDATE_DOCKED_TOOLBAR_STATE, this._dockedToolbarState);
  167. };
  168. /**
  169. * @implements ToolbarDockAPI#isContentToolbarDocked
  170. */
  171. ToolbarDock.prototype.isContentToolbarDocked = function isContentToolbarDocked() {
  172. return this._dashboardPreferences.getPreference(DOCK_PREFERENCE_KEY) || this._currentUIState.focus;
  173. };
  174. ToolbarDock.prototype._uiStateChange = function _uiStateChange(updatedState) {
  175. var previousState = this._currentUIState;
  176. this._currentUIState = Object.assign({}, updatedState);
  177. if (updatedState.focus || previousState.focus) {
  178. if (this.isContentToolbarDocked()) {
  179. this._renderContentToolbar(false, true);
  180. } else {
  181. this.updateODTState([], []);
  182. }
  183. }
  184. this._updateCanvasToolbar();
  185. };
  186. ToolbarDock.prototype._updateCanvasToolbar = function _updateCanvasToolbar() {
  187. this.invokeCallbacks(ToolbarDockAPI.UPDATE_CANVAS_TOOLBAR_STATE, this._getCanvasActions());
  188. };
  189. ToolbarDock.prototype._getCanvasActions = function _getCanvasActions() {
  190. var actions = this._orderActions(this._contentActions.getContentActionList([]));
  191. return { actions: actions };
  192. };
  193. ToolbarDock.prototype._orderActions = function _orderActions(actions) {
  194. actions.forEach(function (item) {
  195. if (item.order === undefined) {
  196. item.order = ActionTypes[item.name];
  197. }
  198. });
  199. actions.sort(function (a, b) {
  200. if (a.order && b.order || !a.order && !b.order) {
  201. //If both are defined compare. If both are undefined, return NaN, which leaves order unchanged.
  202. return a.order - b.order;
  203. }
  204. // defined order should be before undefined order
  205. if (a.order && !b.order) {
  206. // a before b
  207. return -1;
  208. }
  209. if (!a.order && b.order) {
  210. // b before a
  211. return 1;
  212. }
  213. });
  214. return actions;
  215. };
  216. /*
  217. * @implements ToolbarDockAPI#focusODT
  218. */
  219. ToolbarDock.prototype.focusODT = function focusODT() {
  220. this.invokeCallbacks(ToolbarDockAPI.FOCUS_DOCKED_TOOLBAR);
  221. };
  222. return ToolbarDock;
  223. }(CallbackInvoker);
  224. return ToolbarDock;
  225. });
  226. //# sourceMappingURL=ToolbarDock.js.map