Skip to content

Commit bb504bf

Browse files
committed
Fix index offset calculate error
1 parent c8a1ab6 commit bb504bf

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

dist/index.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
FIXED: 'FIXED',
4949
DYNAMIC: 'DYNAMIC'
5050
};
51-
var LEADING_BUFFER = 1;
51+
var LEADING_BUFFER = 2;
5252

5353
var Virtual = /*#__PURE__*/function () {
5454
function Virtual(param, updateHook) {
@@ -76,7 +76,7 @@
7676

7777
this.range = Object.create(null);
7878

79-
if (this.param && !this.param.disabled) {
79+
if (this.param) {
8080
this.checkRange(0, param.keeps - 1);
8181
} // benchmark test data.
8282
// this.__bsearchCalls = 0
@@ -100,13 +100,13 @@
100100
return range;
101101
}
102102
}, {
103-
key: "isLower",
104-
value: function isLower() {
103+
key: "isBehind",
104+
value: function isBehind() {
105105
return this.direction === DIRECTION_TYPE.BEHIND;
106106
}
107107
}, {
108-
key: "isUpper",
109-
value: function isUpper() {
108+
key: "isFront",
109+
value: function isFront() {
110110
return this.direction === DIRECTION_TYPE.FRONT;
111111
} // return start index offset.
112112

@@ -155,14 +155,14 @@
155155
value: function handleDataSourcesChange() {
156156
var start = this.range.start;
157157

158-
if (this.direction === DIRECTION_TYPE.FRONT) {
158+
if (this.isFront()) {
159159
start = start - LEADING_BUFFER;
160-
} else if (this.direction === DIRECTION_TYPE.BEHIND) {
160+
} else if (this.isBehind()) {
161161
start = start + LEADING_BUFFER;
162162
}
163163

164164
start = Math.max(start, 0);
165-
this.updateRange(start, this.getEndByStart(start));
165+
this.updateRange(this.range.start, this.getEndByStart(start));
166166
} // when slot size change, we also need force update.
167167

168168
}, {
@@ -174,10 +174,6 @@
174174
}, {
175175
key: "handleScroll",
176176
value: function handleScroll(offset) {
177-
if (this.param.disabled) {
178-
return;
179-
}
180-
181177
this.direction = offset < this.offset ? DIRECTION_TYPE.FRONT : DIRECTION_TYPE.BEHIND;
182178
this.offset = offset;
183179

@@ -262,7 +258,7 @@
262258
var offset = 0;
263259
var indexSize = 0;
264260

265-
for (var index = 0; index <= givenIndex; index++) {
261+
for (var index = 0; index < givenIndex; index++) {
266262
// this.__getIndexOffsetCalls++
267263
indexSize = this.sizes.get(this.param.uniqueIds[index]);
268264
offset = offset + (indexSize || this.getEstimateSize());
@@ -312,10 +308,7 @@
312308
this.range.end = end;
313309
this.range.padFront = this.getPadFront();
314310
this.range.padBehind = this.getPadBehind();
315-
316-
if (!this.param.disabled) {
317-
this.updateHook(this.getRange());
318-
}
311+
this.updateHook(this.getRange());
319312
} // return end base on start when going to a new range.
320313

321314
}, {
@@ -591,7 +584,6 @@
591584
slotHeaderSize: 0,
592585
slotFooterSize: 0,
593586
keeps: this.keeps,
594-
disabled: this.disabled,
595587
buffer: Math.round(this.keeps / 3),
596588
// recommend for a third of keeps.
597589
uniqueIds: this.getUniqueIdFromDataSources()
@@ -670,14 +662,14 @@
670662
// ref element is definitely available here.
671663
var root = this.$refs.root;
672664
var range = this.virtual.getRange();
673-
var isLower = this.virtual.isLower();
674-
var isUpper = this.virtual.isUpper();
665+
var isFront = this.virtual.isFront();
666+
var isBehind = this.virtual.isBehind();
675667
var offsetShape = root[this.isHorizontal ? 'clientWidth' : 'clientHeight'];
676668
var scrollShape = root[this.isHorizontal ? 'scrollWidth' : 'scrollHeight'];
677669

678-
if (isUpper && !!this.dataSources.length && offset - this.upperThreshold <= 0) {
670+
if (isFront && !!this.dataSources.length && offset - this.upperThreshold <= 0) {
679671
this.$emit('totop', evt, range);
680-
} else if (isLower && offset + offsetShape + this.lowerThreshold >= scrollShape) {
672+
} else if (isBehind && offset + offsetShape + this.lowerThreshold >= scrollShape) {
681673
this.$emit('tobottom', evt, range);
682674
} else {
683675
this.$emit('scroll', evt, range);
@@ -718,7 +710,7 @@
718710
var _this$$slots = this.$slots,
719711
header = _this$$slots.header,
720712
footer = _this$$slots.footer;
721-
var padding = this.isHorizontal ? "0px ".concat(this.range.padBehind, "px 0px ").concat(this.range.padFront, "px") : "".concat(this.range.padFront, "px 0px ").concat(this.range.padBehind, "px");
713+
var padding = this.disabled ? 0 : this.isHorizontal ? "0px ".concat(this.range.padBehind, "px 0px ").concat(this.range.padFront, "px") : "".concat(this.range.padFront, "px 0px ").concat(this.range.padBehind, "px");
722714
return h(this.rootTag, {
723715
ref: 'root',
724716
on: {

src/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const VirtualList = Vue.component(NAME, {
4545
slotHeaderSize: 0,
4646
slotFooterSize: 0,
4747
keeps: this.keeps,
48-
disabled: this.disabled,
4948
buffer: Math.round(this.keeps / 3), // recommend for a third of keeps.
5049
uniqueIds: this.getUniqueIdFromDataSources()
5150
}, this.onRangeChanged)
@@ -149,7 +148,6 @@ const VirtualList = Vue.component(NAME, {
149148
const slots = []
150149
const start = this.disabled ? 0 : this.range.start
151150
const end = this.disabled ? this.dataSources.length - 1 : this.range.end
152-
153151
for (let index = start; index <= end; index++) {
154152
const dataSource = this.dataSources[index]
155153
if (dataSource) {
@@ -168,7 +166,6 @@ const VirtualList = Vue.component(NAME, {
168166
console.warn(`[${NAME}]: cannot get the index ${index} from data-sources.`)
169167
}
170168
}
171-
172169
return slots
173170
}
174171
},
@@ -177,7 +174,7 @@ const VirtualList = Vue.component(NAME, {
177174
// https://vuejs.org/v2/guide/render-function.html#The-Data-Object-In-Depth
178175
render (h) {
179176
const { header, footer } = this.$slots
180-
const padding = this.isHorizontal
177+
const padding = this.disabled ? 0 : this.isHorizontal
181178
? `0px ${this.range.padBehind}px 0px ${this.range.padFront}px`
182179
: `${this.range.padFront}px 0px ${this.range.padBehind}px`
183180

src/virtual.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const CALC_TYPE = {
1111
FIXED: 'FIXED',
1212
DYNAMIC: 'DYNAMIC'
1313
}
14-
const LEADING_BUFFER = 1
14+
const LEADING_BUFFER = 2
1515

1616
export default class Virtual {
1717
constructor (param, updateHook) {
@@ -37,7 +37,7 @@ export default class Virtual {
3737

3838
// range data.
3939
this.range = Object.create(null)
40-
if (this.param && !this.param.disabled) {
40+
if (this.param) {
4141
this.checkRange(0, param.keeps - 1)
4242
}
4343

@@ -118,7 +118,7 @@ export default class Virtual {
118118

119119
start = Math.max(start, 0)
120120

121-
this.updateRange(start, this.getEndByStart(start))
121+
this.updateRange(this.range.start, this.getEndByStart(start))
122122
}
123123

124124
// when slot size change, we also need force update.
@@ -128,10 +128,6 @@ export default class Virtual {
128128

129129
// calculating range on scroll.
130130
handleScroll (offset) {
131-
if (this.param.disabled) {
132-
return
133-
}
134-
135131
this.direction = offset < this.offset ? DIRECTION_TYPE.FRONT : DIRECTION_TYPE.BEHIND
136132
this.offset = offset
137133

@@ -211,7 +207,7 @@ export default class Virtual {
211207

212208
let offset = 0
213209
let indexSize = 0
214-
for (let index = 0; index <= givenIndex; index++) {
210+
for (let index = 0; index < givenIndex; index++) {
215211
// this.__getIndexOffsetCalls++
216212
indexSize = this.sizes.get(this.param.uniqueIds[index])
217213
offset = offset + (indexSize || this.getEstimateSize())
@@ -260,9 +256,7 @@ export default class Virtual {
260256
this.range.padFront = this.getPadFront()
261257
this.range.padBehind = this.getPadBehind()
262258

263-
if (!this.param.disabled) {
264-
this.updateHook(this.getRange())
265-
}
259+
this.updateHook(this.getRange())
266260
}
267261

268262
// return end base on start when going to a new range.

0 commit comments

Comments
 (0)