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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
|
<template>
<div
v-observe-visibility="handleVisibilityChange"
class="vue-recycle-scroller"
:class="{
ready,
'page-mode': pageMode,
[`direction-${direction}`]: true,
}"
@scroll.passive="handleScroll"
>
<div
v-if="$slots.before"
class="vue-recycle-scroller__slot"
>
<slot
name="before"
/>
</div>
<div
ref="wrapper"
:style="{ [direction === 'vertical' ? 'minHeight' : 'minWidth']: totalSize + 'px' }"
class="vue-recycle-scroller__item-wrapper"
>
<div
v-for="view of pool"
:key="view.nr.id"
:style="ready ? {
transform: useTransform ? `translate${direction === 'vertical' ? 'Y' : 'X'}(${view.position}px)` : null,
top: !useTransform && direction === 'vertical' ? `${view.position}px` : null,
left: !useTransform && direction !== 'vertical' ? `${view.position}px` : null,
} : null"
class="vue-recycle-scroller__item-view"
:class="{ hover: hoverKey === view.nr.key }"
@mouseenter="hoverKey = view.nr.key"
@mouseleave="hoverKey = null"
>
<slot
:item="view.item"
:index="view.nr.index"
:active="view.nr.used"
/>
</div>
</div>
<div
v-if="$slots.after"
class="vue-recycle-scroller__slot"
>
<slot
name="after"
/>
</div>
<ResizeObserver @notify="handleResize" />
</div>
</template>
<script>
import { ResizeObserver } from 'vue-resize'
import 'vue-resize/dist/vue-resize.css'
import { ObserveVisibility } from 'vue-observe-visibility'
import ScrollParent from 'scrollparent'
import config from '../config'
import { props, simpleArray } from './common'
import { supportsPassive } from '../utils'
let uid = 0
export default {
name: 'RecycleScroller',
components: {
ResizeObserver,
},
directives: {
ObserveVisibility,
},
props: {
...props,
itemSize: {
type: Number,
default: null,
},
minItemSize: {
type: [Number, String],
default: null,
},
sizeField: {
type: String,
default: 'size',
},
typeField: {
type: String,
default: 'type',
},
buffer: {
type: Number,
default: 200,
},
pageMode: {
type: Boolean,
default: false,
},
prerender: {
type: Number,
default: 0,
},
emitUpdate: {
type: Boolean,
default: false,
},
useTransform: {
type: Boolean,
default: true,
}
},
data () {
return {
pool: [],
totalSize: 0,
ready: false,
hoverKey: null,
}
},
computed: {
sizes () {
if (this.itemSize === null) {
const sizes = {
'-1': { accumulator: 0 },
}
const items = this.items
const field = this.sizeField
const minItemSize = this.minItemSize
let computedMinSize = 10000
let accumulator = 0
let current
for (let i = 0, l = items.length; i < l; i++) {
current = items[i][field] || minItemSize
if (current < computedMinSize) {
computedMinSize = current
}
accumulator += current
sizes[i] = { accumulator, size: current }
}
// eslint-disable-next-line
this.$_computedMinItemSize = computedMinSize
return sizes
}
return []
},
simpleArray,
},
watch: {
items () {
this.updateVisibleItems(true)
},
pageMode () {
this.applyPageMode()
this.updateVisibleItems(false)
},
sizes: {
handler () {
this.updateVisibleItems(false)
},
deep: true,
},
},
created () {
this.$_startIndex = 0
this.$_endIndex = 0
this.$_views = new Map()
this.$_unusedViews = new Map()
this.$_scrollDirty = false
this.$_lastUpdateScrollPosition = 0
// In SSR mode, we also prerender the same number of item for the first render
// to avoir mismatch between server and client templates
if (this.prerender) {
this.$_prerender = true
this.updateVisibleItems(false)
}
},
mounted () {
this.applyPageMode()
this.$nextTick(() => {
// In SSR mode, render the real number of visible items
this.$_prerender = false
this.updateVisibleItems(true)
this.ready = true
})
},
beforeDestroy () {
this.removeListeners()
},
methods: {
addView (pool, index, item, key, type) {
const view = {
item,
position: 0,
}
const nonReactive = {
id: uid++,
index,
used: true,
key,
type,
}
Object.defineProperty(view, 'nr', {
configurable: false,
value: nonReactive,
})
pool.push(view)
return view
},
unuseView (view, fake = false) {
const unusedViews = this.$_unusedViews
const type = view.nr.type
let unusedPool = unusedViews.get(type)
if (!unusedPool) {
unusedPool = []
unusedViews.set(type, unusedPool)
}
unusedPool.push(view)
if (!fake) {
view.nr.used = false
view.position = -9999
this.$_views.delete(view.nr.key)
}
},
handleResize () {
this.$emit('resize')
if (this.ready) this.updateVisibleItems(false)
},
handleScroll (event) {
if (!this.$_scrollDirty) {
this.$_scrollDirty = true
requestAnimationFrame(() => {
this.$_scrollDirty = false
const { continuous } = this.updateVisibleItems(false, true)
// It seems sometimes chrome doesn't fire scroll event :/
// When non continous scrolling is ending, we force a refresh
if (!continuous) {
clearTimeout(this.$_refreshTimout)
this.$_refreshTimout = setTimeout(this.handleScroll, 100)
}
})
}
},
handleVisibilityChange (isVisible, entry) {
if (this.ready) {
if (isVisible || entry.boundingClientRect.width !== 0 || entry.boundingClientRect.height !== 0) {
this.$emit('visible')
requestAnimationFrame(() => {
this.updateVisibleItems(false)
})
} else {
this.$emit('hidden')
}
}
},
updateVisibleItems (checkItem, checkPositionDiff = false) {
const itemSize = this.itemSize
const minItemSize = this.$_computedMinItemSize
const typeField = this.typeField
const keyField = this.simpleArray ? null : this.keyField
const items = this.items
const count = items.length
const sizes = this.sizes
const views = this.$_views
const unusedViews = this.$_unusedViews
const pool = this.pool
let startIndex, endIndex
let totalSize
if (!count) {
startIndex = endIndex = totalSize = 0
} else if (this.$_prerender) {
startIndex = 0
endIndex = this.prerender
totalSize = null
} else {
const scroll = this.getScroll()
// Skip update if use hasn't scrolled enough
if (checkPositionDiff) {
let positionDiff = scroll.start - this.$_lastUpdateScrollPosition
if (positionDiff < 0) positionDiff = -positionDiff
if ((itemSize === null && positionDiff < minItemSize) || positionDiff < itemSize) {
return {
continuous: true,
}
}
}
this.$_lastUpdateScrollPosition = scroll.start
const buffer = this.buffer
scroll.start -= buffer
scroll.end += buffer
// Variable size mode
if (itemSize === null) {
let h
let a = 0
let b = count - 1
let i = ~~(count / 2)
let oldI
// Searching for startIndex
do {
oldI = i
h = sizes[i].accumulator
if (h < scroll.start) {
a = i
} else if (i < count - 1 && sizes[i + 1].accumulator > scroll.start) {
b = i
}
i = ~~((a + b) / 2)
} while (i !== oldI)
i < 0 && (i = 0)
startIndex = i
// For container style
totalSize = sizes[count - 1].accumulator
// Searching for endIndex
for (endIndex = i; endIndex < count && sizes[endIndex].accumulator < scroll.end; endIndex++);
if (endIndex === -1) {
endIndex = items.length - 1
} else {
endIndex++
// Bounds
endIndex > count && (endIndex = count)
}
} else {
// Fixed size mode
startIndex = ~~(scroll.start / itemSize)
endIndex = Math.ceil(scroll.end / itemSize)
// Bounds
startIndex < 0 && (startIndex = 0)
endIndex > count && (endIndex = count)
totalSize = count * itemSize
}
}
if (endIndex - startIndex > config.itemsLimit) {
this.itemsLimitError()
}
this.totalSize = totalSize
let view
const continuous = startIndex <= this.$_endIndex && endIndex >= this.$_startIndex
if (this.$_continuous !== continuous) {
if (continuous) {
views.clear()
unusedViews.clear()
for (let i = 0, l = pool.length; i < l; i++) {
view = pool[i]
this.unuseView(view)
}
}
this.$_continuous = continuous
} else if (continuous) {
for (let i = 0, l = pool.length; i < l; i++) {
view = pool[i]
if (view.nr.used) {
// Update view item index
if (checkItem) {
view.nr.index = items.findIndex(
item => keyField ? item[keyField] === view.item[keyField] : item === view.item,
)
}
// Check if index is still in visible range
if (
view.nr.index === -1 ||
view.nr.index < startIndex ||
view.nr.index >= endIndex
) {
this.unuseView(view)
}
}
}
}
const unusedIndex = continuous ? null : new Map()
let item, type, unusedPool
let v
for (let i = startIndex; i < endIndex; i++) {
item = items[i]
const key = keyField ? item[keyField] : item
if (key == null) {
throw new Error(`Key is ${key} on item (keyField is '${keyField}')`)
}
view = views.get(key)
if (!itemSize && !sizes[i].size) {
if (view) this.unuseView(view)
continue
}
// No view assigned to item
if (!view) {
type = item[typeField]
unusedPool = unusedViews.get(type)
if (continuous) {
// Reuse existing view
if (unusedPool && unusedPool.length) {
view = unusedPool.pop()
view.item = item
view.nr.used = true
view.nr.index = i
view.nr.key = key
view.nr.type = type
} else {
view = this.addView(pool, i, item, key, type)
}
} else {
// Use existing view
// We don't care if they are already used
// because we are not in continous scrolling
v = unusedIndex.get(type) || 0
if (!unusedPool || v >= unusedPool.length) {
view = this.addView(pool, i, item, key, type)
this.unuseView(view, true)
unusedPool = unusedViews.get(type)
}
view = unusedPool[v]
view.item = item
view.nr.used = true
view.nr.index = i
view.nr.key = key
view.nr.type = type
unusedIndex.set(type, v + 1)
v++
}
views.set(key, view)
} else {
view.nr.used = true
view.item = item
}
// Update position
if (itemSize === null) {
view.position = sizes[i - 1].accumulator
} else {
view.position = i * itemSize
}
}
this.$_startIndex = startIndex
this.$_endIndex = endIndex
if (this.emitUpdate) this.$emit('update', startIndex, endIndex)
// After the user has finished scrolling
// Sort views so text selection is correct
clearTimeout(this.$_sortTimer)
this.$_sortTimer = setTimeout(this.sortViews, 300)
return {
continuous,
}
},
getListenerTarget () {
let target = ScrollParent(this.$el)
// Fix global scroll target for Chrome and Safari
if (window.document && (target === window.document.documentElement || target === window.document.body)) {
target = window
}
return target
},
getScroll () {
const { $el: el, direction } = this
const isVertical = direction === 'vertical'
let scrollState
if (this.pageMode) {
const bounds = el.getBoundingClientRect()
const boundsSize = isVertical ? bounds.height : bounds.width
let start = -(isVertical ? bounds.top : bounds.left)
let size = isVertical ? window.innerHeight : window.innerWidth
if (start < 0) {
size += start
start = 0
}
if (start + size > boundsSize) {
size = boundsSize - start
}
scrollState = {
start,
end: start + size,
}
} else if (isVertical) {
scrollState = {
start: el.scrollTop,
end: el.scrollTop + el.clientHeight,
}
} else {
scrollState = {
start: el.scrollLeft,
end: el.scrollLeft + el.clientWidth,
}
}
return scrollState
},
applyPageMode () {
if (this.pageMode) {
this.addListeners()
} else {
this.removeListeners()
}
},
addListeners () {
this.listenerTarget = this.getListenerTarget()
this.listenerTarget.addEventListener('scroll', this.handleScroll, supportsPassive ? {
passive: true,
} : false)
this.listenerTarget.addEventListener('resize', this.handleResize)
},
removeListeners () {
if (!this.listenerTarget) {
return
}
this.listenerTarget.removeEventListener('scroll', this.handleScroll)
this.listenerTarget.removeEventListener('resize', this.handleResize)
this.listenerTarget = null
},
scrollToItem (index) {
this.$_scrollDirty = true
const { viewport, scrollDirection, scrollDistance } = this.scrollToPosition(index)
viewport[scrollDirection] = scrollDistance
setTimeout(() => {
this.$_scrollDirty = false
this.updateVisibleItems(false, true)
})
},
scrollToPosition (index) {
const getPositionOfItem = (index) => {
if (this.itemSize === null) {
return index > 0 ? this.sizes[index - 1].accumulator : 0
} else {
return index * this.itemSize
}
}
const position = getPositionOfItem(index)
const direction = this.direction === 'vertical'
? { scroll: 'scrollTop', start: 'top' }
: { scroll: 'scrollLeft', start: 'left' }
if (this.pageMode) {
const viewportEl = ScrollParent(this.$el)
// HTML doesn't overflow like other elements
const scrollTop = viewportEl.tagName === 'HTML' ? 0 : viewportEl[direction.scroll]
const viewport = viewportEl.getBoundingClientRect()
const scroller = this.$el.getBoundingClientRect()
const scrollerPosition = scroller[direction.start] - viewport[direction.start]
return {
viewport: viewportEl,
scrollDirection: direction.scroll,
scrollDistance: position + scrollTop + scrollerPosition,
}
}
return {
viewport: this.$el,
scrollDirection: direction.scroll,
scrollDistance: position,
}
},
itemsLimitError () {
setTimeout(() => {
console.log('It seems the scroller element isn\'t scrolling, so it tries to render all the items at once.', 'Scroller:', this.$el)
console.log('Make sure the scroller has a fixed height (or width) and \'overflow-y\' (or \'overflow-x\') set to \'auto\' so it can scroll correctly and only render the items visible in the scroll viewport.')
})
throw new Error('Rendered items limit reached')
},
sortViews () {
this.pool.sort((viewA, viewB) => viewA.nr.index - viewB.nr.index)
},
},
}
</script>
<style>
.vue-recycle-scroller {
position: relative;
}
.vue-recycle-scroller.direction-vertical:not(.page-mode) {
overflow-y: auto;
}
.vue-recycle-scroller.direction-horizontal:not(.page-mode) {
overflow-x: auto;
}
.vue-recycle-scroller.direction-horizontal {
display: flex;
}
.vue-recycle-scroller__slot {
flex: auto 0 0;
}
.vue-recycle-scroller__item-wrapper {
flex: 1;
box-sizing: border-box;
overflow: hidden;
position: relative;
}
.vue-recycle-scroller.ready .vue-recycle-scroller__item-view {
position: absolute;
top: 0;
left: 0;
will-change: transform;
}
.vue-recycle-scroller.direction-vertical .vue-recycle-scroller__item-wrapper {
width: 100%;
}
.vue-recycle-scroller.direction-horizontal .vue-recycle-scroller__item-wrapper {
height: 100%;
}
.vue-recycle-scroller.ready.direction-vertical .vue-recycle-scroller__item-view {
width: 100%;
}
.vue-recycle-scroller.ready.direction-horizontal .vue-recycle-scroller__item-view {
height: 100%;
}
</style>
|