Skip to content

Commit

Permalink
Merge pull request #39 from andrehoffmann30/master
Browse files Browse the repository at this point in the history
FEATURE: add option to show previous and next links
  • Loading branch information
daniellienert authored Apr 7, 2020
2 parents cdc37c6 + 05f94ce commit 7be184b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 41 deletions.
87 changes: 49 additions & 38 deletions Classes/Fusion/PaginationArrayImplementation.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,58 @@
<?php

namespace Flowpack\Listable\Fusion;

use Neos\Flow\Annotations as Flow;
use Neos\Fusion\FusionObjects\AbstractFusionObject;

class PaginationArrayImplementation extends AbstractFusionObject
{
/**
* @return Array
*/
public function evaluate()
{
$maximumNumberOfLinks = $this->fusionValue('maximumNumberOfLinks') - 2;
$itemsPerPage = $this->fusionValue('itemsPerPage');
$totalCount = $this->fusionValue('totalCount');
$currentPage = $this->fusionValue('currentPage');
if ($totalCount > 0 !== true) {
return [];
}
$numberOfPages = ceil($totalCount / $itemsPerPage);
if ($maximumNumberOfLinks > $numberOfPages) {
$maximumNumberOfLinks = $numberOfPages;
}
$delta = floor($maximumNumberOfLinks / 2);
$displayRangeStart = $currentPage - $delta;
$displayRangeEnd = $currentPage + $delta + ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
if ($displayRangeStart < 1) {
$displayRangeEnd -= $displayRangeStart - 1;
}
if ($displayRangeEnd > $numberOfPages) {
$displayRangeStart -= ($displayRangeEnd - $numberOfPages);
}
$displayRangeStart = (integer)max($displayRangeStart, 1);
$displayRangeEnd = (integer)min($displayRangeEnd, $numberOfPages);
$links = \range($displayRangeStart, $displayRangeEnd);
if ($displayRangeStart > 2) {
array_unshift($links, "...");
array_unshift($links, 1);
}
if ($displayRangeEnd + 1 < $numberOfPages) {
$links[] = "...";
$links[] = $numberOfPages;
}
return $links;
}
/**
* @return array
*/
public function evaluate(): array
{
$showPreviousNextLinks = (bool)$this->fusionValue('showPreviousNextLinks');
$maximumNumberOfLinks = $this->fusionValue('maximumNumberOfLinks') - 2;
$itemsPerPage = $this->fusionValue('itemsPerPage');
$totalCount = $this->fusionValue('totalCount');
$currentPage = $this->fusionValue('currentPage');
if ($totalCount > 0 !== true) {
return [];
}
$numberOfPages = ceil($totalCount / $itemsPerPage);
if ($maximumNumberOfLinks > $numberOfPages) {
$maximumNumberOfLinks = $numberOfPages;
}
$delta = floor($maximumNumberOfLinks / 2);
$displayRangeStart = $currentPage - $delta;
$displayRangeEnd = $currentPage + $delta + ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
if ($displayRangeStart < 1) {
$displayRangeEnd -= $displayRangeStart - 1;
}
if ($displayRangeEnd > $numberOfPages) {
$displayRangeStart -= ($displayRangeEnd - $numberOfPages);
}
$displayRangeStart = (integer)max($displayRangeStart, 1);
$displayRangeEnd = (integer)min($displayRangeEnd, $numberOfPages);
$links = \range($displayRangeStart, $displayRangeEnd);
if ($displayRangeStart > 2) {
array_unshift($links, "...");
array_unshift($links, 1);
}
if ($displayRangeEnd + 1 < $numberOfPages) {
$links[] = "...";
$links[] = $numberOfPages;
}

if ($showPreviousNextLinks) {
if ($currentPage > 1) {
array_unshift($links, 'previous');
}
if ($currentPage < $numberOfPages) {
$links[] = 'next';
}
}
return $links;
}
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Configuration options:
| itemsPerPage | Number of items per page when using pagination | 24 |
| maximumNumberOfLinks | Number of page links in pagination | 15 |
| listRenderer | Object used for rendering the list. | 'Flowpack.Listable:Collection' |
| showPreviousNextLinks| Boolean value used to decide whether the previous and next links should be added| false |

When used with ElasticSearch, build the query, but don't execute it, the object will do it for you:

Expand All @@ -73,7 +74,7 @@ prototype(My.Custom:Object) < prototype(Flowpack.Listable:PaginatedCollection) {
```

If you have additional URL parameters (e.g for a date filter) you have to register the argument and change the cache entryDiscriminator in order work accordingly.
HINT: Do not forget to register a corresponding route for your custom argument.
HINT: Do not forget to register a corresponding route for your custom argument.

```
prototype(My.Custom:Object) < prototype(Flowpack.Listable:PaginatedCollection) {
Expand All @@ -87,7 +88,7 @@ prototype(My.Custom:Object) < prototype(Flowpack.Listable:PaginatedCollection) {
entryDiscriminator = ${request.arguments.currentPage + request.arguments.date}
}
}
}
```

This object is configured by default to `dynamic` cache mode for pagination to work. All you have to do is add correct `entryTags` and you are all set.
Expand Down Expand Up @@ -138,6 +139,7 @@ Configuration options:
| itemClass | A total count of items | 'Pagination-item' |
| currentItemClass | A class for a current item | 'isCurrent' |
| currentPage | Current page, starting with 1 | `${request.arguments.currentPage || 1}` |
| showPreviousNextLinks| Boolean value used to decide whether the previous and next links should be added| false |

# FlowQuery Helpers you can use

Expand Down
2 changes: 2 additions & 0 deletions Resources/Private/Fusion/PaginatedCollection.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ prototype(Flowpack.Listable:PaginatedCollection) < prototype(Neos.Fusion:Compone
collection = 'must-be-set'
itemsPerPage = 24
maximumNumberOfLinks = 15
showPreviousNextLinks = false
listRenderer = 'Flowpack.Listable:Collection'

renderer = Neos.Fusion:Array {
Expand Down Expand Up @@ -39,6 +40,7 @@ prototype(Flowpack.Listable:PaginatedCollection) < prototype(Neos.Fusion:Compone
totalCount = ${data.totalCount}
maximumNumberOfLinks = ${props.maximumNumberOfLinks}
itemsPerPage = ${props.itemsPerPage}
showPreviousNextLinks = ${props.showPreviousNextLinks}
}
}

Expand Down
33 changes: 32 additions & 1 deletion Resources/Private/Fusion/Pagination.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ prototype(Flowpack.Listable:PaginationArray) {
maximumNumberOfLinks = ''
totalCount = ''
itemsPerPage = ''
showPreviousNextLinks = false
}

prototype(Flowpack.Listable:PaginationParameters) < prototype(Neos.Fusion:RawArray)
Expand All @@ -12,6 +13,7 @@ prototype(Flowpack.Listable:Pagination) < prototype(Neos.Fusion:Component) {
totalCount = 'to-be-set'
maximumNumberOfLinks = 15
itemsPerPage = 24
showPreviousNextLinks = false

class = 'Pagination'
itemClass = 'Pagination-item'
Expand All @@ -26,6 +28,7 @@ prototype(Flowpack.Listable:Pagination) < prototype(Neos.Fusion:Component) {
maximumNumberOfLinks = ${props.maximumNumberOfLinks}
totalCount = ${props.totalCount}
itemsPerPage = ${props.itemsPerPage}
showPreviousNextLinks = ${props.showPreviousNextLinks}
}
itemName = 'i'
itemRenderer = Neos.Fusion:Case {
Expand All @@ -37,8 +40,36 @@ prototype(Flowpack.Listable:Pagination) < prototype(Neos.Fusion:Component) {
condition = ${String.toInteger(i) == String.toInteger(props.currentPage)}
renderer = ${'<li class="' + props.itemClass + ' ' + props.currentItemClass + '"><a>' + i + '</a></li>'}
}
previous {
condition = ${i == 'previous' && (props.showPreviousNextLinks == true)}
renderer = Neos.Fusion:Tag {
@process.tmpl = ${'<li class="previous">' + value + '</li>'}
tagName = 'a'
attributes.href = Neos.Neos:NodeUri {
node = ${documentNode}
additionalParams = Flowpack.Listable:PaginationParameters {
currentPage = ${String.toInteger(props.currentPage) - 1}
}
}
content = ${i}
}
}
next {
condition = ${i == 'next' && (props.showPreviousNextLinks == true)}
renderer = Neos.Fusion:Tag {
@process.tmpl = ${'<li class="next">' + value + '</li>'}
tagName = 'a'
attributes.href = Neos.Neos:NodeUri {
node = ${documentNode}
additionalParams = Flowpack.Listable:PaginationParameters {
currentPage = ${String.toInteger(props.currentPage) + 1}
}
}
content = ${i}
}
}
link {
condition = ${true}
condition = ${(iterator.isFirst == false && iterator.isLast == false) || (props.showPreviousNextLinks == false)}
renderer = Neos.Fusion:Tag {
@process.tmpl = ${'<li class="' + props.itemClass + '">' + value + '</li>'}
tagName = 'a'
Expand Down

0 comments on commit 7be184b

Please sign in to comment.