2018-02-13 07:06:05 +01:00
|
|
|
|
<slot></slot>
|
2018-01-23 06:30:14 +01:00
|
|
|
|
<script>
|
2018-01-25 17:23:14 +01:00
|
|
|
|
import { virtualListStore } from './virtualListStore'
|
2018-04-06 02:57:36 +02:00
|
|
|
|
import throttle from 'lodash-es/throttle'
|
2018-01-25 17:23:14 +01:00
|
|
|
|
import { isFullscreen, attachFullscreenListener, detachFullscreenListener } from '../../_utils/fullscreen'
|
|
|
|
|
import { mark, stop } from '../../_utils/marks'
|
2018-03-30 08:16:53 +02:00
|
|
|
|
import { doubleRAF } from '../../_utils/doubleRAF'
|
2018-04-30 17:29:04 +02:00
|
|
|
|
import { observe } from 'svelte-extras'
|
2018-01-23 06:30:14 +01:00
|
|
|
|
|
|
|
|
|
const SCROLL_EVENT_DELAY = 300
|
|
|
|
|
|
|
|
|
|
export default {
|
2018-04-20 06:38:01 +02:00
|
|
|
|
oncreate () {
|
2018-01-23 06:30:14 +01:00
|
|
|
|
mark('onCreate VirtualListContainer')
|
2018-04-19 18:37:05 +02:00
|
|
|
|
let {
|
|
|
|
|
realm,
|
|
|
|
|
containerQuery
|
|
|
|
|
} = this.get()
|
|
|
|
|
this.store.setCurrentRealm(realm)
|
|
|
|
|
let node = document.querySelector(containerQuery)
|
2018-02-13 07:06:05 +01:00
|
|
|
|
this.setupScroll(node)
|
|
|
|
|
this.setupFullscreen()
|
2018-04-19 18:37:05 +02:00
|
|
|
|
let { scrollTop } = this.store.get()
|
2018-01-27 17:13:28 +01:00
|
|
|
|
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-04-19 18:37:05 +02:00
|
|
|
|
let { initializedScrollTop } = this.get()
|
|
|
|
|
if (!initializedScrollTop && allVisibleItemsHaveHeight && node) {
|
2018-08-30 06:42:57 +02:00
|
|
|
|
this.set({ 'initializedScrollTop': true })
|
2018-03-21 04:28:53 +01:00
|
|
|
|
mark('set scrollTop')
|
|
|
|
|
console.log('forcing scroll top to ', scrollTop)
|
|
|
|
|
node.scrollTop = scrollTop
|
|
|
|
|
stop('set scrollTop')
|
2018-03-30 08:16:53 +02:00
|
|
|
|
doubleRAF(() => {
|
|
|
|
|
console.log('initialized VirtualList')
|
|
|
|
|
this.fire('initialized')
|
|
|
|
|
})
|
2018-01-25 03:58:10 +01:00
|
|
|
|
}
|
|
|
|
|
})
|
2018-01-24 18:47:31 +01:00
|
|
|
|
} else {
|
2018-03-30 08:16:53 +02:00
|
|
|
|
this.fire('noNeedToScroll')
|
|
|
|
|
this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => {
|
|
|
|
|
if (allVisibleItemsHaveHeight) {
|
|
|
|
|
console.log('initialized VirtualList')
|
|
|
|
|
this.fire('initialized')
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-03-21 04:28:53 +01:00
|
|
|
|
this.store.setForRealm({
|
|
|
|
|
scrollHeight: node.scrollHeight,
|
|
|
|
|
offsetHeight: node.offsetHeight
|
2018-01-24 18:47:31 +01:00
|
|
|
|
})
|
|
|
|
|
}
|
2018-01-23 06:30:14 +01:00
|
|
|
|
stop('onCreate VirtualListContainer')
|
|
|
|
|
},
|
2018-04-20 06:38:01 +02:00
|
|
|
|
ondestroy () {
|
2018-02-13 07:06:05 +01:00
|
|
|
|
this.teardownScroll()
|
|
|
|
|
this.teardownFullscreen()
|
2018-02-13 07:47:25 +01:00
|
|
|
|
this.store.setCurrentRealm(null)
|
2018-02-13 07:06:05 +01:00
|
|
|
|
},
|
2018-01-23 06:30:14 +01:00
|
|
|
|
store: () => virtualListStore,
|
2018-02-13 07:06:05 +01:00
|
|
|
|
methods: {
|
2018-04-30 17:29:04 +02:00
|
|
|
|
observe,
|
2018-04-20 06:38:01 +02:00
|
|
|
|
setupScroll (node) {
|
2018-02-13 07:06:05 +01:00
|
|
|
|
if (!node) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this.scrollListener = throttle(event => {
|
2018-04-19 18:37:05 +02:00
|
|
|
|
let { fullscreen } = this.get()
|
|
|
|
|
if (fullscreen) {
|
2018-01-23 06:30:14 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
2018-02-13 07:06:05 +01:00
|
|
|
|
this.onScroll(event)
|
2018-01-23 06:30:14 +01:00
|
|
|
|
}, SCROLL_EVENT_DELAY, {
|
|
|
|
|
leading: true,
|
|
|
|
|
trailing: true
|
|
|
|
|
})
|
2018-02-13 07:06:05 +01:00
|
|
|
|
node.addEventListener('scroll', this.scrollListener)
|
2018-01-23 06:30:14 +01:00
|
|
|
|
},
|
2018-04-20 06:38:01 +02:00
|
|
|
|
teardownScroll () {
|
2018-04-19 18:37:05 +02:00
|
|
|
|
let { containerQuery } = this.get()
|
|
|
|
|
let node = document.querySelector(containerQuery)
|
2018-02-13 07:06:05 +01:00
|
|
|
|
if (node) {
|
|
|
|
|
node.removeEventListener('scroll', this.scrollListener)
|
2018-01-23 06:30:14 +01:00
|
|
|
|
}
|
2018-02-13 07:06:05 +01:00
|
|
|
|
},
|
2018-04-20 06:38:01 +02:00
|
|
|
|
setupFullscreen () {
|
2018-02-13 07:06:05 +01:00
|
|
|
|
this.onFullscreenChange = this.onFullscreenChange.bind(this)
|
|
|
|
|
attachFullscreenListener(this.onFullscreenChange)
|
|
|
|
|
},
|
2018-04-20 06:38:01 +02:00
|
|
|
|
teardownFullscreen () {
|
2018-02-13 07:06:05 +01:00
|
|
|
|
detachFullscreenListener(this.onFullscreenChange)
|
|
|
|
|
},
|
2018-04-20 06:38:01 +02:00
|
|
|
|
onScroll (event) {
|
2018-03-15 02:24:16 +01:00
|
|
|
|
let { scrollTop, scrollHeight } = event.target
|
2018-03-23 06:53:50 +01:00
|
|
|
|
|
2018-08-21 02:57:38 +02:00
|
|
|
|
doubleRAF(() => {
|
2018-03-15 02:24:16 +01:00
|
|
|
|
mark('onScroll -> setForRealm()')
|
2018-08-30 06:42:57 +02:00
|
|
|
|
this.store.setForRealm({ scrollTop, scrollHeight })
|
2018-03-15 02:24:16 +01:00
|
|
|
|
stop('onScroll -> setForRealm()')
|
2018-01-23 06:30:14 +01:00
|
|
|
|
})
|
|
|
|
|
},
|
2018-04-20 06:38:01 +02:00
|
|
|
|
onFullscreenChange () {
|
2018-01-23 06:30:14 +01:00
|
|
|
|
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
|
2018-05-02 02:05:36 +02:00
|
|
|
|
allVisibleItemsHaveHeight: ({ $allVisibleItemsHaveHeight }) => $allVisibleItemsHaveHeight
|
2018-01-23 06:30:14 +01:00
|
|
|
|
}
|
2018-04-20 06:38:01 +02:00
|
|
|
|
}
|
2018-01-23 06:30:14 +01:00
|
|
|
|
</script>
|