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.

234 lines
7.8 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 TabsBar, { 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 {
  14. Compose,
  15. Notifications,
  16. HomeTimeline,
  17. CommunityTimeline,
  18. PublicTimeline,
  19. HashtagTimeline,
  20. DirectTimeline,
  21. FavouritedStatuses,
  22. ListTimeline,
  23. Directory,
  24. } from '../../ui/util/async-components';
  25. import Icon from 'mastodon/components/icon';
  26. import ComposePanel from './compose_panel';
  27. import NavigationPanel from './navigation_panel';
  28. import detectPassiveEvents from 'detect-passive-events';
  29. import { scrollRight } from '../../../scroll';
  30. const componentMap = {
  31. 'COMPOSE': Compose,
  32. 'HOME': HomeTimeline,
  33. 'NOTIFICATIONS': Notifications,
  34. 'PUBLIC': PublicTimeline,
  35. 'COMMUNITY': CommunityTimeline,
  36. 'HASHTAG': HashtagTimeline,
  37. 'DIRECT': DirectTimeline,
  38. 'FAVOURITES': FavouritedStatuses,
  39. 'LIST': ListTimeline,
  40. 'DIRECTORY': Directory,
  41. };
  42. const messages = defineMessages({
  43. publish: { id: 'compose_form.publish', defaultMessage: 'Toot' },
  44. });
  45. const shouldHideFAB = path => path.match(/^\/statuses\/|^\/search|^\/getting-started/);
  46. export default @(component => injectIntl(component, { withRef: true }))
  47. class ColumnsArea extends ImmutablePureComponent {
  48. static contextTypes = {
  49. router: PropTypes.object.isRequired,
  50. };
  51. static propTypes = {
  52. intl: PropTypes.object.isRequired,
  53. columns: ImmutablePropTypes.list.isRequired,
  54. isModalOpen: PropTypes.bool.isRequired,
  55. singleColumn: PropTypes.bool,
  56. children: PropTypes.node,
  57. };
  58. state = {
  59. shouldAnimate: false,
  60. }
  61. componentWillReceiveProps() {
  62. this.setState({ shouldAnimate: false });
  63. }
  64. componentDidMount() {
  65. if (!this.props.singleColumn) {
  66. this.node.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
  67. }
  68. this.lastIndex = getIndex(this.context.router.history.location.pathname);
  69. this.isRtlLayout = document.getElementsByTagName('body')[0].classList.contains('rtl');
  70. this.setState({ shouldAnimate: true });
  71. }
  72. componentWillUpdate(nextProps) {
  73. if (this.props.singleColumn !== nextProps.singleColumn && nextProps.singleColumn) {
  74. this.node.removeEventListener('wheel', this.handleWheel);
  75. }
  76. }
  77. componentDidUpdate(prevProps) {
  78. if (this.props.singleColumn !== prevProps.singleColumn && !this.props.singleColumn) {
  79. this.node.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
  80. }
  81. this.lastIndex = getIndex(this.context.router.history.location.pathname);
  82. this.setState({ shouldAnimate: true });
  83. }
  84. componentWillUnmount () {
  85. if (!this.props.singleColumn) {
  86. this.node.removeEventListener('wheel', this.handleWheel);
  87. }
  88. }
  89. handleChildrenContentChange() {
  90. if (!this.props.singleColumn) {
  91. const modifier = this.isRtlLayout ? -1 : 1;
  92. this._interruptScrollAnimation = scrollRight(this.node, (this.node.scrollWidth - window.innerWidth) * modifier);
  93. }
  94. }
  95. handleSwipe = (index) => {
  96. this.pendingIndex = index;
  97. const nextLinkTranslationId = links[index].props['data-preview-title-id'];
  98. const currentLinkSelector = '.tabs-bar__link.active';
  99. const nextLinkSelector = `.tabs-bar__link[data-preview-title-id="${nextLinkTranslationId}"]`;
  100. // HACK: Remove the active class from the current link and set it to the next one
  101. // React-router does this for us, but too late, feeling laggy.
  102. document.querySelector(currentLinkSelector).classList.remove('active');
  103. document.querySelector(nextLinkSelector).classList.add('active');
  104. if (!this.state.shouldAnimate && typeof this.pendingIndex === 'number') {
  105. this.context.router.history.push(getLink(this.pendingIndex));
  106. this.pendingIndex = null;
  107. }
  108. }
  109. handleAnimationEnd = () => {
  110. if (typeof this.pendingIndex === 'number') {
  111. this.context.router.history.push(getLink(this.pendingIndex));
  112. this.pendingIndex = null;
  113. }
  114. }
  115. handleWheel = () => {
  116. if (typeof this._interruptScrollAnimation !== 'function') {
  117. return;
  118. }
  119. this._interruptScrollAnimation();
  120. }
  121. setRef = (node) => {
  122. this.node = node;
  123. }
  124. renderView = (link, index) => {
  125. const columnIndex = getIndex(this.context.router.history.location.pathname);
  126. const title = this.props.intl.formatMessage({ id: link.props['data-preview-title-id'] });
  127. const icon = link.props['data-preview-icon'];
  128. const view = (index === columnIndex) ?
  129. React.cloneElement(this.props.children) :
  130. <ColumnLoading title={title} icon={icon} />;
  131. return (
  132. <div className='columns-area columns-area--mobile' key={index}>
  133. {view}
  134. </div>
  135. );
  136. }
  137. renderLoading = columnId => () => {
  138. return columnId === 'COMPOSE' ? <DrawerLoading /> : <ColumnLoading />;
  139. }
  140. renderError = (props) => {
  141. return <BundleColumnError {...props} />;
  142. }
  143. render () {
  144. const { columns, children, singleColumn, isModalOpen, intl } = this.props;
  145. const { shouldAnimate } = this.state;
  146. const columnIndex = getIndex(this.context.router.history.location.pathname);
  147. if (singleColumn) {
  148. 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)}><Icon id='pencil' /></Link>;
  149. const content = columnIndex !== -1 ? (
  150. <ReactSwipeableViews key='content' index={columnIndex} onChangeIndex={this.handleSwipe} onTransitionEnd={this.handleAnimationEnd} animateTransitions={shouldAnimate} springConfig={{ duration: '400ms', delay: '0s', easeFunction: 'ease' }} style={{ height: '100%' }}>
  151. {links.map(this.renderView)}
  152. </ReactSwipeableViews>
  153. ) : (
  154. <div key='content' className='columns-area columns-area--mobile'>{children}</div>
  155. );
  156. return (
  157. <div className='columns-area__panels'>
  158. <div className='columns-area__panels__pane columns-area__panels__pane--compositional'>
  159. <div className='columns-area__panels__pane__inner'>
  160. <ComposePanel />
  161. </div>
  162. </div>
  163. <div className='columns-area__panels__main'>
  164. <TabsBar key='tabs' />
  165. {content}
  166. </div>
  167. <div className='columns-area__panels__pane columns-area__panels__pane--start columns-area__panels__pane--navigational'>
  168. <div className='columns-area__panels__pane__inner'>
  169. <NavigationPanel />
  170. </div>
  171. </div>
  172. {floatingActionButton}
  173. </div>
  174. );
  175. }
  176. return (
  177. <div className={`columns-area ${ isModalOpen ? 'unscrollable' : '' }`} ref={this.setRef}>
  178. {columns.map(column => {
  179. const params = column.get('params', null) === null ? null : column.get('params').toJS();
  180. const other = params && params.other ? params.other : {};
  181. return (
  182. <BundleContainer key={column.get('uuid')} fetchComponent={componentMap[column.get('id')]} loading={this.renderLoading(column.get('id'))} error={this.renderError}>
  183. {SpecificComponent => <SpecificComponent columnId={column.get('uuid')} params={params} multiColumn {...other} />}
  184. </BundleContainer>
  185. );
  186. })}
  187. {React.Children.map(children, child => React.cloneElement(child, { multiColumn: true }))}
  188. </div>
  189. );
  190. }
  191. }