Skip to content

Instantly share code, notes, and snippets.

@brookjordan
Last active September 11, 2018 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brookjordan/7304c29d0e056f86e2fcc30bb70da5d0 to your computer and use it in GitHub Desktop.
Save brookjordan/7304c29d0e056f86e2fcc30bb70da5d0 to your computer and use it in GitHub Desktop.
Pagination buttons
function calculateArrangement({ pageCount = 9, currentPageNumber = 1, buttonCount = 7 } = {}) {
buttonCount = +buttonCount;
if (!buttonCount || buttonCount < 7) { throw Error('Must have 7 or more') }
if (currentPageNumber > pageCount || currentPageNumber < 1) { throw Error('current page must be between 1 and page count') }
let requiredEndsButtonCount = 1;
let requiredCenterButtonCount = 3;
let requiredSpareCountForGoto = 1;
let requiredButtonCount =
requiredEndsButtonCount +
requiredSpareCountForGoto +
requiredCenterButtonCount +
requiredSpareCountForGoto +
requiredEndsButtonCount;
// These are addictional buttons not counting the 7 required ones
let extraButtonCount = buttonCount - requiredButtonCount;
// For a possibly more pleasing arrangement I add an extra value here.
// • addin the +1 creates: [ 1 2 … 5 6 7 8 … 11 12 ]
// • removing it creates: [ 1 … 4 5 6 7 8 9 … 12 ]
let extraEndsNavCount = Math.floor((extraButtonCount + 1) / 4);
let extraCenterNavCount = extraButtonCount - (extraEndsNavCount * 2);
let endsNavButtonCount = extraEndsNavCount + requiredEndsButtonCount;
let centerNavButtonCount = extraCenterNavCount + requiredCenterButtonCount;
let showStartGotoButton = false;
let showEndGotoButton = false;
let incrementFirstCenterNumber = 0;
if (pageCount > buttonCount + 1) {
if (currentPageNumber > (endsNavButtonCount + 2 + 1)) {
showStartGotoButton = true;
}
if (currentPageNumber < pageCount - (endsNavButtonCount + 2)) {
showEndGotoButton = true;
}
}
// This part is shitty…
else if (pageCount === buttonCount + 1) {
if (currentPageNumber > pageCount / 2) {
showStartGotoButton = true;
// compensate for having one less goto button
incrementFirstCenterNumber = 1;
}
if (currentPageNumber < (pageCount + 1) / 2) {
showEndGotoButton = true;
}
}
let startNums = [];
let endNums = [];
let firstCenterNumber = 1;
if (showStartGotoButton) {
startNums = range(endsNavButtonCount);
} else {
centerNavButtonCount += endsNavButtonCount + 1;
}
if (showEndGotoButton) {
endNums = range(endsNavButtonCount, { from: pageCount - endsNavButtonCount + 1 });
} else {
centerNavButtonCount += endsNavButtonCount + 1;
}
centerNavButtonCount = Math.min(pageCount, centerNavButtonCount);
if (showStartGotoButton) {
firstCenterNumber = currentPageNumber - Math.floor((centerNavButtonCount - 1) / 2);
// compensate for removing the end goto button
if (!showEndGotoButton) {
incrementFirstCenterNumber += 1;
}
}
firstCenterNumber += incrementFirstCenterNumber;
firstCenterNumber = Math.min(firstCenterNumber, pageCount - centerNavButtonCount + 1);
let centerNumbers = range(centerNavButtonCount, { from: firstCenterNumber });
return {
startNums,
centerNumbers,
endNums,
}
function range(count, { from = 1 } = {}) {
return new Array(count).fill(null).map((_, i) => i + from);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment