diff --git a/app/components/pagination.hbs b/app/components/pagination.hbs new file mode 100644 index 0000000..786fefd --- /dev/null +++ b/app/components/pagination.hbs @@ -0,0 +1,46 @@ +{{#if (gt this.totalPages 1)}} + +{{/if}} diff --git a/app/components/pagination.js b/app/components/pagination.js new file mode 100644 index 0000000..71afca1 --- /dev/null +++ b/app/components/pagination.js @@ -0,0 +1,65 @@ +import Component from '@glimmer/component'; +import { inject as service } from '@ember/service'; + +export default class PaginationComponent extends Component { + @service router; + + numberPagesToShow = 2; + + get availablePages() { + const availablePages = []; + + for (let i = 1; i <= this.totalPages; i++) { + if (i === this.args.current) { + availablePages.push(i); + } else if ( + i < this.args.current && + i >= this.args.current - this.numberPagesToShow + ) { + availablePages.push(i); + } else if ( + i > this.args.current && + i <= this.args.current + this.numberPagesToShow + ) { + availablePages.push(i); + } else if ( + this.args.current > this.totalPages && + i <= this.args.current + this.numberPagesToShow + ) { + 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() { + return Math.ceil(this.args.count / this.args.perPage); + } + + get previousPage() { + return this.args.current > 1 ? this.args.current - 1 : null; + } + + get nextPage() { + return this.args.current < this.totalPages ? this.args.current + 1 : null; + } +} diff --git a/app/controllers/admin/indexes/item/data.js b/app/controllers/admin/indexes/item/data.js index 4cd4fc7..f3266c9 100644 --- a/app/controllers/admin/indexes/item/data.js +++ b/app/controllers/admin/indexes/item/data.js @@ -5,6 +5,8 @@ import { trackedFunction } from 'ember-resources'; export default class AdminIndexesItemDataController extends Controller { @tracked searchText = 'hello'; + @tracked offset = 0; + @tracked page = 0; request = trackedFunction(this, async () => { if (typeof this.searchText !== 'string' || this.searchText.length < 1) { diff --git a/app/routes/admin/indexes.js b/app/routes/admin/indexes.js index 7a7b9e3..bd4192e 100644 --- a/app/routes/admin/indexes.js +++ b/app/routes/admin/indexes.js @@ -3,7 +3,6 @@ import Route from '@ember/routing/route'; export default class AdminIndexesRoute extends Route { // @service meilisearch; - // async model() { // return this.modelFor('admin'); // } diff --git a/app/templates/admin/indexes/item/data.hbs b/app/templates/admin/indexes/item/data.hbs index 095d2a4..c458801 100644 --- a/app/templates/admin/indexes/item/data.hbs +++ b/app/templates/admin/indexes/item/data.hbs @@ -21,7 +21,7 @@