frontend/src/components/Pagination.vue

66 lines
1.6 KiB
Vue
Raw Normal View History

<template>
2021-01-28 19:25:45 +11:00
<div>
<slot></slot>
</div>
</template>
<script>
import throttle from 'lodash.throttle'
export default {
name: 'Pagination',
2021-01-28 20:21:17 +11:00
props: ['loading', 'query-selector', 'padding-bottom', 'padding-top', 'paginate', 'wait'],
computed: {
element () {
if(this.querySelector){
return document.querySelector(this.querySelector);
} else {
return null;
}
}
},
methods: {
onScroll: throttle(function () {
let paddingBottom = this.paddingBottom || 300;
let paddingTop = this.paddingTop || 150;
let scrollBottom, scrollTop;
//If already loading then do not fire
if(this.loading) return;
if(this.element) {
scrollBottom = Math.floor(
this.element.scrollTop +
this.element.getBoundingClientRect().height +
paddingBottom -
this.element.scrollHeight
);
scrollTop = paddingTop - this.element.scrollTop;
} else {
scrollBottom =
window.innerHeight + window.pageYOffset +
paddingBottom -
document.body.scrollHeight;
scrollTop = paddingTop - document.body.scrollTop;
}
if(scrollBottom > 0) {
2021-01-28 20:21:17 +11:00
if(this.paginate && !this.wait) {
this.$emit('loadNext');
}
} else if(scrollTop > 0) {
this.$emit('loadPrevious');
}
})
},
mounted () {
(this.element || window).addEventListener('scroll', this.onScroll);
},
destroyed () {
(this.element || window).removeEventListener('scroll', this.onScroll);
}
}
</script>