PageAccessoriesModal.jsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import React, { useEffect, useState } from 'react';
  2. import PropTypes from 'prop-types';
  3. import {
  4. Modal, ModalBody, ModalHeader, Nav, NavItem, NavLink, TabContent, TabPane,
  5. } from 'reactstrap';
  6. import { withTranslation } from 'react-i18next';
  7. import PageListIcon from './Icons/PageListIcon';
  8. import TimeLineIcon from './Icons/TimeLineIcon';
  9. import RecentChangesIcon from './Icons/RecentChangesIcon';
  10. import AttachmentIcon from './Icons/AttachmentIcon';
  11. import ShareLinkIcon from './Icons/ShareLinkIcon';
  12. import { withUnstatedContainers } from './UnstatedUtils';
  13. import PageAccessoriesContainer from '../services/PageAccessoriesContainer';
  14. import PageAttachment from './PageAttachment';
  15. import PageTimeline from './PageTimeline';
  16. import PageList from './PageList';
  17. import PageHistory from './PageHistory';
  18. import ShareLink from './ShareLink/ShareLink';
  19. const navTabMapping = {
  20. pagelist: {
  21. icon: <PageListIcon />,
  22. i18n: 'page_list',
  23. index: 0,
  24. },
  25. timeline: {
  26. icon: <TimeLineIcon />,
  27. i18n: 'Timeline View',
  28. index: 1,
  29. },
  30. pageHistory: {
  31. icon: <RecentChangesIcon />,
  32. i18n: 'History',
  33. index: 2,
  34. },
  35. attachment: {
  36. icon: <AttachmentIcon />,
  37. i18n: 'attachment_data',
  38. index: 3,
  39. },
  40. shareLink: {
  41. icon: <ShareLinkIcon />,
  42. i18n: 'share_links.share_link_management',
  43. index: 4,
  44. },
  45. };
  46. const PageAccessoriesModal = (props) => {
  47. const { t, pageAccessoriesContainer } = props;
  48. const { switchActiveTab } = pageAccessoriesContainer;
  49. const { activeTab } = pageAccessoriesContainer.state;
  50. const [sliderWidth, setSliderWidth] = useState(null);
  51. const [sliderMarginLeft, setSliderMarginLeft] = useState(null);
  52. function closeModalHandler() {
  53. if (props.onClose == null) {
  54. return;
  55. }
  56. props.onClose();
  57. }
  58. // Might make this dynamic for px, %, pt, em
  59. function getPercentage(min, max) {
  60. return min / max * 100;
  61. }
  62. useEffect(() => {
  63. if (activeTab === '') {
  64. return;
  65. }
  66. const navTitle = document.getElementById('nav-title');
  67. const navTabs = document.querySelectorAll('li.nav-link');
  68. if (navTitle == null || navTabs == null) {
  69. return;
  70. }
  71. let tempML = 0;
  72. const styles = [].map.call(navTabs, (el) => {
  73. const width = getPercentage(el.offsetWidth, navTitle.offsetWidth);
  74. const marginLeft = tempML;
  75. tempML += width;
  76. return { width, marginLeft };
  77. });
  78. const { width, marginLeft } = styles[navTabMapping[activeTab].index];
  79. setSliderWidth(width);
  80. setSliderMarginLeft(marginLeft);
  81. }, [activeTab]);
  82. return (
  83. <React.Fragment>
  84. <Modal size="xl" isOpen={props.isOpen} toggle={closeModalHandler} className="grw-page-accessories-modal">
  85. {/* [TODO: insert a modal header and move nav tabs there by gw-3890] */}
  86. <ModalHeader className="p-0" toggle={closeModalHandler}>
  87. <Nav className="nav-title" id="nav-title">
  88. {Object.entries(navTabMapping).map(([key, value]) => {
  89. return (
  90. <NavItem key={key} type="button" className={`p-0 nav-link ${activeTab === key && 'active'}`}>
  91. <NavLink onClick={() => { switchActiveTab(key) }}>
  92. {value.icon}
  93. {t(value.i18n)}
  94. </NavLink>
  95. </NavItem>
  96. );
  97. })}
  98. </Nav>
  99. <hr className="my-0 grw-nav-slide-hr border-none" style={{ width: `${sliderWidth}%`, marginLeft: `${sliderMarginLeft}%` }} />
  100. </ModalHeader>
  101. <ModalBody className="overflow-auto grw-modal-body-style p-0">
  102. <TabContent activeTab={activeTab} className="p-5">
  103. <TabPane tabId="pagelist">
  104. {pageAccessoriesContainer.state.activeComponents.has('pagelist') && <PageList />}
  105. </TabPane>
  106. <TabPane tabId="timeline">
  107. {pageAccessoriesContainer.state.activeComponents.has('timeline') && <PageTimeline /> }
  108. </TabPane>
  109. <TabPane tabId="pageHistory">
  110. <div className="overflow-auto">
  111. {pageAccessoriesContainer.state.activeComponents.has('pageHistory') && <PageHistory /> }
  112. </div>
  113. </TabPane>
  114. <TabPane tabId="attachment">
  115. {pageAccessoriesContainer.state.activeComponents.has('attachment') && <PageAttachment />}
  116. </TabPane>
  117. <TabPane tabId="shareLink">
  118. {pageAccessoriesContainer.state.activeComponents.has('shareLink') && <ShareLink />}
  119. </TabPane>
  120. </TabContent>
  121. </ModalBody>
  122. </Modal>
  123. </React.Fragment>
  124. );
  125. };
  126. /**
  127. * Wrapper component for using unstated
  128. */
  129. const PageAccessoriesModalWrapper = withUnstatedContainers(PageAccessoriesModal, [PageAccessoriesContainer]);
  130. PageAccessoriesModal.propTypes = {
  131. t: PropTypes.func.isRequired, // i18next
  132. // pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  133. pageAccessoriesContainer: PropTypes.instanceOf(PageAccessoriesContainer).isRequired,
  134. isOpen: PropTypes.bool.isRequired,
  135. onClose: PropTypes.func,
  136. };
  137. export default withTranslation()(PageAccessoriesModalWrapper);