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.

200 lines
5.9 KiB

  1. import React, { PureComponent } from 'react';
  2. import { ScrollContainer } from 'react-router-scroll-4';
  3. import PropTypes from 'prop-types';
  4. import IntersectionObserverArticleContainer from '../containers/intersection_observer_article_container';
  5. import LoadMore from './load_more';
  6. import IntersectionObserverWrapper from '../features/ui/util/intersection_observer_wrapper';
  7. import { throttle } from 'lodash';
  8. import { List as ImmutableList } from 'immutable';
  9. import classNames from 'classnames';
  10. import { attachFullscreenListener, detachFullscreenListener, isFullscreen } from '../features/ui/util/fullscreen';
  11. export default class ScrollableList extends PureComponent {
  12. static contextTypes = {
  13. router: PropTypes.object,
  14. };
  15. static propTypes = {
  16. scrollKey: PropTypes.string.isRequired,
  17. onLoadMore: PropTypes.func.isRequired,
  18. onScrollToTop: PropTypes.func,
  19. onScroll: PropTypes.func,
  20. trackScroll: PropTypes.bool,
  21. shouldUpdateScroll: PropTypes.func,
  22. isLoading: PropTypes.bool,
  23. hasMore: PropTypes.bool,
  24. prepend: PropTypes.node,
  25. emptyMessage: PropTypes.node,
  26. children: PropTypes.node,
  27. };
  28. static defaultProps = {
  29. trackScroll: true,
  30. };
  31. state = {
  32. lastMouseMove: null,
  33. };
  34. intersectionObserverWrapper = new IntersectionObserverWrapper();
  35. handleScroll = throttle(() => {
  36. if (this.node) {
  37. const { scrollTop, scrollHeight, clientHeight } = this.node;
  38. const offset = scrollHeight - scrollTop - clientHeight;
  39. this._oldScrollPosition = scrollHeight - scrollTop;
  40. if (400 > offset && this.props.onLoadMore && !this.props.isLoading) {
  41. this.props.onLoadMore();
  42. }
  43. if (scrollTop < 100 && this.props.onScrollToTop) {
  44. this.props.onScrollToTop();
  45. } else if (this.props.onScroll) {
  46. this.props.onScroll();
  47. }
  48. }
  49. }, 150, {
  50. trailing: true,
  51. });
  52. handleMouseMove = throttle(() => {
  53. this._lastMouseMove = new Date();
  54. }, 300);
  55. handleMouseLeave = () => {
  56. this._lastMouseMove = null;
  57. }
  58. componentDidMount () {
  59. this.attachScrollListener();
  60. this.attachIntersectionObserver();
  61. attachFullscreenListener(this.onFullScreenChange);
  62. // Handle initial scroll posiiton
  63. this.handleScroll();
  64. }
  65. componentDidUpdate (prevProps) {
  66. const someItemInserted = React.Children.count(prevProps.children) > 0 &&
  67. React.Children.count(prevProps.children) < React.Children.count(this.props.children) &&
  68. this.getFirstChildKey(prevProps) !== this.getFirstChildKey(this.props);
  69. // Reset the scroll position when a new child comes in in order not to
  70. // jerk the scrollbar around if you're already scrolled down the page.
  71. if (someItemInserted && this._oldScrollPosition && this.node.scrollTop > 0) {
  72. const newScrollTop = this.node.scrollHeight - this._oldScrollPosition;
  73. if (this.node.scrollTop !== newScrollTop) {
  74. this.node.scrollTop = newScrollTop;
  75. }
  76. } else {
  77. this._oldScrollPosition = this.node.scrollHeight - this.node.scrollTop;
  78. }
  79. }
  80. componentWillUnmount () {
  81. this.detachScrollListener();
  82. this.detachIntersectionObserver();
  83. detachFullscreenListener(this.onFullScreenChange);
  84. }
  85. onFullScreenChange = () => {
  86. this.setState({ fullscreen: isFullscreen() });
  87. }
  88. attachIntersectionObserver () {
  89. this.intersectionObserverWrapper.connect({
  90. root: this.node,
  91. rootMargin: '300% 0px',
  92. });
  93. }
  94. detachIntersectionObserver () {
  95. this.intersectionObserverWrapper.disconnect();
  96. }
  97. attachScrollListener () {
  98. this.node.addEventListener('scroll', this.handleScroll);
  99. }
  100. detachScrollListener () {
  101. this.node.removeEventListener('scroll', this.handleScroll);
  102. }
  103. getFirstChildKey (props) {
  104. const { children } = props;
  105. let firstChild = children;
  106. if (children instanceof ImmutableList) {
  107. firstChild = children.get(0);
  108. } else if (Array.isArray(children)) {
  109. firstChild = children[0];
  110. }
  111. return firstChild && firstChild.key;
  112. }
  113. setRef = (c) => {
  114. this.node = c;
  115. }
  116. handleLoadMore = (e) => {
  117. e.preventDefault();
  118. this.props.onLoadMore();
  119. }
  120. _recentlyMoved () {
  121. return this._lastMouseMove !== null && ((new Date()) - this._lastMouseMove < 600);
  122. }
  123. render () {
  124. const { children, scrollKey, trackScroll, shouldUpdateScroll, isLoading, hasMore, prepend, emptyMessage } = this.props;
  125. const { fullscreen } = this.state;
  126. const childrenCount = React.Children.count(children);
  127. const loadMore = (hasMore && childrenCount > 0) ? <LoadMore visible={!isLoading} onClick={this.handleLoadMore} /> : null;
  128. let scrollableArea = null;
  129. if (isLoading || childrenCount > 0 || !emptyMessage) {
  130. scrollableArea = (
  131. <div className={classNames('scrollable', { fullscreen })} ref={this.setRef} onMouseMove={this.handleMouseMove} onMouseLeave={this.handleMouseLeave}>
  132. <div role='feed' className='item-list'>
  133. {prepend}
  134. {React.Children.map(this.props.children, (child, index) => (
  135. <IntersectionObserverArticleContainer
  136. key={child.key}
  137. id={child.key}
  138. index={index}
  139. listLength={childrenCount}
  140. intersectionObserverWrapper={this.intersectionObserverWrapper}
  141. saveHeightKey={trackScroll ? `${this.context.router.route.location.key}:${scrollKey}` : null}
  142. >
  143. {child}
  144. </IntersectionObserverArticleContainer>
  145. ))}
  146. {loadMore}
  147. </div>
  148. </div>
  149. );
  150. } else {
  151. scrollableArea = (
  152. <div className='empty-column-indicator' ref={this.setRef}>
  153. {emptyMessage}
  154. </div>
  155. );
  156. }
  157. if (trackScroll) {
  158. return (
  159. <ScrollContainer scrollKey={scrollKey} shouldUpdateScroll={shouldUpdateScroll}>
  160. {scrollableArea}
  161. </ScrollContainer>
  162. );
  163. } else {
  164. return scrollableArea;
  165. }
  166. }
  167. }