You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
portainer/app/react/components/PaginationControls/calculatePageNumber.ts

38 lines
793 B

/**
* Given the position in the sequence of pagination links, figure out what page number corresponds to that position.
*
* @param position
* @param currentPage
* @param paginationRange
* @param totalPages
*/
export function calculatePageNumber(
position: number,
currentPage: number,
paginationRange: number,
totalPages: number
) {
const halfWay = Math.ceil(paginationRange / 2);
if (position === paginationRange) {
return totalPages;
}
if (position === 1) {
return position;
}
if (paginationRange < totalPages) {
if (totalPages - halfWay < currentPage) {
return totalPages - paginationRange + position;
}
if (halfWay < currentPage) {
return currentPage - halfWay + position;
}
return position;
}
return position;
}