2018-03-30 08:16:53 +02:00
|
|
|
<VirtualListContainer :realm :containerQuery on:initialized on:noNeedToScroll >
|
|
|
|
<div class="virtual-list"
|
2018-03-23 01:33:42 +01:00
|
|
|
style="height: {{$height}}px;"
|
|
|
|
ref:node >
|
2018-04-30 02:45:03 +02:00
|
|
|
<VirtualListHeader component={{headerComponent}} virtualProps={{headerProps}} shown={{$showHeader}}/>
|
2018-02-13 07:06:05 +01:00
|
|
|
{{#if $visibleItems}}
|
|
|
|
{{#each $visibleItems as visibleItem @key}}
|
|
|
|
<VirtualListLazyItem :component
|
2018-04-30 02:45:03 +02:00
|
|
|
offset={{visibleItem.offset}}
|
2018-04-30 03:42:03 +02:00
|
|
|
:makeProps
|
2018-04-30 02:45:03 +02:00
|
|
|
key={{visibleItem.key}}
|
|
|
|
index={{visibleItem.index}}
|
2018-02-13 07:06:05 +01:00
|
|
|
/>
|
|
|
|
{{/each}}
|
|
|
|
{{/if}}
|
|
|
|
{{#if $showFooter}}
|
2018-04-30 02:45:03 +02:00
|
|
|
<VirtualListFooter component={{footerComponent}} />
|
2018-02-13 07:06:05 +01:00
|
|
|
{{/if}}
|
|
|
|
</div>
|
|
|
|
</VirtualListContainer>
|
2018-01-15 19:54:02 +01:00
|
|
|
<style>
|
|
|
|
.virtual-list {
|
|
|
|
position: relative;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script>
|
2018-02-13 07:06:05 +01:00
|
|
|
import VirtualListContainer from './VirtualListContainer.html'
|
2018-01-24 03:15:14 +01:00
|
|
|
import VirtualListLazyItem from './VirtualListLazyItem'
|
2018-01-22 01:07:11 +01:00
|
|
|
import VirtualListFooter from './VirtualListFooter.html'
|
2018-02-12 04:15:21 +01:00
|
|
|
import VirtualListHeader from './VirtualListHeader.html'
|
2018-01-25 17:23:14 +01:00
|
|
|
import { virtualListStore } from './virtualListStore'
|
2018-04-06 02:57:36 +02:00
|
|
|
import throttle from 'lodash-es/throttle'
|
2018-01-25 17:23:14 +01:00
|
|
|
import { mark, stop } from '../../_utils/marks'
|
2018-04-06 02:57:36 +02:00
|
|
|
import isEqual from 'lodash-es/isEqual'
|
2018-01-15 19:54:02 +01:00
|
|
|
|
2018-03-21 04:28:53 +01:00
|
|
|
const DISTANCE_FROM_BOTTOM_TO_FIRE = 800
|
2018-02-12 04:15:21 +01:00
|
|
|
const SCROLL_EVENT_THROTTLE = 1000
|
2018-01-16 02:25:32 +01:00
|
|
|
|
2018-01-15 19:54:02 +01:00
|
|
|
export default {
|
2018-01-16 02:25:32 +01:00
|
|
|
oncreate () {
|
2018-02-12 05:12:15 +01:00
|
|
|
this.fireScrollToBottom = throttle(() => {
|
|
|
|
this.fire('scrollToBottom')
|
|
|
|
}, SCROLL_EVENT_THROTTLE)
|
|
|
|
this.fireScrollToTop = throttle(() => {
|
|
|
|
this.fire('scrollToTop')
|
|
|
|
}, SCROLL_EVENT_THROTTLE)
|
2018-02-11 23:17:17 +01:00
|
|
|
this.observe('showFooter', showFooter => {
|
2018-02-12 05:12:15 +01:00
|
|
|
mark('set showFooter')
|
2018-01-27 17:13:28 +01:00
|
|
|
this.store.setForRealm({showFooter: showFooter})
|
2018-02-12 05:12:15 +01:00
|
|
|
mark('set showFooter')
|
2018-01-22 01:07:11 +01:00
|
|
|
})
|
2018-02-12 04:15:21 +01:00
|
|
|
this.observe('showHeader', showHeader => {
|
2018-02-12 05:12:15 +01:00
|
|
|
mark('set showHeader')
|
2018-02-12 04:15:21 +01:00
|
|
|
this.store.setForRealm({showHeader: showHeader})
|
2018-02-12 05:12:15 +01:00
|
|
|
stop('set showHeader')
|
2018-02-12 04:15:21 +01:00
|
|
|
})
|
2018-02-25 19:50:04 +01:00
|
|
|
this.observe('items', (newItems, oldItems) => {
|
|
|
|
if (!newItems || isEqual(newItems, oldItems)) {
|
|
|
|
return
|
|
|
|
}
|
2018-01-17 09:59:15 +01:00
|
|
|
mark('set items')
|
2018-02-25 19:50:04 +01:00
|
|
|
this.store.setForRealm({items: newItems})
|
2018-01-17 09:59:15 +01:00
|
|
|
stop('set items')
|
2018-01-15 19:54:02 +01:00
|
|
|
})
|
2018-02-11 23:17:17 +01:00
|
|
|
this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => {
|
2018-01-25 04:26:08 +01:00
|
|
|
if (allVisibleItemsHaveHeight) {
|
|
|
|
this.fire('initializedVisibleItems')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-17 09:06:24 +01:00
|
|
|
let observedOnce = false
|
|
|
|
|
2018-02-11 23:17:17 +01:00
|
|
|
this.observe('distanceFromBottom', (distanceFromBottom) => {
|
2018-01-17 09:06:24 +01:00
|
|
|
if (!observedOnce) {
|
|
|
|
observedOnce = true // TODO: the first time is always 0... need better way to handle this
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (distanceFromBottom >= 0 &&
|
2018-01-17 06:43:31 +01:00
|
|
|
distanceFromBottom <= DISTANCE_FROM_BOTTOM_TO_FIRE) {
|
2018-01-19 06:52:58 +01:00
|
|
|
this.fireScrollToBottom()
|
2018-01-17 06:43:31 +01:00
|
|
|
}
|
|
|
|
})
|
2018-02-12 04:15:21 +01:00
|
|
|
|
2018-02-13 18:15:10 +01:00
|
|
|
this.observe('scrollTop', (scrollTop) => {
|
|
|
|
this.fire('scrollTopChanged', scrollTop)
|
|
|
|
if (scrollTop === 0) {
|
2018-02-12 04:15:21 +01:00
|
|
|
this.fireScrollToTop()
|
|
|
|
}
|
2018-03-23 01:33:42 +01:00
|
|
|
this.calculateListOffset()
|
2018-02-12 04:15:21 +01:00
|
|
|
})
|
2018-01-16 03:29:28 +01:00
|
|
|
},
|
2018-01-15 19:54:02 +01:00
|
|
|
data: () => ({
|
2018-01-15 21:23:28 +01:00
|
|
|
component: null
|
2018-01-15 19:54:02 +01:00
|
|
|
}),
|
2018-01-15 21:23:28 +01:00
|
|
|
store: () => virtualListStore,
|
2018-01-15 19:54:02 +01:00
|
|
|
components: {
|
2018-02-13 07:06:05 +01:00
|
|
|
VirtualListContainer,
|
2018-01-24 03:15:14 +01:00
|
|
|
VirtualListLazyItem,
|
2018-02-12 04:15:21 +01:00
|
|
|
VirtualListFooter,
|
|
|
|
VirtualListHeader
|
2018-01-17 08:16:15 +01:00
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
distanceFromBottom: ($scrollHeight, $scrollTop, $offsetHeight) => {
|
|
|
|
return $scrollHeight - $scrollTop - $offsetHeight
|
2018-01-25 04:26:08 +01:00
|
|
|
},
|
2018-02-13 18:15:10 +01:00
|
|
|
scrollTop: ($scrollTop) => $scrollTop,
|
2018-01-25 04:26:08 +01:00
|
|
|
// TODO: bug in svelte store, shouldn't need to do this
|
2018-04-20 06:38:01 +02:00
|
|
|
allVisibleItemsHaveHeight: ($allVisibleItemsHaveHeight) => $allVisibleItemsHaveHeight
|
2018-03-23 01:33:42 +01:00
|
|
|
},
|
|
|
|
methods: {
|
2018-04-20 06:38:01 +02:00
|
|
|
calculateListOffset () {
|
2018-03-23 01:33:42 +01:00
|
|
|
// TODO: better way to get the offset top?
|
|
|
|
let node = this.refs.node
|
|
|
|
if (!node) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mark('calculateListOffset')
|
|
|
|
let listOffset = node.offsetParent.offsetTop
|
|
|
|
this.store.setForRealm({listOffset})
|
|
|
|
stop('calculateListOffset')
|
|
|
|
}
|
2018-01-15 19:54:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|