koel/resources/assets/js/mixins/infinite-scroll.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

2015-12-13 04:42:28 +00:00
import $ from 'jquery';
import toTopButton from '../components/shared/to-top-button.vue';
2015-12-13 04:42:28 +00:00
/**
2015-12-14 03:45:15 +00:00
* Add a "infinite scroll" functionality to any component using this mixin.
2015-12-13 16:13:08 +00:00
* Such a component should:
2015-12-13 04:42:28 +00:00
* - have the parent DOM element defined as "wrapper": v-el:wrapper
* - have a `scrolling` method bound to `scroll` event on such element: @scroll="scrolling"
* - have the array of all objects named as `items`, either as data, computed, or a prop
*/
export default {
components: { toTopButton },
2015-12-13 04:42:28 +00:00
data() {
return {
numOfItems: 30, // Number of currently loaded and displayed items
perPage: 30, // Number of items to be loaded per "page"
showBackToTop: false,
2015-12-13 04:42:28 +00:00
};
},
methods: {
scrolling(e) {
2016-03-28 13:38:14 +00:00
const $wrapper = $(this.$els.wrapper);
2015-12-13 04:42:28 +00:00
2015-12-13 16:13:08 +00:00
// Here we check if the user has scrolled to the end of the wrapper (or 32px to the end).
2015-12-13 04:42:28 +00:00
// If that's true, load more items.
if ($wrapper.scrollTop() + $wrapper.innerHeight() >= $wrapper[0].scrollHeight - 32) {
this.displayMore();
}
this.showBackToTop = $wrapper.scrollTop() > 64;
2015-12-13 04:42:28 +00:00
},
2016-01-07 09:03:38 +00:00
/**
* Load and display more items into the scrollable area.
*/
2015-12-13 04:42:28 +00:00
displayMore() {
this.numOfItems += this.perPage;
},
/**
* Scroll to top fo the wrapper.
*/
scrollToTop() {
$(this.$els.wrapper).animate({ scrollTop: 0}, 500);
this.showBackToTop = false;
}
2015-12-13 04:42:28 +00:00
},
2015-12-30 04:14:47 +00:00
events: {
'koel:teardown': function () {
this.numOfItems = 30;
this.showBackToTop = false;
2015-12-30 04:14:47 +00:00
},
},
2015-12-13 04:42:28 +00:00
};