'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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; } /** * Licensed Materials - Property of IBM * IBM Business Analytics (C) Copyright IBM Corp. 2019, 2020 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ 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) { var DOCK_PREFERENCE_KEY = 'isToolbarDocked'; var ToolbarDock = function (_CallbackInvoker) { _inherits(ToolbarDock, _CallbackInvoker); function ToolbarDock(_ref) { var features = _ref.features; _classCallCheck(this, ToolbarDock); var dashboard = features.API; var logger = dashboard.getService(DashboardAPI.GLOBAL_SERVICES.LOGGER); var _this = _possibleConstructorReturn(this, _CallbackInvoker.call(this, { logger: logger })); _this._dashboard = dashboard; _this._contentActions = features.ContentActions; _this._dashboardState = features.DashboardState; _this._dashboardPreferences = features.DashboardPreferences; _this._dockedToolbarState = { actions: [], selectedIds: [] }; return _this; } ToolbarDock.prototype.initialize = function initialize() { var _this2 = this; var isExplore = this._dashboard.getType().toUpperCase() === 'EXPLORE'; if (!isExplore) { this._api = APIFactory.createAPI(this, [ToolbarDockAPI, CallbackInvoker]); var dockedState = this._dashboardPreferences.getPreference(DOCK_PREFERENCE_KEY); if (dockedState === undefined) { this._dashboardPreferences.setPreference(DOCK_PREFERENCE_KEY, true); this._dashboardPreferences.savePreferences().catch(function (error) { _this2._logger.error('unable to save dashboard preferences', error.message); }); } this._dashboard.getCanvasWhenReady().then(function (canvas) { _this2._canvas = canvas; canvas.on('change:selections:select', _this2.onContentSelectionChange, _this2); canvas.on('change:selections:deselect', _this2.onContentSelectionChange, _this2); _this2._dashboard.on('dashboard:show', _this2.onDashboardShow, _this2); _this2._updateCanvasToolbar(); _this2._currentUIState = Object.assign({}, _this2._dashboardState.getUiState()); _this2._dashboardState.onUiStateChange(_this2._uiStateChange.bind(_this2)); }); } else { this._api = undefined; } }; ToolbarDock.prototype.getAPI = function getAPI() { return this._api; }; ToolbarDock.prototype.destroy = function destroy() { if (this._canvas) { this._canvas.off('change:selections:select', this.onContentSelectionChange, this); this._canvas.off('change:selections:deselect', this.onContentSelectionChange, this); } if (this._dashboard) { this._dashboard.off('dashboard:show', this.onDashboardShow, this); } this._api = null; }; ToolbarDock.prototype.getOwnerIDs = function getOwnerIDs() { return this._dockedToolbarState.selectedIds; }; ToolbarDock.prototype.onDashboardShow = function onDashboardShow() { if (this.isContentToolbarDocked()) { //isContentToolbarDocked check is redundant with the check inside _renderContentToolbar. //Ideally _renderContentToolbar should either render empty or render the view this._renderContentToolbar(); } else { this.updateODTState([], []); } }; ToolbarDock.prototype.onContentSelectionChange = function onContentSelectionChange() { this._renderContentToolbar(); }; ToolbarDock.prototype._getSelectedIds = function _getSelectedIds() { var selectedIds = []; if (this._currentUIState.focus) { var widgets = this._canvas.getWidgets(); for (var i = 0; i < widgets.length; i++) { if (widgets[i].isWidgetMaximized()) { selectedIds.push(widgets[i].getId()); break; } } } else { var currentSelection = this._canvas.getSelectedContentList(); selectedIds = currentSelection.map(function (content) { return content.getId(); }); selectedIds.sort(); } return selectedIds; }; ToolbarDock.prototype._renderContentToolbar = function _renderContentToolbar() { var focusToolbar = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; var forceRender = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; if (this.isContentToolbarDocked()) { var selectedIds = this._getSelectedIds(); if (selectedIds.length === 0) { this.updateODTState([]); } else { if (forceRender || !_.isEqual(this._dockedToolbarState.selectedIds, selectedIds)) { this.updateODTState(this._contentActions.getContentActionList(selectedIds), selectedIds); if (focusToolbar && selectedIds.length) { this.focusODT(); } } } } }; /** API implementations */ /** * @implements ToolbarDockAPI#toggle * @description will toggle the toolbar state to change from dock/undock. We do this currently by triggering a click on the * selection which will prompt the toolbar to re-render * * Note: We are triggering a click to show/hide the floating toolbar */ ToolbarDock.prototype.toggle = function toggle() { var _this3 = this; var docked = !this.isContentToolbarDocked(); this._dashboardPreferences.setPreference(DOCK_PREFERENCE_KEY, docked); this._dashboardPreferences.savePreferences().catch(function (error) { _this3._logger.error('Unable to save dashboard preferences', error.message); }); var currentSelection = this._canvas.getSelectedContentList(); if (docked === true) { this._renderContentToolbar(); } else { this.updateODTState([], []); } if (currentSelection.length > 0) { document.getElementById(currentSelection[0].getId()).click(); } }; /** * @implements ToolbarDockAPI#disableDockedODT */ ToolbarDock.prototype.disableDockedODT = function disableDockedODT() { this._dashboardPreferences.setPreference(DOCK_PREFERENCE_KEY, false); this._dashboardPreferences.savePreferences(); this.updateODTState([], []); }; /** * @implements ToolbarDockAPI#enableDockedODT */ ToolbarDock.prototype.enableDockedODT = function enableDockedODT() { this._dashboardPreferences.setPreference(DOCK_PREFERENCE_KEY, true); this._dashboardPreferences.savePreferences(); this.updateODTState([], []); }; /** * @implements ToolbarDockAPI#updateODTState */ ToolbarDock.prototype.updateODTState = function updateODTState(actions, selectedIds) { this._dockedToolbarState.actions = this._orderActions(actions); this._dockedToolbarState.selectedIds = selectedIds || undefined; this.invokeCallbacks(ToolbarDockAPI.UPDATE_DOCKED_TOOLBAR_STATE, this._dockedToolbarState); }; /** * @implements ToolbarDockAPI#isContentToolbarDocked */ ToolbarDock.prototype.isContentToolbarDocked = function isContentToolbarDocked() { return this._dashboardPreferences.getPreference(DOCK_PREFERENCE_KEY) || this._currentUIState.focus; }; ToolbarDock.prototype._uiStateChange = function _uiStateChange(updatedState) { var previousState = this._currentUIState; this._currentUIState = Object.assign({}, updatedState); if (updatedState.focus || previousState.focus) { if (this.isContentToolbarDocked()) { this._renderContentToolbar(false, true); } else { this.updateODTState([], []); } } this._updateCanvasToolbar(); }; ToolbarDock.prototype._updateCanvasToolbar = function _updateCanvasToolbar() { this.invokeCallbacks(ToolbarDockAPI.UPDATE_CANVAS_TOOLBAR_STATE, this._getCanvasActions()); }; ToolbarDock.prototype._getCanvasActions = function _getCanvasActions() { var actions = this._orderActions(this._contentActions.getContentActionList([])); return { actions: actions }; }; ToolbarDock.prototype._orderActions = function _orderActions(actions) { actions.forEach(function (item) { if (item.order === undefined) { item.order = ActionTypes[item.name]; } }); actions.sort(function (a, b) { if (a.order && b.order || !a.order && !b.order) { //If both are defined compare. If both are undefined, return NaN, which leaves order unchanged. return a.order - b.order; } // defined order should be before undefined order if (a.order && !b.order) { // a before b return -1; } if (!a.order && b.order) { // b before a return 1; } }); return actions; }; /* * @implements ToolbarDockAPI#focusODT */ ToolbarDock.prototype.focusODT = function focusODT() { this.invokeCallbacks(ToolbarDockAPI.FOCUS_DOCKED_TOOLBAR); }; return ToolbarDock; }(CallbackInvoker); return ToolbarDock; }); //# sourceMappingURL=ToolbarDock.js.map