The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

199 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. onScrollToBottom: PropTypes.func,
  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.onScrollToBottom && !this.props.isLoading) {
  41. this.props.onScrollToBottom();
  42. } else if (scrollTop < 100 && this.props.onScrollToTop) {
  43. this.props.onScrollToTop();
  44. } else if (this.props.onScroll) {
  45. this.props.onScroll();
  46. }
  47. }
  48. }, 150, {
  49. trailing: true,
  50. });
  51. handleMouseMove = throttle(() => {
  52. this._lastMouseMove = new Date();
  53. }, 300);
  54. handleMouseLeave = () => {
  55. this._lastMouseMove = null;
  56. }
  57. componentDidMount () {
  58. this.attachScrollListener();
  59. this.attachIntersectionObserver();
  60. attachFullscreenListener(this.onFullScreenChange);
  61. // Handle initial scroll posiiton
  62. this.handleScroll();
  63. }
  64. componentDidUpdate (prevProps) {
  65. const someItemInserted = React.Children.count(prevProps.children) > 0 &&
  66. React.Children.count(prevProps.children) < React.Children.count(this.props.children) &&
  67. this.getFirstChildKey(prevProps) !== this.getFirstChildKey(this.props);
  68. // Reset the scroll position when a new child comes in in order not to
  69. // jerk the scrollbar around if you're already scrolled down the page.
  70. if (someItemInserted && this._oldScrollPosition && this.node.scrollTop > 0) {
  71. const newScrollTop = this.node.scrollHeight - this._oldScrollPosition;
  72. if (this.node.scrollTop !== newScrollTop) {
  73. this.node.scrollTop = newScrollTop;
  74. }
  75. } else {
  76. this._oldScrollPosition = this.node.scrollHeight - this.node.scrollTop;
  77. }
  78. }
  79. componentWillUnmount () {
  80. this.detachScrollListener();
  81. this.detachIntersectionObserver();
  82. detachFullscreenListener(this.onFullScreenChange);
  83. }
  84. onFullScreenChange = () => {
  85. this.setState({ fullscreen: isFullscreen() });
  86. }
  87. attachIntersectionObserver () {
  88. this.intersectionObserverWrapper.connect({
  89. root: this.node,
  90. rootMargin: '300% 0px',
  91. });
  92. }
  93. detachIntersectionObserver () {
  94. this.intersectionObserverWrapper.disconnect();
  95. }
  96. attachScrollListener () {
  97. this.node.addEventListener('scroll', this.handleScroll);
  98. }
  99. detachScrollListener () {
  100. this.node.removeEventListener('scroll', this.handleScroll);
  101. }
  102. getFirstChildKey (props) {
  103. const { children } = props;
  104. let firstChild = children;
  105. if (children instanceof ImmutableList) {
  106. firstChild = children.get(0);
  107. } else if (Array.isArray(children)) {
  108. firstChild = children[0];
  109. }
  110. return firstChild && firstChild.key;
  111. }
  112. setRef = (c) => {
  113. this.node = c;
  114. }
  115. handleLoadMore = (e) => {
  116. e.preventDefault();
  117. this.props.onScrollToBottom();
  118. }
  119. _recentlyMoved () {
  120. return this._lastMouseMove !== null && ((new Date()) - this._lastMouseMove < 600);
  121. }
  122. render () {
  123. const { children, scrollKey, trackScroll, shouldUpdateScroll, isLoading, hasMore, prepend, emptyMessage } = this.props;
  124. const { fullscreen } = this.state;
  125. const childrenCount = React.Children.count(children);
  126. const loadMore = (hasMore && childrenCount > 0) ? <LoadMore visible={!isLoading} onClick={this.handleLoadMore} /> : null;
  127. let scrollableArea = null;
  128. if (isLoading || childrenCount > 0 || !emptyMessage) {
  129. scrollableArea = (
  130. <div className={classNames('scrollable', { fullscreen })} ref={this.setRef} onMouseMove={this.handleMouseMove} onMouseLeave={this.handleMouseLeave}>
  131. <div role='feed' className='item-list'>
  132. {prepend}
  133. {React.Children.map(this.props.children, (child, index) => (
  134. <IntersectionObserverArticleContainer
  135. key={child.key}
  136. id={child.key}
  137. index={index}
  138. listLength={childrenCount}
  139. intersectionObserverWrapper={this.intersectionObserverWrapper}
  140. saveHeightKey={trackScroll ? `${this.context.router.route.location.key}:${scrollKey}` : null}
  141. >
  142. {child}
  143. </IntersectionObserverArticleContainer>
  144. ))}
  145. {loadMore}
  146. </div>
  147. </div>
  148. );
  149. } else {
  150. scrollableArea = (
  151. <div className='empty-column-indicator' ref={this.setRef}>
  152. {emptyMessage}
  153. </div>
  154. );
  155. }
  156. if (trackScroll) {
  157. return (
  158. <ScrollContainer scrollKey={scrollKey} shouldUpdateScroll={shouldUpdateScroll}>
  159. {scrollableArea}
  160. </ScrollContainer>
  161. );
  162. } else {
  163. return scrollableArea;
  164. }
  165. }
  166. }