InAppSlideoutState.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. 'use strict';
  2. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * Licensed Materials - Property of IBM
  6. * IBM Business Analytics (C) Copyright IBM Corp. 2019, 2020
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. /**
  10. * @class InAppSlideout
  11. * @hideconstructor
  12. * @classdesc API class that is used to manage InAppSlideout
  13. */
  14. define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory', './InAppSlideoutStateAPI'], function (APIFactory, InAppSlideoutAPI) {
  15. function deepClone(obj) {
  16. return JSON.parse(JSON.stringify(obj));
  17. }
  18. var InAppSlideout = function () {
  19. function InAppSlideout(_ref) {
  20. var features = _ref.features;
  21. _classCallCheck(this, InAppSlideout);
  22. this._dashboardApi = features.API;
  23. this._dashboardState = features.DashboardState;
  24. this._contentActions = features.ContentActions;
  25. this._contributions = {
  26. items: []
  27. };
  28. this._stateListeners = [];
  29. this._previousDashboardState = deepClone(this._dashboardState.getUiState());
  30. this._dashboardStateIgnoreList = ['dirty'];
  31. }
  32. InAppSlideout.prototype.getAPI = function getAPI() {
  33. return this._api;
  34. };
  35. InAppSlideout.prototype.initialize = function initialize() {
  36. this._api = APIFactory.createAPI(this, [InAppSlideoutAPI]);
  37. this._dashboardState.onUiStateChange(this._onUiStateChange.bind(this));
  38. this._updateContributionSelections();
  39. };
  40. InAppSlideout.prototype.getUiState = function getUiState() {
  41. return this._dashboardState.getUiState().sidePanel;
  42. };
  43. InAppSlideout.prototype._filterCaredDashboardState = function _filterCaredDashboardState(dashboardState) {
  44. var _this = this;
  45. return JSON.stringify(dashboardState, function (key, value) {
  46. if (_this._dashboardStateIgnoreList.indexOf(key) !== -1) {
  47. return undefined;
  48. } else {
  49. return value;
  50. }
  51. });
  52. };
  53. InAppSlideout.prototype._onUiStateChange = function _onUiStateChange(state) {
  54. var currentCaredDashboardState = this._filterCaredDashboardState(state);
  55. var previousCaredDashboardState = this._filterCaredDashboardState(this._previousDashboardState);
  56. if (currentCaredDashboardState !== previousCaredDashboardState) {
  57. this._updateContributionSelections(state.sidePanel);
  58. this._stateListeners.forEach(function (fn) {
  59. return fn(state);
  60. });
  61. }
  62. this._previousDashboardState = deepClone(state);
  63. };
  64. InAppSlideout.prototype._updateContributionSelections = function _updateContributionSelections() {
  65. var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.getUiState();
  66. this._contributions.items.forEach(function (_ref2) {
  67. var contribution = _ref2.contribution,
  68. id = _ref2.id;
  69. contribution.selected = state.current === id;
  70. });
  71. };
  72. InAppSlideout.prototype.registerContribution = function registerContribution(id, contentContribution) {
  73. var existing = this.getContribution(id);
  74. if (existing) {
  75. throw new Error('Contribution with id "' + id + '" is already registered');
  76. }
  77. contentContribution = this._populateContentContribution(id, contentContribution);
  78. this._registerContentContribution(id, contentContribution);
  79. this._contributions.items.push({
  80. id: id,
  81. contribution: contentContribution
  82. });
  83. this._updateContributionSelections();
  84. };
  85. InAppSlideout.prototype._populateContentContribution = function _populateContentContribution(id, contribution) {
  86. var _this2 = this;
  87. var toggleState = function toggleState() {
  88. var uiState = _this2.getUiState();
  89. if (uiState.current === id) {
  90. var isOpen = !uiState.isOpen;
  91. _this2._dashboardState.setSidePanelOpen(isOpen);
  92. if (!isOpen) {
  93. // closing side panel clears the current side pane view
  94. _this2._dashboardState.setSidePanelCurrentView(null);
  95. }
  96. } else {
  97. _this2._dashboardState.setSidePanelCurrentView(id);
  98. if (!uiState.isOpen) {
  99. _this2.open();
  100. }
  101. }
  102. };
  103. return _extends({}, contribution, {
  104. checked: this.getUiState().isOpen,
  105. action: toggleState,
  106. offAction: toggleState,
  107. selected: false
  108. });
  109. };
  110. InAppSlideout.prototype._registerContentContribution = function _registerContentContribution(id, contentContribution) {
  111. this._contentActions.registerProvider(id, {
  112. getContentActionList: function getContentActionList() {
  113. var idList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  114. var contributions = [];
  115. if (contentContribution.isEnabled(idList)) {
  116. contributions.push(contentContribution);
  117. }
  118. return contributions;
  119. }
  120. });
  121. };
  122. InAppSlideout.prototype.onStateChange = function onStateChange(fn) {
  123. var _this3 = this;
  124. var newLength = this._stateListeners.push(fn);
  125. var elementIndex = newLength - 1;
  126. var removeFn = function removeFn() {
  127. _this3._stateListeners.splice(elementIndex, 1);
  128. };
  129. return removeFn;
  130. };
  131. InAppSlideout.prototype.removeContribution = function removeContribution(id) {
  132. var index = this._contributions.items.findIndex(function (item) {
  133. return item.id === id;
  134. });
  135. if (index === -1) {
  136. throw new Error('Contribution with id "' + id + '" does not exist');
  137. }
  138. this._contributions.items.splice(index, 1);
  139. };
  140. InAppSlideout.prototype.getContribution = function getContribution(id) {
  141. return this._contributions.items.find(function (item) {
  142. return item.id === id;
  143. });
  144. };
  145. InAppSlideout.prototype.getContributions = function getContributions() {
  146. return this._contributions.items;
  147. };
  148. InAppSlideout.prototype.getCurrent = function getCurrent() {
  149. var _getUiState = this.getUiState(),
  150. current = _getUiState.current;
  151. return this.getContribution(current);
  152. };
  153. InAppSlideout.prototype.isOpen = function isOpen() {
  154. return this._dashboardState.getUiState().sidePanel.isOpen;
  155. };
  156. InAppSlideout.prototype.open = function open() {
  157. this._dashboardState.setSidePanelOpen(true);
  158. };
  159. InAppSlideout.prototype.close = function close() {
  160. this._dashboardState.setSidePanelOpen(false);
  161. this._dashboardState.setSidePanelCurrentView(null);
  162. };
  163. InAppSlideout.prototype.destroy = function destroy() {
  164. this._dashboardApi = null;
  165. this._api = null;
  166. };
  167. return InAppSlideout;
  168. }();
  169. return InAppSlideout;
  170. });
  171. //# sourceMappingURL=InAppSlideoutState.js.map