meilisearch-admin/app/components/pagination.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-03-25 11:23:53 +00:00
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
export default class PaginationComponent extends Component {
@service router;
get availablePages() {
const availablePages = [];
2022-10-24 08:09:53 +00:00
const numberPagesToShow = 2;
2022-03-25 11:23:53 +00:00
for (let i = 1; i <= this.totalPages; i++) {
2022-10-24 08:09:53 +00:00
if (i === this.current) {
2022-03-25 11:23:53 +00:00
availablePages.push(i);
2022-10-24 08:09:53 +00:00
} else if (i < this.current && i >= this.current - numberPagesToShow) {
2022-03-25 11:23:53 +00:00
availablePages.push(i);
2022-10-24 08:09:53 +00:00
} else if (i > this.current && i <= this.current + numberPagesToShow) {
2022-03-25 11:23:53 +00:00
availablePages.push(i);
}
}
const firstPage = availablePages[0];
if (firstPage > 1) {
if (firstPage - 1 > 1) {
availablePages.unshift('...');
}
availablePages.unshift(1);
}
const lastPage = availablePages[availablePages.length - 1];
if (lastPage < this.totalPages) {
if (lastPage + 1 < this.totalPages) {
availablePages.push('...');
}
availablePages.push(this.totalPages);
}
return availablePages;
}
get totalPages() {
2022-10-24 08:09:53 +00:00
return Math.ceil(this.args.total / this.args.limit);
2022-03-25 11:23:53 +00:00
}
get previousPage() {
2022-10-24 08:09:53 +00:00
return this.args.offset - this.args.limit >= 0;
2022-03-25 11:23:53 +00:00
}
get nextPage() {
2022-10-24 08:09:53 +00:00
return this.args.offset + this.args.limit < this.args.total;
}
get current() {
return Math.ceil(this.args.offset / this.args.limit) + 1;
2022-03-25 11:23:53 +00:00
}
}