logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

columns_area.js (6573B)


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