From 13bd4de60054cd5aafc3cae8fb88e5f391359524 Mon Sep 17 00:00:00 2001 From: Haytham Salama Date: Fri, 22 Sep 2023 23:21:53 +0300 Subject: [PATCH 01/10] fix(Pagination): correct `displayedPages()` --- .../components/navigation/Pagination.vue | 67 ++++++++----------- 1 file changed, 28 insertions(+), 39 deletions(-) diff --git a/src/runtime/components/navigation/Pagination.vue b/src/runtime/components/navigation/Pagination.vue index ed02753c57..890f06f86c 100644 --- a/src/runtime/components/navigation/Pagination.vue +++ b/src/runtime/components/navigation/Pagination.vue @@ -127,53 +127,42 @@ export default defineComponent({ const pages = computed(() => Array.from({ length: Math.ceil(props.total / props.pageCount) }, (_, i) => i + 1)) const displayedPages = computed(() => { - if (!props.max || pages.value.length <= 5) { - return pages.value - } else { - const current = currentPage.value - const max = pages.value.length - const r = Math.floor((Math.min(props.max, max) - 5) / 2) - const r1 = current - r - const r2 = current + r - const beforeWrapped = r1 - 1 > 1 - const afterWrapped = r2 + 1 < max - const items: Array = [1] - - if (beforeWrapped) items.push(props.divider) - - if (!afterWrapped) { - const addedItems = (current + r + 2) - max - for (let i = current - r - addedItems; i <= current - r - 1; i++) { - items.push(i) - } - } + const totalItems = props.total + const current = currentPage.value + const perPage = props.pageCount + const lastPage = Math.ceil(totalItems / perPage) - for (let i = r1 > 2 ? (r1) : 2; i <= Math.min(max, r2); i++) { - items.push(i) - } + const maxDisplayedPages = Math.min(props.max, perPage) + const halfDisplayedPages = Math.floor(maxDisplayedPages / 2) + const startDisplayedPage = Math.max(2, current - halfDisplayedPages) + const endDisplayedPage = Math.min(startDisplayedPage + maxDisplayedPages - 2, Math.ceil(totalItems / perPage)) + + const items = [] - if (!beforeWrapped) { - const addedItems = 1 - (current - r - 2) - for (let i = current + r + 1; i <= current + r + addedItems; i++) { - items.push(i) - } + if (totalItems <= maxDisplayedPages) { + for (let i = 1; i <= totalItems; i++) { + items.push(i) } + return items + } - if (afterWrapped) items.push(props.divider) + items.push(1) - if (r2 < max) items.push(max) + if (current > halfDisplayedPages + 1) { + items.push(props.divider) + } - // Replace divider by number on start edge case [1, '…', 3, ...] - if (items.length >= 3 && items[1] === props.divider && items[2] === 3) { - items[1] = 2 - } - // Replace divider by number on end edge case [..., 48, '…', 50] - if (items.length >= 3 && items[items.length - 2] === props.divider && items[items.length - 1] === items.length) { - items[items.length - 2] = items.length - 1 - } + for (let i = startDisplayedPage; i < endDisplayedPage; i++) { + items.push(i) + } - return items + if (current < lastPage - halfDisplayedPages) { + items.push(props.divider) } + + items.push(lastPage) + + return items }) const canGoPrev = computed(() => currentPage.value > 1) From 9f3d5fd7e9e3634e54fab50447d9e2e3d30275a9 Mon Sep 17 00:00:00 2001 From: Haytham Salama Date: Sat, 23 Sep 2023 00:06:41 +0300 Subject: [PATCH 02/10] up --- src/runtime/components/navigation/Pagination.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/components/navigation/Pagination.vue b/src/runtime/components/navigation/Pagination.vue index 890f06f86c..a33f59f944 100644 --- a/src/runtime/components/navigation/Pagination.vue +++ b/src/runtime/components/navigation/Pagination.vue @@ -132,7 +132,7 @@ export default defineComponent({ const perPage = props.pageCount const lastPage = Math.ceil(totalItems / perPage) - const maxDisplayedPages = Math.min(props.max, perPage) + const maxDisplayedPages = props.max const halfDisplayedPages = Math.floor(maxDisplayedPages / 2) const startDisplayedPage = Math.max(2, current - halfDisplayedPages) const endDisplayedPage = Math.min(startDisplayedPage + maxDisplayedPages - 2, Math.ceil(totalItems / perPage)) From 12290ae475b099027a19043bc91cc957b03adc75 Mon Sep 17 00:00:00 2001 From: Haytham Salama Date: Sat, 23 Sep 2023 00:11:11 +0300 Subject: [PATCH 03/10] up --- src/runtime/components/navigation/Pagination.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/components/navigation/Pagination.vue b/src/runtime/components/navigation/Pagination.vue index a33f59f944..2992b8d40f 100644 --- a/src/runtime/components/navigation/Pagination.vue +++ b/src/runtime/components/navigation/Pagination.vue @@ -156,7 +156,7 @@ export default defineComponent({ items.push(i) } - if (current < lastPage - halfDisplayedPages) { + if (current < lastPage - halfDisplayedPages && endDisplayedPage < lastPage) { items.push(props.divider) } From a0b7286b6cd3a8cdf9da4032f874b5f6f59228f7 Mon Sep 17 00:00:00 2001 From: Haytham Salama Date: Sat, 23 Sep 2023 01:34:27 +0300 Subject: [PATCH 04/10] up --- src/runtime/components/navigation/Pagination.vue | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/runtime/components/navigation/Pagination.vue b/src/runtime/components/navigation/Pagination.vue index 2992b8d40f..44db3044d3 100644 --- a/src/runtime/components/navigation/Pagination.vue +++ b/src/runtime/components/navigation/Pagination.vue @@ -128,19 +128,20 @@ export default defineComponent({ const displayedPages = computed(() => { const totalItems = props.total + const totalPages = pages.value.length const current = currentPage.value const perPage = props.pageCount const lastPage = Math.ceil(totalItems / perPage) const maxDisplayedPages = props.max const halfDisplayedPages = Math.floor(maxDisplayedPages / 2) - const startDisplayedPage = Math.max(2, current - halfDisplayedPages) - const endDisplayedPage = Math.min(startDisplayedPage + maxDisplayedPages - 2, Math.ceil(totalItems / perPage)) + const startDisplayedPage = Math.max(2, current + 1 - halfDisplayedPages) + const endDisplayedPage = Math.min(startDisplayedPage + maxDisplayedPages - 2, lastPage) const items = [] - if (totalItems <= maxDisplayedPages) { - for (let i = 1; i <= totalItems; i++) { + if (totalPages <= maxDisplayedPages) { + for (let i = 1; i <= totalPages; i++) { items.push(i) } return items @@ -148,7 +149,7 @@ export default defineComponent({ items.push(1) - if (current > halfDisplayedPages + 1) { + if (current > halfDisplayedPages + 1 && startDisplayedPage > 2) { items.push(props.divider) } From c20140c30472712ee17dc2fb6441cd22e4d720be Mon Sep 17 00:00:00 2001 From: Haytham Salama Date: Wed, 27 Sep 2023 23:10:54 +0300 Subject: [PATCH 05/10] up --- .../components/navigation/Pagination.vue | 54 ++++++++++++++----- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/src/runtime/components/navigation/Pagination.vue b/src/runtime/components/navigation/Pagination.vue index 44db3044d3..d14d546ead 100644 --- a/src/runtime/components/navigation/Pagination.vue +++ b/src/runtime/components/navigation/Pagination.vue @@ -1,4 +1,5 @@