From d7db5026a341c97dea82d4307393b48c8e41fcd1 Mon Sep 17 00:00:00 2001 From: Jeremy Epstein Date: Mon, 22 Mar 2021 11:01:30 +1100 Subject: [PATCH 1/4] Pagination: add pageOnEmptyData option --- src/Plugins/Pagination.js | 6 ++- test/PaginationTest.js | 40 +++++++++++++++++++ .../paged/paged-empty-pageonemptydata.njk | 8 ++++ test/stubs/paged/paged-empty.njk | 7 ++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 test/stubs/paged/paged-empty-pageonemptydata.njk create mode 100644 test/stubs/paged/paged-empty.njk diff --git a/src/Plugins/Pagination.js b/src/Plugins/Pagination.js index 5815ffcc3..abed3cdb2 100755 --- a/src/Plugins/Pagination.js +++ b/src/Plugins/Pagination.js @@ -183,7 +183,11 @@ class Pagination { let hrefs = []; let overrides = []; - for (let pageNumber = 0, k = items.length; pageNumber < k; pageNumber++) { + const totalPages = + items.length ? items.length : ( + this.data.pagination.pageOnEmptyData ? 1 : 0); + + for (let pageNumber = 0; pageNumber < totalPages; pageNumber++) { let cloned = tmpl.clone(); // TODO maybe also move this permalink additions up into the pagination class diff --git a/test/PaginationTest.js b/test/PaginationTest.js index 8f165f6c9..65af782c6 100644 --- a/test/PaginationTest.js +++ b/test/PaginationTest.js @@ -44,6 +44,46 @@ test("No pagination", async (t) => { t.is((await paging.getPageTemplates()).length, 0); }); +test("Empty paged data", async (t) => { + let eleventyConfig = new TemplateConfig(); + let tmpl = getNewTemplate( + "./test/stubs/paged/paged-empty.njk", + "./test/stubs/", + "./dist", + null, + null, + eleventyConfig + ); + + let data = await tmpl.getData(); + let paging = new Pagination(data, tmpl.config); + paging.setTemplate(tmpl); + + t.is(paging.getPageCount(), 0); + t.is(paging.pagedItems.length, 0); + t.is((await paging.getPageTemplates()).length, 0); +}); + +test("Empty paged data with pageOnEmptyData enabled", async (t) => { + let eleventyConfig = new TemplateConfig(); + let tmpl = getNewTemplate( + "./test/stubs/paged/paged-empty-pageonemptydata.njk", + "./test/stubs/", + "./dist", + null, + null, + eleventyConfig + ); + + let data = await tmpl.getData(); + let paging = new Pagination(data, tmpl.config); + paging.setTemplate(tmpl); + + t.is(paging.getPageCount(), 0); + t.is(paging.pagedItems.length, 0); + t.is((await paging.getPageTemplates()).length, 1); +}); + test("Pagination enabled in frontmatter", async (t) => { let eleventyConfig = new TemplateConfig(); let tmpl = getNewTemplate( diff --git a/test/stubs/paged/paged-empty-pageonemptydata.njk b/test/stubs/paged/paged-empty-pageonemptydata.njk new file mode 100644 index 000000000..9a9583e5f --- /dev/null +++ b/test/stubs/paged/paged-empty-pageonemptydata.njk @@ -0,0 +1,8 @@ +--- +pagination: + data: items + size: 1 + pageOnEmptyData: true +items: [] +--- +
    {% for item in pagination.items %}
  1. {{ item }}
  2. {% endfor %}
diff --git a/test/stubs/paged/paged-empty.njk b/test/stubs/paged/paged-empty.njk new file mode 100644 index 000000000..784012e65 --- /dev/null +++ b/test/stubs/paged/paged-empty.njk @@ -0,0 +1,7 @@ +--- +pagination: + data: items + size: 1 +items: [] +--- +
    {% for item in pagination.items %}
  1. {{ item }}
  2. {% endfor %}
From 83ea7a2c0f4295f6247d09f2e50ac0467bef6a00 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Wed, 11 May 2022 17:01:24 -0500 Subject: [PATCH 2/4] Update Pagination.js --- src/Plugins/Pagination.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/Pagination.js b/src/Plugins/Pagination.js index b77cdb36b..f8fc4c3fb 100755 --- a/src/Plugins/Pagination.js +++ b/src/Plugins/Pagination.js @@ -337,7 +337,7 @@ class Pagination { } for ( - let pageNumber = 0, pageNumberStop = items.length; + let pageNumber = 0; pageNumber < pageNumberStop; pageNumber++ ) { From 34f01466fa53263372f458a04868c5df44d87507 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Wed, 11 May 2022 17:01:54 -0500 Subject: [PATCH 3/4] Update Pagination.js --- src/Plugins/Pagination.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/Pagination.js b/src/Plugins/Pagination.js index f8fc4c3fb..ad63c5dc7 100755 --- a/src/Plugins/Pagination.js +++ b/src/Plugins/Pagination.js @@ -332,7 +332,7 @@ class Pagination { // template for some things) let pageNumberStop = items.length; - if(items.length === 0 && this.data.pagination.pageOnEmptyData) { + if(pageNumberStop === 0 && this.data.pagination.pageOnEmptyData) { pageNumberStop = 1; } From 045ca9921452844f0774153c49c06717c9272a4b Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Wed, 11 May 2022 17:09:27 -0500 Subject: [PATCH 4/4] Defer to approach in #2208 --- src/Plugins/Pagination.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Plugins/Pagination.js b/src/Plugins/Pagination.js index ad63c5dc7..8f49293f7 100755 --- a/src/Plugins/Pagination.js +++ b/src/Plugins/Pagination.js @@ -331,14 +331,9 @@ class Pagination { // so that we don’t have the memory cost of the full template (and can reuse the parent // template for some things) - let pageNumberStop = items.length; - if(pageNumberStop === 0 && this.data.pagination.pageOnEmptyData) { - pageNumberStop = 1; - } - for ( let pageNumber = 0; - pageNumber < pageNumberStop; + pageNumber < items.length; pageNumber++ ) { let cloned = this.template.clone();