Skip to content

Commit dbda4c2

Browse files
committed
Render pagination links in collection panel
1 parent 1b453f8 commit dbda4c2

File tree

4 files changed

+148
-18
lines changed

4 files changed

+148
-18
lines changed

DashboardPanelCollection.module

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,13 @@ class DashboardPanelCollection extends DashboardPanel
188188
// Buttons (add new)
189189
$buttons = $this->renderFooterButtons();
190190
if ($buttons) {
191-
$footer .= "<div>{$buttons}</div>";
191+
$footer .= "<div class='DashboardFooterButtons'>{$buttons}</div>";
192192
}
193193

194194
// Pagination
195-
if ($this->pagination && $this->collection->hasPagination()) {
196-
$pagination = sprintf(
197-
$this->_('Showing %d of %d'),
198-
$this->collection->count,
199-
$this->collection->getTotal()
200-
);
201-
$footer .= "<div>{$pagination}</div>";
195+
if ($this->supportsPagination()) {
196+
$pagination = $this->renderPaginationPager($this->collection);
197+
$footer .= "<div class='DashboardFooterPagination'>{$pagination}</div>";
202198
}
203199

204200
return $footer ? "<div>{$footer}</div>" : '';
@@ -256,15 +252,9 @@ class DashboardPanelCollection extends DashboardPanel
256252
}
257253
}
258254

259-
// Update collection after changes
260-
// e.g. when pages trashed
255+
// Update collection after changes, e.g. when pages trashed
261256
if ($changed) {
262-
if ($this->collection instanceof PageArray) {
263-
$selectors = $this->collection->getSelectors();
264-
if ($selectors) {
265-
$this->collection = $this->pages->find($selectors, ['cache' => false]);
266-
}
267-
}
257+
$this->refetchCollection();
268258
}
269259
}
270260

@@ -308,11 +298,80 @@ class DashboardPanelCollection extends DashboardPanel
308298

309299
if ($this->config->ajax) {
310300
$this->executeAjaxActions();
301+
$this->refetchCollectionForPagination();
311302
}
312303

313304
return true;
314305
}
315306

