Created
June 4, 2025 21:04
-
-
Save robnagler/ecd48b7de00e50572e71b1c3fe079054 to your computer and use it in GitHub Desktop.
XFEL DAMNIT-web Pagination widget rewrite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/European-XFEL/DAMNIT-web/blob/fd5658ab64654a27830b5868cb0d743607ae230d/frontend/src/features/table/use-pagination.ts | |
export function usePagination(proposal, pageSize = 10) { | |
const store = useAppStore() | |
const pages = new Pages(store) | |
const visibleRegion = reactive({ | |
x: 0, | |
y: 0, | |
width: 0, | |
height: 0 | |
}) | |
const loadPages = () => { | |
if (visibleRegion.width === 0 || visibleRegion.height === 0) { | |
return; | |
} | |
const pageSize = toValue(pageSize); | |
const firstPage = Math.max(0, Math.floor((visibleRegion.y - pageSize / 2) / pageSize)) | |
const lastPage = Math.floor((visibleRegion.y + visibleRegion.height + pageSize / 2) / pageSize) | |
range(firstPage + 1, lastPage + 2).forEach((page) => { | |
if (! pages.value.isLoaded(page)) { | |
pages.loadPage(page, toValue(proposal), pageSize) | |
} | |
}) | |
}); | |
watchEffect(loadPages); | |
return { | |
onVisibleRegionChanged: (rect) => { | |
visibleRegion.x = rect.x | |
visibleRegion.y = rect.y | |
visibleRegion.width = rect.width | |
visibleRegion.height = rect.height | |
}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment