pinafore/routes/_components/virtualList/VirtualList.html

109 lines
3.6 KiB
HTML
Raw Normal View History

2018-02-13 07:32:56 +01:00
<VirtualListContainer :realm :containerQuery >
<div class="virtual-list {{shown ? '' : 'hidden'}}" style="height: {{$height}}px;">
<VirtualListHeader component="{{headerComponent}}" virtualProps="{{headerProps}}" shown="{{$showHeader}}"/>
{{#if $visibleItems}}
{{#each $visibleItems as visibleItem @key}}
<VirtualListLazyItem :component
offset="{{visibleItem.offset}}"
makeProps="{{makeProps}}"
key="{{visibleItem.key}}"
index="{{visibleItem.index}}"
/>
{{/each}}
{{/if}}
{{#if $showFooter}}
<VirtualListFooter component="{{footerComponent}}" />
{{/if}}
</div>
</VirtualListContainer>
2018-01-15 19:54:02 +01:00
<style>
.virtual-list {
position: relative;
2018-01-21 23:31:59 +01:00
transition: opacity 0.25s linear;
2018-01-15 19:54:02 +01:00
}
</style>
<script>
import VirtualListContainer from './VirtualListContainer.html'
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'
import throttle from 'lodash/throttle'
2018-01-25 17:23:14 +01:00
import { mark, stop } from '../../_utils/marks'
2018-01-15 19:54:02 +01:00
const DISTANCE_FROM_BOTTOM_TO_FIRE = 400
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 () {
this.fireScrollToBottom = throttle(() => {
this.fire('scrollToBottom')
}, SCROLL_EVENT_THROTTLE)
this.fireScrollToTop = throttle(() => {
this.fire('scrollToTop')
}, SCROLL_EVENT_THROTTLE)
this.observe('showFooter', showFooter => {
mark('set showFooter')
2018-01-27 17:13:28 +01:00
this.store.setForRealm({showFooter: showFooter})
mark('set showFooter')
2018-01-22 01:07:11 +01:00
})
2018-02-12 04:15:21 +01:00
this.observe('showHeader', showHeader => {
mark('set showHeader')
2018-02-12 04:15:21 +01:00
this.store.setForRealm({showHeader: showHeader})
stop('set showHeader')
2018-02-12 04:15:21 +01:00
})
this.observe('itemsStringified', (itemsStringified) => {
let items = typeof itemsStringified === 'undefined' ? undefined :
JSON.parse(itemsStringified)
2018-01-17 09:59:15 +01:00
mark('set items')
2018-01-27 17:13:28 +01:00
this.store.setForRealm({items: items})
2018-01-17 09:59:15 +01:00
stop('set items')
2018-01-15 19:54:02 +01:00
})
this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => {
if (allVisibleItemsHaveHeight) {
this.fire('initializedVisibleItems')
}
})
2018-01-17 09:06:24 +01:00
let observedOnce = false
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) {
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-01-16 03:29:28 +01:00
},
2018-01-15 19:54:02 +01:00
data: () => ({
component: null
2018-01-15 19:54:02 +01:00
}),
store: () => virtualListStore,
2018-01-15 19:54:02 +01:00
components: {
VirtualListContainer,
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-02-13 18:15:10 +01:00
scrollTop: ($scrollTop) => $scrollTop,
// TODO: bug in svelte store, shouldn't need to do this
allVisibleItemsHaveHeight: ($allVisibleItemsHaveHeight) => $allVisibleItemsHaveHeight,
2018-02-13 07:49:02 +01:00
// TODO: hack to avoid getting called too often
itemsStringified: (items) => JSON.stringify(items)
2018-01-15 19:54:02 +01:00
}
}
</script>