Pinafore-Web-Client-Frontend/src/routes/_components/virtualList/VirtualList.html

138 lines
4.4 KiB
HTML
Raw Normal View History

<VirtualListContainer {realm} on:initialized on:noNeedToScroll >
<div class="virtual-list"
style="height: {$height}px;"
ref:node >
<VirtualListHeader component={headerComponent} virtualProps={headerProps} shown={$showHeader}/>
{#if $visibleItems}
{#each $visibleItems as visibleItem (visibleItem.key)}
<VirtualListLazyItem {component}
offset={visibleItem.offset}
{makeProps}
key={visibleItem.key}
index={visibleItem.index}
/>
{/each}
{#if !$visibleItems.length}
<div class="nothing-to-show">
{intl.nothingToShow}
</div>
{/if}
{/if}
{#if $showFooter}
<VirtualListFooter component={footerComponent} />
{/if}
</div>
</VirtualListContainer>
2018-01-15 19:54:02 +01:00
<style>
.virtual-list {
position: relative;
width: 100%;
2018-01-15 19:54:02 +01:00
}
.nothing-to-show {
font-size: 1.1em;
width: 100%;
padding: 20px 0;
text-align: center;
}
2018-01-15 19:54:02 +01:00
</style>
<script>
import VirtualListContainer from './VirtualListContainer.html'
import VirtualListLazyItem from './VirtualListLazyItem'
2018-01-22 01:07:11 +01:00
import VirtualListFooter from './VirtualListFooter.html'
2018-02-12 04:15:21 +01:00
import VirtualListHeader from './VirtualListHeader.html'
2018-01-25 17:23:14 +01:00
import { virtualListStore } from './virtualListStore'
import throttle from 'lodash-es/throttle'
2018-01-25 17:23:14 +01:00
import { mark, stop } from '../../_utils/marks'
import isEqual from 'lodash-es/isEqual'
import { observe } from 'svelte-extras'
2018-01-15 19:54:02 +01:00
const DISTANCE_FROM_BOTTOM_TO_FIRE = 800
2018-02-12 04:15:21 +01:00
const SCROLL_EVENT_THROTTLE = 1000
2018-01-16 02:25:32 +01:00
2018-01-15 19:54:02 +01:00
export default {
2018-01-16 02:25:32 +01:00
oncreate () {
this.fireScrollToBottom = throttle(() => {
this.fire('scrollToBottom')
}, SCROLL_EVENT_THROTTLE)
this.fireScrollToTop = throttle(() => {
this.fire('scrollToTop')
}, SCROLL_EVENT_THROTTLE)
this.observe('showFooter', showFooter => {
mark('set showFooter')
this.store.setForRealm({ showFooter: showFooter })
mark('set showFooter')
2018-01-22 01:07:11 +01:00
})
2018-02-12 04:15:21 +01:00
this.observe('showHeader', showHeader => {
mark('set showHeader')
this.store.setForRealm({ showHeader: showHeader })
stop('set showHeader')
2018-02-12 04:15:21 +01:00
})
2018-02-25 19:50:04 +01:00
this.observe('items', (newItems, oldItems) => {
if (!newItems || isEqual(newItems, oldItems)) {
return
}
2018-01-17 09:59:15 +01:00
mark('set items')
this.store.setForRealm({ items: newItems })
2018-01-17 09:59:15 +01:00
stop('set items')
2018-01-15 19:54:02 +01:00
})
// We observe on the component rather than the store to avoid a leak in store listeners
// (Svelte automatically removes component listeners, but not store listeners)
this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => {
feat: Add support for keyboard shortcuts (#870) * Add support for keyboard shortcuts. This change introduces a Shortcut component for defining global keyboard shortcuts from whichever component makes more sense. This change also adds an initial set of navigation shortcuts: - Backspace to leave a modal dialog or to go back - g t to go to the federated timeline - g f to go to the favorite page - g h to go to the home page - g n to go to the notification page - g c to go to the community page - s to go to the search page These shortcuts are loaded asynchronously from _layout.html In modal dialogs, shortcuts are also modal, to avoid strange or overly complex behavior. This is implemented by grouping shortcuts into scopes, and activating a separate 'modal' scope when entering a modal dialog, so a separate set of shortcuts can be enabled in modal dialog. Modal dialogs can be exited by pressing 'Backspace'. * Navigate up/down lists using keyboard shortcuts. This change introduces keyboard shortcuts for navigating in lists and virtual lists. j or arrow up selects the next element, k or arrow down, the previous element. Selecting an element scrolls the list up and down, as necessary. This change also allows directing keyboard shortcuts to the active element and defines the following shortcuts, for the active status: - f to favorite or unfavorite it - b to boost or unboost it - r to reply to it - o to open its thread - x to toggle the display of a CW - y to toggle the display of sensitive medias This works by defining a keyboard shortcut scope for each list element. A new component, ScrollListShortcuts, keeps track of the active element, based on list or virtual list elements and redirects shortcuts to the active element's scope. ScrollListShortcuts keeps the active element in the current realm of the store, so the active element is restored when going back to the list. * Typing h or ? displays the list of available keyboard shortcuts. This change introduces a new modal dialog that documents the list of available shortcuts.
2019-01-13 19:03:29 +01:00
this.calculateListOffset()
if (allVisibleItemsHaveHeight) {
this.fire('initializedVisibleItems')
}
})
this.observe('distanceFromBottom', (distanceFromBottom) => {
2018-01-17 09:06:24 +01:00
if (distanceFromBottom >= 0 &&
2018-01-17 06:43:31 +01:00
distanceFromBottom <= DISTANCE_FROM_BOTTOM_TO_FIRE) {
this.fireScrollToBottom()
2018-01-17 06:43:31 +01:00
}
}, { init: false })
2018-02-12 04:15:21 +01:00
this.observe('scrollTop', (scrollTop) => {
2018-02-13 18:15:10 +01:00
this.fire('scrollTopChanged', scrollTop)
if (scrollTop === 0) {
2018-02-12 04:15:21 +01:00
this.fireScrollToTop()
}
this.calculateListOffset()
2018-02-12 04:15:21 +01:00
})
2018-01-16 03:29:28 +01:00
},
2018-01-15 19:54:02 +01:00
data: () => ({
component: null
2018-01-15 19:54:02 +01:00
}),
store: () => virtualListStore,
2018-01-15 19:54:02 +01:00
components: {
VirtualListContainer,
VirtualListLazyItem,
2018-02-12 04:15:21 +01:00
VirtualListFooter,
VirtualListHeader
2018-01-17 08:16:15 +01:00
},
computed: {
distanceFromBottom: ({ $scrollHeight, $scrollTop, $offsetHeight }) => {
2018-01-17 08:16:15 +01:00
return $scrollHeight - $scrollTop - $offsetHeight
},
scrollTop: ({ $scrollTop }) => $scrollTop,
allVisibleItemsHaveHeight: ({ $allVisibleItemsHaveHeight }) => $allVisibleItemsHaveHeight,
visibleItemKeys: ({ $visibleItems }) => ($visibleItems || []).map(_ => _.key)
},
methods: {
observe,
2018-04-20 06:38:01 +02:00
calculateListOffset () {
// TODO: better way to get the offset top?
2019-08-03 22:49:37 +02:00
const node = this.refs.node
if (!node) {
return
}
mark('calculateListOffset')
2019-08-03 22:49:37 +02:00
const { offsetParent } = node
// TODO: offsetParent is null sometimes in testcafe tests
2019-08-03 22:49:37 +02:00
const listOffset = offsetParent ? offsetParent.offsetTop : 0
this.store.setForRealm({ listOffset })
stop('calculateListOffset')
}
2018-01-15 19:54:02 +01:00
}
}
feat: Add support for keyboard shortcuts (#870) * Add support for keyboard shortcuts. This change introduces a Shortcut component for defining global keyboard shortcuts from whichever component makes more sense. This change also adds an initial set of navigation shortcuts: - Backspace to leave a modal dialog or to go back - g t to go to the federated timeline - g f to go to the favorite page - g h to go to the home page - g n to go to the notification page - g c to go to the community page - s to go to the search page These shortcuts are loaded asynchronously from _layout.html In modal dialogs, shortcuts are also modal, to avoid strange or overly complex behavior. This is implemented by grouping shortcuts into scopes, and activating a separate 'modal' scope when entering a modal dialog, so a separate set of shortcuts can be enabled in modal dialog. Modal dialogs can be exited by pressing 'Backspace'. * Navigate up/down lists using keyboard shortcuts. This change introduces keyboard shortcuts for navigating in lists and virtual lists. j or arrow up selects the next element, k or arrow down, the previous element. Selecting an element scrolls the list up and down, as necessary. This change also allows directing keyboard shortcuts to the active element and defines the following shortcuts, for the active status: - f to favorite or unfavorite it - b to boost or unboost it - r to reply to it - o to open its thread - x to toggle the display of a CW - y to toggle the display of sensitive medias This works by defining a keyboard shortcut scope for each list element. A new component, ScrollListShortcuts, keeps track of the active element, based on list or virtual list elements and redirects shortcuts to the active element's scope. ScrollListShortcuts keeps the active element in the current realm of the store, so the active element is restored when going back to the list. * Typing h or ? displays the list of available keyboard shortcuts. This change introduces a new modal dialog that documents the list of available shortcuts.
2019-01-13 19:03:29 +01:00
</script>