pinafore/routes/_components/virtualList/VirtualListContainer.html

93 lines
3.0 KiB
HTML
Raw Normal View History

<slot></slot>
2018-01-23 06:30:14 +01:00
<script>
2018-01-25 17:23:14 +01:00
import { virtualListStore } from './virtualListStore'
2018-01-23 06:30:14 +01:00
import throttle from 'lodash/throttle'
2018-01-25 17:23:14 +01:00
import { isFullscreen, attachFullscreenListener, detachFullscreenListener } from '../../_utils/fullscreen'
import { mark, stop } from '../../_utils/marks'
2018-01-23 06:30:14 +01:00
const SCROLL_EVENT_DELAY = 300
export default {
oncreate() {
mark('onCreate VirtualListContainer')
let node = document.querySelector('.container')
this.setupScroll(node)
this.setupFullscreen()
2018-01-31 06:55:23 +01:00
this.store.setCurrentRealm(this.get('realm'))
2018-01-27 17:13:28 +01:00
let scrollTop = this.store.get('scrollTop')
if (scrollTop > 0) {
2018-01-25 03:58:10 +01:00
this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => {
2018-01-27 17:22:23 +01:00
console.log('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight)
2018-01-27 17:13:28 +01:00
if (!this.get('initializedScrollTop') && allVisibleItemsHaveHeight && node) {
this.set({'initializedScrollTop': true})
2018-01-25 03:58:10 +01:00
requestAnimationFrame(() => {
mark('set scrollTop')
2018-01-27 17:22:23 +01:00
console.log('forcing scroll top to ', scrollTop)
2018-01-27 17:13:28 +01:00
node.scrollTop = scrollTop
2018-01-25 03:58:10 +01:00
stop('set scrollTop')
})
}
})
2018-01-24 18:47:31 +01:00
} else {
2018-01-27 17:13:28 +01:00
this.store.setForRealm({
2018-01-24 18:47:31 +01:00
scrollHeight: node.scrollHeight,
offsetHeight: node.offsetHeight
})
}
2018-01-23 06:30:14 +01:00
stop('onCreate VirtualListContainer')
},
ondestroy() {
this.teardownScroll()
this.teardownFullscreen()
},
2018-01-23 06:30:14 +01:00
store: () => virtualListStore,
methods: {
setupScroll(node) {
if (!node) {
return
}
this.scrollListener = throttle(event => {
2018-01-23 06:30:14 +01:00
if (this.get('fullscreen')) {
return
}
this.onScroll(event)
2018-01-23 06:30:14 +01:00
}, SCROLL_EVENT_DELAY, {
leading: true,
trailing: true
})
node.addEventListener('scroll', this.scrollListener)
2018-01-23 06:30:14 +01:00
},
teardownScroll() {
let node = document.querySelector('.container')
if (node) {
node.removeEventListener('scroll', this.scrollListener)
2018-01-23 06:30:14 +01:00
}
},
setupFullscreen() {
this.onFullscreenChange = this.onFullscreenChange.bind(this)
attachFullscreenListener(this.onFullscreenChange)
},
teardownFullscreen() {
detachFullscreenListener(this.onFullscreenChange)
},
2018-01-23 06:30:14 +01:00
onScroll(event) {
mark('onScroll')
2018-01-27 17:13:28 +01:00
this.store.setForRealm({
2018-01-23 06:30:14 +01:00
scrollTop: event.target.scrollTop,
scrollHeight: event.target.scrollHeight
})
stop('onScroll')
2018-01-23 06:30:14 +01:00
},
onFullscreenChange() {
mark('onFullscreenChange')
2018-01-27 17:22:23 +01:00
console.log('is fullscreen? ', isFullscreen())
2018-01-23 06:30:14 +01:00
this.set({ fullscreen: isFullscreen() })
stop('onFullscreenChange')
}
2018-01-25 03:58:10 +01:00
},
computed: {
// TODO: bug in svelte/store the observer in oncreate() never get removed without this hack
allVisibleItemsHaveHeight: ($allVisibleItemsHaveHeight) => $allVisibleItemsHaveHeight
2018-01-23 06:30:14 +01:00
}
};
</script>