pinafore/routes/_components/virtualList/VirtualListContainer.html

103 lines
3.1 KiB
HTML
Raw Normal View History

<div class="container"
on:scroll="onScroll(event)"
on:fullscreen="onFullscreenChange()"
ref:node>
2018-01-23 05:30:14 +00:00
<slot></slot>
</div>
<script>
2018-01-25 16:23:14 +00:00
import { virtualListStore } from './virtualListStore'
2018-01-23 05:30:14 +00:00
import throttle from 'lodash/throttle'
2018-01-25 16:23:14 +00:00
import { isFullscreen, attachFullscreenListener, detachFullscreenListener } from '../../_utils/fullscreen'
import { mark, stop } from '../../_utils/marks'
2018-01-23 05:30:14 +00:00
const SCROLL_EVENT_DELAY = 300
export default {
oncreate() {
mark('onCreate VirtualListContainer')
let node = this.refs.node
2018-01-27 16:13:28 +00:00
let realm = this.get('realm')
this.store.set({currentRealm: realm})
let scrollTop = this.store.get('scrollTop')
if (scrollTop > 0) {
2018-01-25 02:58:10 +00:00
this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => {
if (process.env.NODE_ENV !== 'production') {
console.log('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight)
}
2018-01-27 16:13:28 +00:00
if (!this.get('initializedScrollTop') && allVisibleItemsHaveHeight && node) {
this.set({'initializedScrollTop': true})
2018-01-25 02:58:10 +00:00
requestAnimationFrame(() => {
mark('set scrollTop')
if (process.env.NODE_ENV !== 'production') {
2018-01-27 16:13:28 +00:00
console.log('forcing scroll top to ', scrollTop)
2018-01-25 02:58:10 +00:00
}
2018-01-27 16:13:28 +00:00
node.scrollTop = scrollTop
2018-01-25 02:58:10 +00:00
stop('set scrollTop')
})
}
})
2018-01-24 17:47:31 +00:00
} else {
2018-01-27 16:13:28 +00:00
this.store.setForRealm({
2018-01-24 17:47:31 +00:00
scrollHeight: node.scrollHeight,
offsetHeight: node.offsetHeight
})
}
2018-01-23 05:30:14 +00:00
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) {
2018-01-27 16:13:28 +00:00
this.store.setForRealm({
2018-01-23 05:30:14 +00:00
scrollTop: event.target.scrollTop,
scrollHeight: event.target.scrollHeight
})
},
onFullscreenChange() {
mark('onFullscreenChange')
2018-01-25 02:04:25 +00:00
if (process.env.NODE_ENV !== 'production') {
console.log('is fullscreen? ', isFullscreen())
}
2018-01-23 05:30:14 +00:00
this.set({ fullscreen: isFullscreen() })
stop('onFullscreenChange')
}
2018-01-25 02:58:10 +00:00
},
computed: {
// TODO: bug in svelte/store the observer in oncreate() never get removed without this hack
allVisibleItemsHaveHeight: ($allVisibleItemsHaveHeight) => $allVisibleItemsHaveHeight
2018-01-23 05:30:14 +00:00
}
};
</script>