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.

188 lines
6.4 KiB

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