Skip to content

Commit 7be184b

Browse files
Merge pull request #39 from andrehoffmann30/master
FEATURE: add option to show previous and next links
2 parents cdc37c6 + 05f94ce commit 7be184b

4 files changed

Lines changed: 87 additions & 41 deletions

File tree

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,58 @@
11
<?php
2+
23
namespace Flowpack\Listable\Fusion;
34

45
use Neos\Flow\Annotations as Flow;
56
use Neos\Fusion\FusionObjects\AbstractFusionObject;
67

78
class PaginationArrayImplementation extends AbstractFusionObject
89
{
9-
/**
10-
* @return Array
11-
*/
12-
public function evaluate()
13-
{
14-
$maximumNumberOfLinks = $this->fusionValue('maximumNumberOfLinks') - 2;
15-
$itemsPerPage = $this->fusionValue('itemsPerPage');
16-
$totalCount = $this->fusionValue('totalCount');
17-
$currentPage = $this->fusionValue('currentPage');
18-
if ($totalCount > 0 !== true) {
19-
return [];
20-
}
21-
$numberOfPages = ceil($totalCount / $itemsPerPage);
22-
if ($maximumNumberOfLinks > $numberOfPages) {
23-
$maximumNumberOfLinks = $numberOfPages;
24-
}
25-
$delta = floor($maximumNumberOfLinks / 2);
26-
$displayRangeStart = $currentPage - $delta;
27-
$displayRangeEnd = $currentPage + $delta + ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
28-
if ($displayRangeStart < 1) {
29-
$displayRangeEnd -= $displayRangeStart - 1;
30-
}
31-
if ($displayRangeEnd > $numberOfPages) {
32-
$displayRangeStart -= ($displayRangeEnd - $numberOfPages);
33-
}
34-
$displayRangeStart = (integer)max($displayRangeStart, 1);
35-
$displayRangeEnd = (integer)min($displayRangeEnd, $numberOfPages);
36-
$links = \range($displayRangeStart, $displayRangeEnd);
37-
if ($displayRangeStart > 2) {
38-
array_unshift($links, "...");
39-
array_unshift($links, 1);
40-
}
41-
if ($displayRangeEnd + 1 < $numberOfPages) {
42-
$links[] = "...";
43-
$links[] = $numberOfPages;
44-
}
45-
return $links;
46-
}
10+
/**
11+
* @return array
12+
*/
13+
public function evaluate(): array
14+
{
15+
$showPreviousNextLinks = (bool)$this->fusionValue('showPreviousNextLinks');
16+
$maximumNumberOfLinks = $this->fusionValue('maximumNumberOfLinks') - 2;
17+
$itemsPerPage = $this->fusionValue('itemsPerPage');
18+
$totalCount = $this->fusionValue('totalCount');
19+
$currentPage = $this->fusionValue('currentPage');
20+
if ($totalCount > 0 !== true) {
21+
return [];
22+
}
23+
$numberOfPages = ceil($totalCount / $itemsPerPage);
24+
if ($maximumNumberOfLinks > $numberOfPages) {
25+
$maximumNumberOfLinks = $numberOfPages;
26+
}
27+
$delta = floor($maximumNumberOfLinks / 2);
28+
$displayRangeStart = $currentPage - $delta;
29+
$displayRangeEnd = $currentPage + $delta + ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
30+
if ($displayRangeStart < 1) {
31+
$displayRangeEnd -= $displayRangeStart - 1;
32+
}
33+
if ($displayRangeEnd > $numberOfPages) {
34+
$displayRangeStart -= ($displayRangeEnd - $numberOfPages);
35+
}
36+
$displayRangeStart = (integer)max($displayRangeStart, 1);
37+
$displayRangeEnd = (integer)min($displayRangeEnd, $numberOfPages);
38+
$links = \range($displayRangeStart, $displayRangeEnd);
39+
if ($displayRangeStart > 2) {
40+
array_unshift($links, "...");
41+
array_unshift($links, 1);
42+
}
43+
if ($displayRangeEnd + 1 < $numberOfPages) {
44+
$links[] = "...";
45+
$links[] = $numberOfPages;
46+
}
47+
48+
if ($showPreviousNextLinks) {
49+
if ($currentPage > 1) {
50+
array_unshift($links, 'previous');
51+
}
52+
if ($currentPage < $numberOfPages) {
53+
$links[] = 'next';
54+
}
55+
}
56+
return $links;
57+
}
4758
}

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Configuration options:
5858
| itemsPerPage | Number of items per page when using pagination | 24 |
5959
| maximumNumberOfLinks | Number of page links in pagination | 15 |
6060
| listRenderer | Object used for rendering the list. | 'Flowpack.Listable:Collection' |
61+
| showPreviousNextLinks| Boolean value used to decide whether the previous and next links should be added| false |
6162

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

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

