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

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-12-13 04:42:28 +00:00
import $ from 'jquery';
2016-06-25 05:24:55 +00:00
import { event } from '../utils';
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.
* Such a component should have a `scrolling` method bound to `scroll` event on
* the wrapper element: @scroll="scrolling"
2015-12-13 04:42:28 +00:00
*/
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) {
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.
2016-06-08 09:56:20 +00:00
if (e.target.scrollTop + e.target.clientHeight >= e.target.scrollHeight - 32) {
2015-12-13 04:42:28 +00:00
this.displayMore();
}
2016-06-08 09:56:20 +00:00
this.showBackToTop = e.target.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() {
2016-06-25 05:24:55 +00:00
$(this.$refs.wrapper).animate({ scrollTop: 0}, 500);
this.showBackToTop = false;
2016-06-08 09:56:20 +00:00
},
2015-12-13 04:42:28 +00:00
},
2015-12-30 04:14:47 +00:00
2016-06-25 05:24:55 +00:00
created() {
event.on('koel:teardown', () => {
2015-12-30 04:14:47 +00:00
this.numOfItems = 30;
this.showBackToTop = false;
2016-06-25 05:24:55 +00:00
});
2015-12-30 04:14:47 +00:00
},
2015-12-13 04:42:28 +00:00
};