2018-03-30 06:16:53 +00:00
|
|
|
<div class="pseudo-virtual-list" on:initialized ref:node>
|
2018-04-13 02:38:41 +00:00
|
|
|
{{#each wrappedItems as wrappedItem, i @item}}
|
|
|
|
<PseudoVirtualListLazyItem
|
2018-04-30 01:42:03 +00:00
|
|
|
:component
|
2018-04-30 00:45:03 +00:00
|
|
|
index={{i}}
|
|
|
|
length={{wrappedItems.length}}
|
2018-04-30 01:42:03 +00:00
|
|
|
:makeProps
|
2018-04-30 00:45:03 +00:00
|
|
|
key={{wrappedItem.item}}
|
2018-04-30 01:42:03 +00:00
|
|
|
:intersectionObserver
|
2018-04-30 00:45:03 +00:00
|
|
|
isIntersecting={{isIntersecting(wrappedItem.item, $intersectionStates)}}
|
|
|
|
isCached={{isCached(wrappedItem.item, $intersectionStates)}}
|
|
|
|
height={{getHeight(wrappedItem.item, $intersectionStates)}}
|
2018-04-13 02:38:41 +00:00
|
|
|
/>
|
|
|
|
{{/each}}
|
2018-01-30 03:22:28 +00:00
|
|
|
</div>
|
|
|
|
<style>
|
|
|
|
.pseudo-virtual-list {
|
|
|
|
position: relative;
|
|
|
|
}
|
|
|
|
</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-31 02:26:13 +00:00
|
|
|
import { pseudoVirtualListStore } from './pseudoVirtualListStore'
|
2018-01-30 03:22:28 +00:00
|
|
|
|
|
|
|
export default {
|
2018-04-20 04:38:01 +00:00
|
|
|
oncreate () {
|
2018-01-31 02:26:13 +00:00
|
|
|
mark('PseudoVirtualList oncreate()')
|
2018-04-19 16:37:05 +00:00
|
|
|
let { realm } = this.get()
|
|
|
|
this.store.setCurrentRealm(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.
|
2018-04-19 16:37:05 +00:00
|
|
|
let { intersectionStates } = this.store.get()
|
2018-01-31 02:26:13 +00:00
|
|
|
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})
|
|
|
|
|
2018-04-19 16:37:05 +00:00
|
|
|
let { containerQuery } = this.get()
|
2018-03-05 01:16:33 +00:00
|
|
|
let intersectionObserver = new IntersectionObserver(this.onIntersection.bind(this), {
|
2018-04-19 16:37:05 +00:00
|
|
|
root: document.querySelector(containerQuery),
|
2018-01-31 02:26:13 +00:00
|
|
|
rootMargin: '300% 0px'
|
2018-03-05 01:16:33 +00:00
|
|
|
})
|
|
|
|
this.set({intersectionObserver})
|
2018-01-30 17:38:14 +00:00
|
|
|
this.observe('allItemsHaveHeight', allItemsHaveHeight => {
|
|
|
|
console.log('allItemsHaveHeight', allItemsHaveHeight)
|
2018-04-19 16:37:05 +00:00
|
|
|
let { initialized } = this.get()
|
|
|
|
if (allItemsHaveHeight && !initialized) {
|
2018-01-30 03:22:28 +00:00
|
|
|
this.set({initialized: true})
|
2018-03-30 06:16:53 +00:00
|
|
|
console.log('initialized PseudoVirtualList')
|
|
|
|
this.fire('initialized')
|
2018-01-30 03:22:28 +00:00
|
|
|
}
|
|
|
|
})
|
2018-01-31 02:26:13 +00:00
|
|
|
stop('PseudoVirtualList oncreate()')
|
2018-01-30 03:22:28 +00:00
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
ondestroy () {
|
2018-04-19 16:37:05 +00:00
|
|
|
let { intersectionObserver } = this.get()
|
2018-01-30 17:38:14 +00:00
|
|
|
if (intersectionObserver) {
|
|
|
|
intersectionObserver.disconnect()
|
|
|
|
}
|
2018-02-13 06:47:25 +00:00
|
|
|
this.store.setCurrentRealm(null)
|
2018-01-30 17:38:14 +00:00
|
|
|
},
|
|
|
|
helpers: {
|
2018-04-20 04:38:01 +00:00
|
|
|
isIntersecting (key, $intersectionStates) {
|
2018-01-31 05:17:01 +00:00
|
|
|
return !!($intersectionStates[key] && $intersectionStates[key].isIntersecting)
|
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
isCached (key, $intersectionStates) {
|
2018-01-31 05:17:01 +00:00
|
|
|
return !!($intersectionStates[key] && $intersectionStates[key].isCached)
|
2018-01-30 17:38:14 +00:00
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
getHeight (key, $intersectionStates) {
|
2018-01-31 05:34:35 +00:00
|
|
|
return $intersectionStates[key] && $intersectionStates[key].height
|
2018-01-30 17:38:14 +00:00
|
|
|
}
|
|
|
|
},
|
2018-01-30 03:22:28 +00:00
|
|
|
methods: {
|
2018-04-20 04:38:01 +00:00
|
|
|
scrollToPosition (element) {
|
2018-04-19 16:37:05 +00:00
|
|
|
let { scrolledToPosition } = this.get()
|
|
|
|
if (scrolledToPosition) {
|
2018-01-30 17:38:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
this.set({scrolledToPosition: true})
|
2018-03-30 06:01:36 +00:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
console.log('scrolling element into view')
|
|
|
|
element.scrollIntoView(true)
|
|
|
|
})
|
2018-01-30 03:22:28 +00:00
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
onIntersection (entries) {
|
2018-01-30 17:38:14 +00:00
|
|
|
mark('onIntersection')
|
|
|
|
let newIntersectionStates = {}
|
2018-04-19 16:37:05 +00:00
|
|
|
let { scrollToItem } = this.get()
|
|
|
|
let { intersectionStates } = this.store.get()
|
2018-01-30 17:38:14 +00:00
|
|
|
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
|
2018-01-30 17:38:14 +00:00
|
|
|
}
|
|
|
|
if (scrollToItem === key) {
|
2018-03-30 06:01:36 +00:00
|
|
|
this.scrollToPosition(entry.target)
|
2018-01-30 17:38:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
intersectionStates = Object.assign(intersectionStates, newIntersectionStates)
|
2018-01-31 02:26:13 +00:00
|
|
|
this.store.setForRealm({intersectionStates: intersectionStates})
|
2018-01-30 17:38:14 +00:00
|
|
|
stop('onIntersection')
|
2018-01-30 03:22:28 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2018-04-13 02:38:41 +00:00
|
|
|
wrappedItems: (items) => items ? items.map(item => ({item})) : [],
|
2018-01-31 02:26:13 +00:00
|
|
|
allItemsHaveHeight: (items, $intersectionStates) => {
|
2018-02-09 02:54:48 +00:00
|
|
|
if (!items) {
|
2018-01-30 17:38:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
for (let item of items) {
|
2018-01-31 02:26:13 +00:00
|
|
|
if (!$intersectionStates[item]) {
|
2018-01-30 17:38:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2018-01-30 03:22:28 +00:00
|
|
|
},
|
|
|
|
components: {
|
|
|
|
PseudoVirtualListLazyItem
|
2018-01-31 02:26:13 +00:00
|
|
|
},
|
|
|
|
store: () => pseudoVirtualListStore
|
2018-01-30 03:22:28 +00:00
|
|
|
}
|
|
|
|
</script>
|