2018-01-30 17:38:14 +00:00
|
|
|
<div class="pseudo-virtual-list {{shown ? '' : 'hidden'}}" on:initializedVisibleItems ref:node>
|
2018-01-30 03:22:28 +00:00
|
|
|
{{#each wrappedItems as wrappedItem, i @item}}
|
|
|
|
<PseudoVirtualListLazyItem
|
|
|
|
component="{{component}}"
|
|
|
|
index="{{i}}"
|
|
|
|
length="{{items.length}}"
|
|
|
|
makeProps="{{makeProps}}"
|
|
|
|
key="{{wrappedItem.item}}"
|
2018-01-30 17:38:14 +00:00
|
|
|
intersectionObserver="{{intersectionObserver}}"
|
|
|
|
hide="{{shouldHide(wrappedItem.item, intersectionStates)}}"
|
|
|
|
height="{{getHeight(wrappedItem.item, intersectionStates)}}"
|
2018-01-30 03:22:28 +00:00
|
|
|
on:renderedListItem="onRenderedListItem()"
|
|
|
|
/>
|
|
|
|
{{/each}}
|
|
|
|
</div>
|
|
|
|
<style>
|
|
|
|
.pseudo-virtual-list {
|
|
|
|
position: relative;
|
|
|
|
transition: opacity 0.25s linear;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script>
|
|
|
|
|
|
|
|
import PseudoVirtualListLazyItem from './PseudoVirtualListLazyItem.html'
|
2018-01-30 17:38:14 +00:00
|
|
|
import { getRectFromEntry } from '../../_utils/getRectFromEntry'
|
|
|
|
import { mark, stop } from '../../_utils/marks'
|
2018-01-30 03:22:28 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
oncreate() {
|
2018-01-30 17:38:14 +00:00
|
|
|
this.set({
|
|
|
|
intersectionObserver: new IntersectionObserver(this.onIntersection.bind(this), {
|
|
|
|
root: document.getElementsByClassName('container')[0], // TODO: fix this
|
|
|
|
rootMargin: '300% 0px'
|
|
|
|
}),
|
|
|
|
intersectionStates: {}
|
|
|
|
})
|
|
|
|
this.observe('allItemsHaveHeight', allItemsHaveHeight => {
|
|
|
|
console.log('allItemsHaveHeight', allItemsHaveHeight)
|
|
|
|
if (allItemsHaveHeight && !this.get('initialized')) {
|
2018-01-30 03:22:28 +00:00
|
|
|
this.set({initialized: true})
|
2018-01-30 17:38:14 +00:00
|
|
|
console.log('initializedVisibleItems')
|
2018-01-30 03:22:28 +00:00
|
|
|
this.fire('initializedVisibleItems')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2018-01-30 17:38:14 +00:00
|
|
|
ondestroy() {
|
|
|
|
let intersectionObserver = this.get('intersectionObserver')
|
|
|
|
if (intersectionObserver) {
|
|
|
|
intersectionObserver.disconnect()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data: () => ({
|
|
|
|
intersectionStates: {}
|
|
|
|
}),
|
|
|
|
helpers: {
|
|
|
|
shouldHide(key, intersectionStates) {
|
|
|
|
return !!(intersectionStates[key] && !intersectionStates[key].isIntersecting)
|
|
|
|
},
|
|
|
|
getHeight(key, intersectionStates) {
|
|
|
|
return intersectionStates[key] && intersectionStates[key].rect.height
|
|
|
|
}
|
|
|
|
},
|
2018-01-30 03:22:28 +00:00
|
|
|
methods: {
|
2018-01-30 17:38:14 +00:00
|
|
|
scrollToPosition(rect) {
|
|
|
|
if (this.get('scrolledToPosition')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.set({scrolledToPosition: true})
|
|
|
|
console.log('scrollToPosition', rect)
|
2018-01-30 03:22:28 +00:00
|
|
|
// TODO: there should be some cleaner way to grab the container element
|
2018-01-30 17:38:14 +00:00
|
|
|
let container = document.getElementsByClassName('container')[0]
|
2018-01-30 03:22:28 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2018-01-30 17:38:14 +00:00
|
|
|
onIntersection(entries) {
|
|
|
|
mark('onIntersection')
|
|
|
|
let newIntersectionStates = {}
|
|
|
|
let scrollToItem = this.get('scrollToItem')
|
|
|
|
let intersectionStates = this.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,
|
|
|
|
rect: rect
|
|
|
|
}
|
|
|
|
if (scrollToItem === key) {
|
|
|
|
this.scrollToPosition(rect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
intersectionStates = Object.assign(intersectionStates, newIntersectionStates)
|
|
|
|
this.set({intersectionStates: intersectionStates})
|
|
|
|
stop('onIntersection')
|
2018-01-30 03:22:28 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2018-01-30 17:38:14 +00:00
|
|
|
wrappedItems: (items) => items.map(item => ({item: item})),
|
|
|
|
allItemsHaveHeight: (items, intersectionStates) => {
|
|
|
|
if (!items.length) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for (let item of items) {
|
|
|
|
if (!intersectionStates[item]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2018-01-30 03:22:28 +00:00
|
|
|
},
|
|
|
|
components: {
|
|
|
|
PseudoVirtualListLazyItem
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|