semaphore/routes/_components/Layout.html

81 lines
2.0 KiB
HTML

<:Window bind:innerHeight bind:online />
<Nav page={{page}} />
<div class="container" on:scroll="onScroll(event)" ref:node>
<main>
<slot></slot>
</main>
</div>
<script>
import Nav from './Nav.html';
import { virtualListStore } from '../_utils/virtualListStore'
import debounce from 'lodash/debounce'
import throttle from 'lodash/throttle'
import { toast } from '../_utils/toast'
import { mark, stop } from '../_utils/marks'
const SCROLL_EVENT_DELAY = 300
const RESIZE_EVENT_DELAY = 700
const OFFLINE_DELAY = 1000
const notifyOffline = debounce(() => {
toast.say('You appear to be offline. You can still read toots while offline.')
}, OFFLINE_DELAY)
export default {
oncreate() {
mark('onCreate Layout')
let node = this.refs.node
this.observe('online', online => {
document.body.classList.toggle('offline', !online)
if (!online) {
notifyOffline()
}
})
this.observe('innerHeight', debounce(() => {
// respond to window resize events
this.store.set({
offsetHeight: node.offsetHeight
})
}, RESIZE_EVENT_DELAY))
this.store.set({
scrollTop: node.scrollTop,
scrollHeight: node.scrollHeight,
offsetHeight: node.offsetHeight
})
stop('onCreate Layout')
},
components: {
Nav
},
store: () => virtualListStore,
events: {
scroll(node, callback) {
const onScroll = throttle(event => {
mark('onScroll')
callback(event)
stop('onScroll')
}, SCROLL_EVENT_DELAY, {
leading: true,
trailing: true
})
node.addEventListener('scroll', onScroll);
return {
teardown() {
node.removeEventListener('scroll', onScroll);
}
};
}
},
methods: {
onScroll(event) {
this.store.set({
scrollTop: event.target.scrollTop,
scrollHeight: event.target.scrollHeight
})
}
}
};
</script>