闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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