Skip to content

Commit 0efac4b

Browse files
committed
Update
1 parent 10617d6 commit 0efac4b

File tree

4 files changed

+69
-73
lines changed

4 files changed

+69
-73
lines changed

src/index.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* virtual list default component.
2+
* virtual list default component
33
*/
44

55
import Vue from 'vue'
@@ -12,7 +12,7 @@ const EVENT_TYPE = {
1212
SLOT: 'slot_resize'
1313
}
1414
const SLOT_TYPE = {
15-
HEADER: 'header', // string value also use for aria role attribute.
15+
HEADER: 'header', // string value also use for aria role attribute
1616
FOOTER: 'footer'
1717
}
1818

@@ -41,21 +41,21 @@ const VirtualList = Vue.component(NAME, {
4141
this.directionKey = this.isHorizontal ? 'scrollLeft' : 'scrollTop'
4242

4343
this.virtual = new Virtual({
44-
size: this.size, // also could be a estimate value.
44+
size: this.size, // also could be a estimate value
4545
slotHeaderSize: 0,
4646
slotFooterSize: 0,
4747
keeps: this.keeps,
48-
buffer: Math.round(this.keeps / 3), // recommend for a third of keeps.
48+
buffer: Math.round(this.keeps / 3), // recommend for a third of keeps
4949
uniqueIds: this.getUniqueIdFromDataSources()
5050
}, this.onRangeChanged)
5151

52-
// also need sync initial range first.
52+
// also need sync initial range first
5353
this.range = this.virtual.getRange()
5454

55-
// listen item size changing.
55+
// listen item size changing
5656
this.$on(EVENT_TYPE.ITEM, this.onItemResized)
5757

58-
// listen slot size changing.
58+
// listen slot size changing
5959
if (this.$slots.header || this.$slots.footer) {
6060
this.$on(EVENT_TYPE.SLOT, this.onSlotResized)
6161
}
@@ -66,7 +66,7 @@ const VirtualList = Vue.component(NAME, {
6666
},
6767

6868
mounted () {
69-
// set position.
69+
// set position
7070
if (this.start) {
7171
this.scrollToIndex(this.start)
7272
} else if (this.offset) {
@@ -77,32 +77,32 @@ const VirtualList = Vue.component(NAME, {
7777
},
7878

7979
methods: {
80-
// set current scroll position to a expectant offset.
80+
// set current scroll position to a expectant offset
8181
scrollToOffset (offset) {
8282
const { root } = this.$refs
8383
if (root) {
8484
root[this.directionKey] = offset || 0
8585
}
8686
},
8787

88-
// set current scroll position to a expectant index.
88+
// set current scroll position to a expectant index
8989
scrollToIndex (index) {
9090
const offset = this.virtual.getOffset(index)
9191
this.scrollToOffset(offset)
9292
},
9393

94-
// ----------- public method end. -----------
94+
// ----------- public method end -----------
9595

9696
getUniqueIdFromDataSources () {
9797
return this.dataSources.map((dataSource) => dataSource[this.dataKey])
9898
},
9999

100-
// event called when every item mounted or size changed.
100+
// event called when each item mounted or size changed
101101
onItemResized (id, size) {
102102
this.virtual.saveSize(id, size)
103103
},
104104

105-
// event called when slot mounted or size changed.
105+
// event called when slot mounted or size changed
106106
onSlotResized (type, size, hasInit) {
107107
if (type === SLOT_TYPE.HEADER) {
108108
this.virtual.updateParam('slotHeaderSize', size)
@@ -115,7 +115,7 @@ const VirtualList = Vue.component(NAME, {
115115
}
116116
},
117117

118-
// here is the rerendering entry.
118+
// here is the rerendering entry
119119
onRangeChanged (range) {
120120
this.range = range
121121
},
@@ -130,7 +130,7 @@ const VirtualList = Vue.component(NAME, {
130130
const offsetShape = root[this.isHorizontal ? 'clientWidth' : 'clientHeight']
131131
const scrollShape = root[this.isHorizontal ? 'scrollWidth' : 'scrollHeight']
132132

133-
// iOS scrolling spring-back behavior will make direction mistake.
133+
// iOS scroll-spring-back behavior will make direction mistake
134134
if (offset + offsetShape > scrollShape) {
135135
return
136136
}
@@ -139,22 +139,19 @@ const VirtualList = Vue.component(NAME, {
139139
this.emitEvent(offset, offsetShape, scrollShape, evt)
140140
},
141141

142-
// emit event in special position.
142+
// emit event in special position
143143
emitEvent (offset, offsetShape, scrollShape, evt) {
144144
const range = this.virtual.getRange()
145-
const isFront = this.virtual.isFront()
146-
const isBehind = this.virtual.isBehind()
147-
148-
if (isFront && !!this.dataSources.length && offset - this.topThreshold <= 0) {
145+
if (this.virtual.isFront() && !!this.dataSources.length && offset - this.topThreshold <= 0) {
149146
this.$emit('totop', evt, range)
150-
} else if (isBehind && offset + offsetShape + this.bottomThreshold >= scrollShape) {
147+
} else if (this.virtual.isBehind() && offset + offsetShape + this.bottomThreshold >= scrollShape) {
151148
this.$emit('tobottom', evt, range)
152149
} else {
153150
this.$emit('scroll', evt, range)
154151
}
155152
},
156153

157-
// get the real render slots based on range data.
154+
// get the real render slots based on range data
158155
getRenderSlots (h) {
159156
const slots = []
160157
const start = this.disabled ? 0 : this.range.start
@@ -182,7 +179,7 @@ const VirtualList = Vue.component(NAME, {
182179
}
183180
},
184181

185-
// render function, a closer-to-the-compiler alternative to templates.
182+
// render function, a closer-to-the-compiler alternative to templates
186183
// https://vuejs.org/v2/guide/render-function.html#The-Data-Object-In-Depth
187184
render (h) {
188185
const { header, footer } = this.$slots
@@ -196,7 +193,7 @@ const VirtualList = Vue.component(NAME, {
196193
'&scroll': this.onScroll
197194
}
198195
}, [
199-
// header slot.
196+
// header slot
200197
header ? h(Slot, {
201198
class: this.headerClass,
202199
props: {
@@ -206,7 +203,7 @@ const VirtualList = Vue.component(NAME, {
206203
}
207204
}, header) : null,
208205

209-
// main list.
206+
// main list
210207
h(this.wrapTag, {
211208
class: this.wrapClass,
212209
attrs: {
@@ -217,7 +214,7 @@ const VirtualList = Vue.component(NAME, {
217214
}
218215
}, this.getRenderSlots(h)),
219216

220-
// footer slot.
217+
// footer slot
221218
footer ? h(Slot, {
222219
class: this.footerClass,
223220
props: {

src/item.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* item and slot component both use similar wrapper
3-
* we need to know their size change at any time.
3+
* we need to know their size change at any time
44
*/
55

66
import Vue from 'vue'
@@ -13,12 +13,12 @@ const Wrapper = {
1313
},
1414

1515
mounted () {
16-
// dispatch once at initial.
16+
// dispatch once at initial
1717
this.dispatchSizeChange()
1818

1919
if (typeof ResizeObserver !== 'undefined') {
2020
this.resizeObserver = new ResizeObserver(() => {
21-
// dispatch when size changed.
21+
// dispatch when size changed
2222
if (this.hasInitial) {
2323
this.dispatchSizeChange()
2424
} else {
@@ -41,14 +41,14 @@ const Wrapper = {
4141
return this.$el ? this.$el[this.shapeKey] : 0
4242
},
4343

44-
// tell parent current size identify by unqiue key.
44+
// tell parent current size identify by unqiue key
4545
dispatchSizeChange () {
4646
this.$parent.$emit(this.event, this.uniqueKey, this.getCurrentSize(), this.hasInitial)
4747
}
4848
}
4949
}
5050

51-
// wrapping for item.
51+
// wrapping for item
5252
export const Item = Vue.component('virtual-list-item', {
5353
mixins: [Wrapper],
5454

@@ -66,7 +66,7 @@ export const Item = Vue.component('virtual-list-item', {
6666
}
6767
})
6868

69-
// wrapping for slot.
69+
// wrapping for slot
7070
export const Slot = Vue.component('virtual-list-slot', {
7171
mixins: [Wrapper],
7272

src/props.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* props declaration for default, item and slot component.
2+
* props declaration for default, item and slot component
33
*/
44

55
export const VirtualProps = {
@@ -43,7 +43,7 @@ export const VirtualProps = {
4343

4444
direction: {
4545
type: String,
46-
default: 'vertical' // the other value is horizontal.
46+
default: 'vertical' // the other value is horizontal
4747
},
4848
topThreshold: {
4949
type: Number,

0 commit comments

Comments
 (0)