infiniteScroll.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* eslint-disable
  2. max-len,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. import App from '../../../base'
  13. export default App.directive('infiniteScroll', () => ({
  14. link(scope, element, attrs, ctrl) {
  15. const innerElement = element.find('.infinite-scroll-inner')
  16. element.css({ 'overflow-y': 'auto' })
  17. const atEndOfListView = function () {
  18. if (attrs.infiniteScrollUpwards != null) {
  19. return atTopOfListView()
  20. } else {
  21. return atBottomOfListView()
  22. }
  23. }
  24. var atTopOfListView = () => element.scrollTop() < 30
  25. var atBottomOfListView = () =>
  26. element.scrollTop() + element.height() >= innerElement.height() - 30
  27. const listShorterThanContainer = () =>
  28. element.height() > innerElement.height()
  29. var loadUntilFull = function () {
  30. if (
  31. (listShorterThanContainer() || atEndOfListView()) &&
  32. !scope.$eval(attrs.infiniteScrollDisabled)
  33. ) {
  34. const promise = scope.$eval(attrs.infiniteScroll)
  35. return promise.then(() => setTimeout(() => loadUntilFull(), 0))
  36. }
  37. }
  38. element.on('scroll', event => loadUntilFull())
  39. return scope.$watch(attrs.infiniteScrollInitialize, function (value) {
  40. if (value) {
  41. return loadUntilFull()
  42. }
  43. })
  44. },
  45. }))