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.

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