From 7f256663e0bc4f3b0eb6d97a3cd797a8ccad4ed4 Mon Sep 17 00:00:00 2001 From: Stanislav Romanov Date: Mon, 24 Oct 2022 11:09:53 +0300 Subject: [PATCH] Refactore pagination --- app/components/pagination.hbs | 8 ++--- app/components/pagination.js | 30 +++++++------------ app/helpers/mul.js | 5 ++++ .../admin/indexes/item/documents/index.js | 14 +++++++-- .../admin/indexes/item/documents/index.hbs | 6 ++-- tests/integration/helpers/mul-test.js | 17 +++++++++++ 6 files changed, 52 insertions(+), 28 deletions(-) create mode 100644 app/helpers/mul.js create mode 100644 tests/integration/helpers/mul-test.js diff --git a/app/components/pagination.hbs b/app/components/pagination.hbs index ce35356..eb5e294 100644 --- a/app/components/pagination.hbs +++ b/app/components/pagination.hbs @@ -12,7 +12,7 @@ @@ -20,7 +20,7 @@ {{/if}} {{#each this.availablePages as |i|}} - {{#if (eq @current i)}} + {{#if (eq this.current i)}} {{i}} @@ -32,7 +32,7 @@ {{i}} @@ -43,7 +43,7 @@ diff --git a/app/components/pagination.js b/app/components/pagination.js index 71afca1..f39d2b7 100644 --- a/app/components/pagination.js +++ b/app/components/pagination.js @@ -4,28 +4,16 @@ import { inject as service } from '@ember/service'; export default class PaginationComponent extends Component { @service router; - numberPagesToShow = 2; - get availablePages() { const availablePages = []; + const numberPagesToShow = 2; for (let i = 1; i <= this.totalPages; i++) { - if (i === this.args.current) { + if (i === this.current) { availablePages.push(i); - } else if ( - i < this.args.current && - i >= this.args.current - this.numberPagesToShow - ) { + } else if (i < this.current && i >= this.current - 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 - ) { + } else if (i > this.current && i <= this.current + numberPagesToShow) { availablePages.push(i); } } @@ -52,14 +40,18 @@ export default class PaginationComponent extends Component { } get totalPages() { - return Math.ceil(this.args.count / this.args.perPage); + return Math.ceil(this.args.total / this.args.limit); } get previousPage() { - return this.args.current > 1 ? this.args.current - 1 : null; + return this.args.offset - this.args.limit >= 0; } get nextPage() { - return this.args.current < this.totalPages ? this.args.current + 1 : null; + return this.args.offset + this.args.limit < this.args.total; + } + + get current() { + return Math.ceil(this.args.offset / this.args.limit) + 1; } } diff --git a/app/helpers/mul.js b/app/helpers/mul.js new file mode 100644 index 0000000..32a1a57 --- /dev/null +++ b/app/helpers/mul.js @@ -0,0 +1,5 @@ +import { helper } from '@ember/component/helper'; + +export default helper(function mul([a, b]) { + return Math.imul(a, b); +}); diff --git a/app/routes/admin/indexes/item/documents/index.js b/app/routes/admin/indexes/item/documents/index.js index ba5b435..793bd9f 100644 --- a/app/routes/admin/indexes/item/documents/index.js +++ b/app/routes/admin/indexes/item/documents/index.js @@ -45,10 +45,20 @@ const searchSchema = { export default class AdminIndexesItemDocumentsIndexRoute extends Route { queryParams = { q: { refreshModel: true }, - limit: { refreshModel: true }, offset: { refreshModel: true }, - sort: { refreshModel: true }, + limit: { refreshModel: true }, + filter: { refreshModel: true }, + facets: { refreshModel: true }, attributesToRetrieve: { refreshModel: true }, + attributesToCrop: { refreshModel: true }, + cropLength: { refreshModel: true }, + cropMarker: { refreshModel: true }, + attributesToHighlight: { refreshModel: true }, + highlightPreTag: { refreshModel: true }, + highlightPostTag: { refreshModel: true }, + showMatchesPosition: { refreshModel: true }, + sort: { refreshModel: true }, + matchingStrategy: { refreshModel: true }, }; async model(options) { diff --git a/app/templates/admin/indexes/item/documents/index.hbs b/app/templates/admin/indexes/item/documents/index.hbs index cd4e14b..b1831a2 100644 --- a/app/templates/admin/indexes/item/documents/index.hbs +++ b/app/templates/admin/indexes/item/documents/index.hbs @@ -228,9 +228,9 @@ {{@model.data.estimatedTotalHits}} diff --git a/tests/integration/helpers/mul-test.js b/tests/integration/helpers/mul-test.js new file mode 100644 index 0000000..d0fd457 --- /dev/null +++ b/tests/integration/helpers/mul-test.js @@ -0,0 +1,17 @@ +import { module, test } from 'qunit'; +import { setupRenderingTest } from 'meiliadmin/tests/helpers'; +import { render } from '@ember/test-helpers'; +import { hbs } from 'ember-cli-htmlbars'; + +module('Integration | Helper | mul', function (hooks) { + setupRenderingTest(hooks); + + // TODO: Replace this with your real tests. + test('it renders', async function (assert) { + this.set('inputValue', '1234'); + + await render(hbs`{{mul this.inputValue}}`); + + assert.dom(this.element).hasText('1234'); + }); +});