-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
71 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { getPaginationNumbers } from "./getPagination"; | ||
|
||
test("getPagination", () => { | ||
const totalNumberOfPages = 15; | ||
const currentPage = 1; | ||
|
||
expect(getPaginationNumbers(totalNumberOfPages, currentPage)).toStrictEqual([1, 2, 3, "...", 15]); | ||
|
||
expect(getPaginationNumbers(37, 4)).toStrictEqual([1, "...", 3, 4, 5, "...", 37]); | ||
|
||
expect(getPaginationNumbers(28, 7)).toStrictEqual([1, "...", 6, 7, 8, "...", 28]); | ||
|
||
expect(getPaginationNumbers(5, 1)).toStrictEqual([1, 2, 3, 4, 5]); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
export function getPaginationNumbers(totalNumberOfPages: number, currentPage: number) { | ||
const adjacentToCurrentPage = 1; // This is the number(s) we see next to the currentPage | ||
const totalNumbersShownInPagination = 6; | ||
let calculatedPagination: number[] = []; | ||
|
||
const pageNumbersAsArray = (startRange: number, endRange: number) => { | ||
return [...Array(endRange + 1).keys()].slice(startRange); | ||
}; | ||
|
||
const minimumAmountInBetweenToShowEllipsis = 2; | ||
// Without ellipsis aka normal case | ||
if (totalNumberOfPages <= totalNumbersShownInPagination) { | ||
return (calculatedPagination = pageNumbersAsArray(1, totalNumberOfPages)); | ||
} | ||
|
||
// Otherwise, we show the ellipses | ||
const toTheRightOfCurrent = Math.min(currentPage + adjacentToCurrentPage, totalNumberOfPages); | ||
const toTheLeftOfCurrent = Math.max(currentPage - adjacentToCurrentPage, 1); | ||
|
||
const showRightEllipsis = toTheRightOfCurrent < totalNumberOfPages - minimumAmountInBetweenToShowEllipsis; // e.g. "1 2 3 ... 7" | ||
const showLeftEllipsis = toTheLeftOfCurrent > minimumAmountInBetweenToShowEllipsis; // e.g. 1 ... 5 6 7" | ||
|
||
if (showRightEllipsis && !showLeftEllipsis) { | ||
let leftSideNumbers = 3; | ||
let leftPageNumbersAsArray = pageNumbersAsArray(1, leftSideNumbers); | ||
return [...leftPageNumbersAsArray, "...", totalNumberOfPages]; | ||
} | ||
|
||
if (showLeftEllipsis && !showRightEllipsis) { | ||
let rightSideNumbers = 3; | ||
let rightPageNumbersAsArray = pageNumbersAsArray(totalNumberOfPages - rightSideNumbers, totalNumberOfPages); | ||
return [1, "...", ...rightPageNumbersAsArray]; | ||
} | ||
|
||
if (showRightEllipsis && showLeftEllipsis) { | ||
let middleNumbers = pageNumbersAsArray(toTheLeftOfCurrent, toTheRightOfCurrent); | ||
return [1, "...", ...middleNumbers, "...", totalNumberOfPages]; | ||
} | ||
|
||
return calculatedPagination; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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