JSFiddle - React, Tailwind, and code Playground
by lbstr
JavaScript
var TableManager = function(){};
TableManager.prototype.buildPageSets = function(newPage, firstPage, lastPage, numConsecutive) {
var firstSet = this.buildFirstPageSet(newPage, firstPage, lastPage, numConsecutive),
midSet = this.buildMiddlePageSet(newPage, firstPage, lastPage, numConsecutive),
lastSet = this.buildLastPageSet(newPage, firstPage, lastPage, numConsecutive),
html = [
firstSet,
midSet,
lastSet,
];
// add ellipses as needed
return html;
};
// ONE 2 3 ... 22
// 1 TWO 3 ... 22
// 1 2 THR 4 ... 22
// 1 ... 3 FOU 5 ... 22
TableManager.prototype.buildFirstPageSet = function(newPage, firstPage, lastPage, numConsecutive) {
var set = [ firstPage ],
maxForFullSet = firstPage - 1 + numConsecutive, // using example above, this would be 3
push = function(num) {
if (num >= firstPage && num <= lastPage) {
set.push(num);
}
},
page;
// Using the example above, if newPage is 4 or higher,
// this set is just a 1. Otherwise, we add 2 and 3.
// If newPage is 3, we add 4.
if (newPage <= maxForFullSet) {
page = firstPage + 1;
while (page <= maxForFullSet) {
push(page);
page++;
}
if (newPage === maxForFullSet) {
push(maxForFullSet + 1);
}
}
return set;
};
// 1 ... 3 FOU 5 ... 22
// 1 ... 10 ELE 12 ... 22
// 1 ... 18 NIN 20 ... 22
TableManager.prototype.buildMiddlePageSet = function(newPage, firstPage, lastPage, numConsecutive) {
var set = [],
min = firstPage + numConsecutive,
max = lastPage - numConsecutive,
push = function(num) {
if (num >= firstPage && num <= lastPage) {
set.push(num);
}
},
low, high, page;
if (newPage >= min && newPage <= max) {
// the following logic is so that if 4 is...