InAppSlideoutContainer.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. 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; }
  5. 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; }
  6. /*
  7. *+------------------------------------------------------------------------+
  8. *| Licensed Materials - Property of IBM
  9. *| IBM Cognos Products: Content Explorer
  10. *| (C) Copyright IBM Corp. 2019, 2020
  11. *|
  12. *| US Government Users Restricted Rights - Use, duplication or disclosure
  13. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  14. *+------------------------------------------------------------------------+
  15. */
  16. define(['react', 'ca-ui-toolkit', 'prop-types', 'jquery'], function (React, _ref, PropTypes, $) {
  17. var Container = _ref.Container,
  18. Slideout = _ref.Slideout;
  19. // TODO: use colors from ba-ui-toolkit essentials
  20. var BORDER_COLOR = '#eaeaea'; // $gray1
  21. var CONTRIBUTION_CLASS_NAME = 'slideout__contributions';
  22. var createContributionNode = function createContributionNode(contributionId) {
  23. var node = document.createElement('div');
  24. node.dataset.contributionId = contributionId;
  25. node.setAttribute('class', CONTRIBUTION_CLASS_NAME + '__item');
  26. node.style.overflow = 'auto';
  27. return node;
  28. };
  29. var contributionNodeMatchFn = function contributionNodeMatchFn(node, contributionId) {
  30. return node.dataset.contributionId === contributionId;
  31. };
  32. var findContributionNode = function findContributionNode(parentNode, contributionId) {
  33. var childNodes = Array.from(parentNode.childNodes);
  34. return childNodes.find(function (child) {
  35. return contributionNodeMatchFn(child, contributionId);
  36. });
  37. };
  38. /**
  39. * Hide all contribution nodes, except the node that matches the current contribution id.
  40. * The nodes representing the current contribution, should always be displayed, everything
  41. * else should be hidden.
  42. * @param {Node} parentNode - DOM element containing all the contributions
  43. * @param {string} contributionId - current contribution id
  44. */
  45. var hideContributionNodesExceptCurrent = function hideContributionNodesExceptCurrent(parentNode, contributionId) {
  46. var childNodes = Array.from(parentNode.childNodes);
  47. childNodes.forEach(function (child) {
  48. child.style.display = contributionNodeMatchFn(child, contributionId) ? '' : 'none';
  49. });
  50. };
  51. var InAppSlideoutContainer = function (_React$Component) {
  52. _inherits(InAppSlideoutContainer, _React$Component);
  53. function InAppSlideoutContainer(props) {
  54. _classCallCheck(this, InAppSlideoutContainer);
  55. var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
  56. _this.contentRef = React.createRef();
  57. return _this;
  58. }
  59. InAppSlideoutContainer.prototype.componentDidMount = function componentDidMount() {
  60. this._updateInAppSlideout();
  61. };
  62. InAppSlideoutContainer.prototype.componentWillUnmount = function componentWillUnmount() {
  63. this._onComponentMountedChange( /*isMounted*/false);
  64. };
  65. InAppSlideoutContainer.prototype.componentDidUpdate = function componentDidUpdate() {
  66. this._updateInAppSlideout();
  67. };
  68. InAppSlideoutContainer.prototype._getContainerNode = function _getContainerNode() {
  69. var contribution = this.props.contribution;
  70. var contributionId = contribution.getProviderId();
  71. var refNode = this.contentRef.current;
  72. if (!refNode) {
  73. // If something goes wrong and we can't get ahold of the content reference,
  74. // we will be unable to build a parent node
  75. return;
  76. }
  77. var resultNode = findContributionNode(refNode, contributionId);
  78. if (!resultNode) {
  79. resultNode = createContributionNode(contributionId);
  80. refNode.appendChild(resultNode);
  81. }
  82. return resultNode;
  83. };
  84. InAppSlideoutContainer.prototype._clearContainerNode = function _clearContainerNode() {
  85. var containerNode = this._getContainerNode();
  86. if (containerNode) {
  87. containerNode.innerHTML = '';
  88. }
  89. };
  90. InAppSlideoutContainer.prototype._updateInAppSlideout = function _updateInAppSlideout() {
  91. var _this2 = this;
  92. var _props = this.props,
  93. contribution = _props.contribution,
  94. options = _props.options;
  95. if (!contribution) {
  96. return;
  97. }
  98. var contributionId = contribution.getProviderId();
  99. hideContributionNodesExceptCurrent(this.contentRef.current, contributionId);
  100. var containerNode = this._getContainerNode();
  101. if (!containerNode) {
  102. return;
  103. }
  104. contribution.render(_extends({}, options, {
  105. parentNode: containerNode
  106. })).then(function (node) {
  107. if (!node) {
  108. return;
  109. }
  110. var childPanelId = node.dataset.childPanelId;
  111. if (options.isChild || childPanelId) {
  112. _this2._updateChildView(node, options);
  113. } else {
  114. _this2._updateParentView(node);
  115. }
  116. _this2._onComponentMountedChange( /*isMounted*/true);
  117. });
  118. };
  119. InAppSlideoutContainer.prototype._updateChildView = function _updateChildView(node) {
  120. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  121. var active = this.props.active;
  122. if (active) {
  123. var containerNode = this._getContainerNode();
  124. var firstChild = containerNode.firstElementChild;
  125. if (firstChild) {
  126. if (containerNode.childNodes.length > 1) {
  127. // Canvas Tab properties is the only in R7 that uses refreshChild flag
  128. if (options.refreshChild) {
  129. // if is refreshing, then remove old child
  130. // using jquery remove can also clean up any attached handlers
  131. $(firstChild).remove();
  132. firstChild = containerNode.firstElementChild;
  133. }
  134. } else {
  135. firstChild.style.display = 'none';
  136. }
  137. containerNode.insertBefore(node, firstChild);
  138. containerNode.childNodes[0].focus();
  139. }
  140. }
  141. };
  142. InAppSlideoutContainer.prototype._updateParentView = function _updateParentView(node) {
  143. var active = this.props.active;
  144. if (active) {
  145. this._clearContainerNode();
  146. var containerNode = this._getContainerNode();
  147. if (containerNode) {
  148. containerNode.insertBefore(node, containerNode.firstElementChild);
  149. containerNode.childNodes[0].focus();
  150. }
  151. }
  152. };
  153. /**
  154. * Invoke registered views when the InAppSlideoutContainer component get mount/unmounted in the DOM
  155. *
  156. * @param {booean} isMounted -isMounted boolean flag
  157. */
  158. InAppSlideoutContainer.prototype._onComponentMountedChange = function _onComponentMountedChange(isMounted) {
  159. var _props2 = this.props,
  160. onDomStateChange = _props2.onDomStateChange,
  161. contribution = _props2.contribution;
  162. if (contribution && contribution.getProviderId && onDomStateChange) {
  163. onDomStateChange({
  164. type: isMounted ? 'mounted' : 'unmounted',
  165. current: contribution.getProviderId()
  166. });
  167. }
  168. };
  169. InAppSlideoutContainer.prototype.render = function render() {
  170. var active = this.props.active;
  171. return React.createElement(
  172. Container,
  173. { relative: true, height: '100%', width: '100%' },
  174. React.createElement(
  175. Slideout,
  176. {
  177. animationOnRender: true,
  178. toDirection: '',
  179. active: active,
  180. width: '100%',
  181. style: {
  182. borderLeft: '1px solid ' + BORDER_COLOR,
  183. borderTop: '1px solid ' + BORDER_COLOR
  184. }
  185. },
  186. React.createElement('div', { ref: this.contentRef, className: CONTRIBUTION_CLASS_NAME })
  187. )
  188. );
  189. };
  190. return InAppSlideoutContainer;
  191. }(React.Component);
  192. InAppSlideoutContainer.propTypes = {
  193. active: PropTypes.bool,
  194. contribution: PropTypes.object.isRequired,
  195. options: PropTypes.object,
  196. onDomStateChange: PropTypes.func
  197. };
  198. InAppSlideoutContainer.defaultProps = {
  199. active: false,
  200. options: {},
  201. onDomStateChange: function onDomStateChange() {}
  202. };
  203. return InAppSlideoutContainer;
  204. });
  205. //# sourceMappingURL=InAppSlideoutContainer.js.map