Last active
October 13, 2021 07:49
-
-
Save dhutaryan/7118c04b7b47c8c6d88129ae4444265e to your computer and use it in GitHub Desktop.
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
const MAX_PAGES_BLOCK = 3; | |
const SIDE_PAGE_COUNT = (MAX_PAGES_BLOCK - 1) / 2; | |
getPages() { | |
if (this.totalPages <= MAX_PAGES_BLOCK + 1) { | |
return this.range(1, this.totalPages); | |
} | |
const leftSidePages = this.getLeftSidePages(); | |
const rightSidePages = this.getRightSidePages(); | |
console.log(leftSidePages, rightSidePages); | |
return leftSidePages.concat([this.currentPage]).concat(rightSidePages); | |
} | |
getLeftSidePages(): number[] { | |
const leftShiftedPage = this.currentPage - MAX_PAGES_BLOCK; | |
if (leftShiftedPage < 0) { | |
return this.range(1, this.currentPage - 1); | |
} | |
if (leftShiftedPage >= 0) { | |
const correction = | |
this.currentPage === this.totalPages ? SIDE_PAGE_COUNT : 0; | |
return [1] | |
.concat(leftShiftedPage === 0 ? [] : [0]) | |
.concat( | |
this.range( | |
this.currentPage - SIDE_PAGE_COUNT - correction, | |
this.currentPage - 1, | |
), | |
); | |
} | |
} | |
getRightSidePages(): number[] { | |
const rightShiftedPage = this.currentPage + MAX_PAGES_BLOCK; | |
if (rightShiftedPage > this.totalPages) { | |
return this.range(this.currentPage + 1, this.totalPages); | |
} | |
if (rightShiftedPage <= this.totalPages) { | |
const correction = this.currentPage === 1 ? SIDE_PAGE_COUNT : 0; | |
return this.range( | |
this.currentPage + 1, | |
this.currentPage + SIDE_PAGE_COUNT + correction, | |
) | |
.concat(rightShiftedPage > this.totalPages ? [] : [0]) | |
.concat([this.totalPages]); | |
} | |
} | |
range(start: number, end: number): number[] { | |
return Array.from(Array(end - start + 1), (_, index) => index + start); | |
} | |
getTotalPages(): number { | |
return Math.ceil(this.length / this.pageSize); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment