Created
July 29, 2020 14:10
-
-
Save evaisse/a2ab7618b935db4169683385e5950c29 to your computer and use it in GitHub Desktop.
A simple pagination class
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
/** | |
var pagination = new Pagination(itemPerPage, totalItemCount, currentPageNumber); | |
<ul class="pagination" ng-if="pagination"> | |
<li ng-repeat="link in pagination.getLinks()" | |
ng-class="{active: link.active, disabled: link.disabled}"> | |
<a ng-if="link.type === 'shortcut'" aria-label="{{link.label}}" ng-click="updateSearchParams({ page: link.page })"> | |
<span aria-hidden="true">{{link.label}}</span> | |
</a> | |
<a ng-if="link.type === 'pageIndex'" aria-label="{{link.label}}" ng-click="updateSearchParams({ page: link.page })"> | |
<span aria-hidden="true">{{link.label}}</span> | |
</a> | |
<a ng-if="link.type === 'separator'"> | |
<span aria-hidden="true">...</span> | |
</a> | |
</li> | |
</ul> | |
*/ | |
var Pagination = function(perPage, itemsCount, currentPage, options) { | |
this.perPage = perPage; | |
this.itemsCount = itemsCount; | |
this.page = currentPage || 1; | |
this.options = Object.assign({ | |
// range: | |
hideSinglePage: false, | |
}, options); | |
this.compute(); | |
}; | |
Pagination.prototype.setPage = function(currentPage) { | |
this.page = currentPage; | |
this.compute(); | |
}; | |
Pagination.prototype.setItemsCount = function(itemsCount) { | |
this.itemsCount = itemsCount; | |
this.compute(); | |
}; | |
/** | |
* Compute when using new vars | |
*/ | |
Pagination.prototype.compute = function() { | |
this.pagesCount = Math.ceil(this.itemsCount / this.perPage); | |
this.page = Math.max(1, Math.min(this.page, this.pagesCount)); | |
var n2l = this.pagesCount - 1; | |
var links = []; | |
var i = 0; | |
var currentPage = this.page; | |
this.prev = this.page - 1; | |
this.next = this.page + 1; | |
if (this.prev < 1) { | |
this.prev = 1; | |
} | |
if (this.next > this.pagesCount) { | |
this.next = this.pagesCount; | |
} | |
if (this.pagesCount === 1 && this.options.hideSinglePage) { | |
return; | |
} | |
/** | |
* @params {String} label | |
* @params {Number} pageNum | |
* @params {String} tags | |
*/ | |
var addLink = function(label, pageNum) { | |
var type = "pageIndex"; | |
var active = pageNum === currentPage; | |
var disabled = false; | |
if (typeof label === "string") { | |
type = "shortcut"; | |
active = false; | |
} | |
if (label === '...') { | |
type = 'separator'; | |
active = false; | |
disabled = true; | |
} | |
links.push({ | |
active: active, | |
type: type, | |
label: label+'', | |
page: pageNum, | |
disabled: disabled | |
}); | |
}; | |
if (this.isFirstPage()) { | |
addLink('first', 1); | |
addLink('previous', this.prev); | |
} | |
if (this.pagesCount < (7 + 6)) { /* few pages */ | |
addLink(1, 1); | |
for (i = 2; i <= this.pagesCount; i++) { | |
addLink(i, i); | |
} | |
} else { /* many pages */ | |
if (this.page < (2 + 6)) { | |
for (i = 1; i < 10; i++) { | |
addLink(i, i); | |
} | |
addLink('...'); | |
addLink(n2l, n2l); | |
addLink(this.pagesCount, this.pagesCount); | |
} else if (this.page > 7 && this.page < (this.pagesCount - 6)) { | |
addLink(1, 1); | |
addLink(2, 2); | |
addLink('...'); | |
for (i = this.page - 3; i <= (this.page + 3); i++) { | |
addLink(i, i); | |
} | |
addLink('...'); | |
addLink(n2l, n2l); | |
addLink(this.pagesCount, this.pagesCount); | |
} else { | |
addLink(1, 1); | |
addLink(2, 2); | |
addLink('...'); | |
for (i = (this.pagesCount - 8); i <= this.pagesCount; i++) { | |
addLink(i, i); | |
} | |
} | |
} | |
if (!this.isLastPage()) { | |
addLink('next', this.page + 1); | |
addLink('last', this.pagesCount); | |
} | |
this.links = links; | |
}; | |
Pagination.prototype.isFirstPage = function() { | |
return (this.page === 1); | |
}; | |
Pagination.prototype.isLastPage = function() { | |
return (this.page === this.pagesCount); | |
}; | |
Pagination.prototype.getLinks = function() { | |
return this.links; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment