pinafore/routes/_components/pseudoVirtualList/PseudoVirtualList.html

135 lines
4.7 KiB
HTML
Raw Normal View History

<div class="pseudo-virtual-list {{shown ? '' : 'hidden'}}" on:initializedVisibleItems ref:node>
{{#each wrappedItems as wrappedItem, i @item}}
<PseudoVirtualListLazyItem
component="{{component}}"
index="{{i}}"
length="{{items.length}}"
makeProps="{{makeProps}}"
key="{{wrappedItem.item}}"
intersectionObserver="{{intersectionObserver}}"
2018-01-31 05:17:01 +00:00
isIntersecting="{{isIntersecting(wrappedItem.item, $intersectionStates)}}"
isCached="{{isCached(wrappedItem.item, $intersectionStates)}}"
2018-01-31 02:26:13 +00:00
height="{{getHeight(wrappedItem.item, $intersectionStates)}}"
/>
{{/each}}
</div>
<style>
.pseudo-virtual-list {
position: relative;
transition: opacity 0.25s linear;
}
</style>
<script>
import PseudoVirtualListLazyItem from './PseudoVirtualListLazyItem.html'
import { getRectFromEntry } from '../../_utils/getRectFromEntry'
import { mark, stop } from '../../_utils/marks'
2018-01-31 02:26:13 +00:00
import { pseudoVirtualListStore } from './pseudoVirtualListStore'
export default {
oncreate() {
2018-01-31 02:26:13 +00:00
mark('PseudoVirtualList oncreate()')
2018-01-31 05:55:23 +00:00
this.store.setCurrentRealm(this.get('realm'))
2018-01-31 02:26:13 +00:00
// When re-rendering, assume all items are non-intersecting until told otherwise.
// We already have the heights cached.
let intersectionStates = this.store.get('intersectionStates')
let keys = Object.keys(intersectionStates)
for (let key of keys) {
2018-01-31 05:17:01 +00:00
intersectionStates[key].isCached = true
2018-01-31 02:26:13 +00:00
}
this.store.setForRealm({intersectionStates: intersectionStates})
this.set({intersectionObserver: new IntersectionObserver(this.onIntersection.bind(this), {
root: document.getElementsByClassName('container')[0], // TODO: fix this
rootMargin: '300% 0px'
})})
this.observe('allItemsHaveHeight', allItemsHaveHeight => {
console.log('allItemsHaveHeight', allItemsHaveHeight)
if (allItemsHaveHeight && !this.get('initialized')) {
this.set({initialized: true})
console.log('initializedVisibleItems')
this.fire('initializedVisibleItems')
}
})
2018-01-31 02:26:13 +00:00
stop('PseudoVirtualList oncreate()')
},
ondestroy() {
let intersectionObserver = this.get('intersectionObserver')
if (intersectionObserver) {
intersectionObserver.disconnect()
}
},
helpers: {
2018-01-31 05:17:01 +00:00
isIntersecting(key, $intersectionStates) {
return !!($intersectionStates[key] && $intersectionStates[key].isIntersecting)
},
isCached(key, $intersectionStates) {
return !!($intersectionStates[key] && $intersectionStates[key].isCached)
},
2018-01-31 02:26:13 +00:00
getHeight(key, $intersectionStates) {
2018-01-31 05:34:35 +00:00
return $intersectionStates[key] && $intersectionStates[key].height
}
},
methods: {
scrollToPosition(rect) {
if (this.get('scrolledToPosition')) {
return
}
this.set({scrolledToPosition: true})
2018-01-31 05:34:35 +00:00
console.log('scrollToPosition', rect.top)
// TODO: there should be some cleaner way to grab the container element
let container = document.getElementsByClassName('container')[0]
if (!container) {
return
}
let containerRect = container.getBoundingClientRect()
let scrollTop = rect.top - containerRect.top
if (scrollTop !== 0) {
requestAnimationFrame(() => {
console.log('setting scrollTop to ', scrollTop)
container.scrollTop = scrollTop
})
}
},
onIntersection(entries) {
mark('onIntersection')
let newIntersectionStates = {}
let scrollToItem = this.get('scrollToItem')
2018-01-31 02:26:13 +00:00
let intersectionStates = this.store.get('intersectionStates')
for (let entry of entries) {
let key = entry.target.getAttribute('pseudo-virtual-list-key')
let rect = getRectFromEntry(entry)
newIntersectionStates[key] = {
isIntersecting: entry.isIntersecting,
2018-01-31 05:34:35 +00:00
height: rect.height
}
if (scrollToItem === key) {
this.scrollToPosition(rect)
}
}
intersectionStates = Object.assign(intersectionStates, newIntersectionStates)
2018-01-31 02:26:13 +00:00
this.store.setForRealm({intersectionStates: intersectionStates})
stop('onIntersection')
}
},
computed: {
wrappedItems: (items) => items.map(item => ({item: item})),
2018-01-31 02:26:13 +00:00
allItemsHaveHeight: (items, $intersectionStates) => {
if (!items.length) {
return false
}
for (let item of items) {
2018-01-31 02:26:13 +00:00
if (!$intersectionStates[item]) {
return false
}
}
return true
}
},
components: {
PseudoVirtualListLazyItem
2018-01-31 02:26:13 +00:00
},
store: () => pseudoVirtualListStore
}
</script>