logo

pleroma-fe

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

with_load_more.js (3617B)


  1. import Vue from 'vue'
  2. import isEmpty from 'lodash/isEmpty'
  3. import { getComponentProps } from '../../services/component_utils/component_utils'
  4. import './with_load_more.scss'
  5. import { FontAwesomeIcon as FAIcon } from '@fortawesome/vue-fontawesome'
  6. import { library } from '@fortawesome/fontawesome-svg-core'
  7. import {
  8. faCircleNotch
  9. } from '@fortawesome/free-solid-svg-icons'
  10. library.add(
  11. faCircleNotch
  12. )
  13. const withLoadMore = ({
  14. fetch, // function to fetch entries and return a promise
  15. select, // function to select data from store
  16. destroy, // function called at "destroyed" lifecycle
  17. childPropName = 'entries', // name of the prop to be passed into the wrapped component
  18. additionalPropNames = [] // additional prop name list of the wrapper component
  19. }) => (WrappedComponent) => {
  20. const originalProps = Object.keys(getComponentProps(WrappedComponent))
  21. const props = originalProps.filter(v => v !== childPropName).concat(additionalPropNames)
  22. return Vue.component('withLoadMore', {
  23. props,
  24. data () {
  25. return {
  26. loading: false,
  27. bottomedOut: false,
  28. error: false,
  29. entries: []
  30. }
  31. },
  32. created () {
  33. window.addEventListener('scroll', this.scrollLoad)
  34. if (this.entries.length === 0) {
  35. this.fetchEntries()
  36. }
  37. },
  38. destroyed () {
  39. window.removeEventListener('scroll', this.scrollLoad)
  40. destroy && destroy(this.$props, this.$store)
  41. },
  42. methods: {
  43. // Entries is not a computed because computed can't track the dynamic
  44. // selector for changes and won't trigger after fetch.
  45. updateEntries () {
  46. this.entries = select(this.$props, this.$store) || []
  47. },
  48. fetchEntries () {
  49. if (!this.loading) {
  50. this.loading = true
  51. this.error = false
  52. fetch(this.$props, this.$store)
  53. .then((newEntries) => {
  54. this.loading = false
  55. this.bottomedOut = isEmpty(newEntries)
  56. })
  57. .catch(() => {
  58. this.loading = false
  59. this.error = true
  60. })
  61. .finally(() => {
  62. this.updateEntries()
  63. })
  64. }
  65. },
  66. scrollLoad (e) {
  67. const bodyBRect = document.body.getBoundingClientRect()
  68. const height = Math.max(bodyBRect.height, -(bodyBRect.y))
  69. if (this.loading === false &&
  70. this.bottomedOut === false &&
  71. this.$el.offsetHeight > 0 &&
  72. (window.innerHeight + window.pageYOffset) >= (height - 750)
  73. ) {
  74. this.fetchEntries()
  75. }
  76. }
  77. },
  78. render (h) {
  79. const props = {
  80. props: {
  81. ...this.$props,
  82. [childPropName]: this.entries
  83. },
  84. on: this.$listeners,
  85. scopedSlots: this.$scopedSlots
  86. }
  87. const children = Object.entries(this.$slots).map(([key, value]) => h('template', { slot: key }, value))
  88. return (
  89. <div class="with-load-more">
  90. <WrappedComponent {...props}>
  91. {children}
  92. </WrappedComponent>
  93. <div class="with-load-more-footer">
  94. {this.error &&
  95. <button onClick={this.fetchEntries} class="button-unstyled -link -fullwidth alert error">
  96. {this.$t('general.generic_error')}
  97. </button>
  98. }
  99. {!this.error && this.loading && <FAIcon spin icon="circle-notch"/>}
  100. {!this.error && !this.loading && !this.bottomedOut && <a onClick={this.fetchEntries}>{this.$t('general.more')}</a>}
  101. </div>
  102. </div>
  103. )
  104. }
  105. })
  106. }
  107. export default withLoadMore