Paginator: introducing "..." pseudo-pages

If delta between pages in paginator is greater than 2, it adds "..." inbetween.
If delta is equal to 2, it adds missing page link instead.

Examples:

    1,4,5 gets converted to 1,...,4,5
    1,3,4 gets converted to 1,2,3,4
This commit is contained in:
Marcin Kurczewski 2013-11-22 08:52:24 +01:00
parent 467f0c6b93
commit 6e9a18c0ae
2 changed files with 50 additions and 33 deletions

View file

@ -10,12 +10,12 @@
.paginator li {
display: inline-block;
margin-right: 0.5em;
}
.paginator li a {
display: inline-block;
padding: 0.2em 0.5em;
margin-right: 0.5em;
background: #eee;
border: 1px solid silver;
color: black;

View file

@ -1,13 +1,27 @@
<?php
$pagesVisible = [];
$pagesVisible []= 1;
$pagesVisible []= $this->context->transport->paginator->pageCount;
$page = $this->context->transport->paginator->page;
$pageCount = $this->context->transport->paginator->pageCount;
$delta = 3;
$pagesVisible = array_merge($pagesVisible, range($this->context->transport->paginator->page - $delta, $this->context->transport->paginator->page + $delta));
$pagesVisible = array_filter($pagesVisible, function($x) { return $x >= 1 and $x <= $this->context->transport->paginator->pageCount; });
$pagesVisible = [1, $pageCount];
$pagesVisible = array_merge($pagesVisible, range($page - $delta, $page + $delta));
$pagesVisible = array_filter($pagesVisible, function($x) use ($pageCount) { return $x >= 1 and $x <= $pageCount; });
$pagesVisible = array_unique($pagesVisible);
sort($pagesVisible, SORT_NUMERIC);
$finalPages = [$pagesVisible[0]];
for ($i = 1; $i < count($pagesVisible); $i ++)
{
$prevPage = $pagesVisible[$i - 1];
$subPage = $pagesVisible[$i];
if ($subPage - $prevPage == 2)
$finalPages []= $subPage - 1;
elseif ($subPage - $prevPage > 2)
$finalPages []= null;
$finalPages []= $subPage;
}
$pagesVisible = $finalPages;
if (!function_exists('pageUrl'))
{
function pageUrl($page)
@ -15,8 +29,7 @@
$context = \Chibi\Registry::getContext();
$controller = $context->route->simpleControllerName;
$action = $context->route->simpleActionName;
$page = max(1, $page);
$page = min($context->transport->paginator->pageCount, $page);
$page = max(1, min($context->transport->paginator->pageCount, $page));
$params = $context->route->arguments;
$params['page'] = $page;
return \Chibi\UrlHelper::route($controller, $action, $params);
@ -27,34 +40,38 @@
<?php if (!empty($pagesVisible)): ?>
<nav class="paginator-wrapper">
<ul class="paginator">
<?php if ($this->context->transport->paginator->page > 1): ?>
<?php if ($page > 1): ?>
<li class="prev">
<?php else: ?>
<li class="prev disabled">
<?php endif ?>
<a href="<?php echo pageUrl($this->context->transport->paginator->page - 1) ?>">
<a href="<?php echo pageUrl($page - 1) ?>">
&laquo;
</a>
</li>
<?php foreach ($pagesVisible as $page): ?>
<?php if ($page == $this->context->transport->paginator->page): ?>
<?php foreach ($pagesVisible as $subPage): ?>
<?php if ($subPage === null): ?>
<li>&hellip;</li>
<?php else: ?>
<?php if ($subPage == $page): ?>
<li class="active">
<?php else: ?>
<li>
<?php endif ?>
<a href="<?php echo pageUrl($page) ?>">
<?php echo $page ?>
<a href="<?php echo pageUrl($subPage) ?>">
<?php echo $subPage ?>
</a>
</li>
<?php endif ?>
<?php endforeach ?>
<?php if ($this->context->transport->paginator->page < $this->context->transport->paginator->pageCount): ?>
<?php if ($page < $pageCount): ?>
<li class="next">
<?php else: ?>
<li class="next disabled">
<?php endif ?>
<a href="<?php echo pageUrl($this->context->transport->paginator->page + 1) ?>">
<a href="<?php echo pageUrl($page + 1) ?>">
&raquo;
</a>
</li>