307+
protected function supportsPagination()
308+
{
309+
return (
310+
$this->pagination &&
311+
$this->collection instanceof PageArray &&
312+
$this->collection->hasPagination() &&
313+
$this->collection->getSelectors()
314+
);
315+
}
316+
317+
protected function refetchCollection()
318+
{
319+
if (!($this->collection instanceof PageArray)) {
320+
return;
321+
}
322+
323+
/** @var \ProcessWire\Selectors $selectors */
324+
$selectors = $this->collection->getSelectors();
325+
if (!$selectors) {
326+
return;
327+
}
328+
329+
$this->collection = $this->pages->find($selectors, ['cache' => false]);
330+
}
331+
332+
protected function refetchCollectionForPagination()
333+
{
334+
if (!$this->config->ajax) return;
335+
if (!$this->supportsPagination()) return;
336+
337+
$this->wire()->input->setPageNum($_POST['page'] ?? 1);
338+
$this->refetchCollection();
339+
}
340+
341+
protected function renderPaginationSummary(PageArray $items)
342+
{
343+
return sprintf(
344+
$this->_('Showing %d of %d'),
345+
$items->count,
346+
$items->getTotal()
347+
);
348+
}
349+
350+
protected function renderPaginationPager(PageArray $items)
351+
{
352+
/** @var \ProcessWire\MarkupPagerNav $pager */
353+
$pager = $this->wire()->modules->get('MarkupPagerNav');
354+
355+
$summary = $items->getPaginationString();
356+
$pagination = $pager->render($items, [
357+
'numPageLinks' => 3,
358+
'listClass' => 'uk-pagination',
359+
'listMarkup' => "<ul class='uk-pagination DashboardPagination'>{out}</ul>",
360+
'linkMarkup' => "<a href='{url}' data-pagination>{out}</a>",
361+
'currentItemClass' => 'uk-active',
362+
'separatorItemLabel' => '<span>&hellip;</span>',
363+
'separatorItemClass' => 'DashboardPaginationSeparator',
364+
'currentLinkMarkup' => "<a href='{url}' data-pagination>{out}</a>",
365+
'nextItemLabel' => '<i class="fa fa-angle-right"></i>',
366+
'previousItemLabel' => '<i class="fa fa-angle-left"></i>',
367+
'nextItemClass' => '',
368+
'previousItemClass' => '',
369+
'lastItemClass' => '',
370+
]);
371+
372+
return "<div>{$summary}</div> {$pagination}";
373+
}
374+
316375
/**
317376
* Render footer buttons
318377
* Taken from PageLister, so it should work for most cases.

src/Dashboard.css

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
--dashboard-color-text-light: #97aab4;
2020
--dashboard-color-icon: #97aab4;
2121
--dashboard-color-button-bg: #f0f3f7;
22+
--dashboard-color-button-border: #d7dadf;
2223
--dashboard-color-button-text: #354b60;
2324
--dashboard-color-button-hover-bg: #6c8dae;
2425
--dashboard-color-button-hover-text: #fff;
@@ -410,3 +411,59 @@ body.Dashboard {
410411
}
411412
}
412413
}
414+
415+
/* Pagination */
416+
417+
.DashboardPagination {
418+
margin: 0;
419+
> * > * {
420+
background: var(--dashboard-color-button-bg);
421+
color: var(--dashboard-color-button-text);
422+
border-color: var(--dashboard-color-button-border);
423+
transition: background 200ms, color 200ms, border 200ms;
424+
}
425+
> * > :hover,
426+
> * > :focus {
427+
background: var(--dashboard-color-button-hover-bg);
428+
color: var(--dashboard-color-button-hover-text);
429+
border-color: var(--dashboard-color-button-hover-bg);
430+
}
431+
> .uk-active > *,
432+
> .uk-active > * > * {
433+
background: var(--dashboard-color-button-hover-bg);
434+
color: var(--dashboard-color-button-hover-text);
435+
border-color: var(--dashboard-color-button-hover-bg);
436+
}
437+
}
438+
439+
.DashboardPaginationSeparator {
440+
pointer-events: none;
441+
}
442+
443+
.DashboardFooterButtons {
444+
margin: -.5em;
445+
display: flex;
446+
flex-wrap: wrap;
447+
> * {
448+
display: inline-block;
449+
margin: .5em;
450+
}
451+
.ui-button {
452+
margin-right: 0;
453+
}
454+
}
455+
456+
.DashboardFooterPagination {
457+
display: flex;
458+
flex-wrap: wrap;
459+
align-items: baseline;
460+
justify-content: flex-end;
461+
margin-bottom: -.5em;
462+
margin-right: -1em;
463+
margin-left: auto;
464+
padding-left: 1em;
465+
> * {
466+
margin-bottom: .5em;
467+
margin-right: 1em;
468+
}
469+
}

src/DashboardPanelCollection.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
> div {
1414
display: flex;
1515
justify-content: space-between;
16-
align-items: center;
16+
align-items: baseline;
1717
}
1818
}
1919

src/DashboardPanelCollection.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33
function initPanel($panel) {
44
const $reloadlinks = $panel.find('.pw-modal[data-reload-on-close]');
55
if ($reloadlinks.length) {
6+
const page = $panel.data('page') || 1;
7+
const params = { page };
68
$reloadlinks.on('pw-modal-closed', () => {
7-
$panel.trigger('reload', { animate: true });
9+
$panel.trigger('reload', { animate: false, params });
10+
});
11+
}
12+
13+
const $paginationLinks = $panel.find('a[data-pagination]');
14+
if ($paginationLinks.length) {
15+
$paginationLinks.on('click', (event) => {
16+
event.preventDefault();
17+
const url = event.currentTarget.href;
18+
const page = (url.match(/\d+$/) || [])[0] || 1;
19+
const params = { page };
20+
$panel.data('page', page);
21+
$panel.trigger('reload', { animate: false, params });
822
});
923
}
1024

0 commit comments

Comments
 (0)