Pinafore-Web-Client-Frontend/routes/_components/virtualList/VirtualListContainer.html

120 lines
3.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="container"
on:scroll="onScroll(event)"
on:fullscreen="onFullscreenChange()"
ref:node>
<slot></slot>
</div>
<script>
import { virtualListStore } from './virtualListStore'
import throttle from 'lodash/throttle'
import { isFullscreen, attachFullscreenListener, detachFullscreenListener } from '../../_utils/fullscreen'
import { mark, stop } from '../../_utils/marks'
const SCROLL_EVENT_DELAY = 300
export default {
oncreate() {
mark('onCreate VirtualListContainer')
let node = this.refs.node
let realm = this.get('realm')
this.store.set({currentRealm: realm})
this.observe('scrollToItem', scrollToItem => {
if (scrollToItem && node && !this.store.get('containerTop')) {
// have to calculate difference between container and virtual list tops in order to scroll
this.store.set({containerTop: node.getBoundingClientRect().top})
}
})
let scrollTop = this.store.get('scrollTop')
if (scrollTop > 0) {
this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => {
console.log('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight)
if (!this.get('initializedScrollTop') && allVisibleItemsHaveHeight && node) {
this.set({'initializedScrollTop': true})
requestAnimationFrame(() => {
mark('set scrollTop')
console.log('forcing scrollTop to ', scrollTop)
node.scrollTop = scrollTop
stop('set scrollTop')
})
}
})
} else {
this.store.setForRealm({
scrollHeight: node.scrollHeight,
offsetHeight: node.offsetHeight
})
this.observe('scrollToItemOffset', scrollToItemOffset => {
console.log('scrollToItemOffset', scrollToItemOffset)
if (!this.get('initializedScrollTop') && scrollToItemOffset && node) {
this.set({'initializedScrollTop': true})
requestAnimationFrame(() => {
mark('set scrollToItemOffset')
console.log('forcing scrollItemOffset to ', scrollToItemOffset)
node.scrollTop = scrollToItemOffset
stop('set scrollToItemOffset')
})
}
})
}
stop('onCreate VirtualListContainer')
},
store: () => virtualListStore,
events: {
scroll(node, callback) {
const onScroll = throttle(event => {
mark('onScroll')
if (this.get('fullscreen')) {
return
}
callback(event)
stop('onScroll')
}, SCROLL_EVENT_DELAY, {
leading: true,
trailing: true
})
node.addEventListener('scroll', onScroll)
return {
teardown() {
node.removeEventListener('scroll', onScroll)
}
}
},
fullscreen(node, callback) {
const onFullscreen = (() => {
callback()
})
attachFullscreenListener(onFullscreen)
return {
teardown() {
detachFullscreenListener(onFullscreen)
}
}
}
},
methods: {
onScroll(event) {
console.log('onScroll', event.target.scrollTop)
this.store.setForRealm({
scrollTop: event.target.scrollTop,
scrollHeight: event.target.scrollHeight
})
},
onFullscreenChange() {
mark('onFullscreenChange')
console.log('is fullscreen? ', isFullscreen())
this.set({ fullscreen: isFullscreen() })
stop('onFullscreenChange')
}
},
computed: {
// TODO: bug in svelte/store the observer in oncreate() never get removed without this hack
allVisibleItemsHaveHeight: ($allVisibleItemsHaveHeight) => $allVisibleItemsHaveHeight,
scrollToItemOffset: ($scrollToItemOffset) => $scrollToItemOffset,
scrollToItem: ($scrollToItem) => $scrollToItem
}
};
</script>