You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.7 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { defineMessages, injectIntl } from 'react-intl';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import ImmutablePureComponent from 'react-immutable-pure-component';
  6. import ReactSwipeableViews from 'react-swipeable-views';
  7. import { links, getIndex, getLink } from './tabs_bar';
  8. import { Link } from 'react-router-dom';
  9. import BundleContainer from '../containers/bundle_container';
  10. import ColumnLoading from './column_loading';
  11. import DrawerLoading from './drawer_loading';
  12. import BundleColumnError from './bundle_column_error';
  13. import { Compose, Notifications, HomeTimeline, CommunityTimeline, PublicTimeline, HashtagTimeline, DirectTimeline, FavouritedStatuses, ListTimeline } from '../../ui/util/async-components';
  14. import detectPassiveEvents from 'detect-passive-events';
  15. import { scrollRight } from '../../../scroll';
  16. const componentMap = {
  17. 'COMPOSE': Compose,
  18. 'HOME': HomeTimeline,
  19. 'NOTIFICATIONS': Notifications,
  20. 'PUBLIC': PublicTimeline,
  21. 'COMMUNITY': CommunityTimeline,
  22. 'HASHTAG': HashtagTimeline,
  23. 'DIRECT': DirectTimeline,
  24. 'FAVOURITES': FavouritedStatuses,
  25. 'LIST': ListTimeline,
  26. };
  27. const messages = defineMessages({
  28. publish: { id: 'compose_form.publish', defaultMessage: 'Toot' },
  29. });
  30. const shouldHideFAB = path => path.match(/^\/statuses\//);
  31. export default @(component => injectIntl(component, { withRef: true }))
  32. class ColumnsArea extends ImmutablePureComponent {
  33. static contextTypes = {
  34. router: PropTypes.object.isRequired,
  35. };
  36. static propTypes = {
  37. intl: PropTypes.object.isRequired,
  38. columns: ImmutablePropTypes.list.isRequired,
  39. isModalOpen: PropTypes.bool.isRequired,
  40. singleColumn: PropTypes.bool,
  41. children: PropTypes.node,
  42. };
  43. state = {
  44. shouldAnimate: false,
  45. }
  46. componentWillReceiveProps() {
  47. this.setState({ shouldAnimate: false });
  48. }
  49. componentDidMount() {
  50. if (!this.props.singleColumn) {
  51. this.node.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
  52. }
  53. this.lastIndex = getIndex(this.context.router.history.location.pathname);
  54. this.isRtlLayout = document.getElementsByTagName('body')[0].classList.contains('rtl');
  55. this.setState({ shouldAnimate: true });
  56. }
  57. componentWillUpdate(nextProps) {
  58. if (this.props.singleColumn !== nextProps.singleColumn && nextProps.singleColumn) {
  59. this.node.removeEventListener('wheel', this.handleWheel);
  60. }
  61. }
  62. componentDidUpdate(prevProps) {
  63. if (this.props.singleColumn !== prevProps.singleColumn && !this.props.singleColumn) {
  64. this.node.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
  65. }
  66. this.lastIndex = getIndex(this.context.router.history.location.pathname);
  67. this.setState({ shouldAnimate: true });
  68. }
  69. componentWillUnmount () {
  70. if (!this.props.singleColumn) {
  71. this.node.removeEventListener('wheel', this.handleWheel);
  72. }
  73. }
  74. handleChildrenContentChange() {
  75. if (!this.props.singleColumn) {
  76. const modifier = this.isRtlLayout ? -1 : 1;
  77. this._interruptScrollAnimation = scrollRight(this.node, (this.node.scrollWidth - window.innerWidth) * modifier);
  78. }
  79. }
  80. handleSwipe = (index) => {
  81. this.pendingIndex = index;
  82. const nextLinkTranslationId = links[index].props['data-preview-title-id'];
  83. const currentLinkSelector = '.tabs-bar__link.active';
  84. const nextLinkSelector = `.tabs-bar__link[data-preview-title-id="${nextLinkTranslationId}"]`;
  85. // HACK: Remove the active class from the current link and set it to the next one
  86. // React-router does this for us, but too late, feeling laggy.
  87. document.querySelector(currentLinkSelector).classList.remove('active');
  88. document.querySelector(nextLinkSelector).classList.add('active');
  89. }
  90. handleAnimationEnd = () => {
  91. if (typeof this.pendingIndex === 'number') {
  92. this.context.router.history.push(getLink(this.pendingIndex));
  93. this.pendingIndex = null;
  94. }
  95. }
  96. handleWheel = () => {
  97. if (typeof this._interruptScrollAnimation !== 'function') {
  98. return;
  99. }
  100. this._interruptScrollAnimation();
  101. }
  102. setRef = (node) => {
  103. this.node = node;
  104. }
  105. renderView = (link, index) => {
  106. const columnIndex = getIndex(this.context.router.history.location.pathname);
  107. const title = this.props.intl.formatMessage({ id: link.props['data-preview-title-id'] });
  108. const icon = link.props['data-preview-icon'];
  109. const view = (index === columnIndex) ?
  110. React.cloneElement(this.props.children) :
  111. <ColumnLoading title={title} icon={icon} />;
  112. return (
  113. <div className='columns-area' key={index}>
  114. {view}
  115. </div>
  116. );
  117. }
  118. renderLoading = columnId => () => {
  119. return columnId === 'COMPOSE' ? <DrawerLoading /> : <ColumnLoading />;
  120. }
  121. renderError = (props) => {
  122. return <BundleColumnError {...props} />;
  123. }
  124. render () {
  125. const { columns, children, singleColumn, isModalOpen, intl } = this.props;
  126. const { shouldAnimate } = this.state;
  127. const columnIndex = getIndex(this.context.router.history.location.pathname);
  128. this.pendingIndex = null;
  129. if (singleColumn) {
  130. const floatingActionButton = shouldHideFAB(this.context.router.history.location.pathname) ? null : <Link key='floating-action-button' to='/statuses/new' className='floating-action-button' aria-label={intl.formatMessage(messages.publish)}><i className='fas fa-pencil-alt' /></Link>;
  131. return columnIndex !== -1 ? [
  132. <ReactSwipeableViews key='content' index={columnIndex} onChangeIndex={this.handleSwipe} onTransitionEnd={this.handleAnimationEnd} animateTransitions={shouldAnimate} springConfig={{ duration: '400ms', delay: '0s', easeFunction: 'ease' }} style={{ height: '100%' }}>
  133. {links.map(this.renderView)}
  134. </ReactSwipeableViews>,
  135. floatingActionButton,
  136. ] : [
  137. <div className='columns-area'>{children}</div>,
  138. floatingActionButton,
  139. ];
  140. }
  141. return (
  142. <div className={`columns-area ${ isModalOpen ? 'unscrollable' : '' }`} ref={this.setRef}>
  143. {columns.map(column => {
  144. const params = column.get('params', null) === null ? null : column.get('params').toJS();
  145. const other = params && params.other ? params.other : {};
  146. return (
  147. <BundleContainer key={column.get('uuid')} fetchComponent={componentMap[column.get('id')]} loading={this.renderLoading(column.get('id'))} error={this.renderError}>
  148. {SpecificComponent => <SpecificComponent columnId={column.get('uuid')} params={params} multiColumn {...other} />}
  149. </BundleContainer>
  150. );
  151. })}
  152. {React.Children.map(children, child => React.cloneElement(child, { multiColumn: true }))}
  153. </div>
  154. );
  155. }
  156. }