forked from starkwang/vue-virtual-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtualCollection.vue
274 lines (258 loc) · 8.67 KB
/
VirtualCollection.vue
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
<style scoped>
.vue-virtual-collection {
overflow: scroll;
-webkit-overflow-scrolling: touch;
}
.vue-virtual-collection-container {
position: relative;
}
.vue-virtual-collection .cell-container {
position: absolute;
top: 0;
left: 0;
}
</style>
<template>
<div class="vue-virtual-collection" :style="outerStyle" @scroll.passive="onScroll" ref="outer">
<slot name="header"></slot>
<div class="vue-virtual-collection-container" :style="containerStyle">
<div v-for="item in displayItems" class="cell-container" :key="item.key" :style="getComputedStyle(item)">
<slot name="cell" :data="item.data"></slot>
</div>
</div>
</div>
</template>
<script>
import GroupManager from "./GroupManager"
export default {
props: {
cellSizeAndPositionGetter: {
type: Function,
required: true
},
collection: {
type: Array,
required: true
},
height: {
type: Number,
required: true,
validator (value) {
return value >= 0
}
},
width: {
type: Number,
required: true,
validator (value) {
return value >= 0
}
},
sectionSize: {
type: Number,
default: 300
},
containerPaddingBottom: {
type: Number,
default: 0
},
scrollToBottomRange: {
type: Number
},
scrollTopThreshold: {
type: Number
},
headerSlotHeight: {
type: Number,
default: 0
}
},
data () {
return {
inRangeHit: false,
totalHeight: 0,
totalWidth: 0,
displayItems: []
}
},
watch: {
collection () {
this.resetCollection()
}
},
created () {
this.groupManagers = []
this.onCollectionChanged()
},
methods: {
resetCollection () {
// Dispose previous groups and reset associated data
this.groupManagers.forEach(manager => manager.dispose())
this.groupManagers = []
this.totalHeight = 0
this.totalWidth = 0
this.onCollectionChanged()
},
onCollectionChanged () {
let collection = this.collection
// If the collection is flat, wrap it inside a single group
if (collection.length > 0 && collection[0].group === undefined) {
collection = [{ group: collection }]
}
// Create and store managers for each item group
collection.forEach((groupContainer, i) => {
const groupIndex = i // Capture group index for closure
const unwatch = this.$watch(
() => groupContainer,
() => this.onGroupChanged(groupContainer.group, groupIndex),
{ deep: true }
)
this.groupManagers.push(new GroupManager(
groupContainer.group,
groupIndex,
this.sectionSize,
this.cellSizeAndPositionGetter,
unwatch
))
})
this.updateGridDimensions()
this.flushDisplayItems()
},
updateGridDimensions () {
this.totalHeight = Math.max(...this.groupManagers.map(it => it.totalHeight))
this.totalWidth = Math.max(...this.groupManagers.map(it => it.totalWidth))
},
onGroupChanged (group, index) {
this.groupManagers[index].updateGroup(group)
this.updateGridDimensions()
this.flushDisplayItems()
},
getComputedStyle (displayItem) {
if (!displayItem) return
// Currently displayed items may no longer exist
// if collection has been modified since
const groupManager = this.groupManagers[displayItem.groupIndex]
if (!groupManager) return
const cellMetadatum = groupManager.getCellMetadata(displayItem.itemIndex)
if (!cellMetadatum) return
const { width, height, x, y, style } = cellMetadatum
return {
transform: `translateX(${x}px) translateY(${y}px)`,
width: `${width}px`,
height: `${height}px`,
...style
}
},
onScroll (event) {
this.flushDisplayItems()
const target = event.target
const total = target.scrollHeight - target.offsetHeight
this.$emit("scroll", target)
if (target.scrollTop === 0) {
// We have hit the top. Emit event and don't continue.
this.$emit("scrolled-to-top", target)
if (this.scrollTopThreshold) return
} else if (target.scrollTop === total) {
// We have hit the bottom. Emit event and don't continue.
this.$emit("scrolled-to-bottom", target)
if (this.scrollToBottomRange) return
}
if (this.scrollToBottomRange) {
// We want to scroll sooner than hitting the bottom
if (target.scrollTop < total && target.scrollTop > (total - this.scrollToBottomRange)) {
// We are in the range
if (this.inRangeHit === false) {
// If we didn't hit the range previously, emit event
this.inRangeHit = true
this.$emit("scrolled-to-bottom-range", target)
}
} else {
// Reset range hit
this.inRangeHit = false
}
}
if (this.scrollTopThreshold) {
// We want to scroll sooner than hitting the top
if (target.scrollTop <= this.scrollTopThreshold) {
// We are in the range
if (this.inRangeHit === false) {
// If we didn't hit the range previously, emit event
this.inRangeHit = true
this.$emit("scrolled-to-top-threshold", target)
}
else {
// Reset range hit
this.inRangeHit = false
}
}
}
},
onContainerResized () {
this.resetCollection()
},
flushDisplayItems () {
let scrollTop = 0
let scrollLeft = 0
if (this.$refs.outer) {
scrollTop = this.$refs.outer.scrollTop - this.headerSlotHeight
scrollLeft = this.$refs.outer.scrollLeft
}
const displayItems = []
this.groupManagers.forEach((groupManager, groupIndex) => {
var indices = groupManager.getCellIndices({
height: this.height,
width: this.width,
x: scrollLeft,
y: scrollTop
})
indices.forEach(itemIndex => {
displayItems.push(Object.freeze({
groupIndex,
itemIndex,
key: displayItems.length,
...groupManager.getItem(itemIndex)
}))
})
})
if (window.requestAnimationFrame) {
window.requestAnimationFrame(() => {
this.displayItems = displayItems
this.$forceUpdate()
})
} else {
this.displayItems = displayItems
this.$forceUpdate()
}
}
},
mounted () {
if (ResizeObserver) {
this.resizeObserver = new ResizeObserver(this.onContainerResized)
this.resizeObserver.observe(this.$refs.outer)
} else {
this.$refs.outer.addEventListener("resize", this.onContainerResized)
}
},
beforeDestroy () {
if (ResizeObserver) {
this.resizeObserver.disconnect()
} else {
this.$refs.outer.removeEventListener("resize", this.onContainerResized)
}
},
computed: {
containerStyle () {
return {
height: this.totalHeight + this.containerPaddingBottom + "px",
width: this.totalWidth + "px"
}
},
outerStyle () {
return {
height: this.height + "px",
width: this.width + "px"
}
}
}
}
</script>