2021-01-21 19:02:20 +11:00
|
|
|
<template>
|
2021-01-28 19:25:45 +11:00
|
|
|
<div>
|
2021-01-21 19:02:20 +11:00
|
|
|
<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'],
|
2021-01-21 19:02:20 +11:00
|
|
|
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) {
|
2021-01-21 19:02:20 +11:00
|
|
|
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>
|