ScrollAndFetchHandler.vue 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. position: 1,
  6. maxPosition: 1,
  7. isGettingSet: false,
  8. loadAllSongs: false,
  9. interval: null
  10. };
  11. },
  12. computed: {
  13. setsLoaded() {
  14. return this.position - 1;
  15. },
  16. maxSets() {
  17. return this.maxPosition - 1;
  18. }
  19. },
  20. unmounted() {
  21. clearInterval(this.interval);
  22. },
  23. created() {
  24. window.addEventListener("scroll", this.handleScroll);
  25. },
  26. destroyed() {
  27. window.removeEventListener("scroll", this.handleScroll);
  28. },
  29. methods: {
  30. handleScroll() {
  31. const scrollPosition = document.body.clientHeight + window.scrollY;
  32. const bottomPosition = document.body.scrollHeight;
  33. if (this.loadAllSongs) return false;
  34. if (scrollPosition + 400 >= bottomPosition) this.getSet();
  35. return this.maxPosition === this.position;
  36. },
  37. loadAll() {
  38. this.loadAllSongs = true;
  39. this.interval = setInterval(() => {
  40. if (this.loadAllSongs && this.maxPosition > this.position)
  41. this.getSet();
  42. else {
  43. clearInterval(this.interval);
  44. this.loadAllSongs = false;
  45. }
  46. }, 500);
  47. }
  48. }
  49. };
  50. </script>