The code powering m.abunchtell.com https://m.abunchtell.com
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

140 Zeilen
4.3 KiB

  1. import { debounce } from 'lodash';
  2. import React from 'react';
  3. import { FormattedMessage } from 'react-intl';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import PropTypes from 'prop-types';
  6. import StatusContainer from '../containers/status_container';
  7. import ImmutablePureComponent from 'react-immutable-pure-component';
  8. import LoadGap from './load_gap';
  9. import ScrollableList from './scrollable_list';
  10. export default class StatusList extends ImmutablePureComponent {
  11. static propTypes = {
  12. scrollKey: PropTypes.string.isRequired,
  13. statusIds: ImmutablePropTypes.list.isRequired,
  14. featuredStatusIds: ImmutablePropTypes.list,
  15. onLoadMore: PropTypes.func,
  16. onScrollToTop: PropTypes.func,
  17. onScroll: PropTypes.func,
  18. trackScroll: PropTypes.bool,
  19. shouldUpdateScroll: PropTypes.func,
  20. isLoading: PropTypes.bool,
  21. isPartial: PropTypes.bool,
  22. hasMore: PropTypes.bool,
  23. prepend: PropTypes.node,
  24. emptyMessage: PropTypes.node,
  25. alwaysPrepend: PropTypes.bool,
  26. timelineId: PropTypes.string,
  27. };
  28. static defaultProps = {
  29. trackScroll: true,
  30. };
  31. getFeaturedStatusCount = () => {
  32. return this.props.featuredStatusIds ? this.props.featuredStatusIds.size : 0;
  33. }
  34. getCurrentStatusIndex = (id, featured) => {
  35. if (featured) {
  36. return this.props.featuredStatusIds.indexOf(id);
  37. } else {
  38. return this.props.statusIds.indexOf(id) + this.getFeaturedStatusCount();
  39. }
  40. }
  41. handleMoveUp = (id, featured) => {
  42. const elementIndex = this.getCurrentStatusIndex(id, featured) - 1;
  43. this._selectChild(elementIndex, true);
  44. }
  45. handleMoveDown = (id, featured) => {
  46. const elementIndex = this.getCurrentStatusIndex(id, featured) + 1;
  47. this._selectChild(elementIndex, false);
  48. }
  49. handleLoadOlder = debounce(() => {
  50. this.props.onLoadMore(this.props.statusIds.size > 0 ? this.props.statusIds.last() : undefined);
  51. }, 300, { leading: true })
  52. _selectChild (index, align_top) {
  53. const container = this.node.node;
  54. const element = container.querySelector(`article:nth-of-type(${index + 1}) .focusable`);
  55. if (element) {
  56. if (align_top && container.scrollTop > element.offsetTop) {
  57. element.scrollIntoView(true);
  58. } else if (!align_top && container.scrollTop + container.clientHeight < element.offsetTop + element.offsetHeight) {
  59. element.scrollIntoView(false);
  60. }
  61. element.focus();
  62. }
  63. }
  64. setRef = c => {
  65. this.node = c;
  66. }
  67. render () {
  68. const { statusIds, featuredStatusIds, shouldUpdateScroll, onLoadMore, timelineId, ...other } = this.props;
  69. const { isLoading, isPartial } = other;
  70. if (isPartial) {
  71. return (
  72. <div className='regeneration-indicator'>
  73. <div>
  74. <div className='regeneration-indicator__figure' />
  75. <div className='regeneration-indicator__label'>
  76. <FormattedMessage id='regeneration_indicator.label' tagName='strong' defaultMessage='Loading&hellip;' />
  77. <FormattedMessage id='regeneration_indicator.sublabel' defaultMessage='Your home feed is being prepared!' />
  78. </div>
  79. </div>
  80. </div>
  81. );
  82. }
  83. let scrollableContent = (isLoading || statusIds.size > 0) ? (
  84. statusIds.map((statusId, index) => statusId === null ? (
  85. <LoadGap
  86. key={'gap:' + statusIds.get(index + 1)}
  87. disabled={isLoading}
  88. maxId={index > 0 ? statusIds.get(index - 1) : null}
  89. onClick={onLoadMore}
  90. />
  91. ) : (
  92. <StatusContainer
  93. key={statusId}
  94. id={statusId}
  95. onMoveUp={this.handleMoveUp}
  96. onMoveDown={this.handleMoveDown}
  97. contextType={timelineId}
  98. showThread
  99. />
  100. ))
  101. ) : null;
  102. if (scrollableContent && featuredStatusIds) {
  103. scrollableContent = featuredStatusIds.map(statusId => (
  104. <StatusContainer
  105. key={`f-${statusId}`}
  106. id={statusId}
  107. featured
  108. onMoveUp={this.handleMoveUp}
  109. onMoveDown={this.handleMoveDown}
  110. contextType={timelineId}
  111. showThread
  112. />
  113. )).concat(scrollableContent);
  114. }
  115. return (
  116. <ScrollableList {...other} showLoading={isLoading && statusIds.size === 0} onLoadMore={onLoadMore && this.handleLoadOlder} shouldUpdateScroll={shouldUpdateScroll} ref={this.setRef}>
  117. {scrollableContent}
  118. </ScrollableList>
  119. );
  120. }
  121. }