-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
397 lines (347 loc) · 14.6 KB
/
index.js
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
;(function (root, factory) {
var namespace = 'VirtualScrollList'
if (typeof exports === 'object' && typeof module === 'object') {
module.exports = factory(namespace, require('vue'))
} else if (typeof define === 'function' && define.amd) {
define(['vue'], factory.bind(root, namespace))
} else if (typeof exports === 'object') {
exports[namespace] = factory(namespace, require('vue'))
} else {
root[namespace] = factory(namespace, root['Vue'])
}
})(this, function (namespace, Vue2) {
if (typeof Vue2 === 'object' && typeof Vue2.default === 'function') {
Vue2 = Vue2.default
}
var _debounce = function (func, wait, immediate) {
var timeout
return function () {
var context = this
var args = arguments
var later = function () {
timeout = null
if (!immediate) {
func.apply(context, args)
}
}
var callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) {
func.apply(context, args)
}
}
}
return Vue2.component(namespace, {
props: {
size: { type: Number, required: true },
remain: { type: Number, required: true },
rtag: { type: String, default: 'div' },
wtag: { type: String, default: 'div' },
wclass: { type: String, default: '' },
start: { type: Number, default: 0 },
offset: { type: Number, default: 0 },
variable: [Function, Boolean],
bench: Number,
debounce: Number,
totop: Function,
tobottom: Function,
onscroll: Function,
height:{type:Number,default:0,required:false}
},
created: function () {
var start = this.start >= this.remain ? this.start : 0
var keeps = this.remain + (this.bench || this.remain)
this.delta = {
start: start, // start index.
end: start + keeps - 1, // end index.
keeps: keeps, // nums keeping in real dom.
total: 0, // all items count, update in filter.
offsetAll: 0, // cache all the scrollable offset.
paddingTop: 0, // container wrapper real padding-top.
paddingBottom: 0, // container wrapper real padding-bottom.
varCache: {}, // object to cache variable index height and scroll offset.
varAverSize: 0, // average/estimate item height before variable be calculated.
varLastCalcIndex: 0 // last calculated variable height/offset index, always increase.
}
},
watch: {
size: function () {
this.alter = 'size'
},
remain: function () {
this.alter = 'remain'
},
bench: function () {
this.alter = 'bench'
},
start: function () {
this.alter = 'start'
},
offset: function () {
this.alter = 'offset'
}
},
methods: {
onScroll: function (e) {
var delta = this.delta
var offset = (this.$refs.vsl && this.$refs.vsl.scrollTop) || 0
if (delta.total > delta.keeps) {
this.updateZone(offset)
} else {
delta.end = delta.total - 1
}
var offsetAll = delta.offsetAll
if (this.onscroll) {
this.onscroll(e, {
offset: offset,
offsetAll: offsetAll,
start: delta.start,
end: delta.end
})
}
if (!offset && delta.total) {
this.triggerEvent('totop')
}
if (offset >= offsetAll) {
this.triggerEvent('tobottom')
}
},
// update render zone by scroll offset.
updateZone: function (offset) {
var overs = this.variable
? this.getVarOvers(offset)
: Math.floor(offset / this.size)
var delta = this.delta
var zone = this.getZone(overs)
var bench = this.bench || this.remain
// for better performance, if scroll pass items within now bench, do not update.
if (!zone.isLast && (overs > delta.start) && (overs - delta.start <= bench)) {
return
}
delta.end = zone.end
delta.start = zone.start
this.$forceUpdate()
},
// return the scroll passed items count in variable.
getVarOvers: function (offset) {
var low = 0
var middle = 0
var middleOffset = 0
var delta = this.delta
var high = delta.total
while (low <= high) {
middle = low + Math.floor((high - low) / 2)
middleOffset = this.getVarOffset(middle)
// calculate the average variable height at first binary search.
if (!delta.varAverSize) {
delta.varAverSize = Math.floor(middleOffset / middle)
}
if (middleOffset === offset) {
return middle
} else if (middleOffset < offset) {
low = middle + 1
} else if (middleOffset > offset) {
high = middle - 1
}
}
return low > 0 ? --low : 0
},
// return a variable scroll offset from given index.
getVarOffset: function (index, nocache) {
var delta = this.delta
var cache = delta.varCache[index]
if (!nocache && cache) {
return cache.offset
}
var offset = 0
for (var i = 0; i < index; i++) {
var size = this.getVarSize(i, nocache)
delta.varCache[i] = {
size: size,
offset: offset
}
offset += size
}
delta.varLastCalcIndex = Math.max(delta.varLastCalcIndex, index - 1)
delta.varLastCalcIndex = Math.min(delta.varLastCalcIndex, delta.total - 1)
return offset
},
// return a variable size (height) from given index.
getVarSize: function (index, nocache) {
var cache = this.delta.varCache[index]
if (!nocache && cache) {
return cache.size
}
if (typeof this.variable === 'function') {
return this.variable(index) || 0
} else {
var slot = this.$slots.default[index]
var style = slot && slot.data && slot.data.style
if (style && style.height) {
var shm = style.height.match(/^(.*)px$/)
return (shm && +shm[1]) || 0
}
}
return 0
},
// return the variable paddingTop base current zone.
// @todo: if set a large `start` before variable was calculated,
// here will also case too much offset calculate when list is very large,
// consider use estimate paddingTop in this case just like `getVarPaddingBottom`.
getVarPaddingTop: function () {
return this.getVarOffset(this.delta.start)
},
// return the variable paddingBottom base current zone.
getVarPaddingBottom: function () {
var delta = this.delta
var last = delta.total - 1
if (delta.total - delta.end <= delta.keeps || delta.varLastCalcIndex === last) {
return this.getVarOffset(last) - this.getVarOffset(delta.end)
} else {
// if unreached last zone or uncalculate real behind offset
// return the estimate paddingBottom avoid too much calculate.
return (delta.total - delta.end) * (delta.varAverSize || this.size)
}
},
// retun the variable all heights use to judge reach bottom.
getVarAllHeight: function () {
var delta = this.delta
if (delta.total - delta.end <= delta.keeps || delta.varLastCalcIndex === delta.total - 1) {
return this.getVarOffset(delta.total)
} else {
return this.getVarOffset(delta.start) + (delta.total - delta.end) * (delta.varAverSize || this.size)
}
},
// the ONLY ONE public method, allow the parent update variable by index.
updateVariable: function (index) {
// clear/update all the offfsets and heights ahead of index.
this.getVarOffset(index, true)
},
// return the right zone info base on `start/index`.
getZone: function (index) {
var start, end
var delta = this.delta
index = parseInt(index, 10)
index = Math.max(0, index)
var lastStart = delta.total - delta.keeps
var isLast = (index <= delta.total && index >= lastStart) || (index > delta.total)
if (isLast) {
end = delta.total - 1
start = Math.max(0, lastStart)
} else {
start = index
end = start + delta.keeps - 1
}
return {
end: end,
start: start,
isLast: isLast
}
},
// trigger a props event on parent.
triggerEvent: function (event) {
if (this[event]) {
this[event]()
}
},
// set manual scroll top.
setScrollTop: function (scrollTop) {
var vsl = this.$refs.vsl
if (vsl) {
vsl.scrollTop = scrollTop
}
},
// filter the shown items base on `start` and `end`.
filter: function () {
var delta = this.delta
var slots = this.$slots.default
if (!slots) {
slots = []
delta.start = 0
}
delta.total = slots.length
var paddingTop, paddingBottom, allHeight
var hasPadding = delta.total > delta.keeps
if (this.variable) {
allHeight = this.getVarAllHeight()
paddingTop = hasPadding ? this.getVarPaddingTop() : 0
paddingBottom = hasPadding ? this.getVarPaddingBottom() : 0
} else {
allHeight = this.size * delta.total
paddingTop = this.size * (hasPadding ? delta.start : 0)
paddingBottom = this.size * (hasPadding ? delta.total - delta.keeps : 0) - paddingTop
}
delta.paddingTop = paddingTop
delta.paddingBottom = paddingBottom
delta.offsetAll = allHeight - this.size * this.remain
var targets = []
for (var i = delta.start; i <= delta.end; i++) {
targets.push(slots[i])
}
return targets
}
},
mounted: function () {
if (this.start) {
var start = this.getZone(this.start).start
this.setScrollTop(this.variable ? this.getVarOffset(start) : start * this.size)
} else if (this.offset) {
this.setScrollTop(this.offset)
}
},
// check if delta should update when prorps change.
beforeUpdate: function () {
var delta = this.delta
delta.keeps = this.remain + (this.bench || this.remain)
var calcstart = this.alter === 'start' ? this.start : delta.start
var zone = this.getZone(calcstart)
// if start, size or offset change, update scroll position.
if (~['start', 'size', 'offset'].indexOf(this.alter)) {
this.$nextTick(this.setScrollTop.bind(this, this.alter === 'offset'
? this.offset : this.variable
? this.getVarOffset(zone.isLast ? delta.total : zone.start)
: zone.isLast && (delta.total - calcstart <= this.remain)
? delta.total * this.size : calcstart * this.size)
)
}
// if points out difference, force update once again.
if (calcstart !== zone.start || delta.end !== zone.end || this.alter) {
this.alter = ''
delta.end = zone.end
delta.start = zone.start
this.$forceUpdate()
}
},
render: function (h) {
var list = this.filter()
var delta = this.delta
var dbc = this.debounce
var height = this.height
return h(this.rtag, {
'ref': 'vsl',
'style': {
'display': 'block',
'overflow-x':'hidden',
'overflow-y': 'auto',
'-webkit-overflow-scrolling':'touch',
'scroll-behavior':'smooth',
'height': height == 0 ? this.size * this.remain + 'px' : height + 'px'
},
'class':'virtuallist',
'on': {
'&scroll': dbc ? _debounce(this.onScroll.bind(this), dbc) : this.onScroll
}
}, [
h(this.wtag, {
'style': {
'display': 'block',
'padding-top': delta.paddingTop + 'px',
'padding-bottom': delta.paddingBottom + 'px'
},
'class': this.wclass
}, list)
])
}
})
})