forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQInfiniteScroll.js
More file actions
105 lines (97 loc) · 2.52 KB
/
QInfiniteScroll.js
File metadata and controls
105 lines (97 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { height, offset } from '../../utils/dom.js'
import debounce from '../../utils/debounce.js'
import { getScrollTarget } from '../../utils/scroll.js'
import { listenOpts } from '../../utils/event.js'
export default {
name: 'QInfiniteScroll',
props: {
handler: {
type: Function,
required: true
},
inline: Boolean,
offset: {
type: Number,
default: 0
}
},
data () {
return {
index: 0,
fetching: false,
working: true
}
},
methods: {
poll () {
if (this.fetching || !this.working) {
return
}
let
containerHeight = height(this.scrollContainer),
containerBottom = offset(this.scrollContainer).top + containerHeight,
triggerPosition = offset(this.element).top + height(this.element) - (this.offset || containerHeight)
if (triggerPosition < containerBottom) {
this.loadMore()
}
},
loadMore () {
if (this.fetching || !this.working) {
return
}
this.index++
this.fetching = true
this.handler(this.index, stopLoading => {
this.fetching = false
if (stopLoading) {
this.stop()
return
}
if (this.element.closest('body')) {
this.poll()
}
})
},
reset () {
this.index = 0
},
resume () {
if (this.working !== true) {
this.working = true
this.scrollContainer.addEventListener('scroll', this.poll, listenOpts.passive)
}
this.immediatePoll()
},
stop () {
this.working = false
this.fetching = false
this.scrollContainer.removeEventListener('scroll', this.poll, listenOpts.passive)
}
},
mounted () {
this.$nextTick(() => {
this.element = this.$refs.content
this.scrollContainer = this.inline ? this.$el : getScrollTarget(this.$el)
this.poll()
this.immediatePoll = this.poll
this.poll = debounce(this.poll, 50)
if (this.working === true) {
this.scrollContainer.addEventListener('scroll', this.poll, listenOpts.passive)
}
})
},
beforeDestroy () {
this.scrollContainer.removeEventListener('scroll', this.poll, listenOpts.passive)
},
render (h) {
return h('div', { staticClass: 'q-infinite-scroll' }, [
h('div', {
ref: 'content',
staticClass: 'q-infinite-scroll-content'
}, this.$slots.default),
this.fetching
? h('div', { staticClass: 'q-infinite-scroll-message' }, this.$slots.message)
: null
])
}
}