7576
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.
76-
HINT: Do not forget to register a corresponding route for your custom argument.
77+
HINT: Do not forget to register a corresponding route for your custom argument.
7778

7879
```
7980
prototype(My.Custom:Object) < prototype(Flowpack.Listable:PaginatedCollection) {
@@ -87,7 +88,7 @@ prototype(My.Custom:Object) < prototype(Flowpack.Listable:PaginatedCollection) {
8788
entryDiscriminator = ${request.arguments.currentPage + request.arguments.date}
8889
}
8990
90-
}
91+
}
9192
```
9293

9394
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.
@@ -138,6 +139,7 @@ Configuration options:
138139
| itemClass | A total count of items | 'Pagination-item' |
139140
| currentItemClass | A class for a current item | 'isCurrent' |
140141
| currentPage | Current page, starting with 1 | `${request.arguments.currentPage || 1}` |
142+
| showPreviousNextLinks| Boolean value used to decide whether the previous and next links should be added| false |
141143

142144
# FlowQuery Helpers you can use
143145

Resources/Private/Fusion/PaginatedCollection.fusion

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ prototype(Flowpack.Listable:PaginatedCollection) < prototype(Neos.Fusion:Compone
66
collection = 'must-be-set'
77
itemsPerPage = 24
88
maximumNumberOfLinks = 15
9+
showPreviousNextLinks = false
910
listRenderer = 'Flowpack.Listable:Collection'
1011

1112
renderer = Neos.Fusion:Array {
@@ -39,6 +40,7 @@ prototype(Flowpack.Listable:PaginatedCollection) < prototype(Neos.Fusion:Compone
3940
totalCount = ${data.totalCount}
4041
maximumNumberOfLinks = ${props.maximumNumberOfLinks}
4142
itemsPerPage = ${props.itemsPerPage}
43+
showPreviousNextLinks = ${props.showPreviousNextLinks}
4244
}
4345
}
4446

Resources/Private/Fusion/Pagination.fusion

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ prototype(Flowpack.Listable:PaginationArray) {
44
maximumNumberOfLinks = ''
55
totalCount = ''
66
itemsPerPage = ''
7+
showPreviousNextLinks = false
78
}
89

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

1618
class = 'Pagination'
1719
itemClass = 'Pagination-item'
@@ -26,6 +28,7 @@ prototype(Flowpack.Listable:Pagination) < prototype(Neos.Fusion:Component) {
2628
maximumNumberOfLinks = ${props.maximumNumberOfLinks}
2729
totalCount = ${props.totalCount}
2830
itemsPerPage = ${props.itemsPerPage}
31+
showPreviousNextLinks = ${props.showPreviousNextLinks}
2932
}
3033
itemName = 'i'
3134
itemRenderer = Neos.Fusion:Case {
@@ -37,8 +40,36 @@ prototype(Flowpack.Listable:Pagination) < prototype(Neos.Fusion:Component) {
3740
condition = ${String.toInteger(i) == String.toInteger(props.currentPage)}
3841
renderer = ${'<li class="' + props.itemClass + ' ' + props.currentItemClass + '"><a>' + i + '</a></li>'}
3942
}
43+
previous {
44+
condition = ${i == 'previous' && (props.showPreviousNextLinks == true)}
45+
renderer = Neos.Fusion:Tag {
46+
@process.tmpl = ${'<li class="previous">' + value + '</li>'}
47+
tagName = 'a'
48+
attributes.href = Neos.Neos:NodeUri {
49+
node = ${documentNode}
50+
additionalParams = Flowpack.Listable:PaginationParameters {
51+
currentPage = ${String.toInteger(props.currentPage) - 1}
52+
}
53+
}
54+
content = ${i}
55+
}
56+
}
57+
next {
58+
condition = ${i == 'next' && (props.showPreviousNextLinks == true)}
59+
renderer = Neos.Fusion:Tag {
60+
@process.tmpl = ${'<li class="next">' + value + '</li>'}
61+
tagName = 'a'
62+
attributes.href = Neos.Neos:NodeUri {
63+
node = ${documentNode}
64+
additionalParams = Flowpack.Listable:PaginationParameters {
65+
currentPage = ${String.toInteger(props.currentPage) + 1}
66+
}
67+
}
68+
content = ${i}
69+
}
70+
}
4071
link {
41-
condition = ${true}
72+
condition = ${(iterator.isFirst == false && iterator.isLast == false) || (props.showPreviousNextLinks == false)}
4273
renderer = Neos.Fusion:Tag {
4374
@process.tmpl = ${'<li class="' + props.itemClass + '">' + value + '</li>'}
4475
tagName = 'a'

0 commit comments

Comments
 (0)