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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
<!--
- @copyright Copyright (c) 2022 Louis Chemineau <louis@chmn.me>
-
- @author Louis Chemineau <louis@chmn.me>
-
- @license AGPL-3.0-or-later
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->
<template>
<div v-if="!useWindow && containerElement === null" ref="container" class="vs-container">
<div ref="rowsContainer"
class="vs-rows-container"
:style="rowsContainerStyle">
<slot :rendered-rows="visibleRows" />
<slot name="loader" />
</div>
</div>
<div v-else
ref="rowsContainer"
class="vs-rows-container"
:style="rowsContainerStyle">
<slot :rendered-rows="visibleRows" />
<slot name="loader" />
</div>
</template>
<script>
import { debounce } from 'debounce'
import logger from '../services/logger.js'
/**
* @typedef {object} Row
* @property {number} height - The height of the row.
*/
/**
* @typedef {Row} VisibleRow
* @property {number} distance - The distance from the visible viewport
*/
export default {
name: 'VirtualScrolling',
props: {
rows: {
type: Array,
required: true,
},
containerElement: {
type: HTMLElement,
default: null,
},
useWindow: {
type: Boolean,
default: false,
},
renderDistance: {
type: Number,
default: 10,
},
bottomBufferRatio: {
type: Number,
default: 2,
},
scrollToKey: {
type: String,
default: '',
},
},
data() {
return {
scrollPosition: 0,
containerHeight: 0,
rowsContainerHeight: 0,
/** @type {ResizeObserver} */
resizeObserver: null,
}
},
computed: {
/**
* @return {VisibleRow[]}
*/
visibleRows() {
logger.debug('[VirtualScrolling] Computing visible rows', this.rows)
// Optimisation: get those computed properties once to not go through vue's internal every time we need them.
const containerHeight = this.containerHeight
const containerTop = this.scrollPosition
const containerBottom = containerTop + containerHeight
let currentRowTop = 0
let currentRowBottom = 0
// Compute whether a row should be included in the DOM (shouldRender)
// And how visible the row is.
return this.rows
.reduce((visibleRows, row) => {
currentRowTop = currentRowBottom
currentRowBottom += row.height
let distance = 0
if (currentRowBottom < containerTop) {
distance = (containerTop - currentRowBottom) / containerHeight
} else if (currentRowTop > containerBottom) {
distance = (currentRowTop - containerBottom) / containerHeight
}
if (distance > this.renderDistance) {
return visibleRows
}
return [
...visibleRows,
{
...row,
distance,
},
]
}, [])
},
/**
* Total height of all the rows + some room for the loader.
*
* @return {number}
*/
rowsHeight() {
const loaderHeight = 200
return this.rows
.map(row => row.height)
.reduce((totalHeight, rowHeight) => totalHeight + rowHeight, 0) + loaderHeight
},
/**
* @return {number}
*/
paddingTop() {
if (this.visibleRows.length === 0) {
return 0
}
const firstVisibleRowIndex = this.rows.findIndex(row => row.items === this.visibleRows[0].items)
return this.rows
.map(row => row.height)
.slice(0, firstVisibleRowIndex)
.reduce((totalHeight, rowHeight) => totalHeight + rowHeight, 0)
},
/**
* padding-top is used to replace not included item in the container.
*
* @return {{heigh: string, paddingTop: string}}
*/
rowsContainerStyle() {
return {
height: `${this.rowsHeight}px`,
paddingTop: `${this.paddingTop}px`,
}
},
/**
* Whether the user is near the bottom.
* If true, then the need-content event will be emitted.
*
* @return {boolean}
*/
isNearBottom() {
const buffer = this.containerHeight * this.bottomBufferRatio
return this.scrollPosition + this.containerHeight >= this.rowsHeight - buffer
},
/**
* @return {HTMLElement}
*/
container() {
logger.debug('[VirtualScrolling] Computing container')
if (this.containerElement !== null) {
return this.containerElement
} else if (this.useWindow) {
return window
} else {
return this.$refs.container
}
},
},
watch: {
isNearBottom(value) {
if (value) {
this.$emit('need-content')
}
},
rows() {
// Re-emit need-content when rows is updated and isNearBottom is still true.
// If the height of added rows is under `bottomBufferRatio`, `isNearBottom` will still be true so we need more content.
if (this.isNearBottom) {
this.$emit('need-content')
}
},
scrollToKey(key) {
let currentRowTopDistanceFromTop = 0
for (const row of this.rows) {
if (row.key === key) {
this.$refs.container.scrollTo({ top: currentRowTopDistanceFromTop, behavior: 'smooth' })
return
}
currentRowTopDistanceFromTop += row.height
}
},
},
mounted() {
this.resizeObserver = new ResizeObserver(entries => {
for (const entry of entries) {
const cr = entry.contentRect
if (entry.target === this.container) {
this.containerHeight = cr.height
}
if (entry.target.classList.contains('vs-rows-container')) {
this.rowsContainerHeight = cr.height
}
}
})
if (this.useWindow) {
window.addEventListener('resize', this.updateContainerSize)
this.containerHeight = window.innerHeight
} else {
this.resizeObserver.observe(this.container)
}
this.resizeObserver.observe(this.$refs.rowsContainer)
this.container.addEventListener('scroll', this.updateScrollPosition)
},
beforeDestroy() {
if (this.useWindow) {
window.removeEventListener('resize', this.updateContainerSize)
}
this.resizeObserver.disconnect()
this.container.removeEventListener('scroll', this.updateScrollPosition)
},
methods: {
updateScrollPosition: debounce(function() {
if (this.useWindow) {
this.scrollPosition = this.container.scrollY
} else {
this.scrollPosition = this.container.scrollTop
}
}, 200),
updateContainerSize() {
this.containerHeight = window.innerHeight
},
},
}
</script>
<style scoped lang="scss">
.vs-container {
overflow-y: scroll;
height: 100%;
}
.vs-rows-container {
box-sizing: border-box;
}
</style>
|