-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
!feat(events-incident-history-improvements): event-bound load more tr…
…ait, dynamic result set with soft limit caution when using: this code is currently under heavy development and might change over the next commits
- Loading branch information
Showing
5 changed files
with
398 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
/* Icinga Notifications Web | (c) 2024 Icinga GmbH | GPLv2 */ | ||
|
||
namespace Icinga\Module\Notifications\Common; | ||
|
||
use BadMethodCallException; | ||
use Icinga\Module\Notifications\Widget\ItemList\PageSeparatorItem; | ||
use Icinga\Module\Notifications\Widget\ShowMore; | ||
use ipl\Html\HtmlDocument; | ||
use ipl\Orm\Model; | ||
use ipl\Web\Common\BaseListItem; | ||
use ipl\Web\Url; | ||
|
||
trait BoundLoadMore | ||
{ | ||
/** @var ?int */ | ||
private $lastPageNumber; | ||
|
||
/** @var ?Model */ | ||
private $lastData; | ||
|
||
/** @var int */ | ||
protected $pageSize; | ||
|
||
/** @var int */ | ||
protected $pageNumber; | ||
|
||
/** @var Url */ | ||
protected $loadMoreUrl; | ||
|
||
/** | ||
* Set the page size | ||
* | ||
* @param int $size | ||
* | ||
* @return $this | ||
*/ | ||
public function setPageSize(int $size): self | ||
{ | ||
$this->pageSize = $size; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the url to fetch more items | ||
* | ||
* @param Url $url | ||
* | ||
* @return $this | ||
*/ | ||
public function setLoadMoreUrl(Url $url): self | ||
{ | ||
$this->loadMoreUrl = $url; | ||
|
||
return $this; | ||
} | ||
|
||
protected function bind($entryAddEvent, $listAssembledEvent) | ||
{ | ||
if (! ($this instanceof HtmlDocument && method_exists($this, 'on'))) { | ||
throw new BadMethodCallException( | ||
"The 'LoadMore' trait can only be bound to classes extending the 'ipl\Html\HtmlDocument' class," | ||
. "which in turn needs to implement the event emitter methods (e.g the 'Events' trait)." | ||
); | ||
} | ||
|
||
$this->on($entryAddEvent, function (BaseListItem $entry, Model $data) { | ||
$this->handleEventEntryAdd($entry, $data); | ||
}); | ||
$this->on($listAssembledEvent, function (HtmlDocument $htmlDocument) { | ||
$this->handleEventAssembled($htmlDocument); | ||
}); | ||
} | ||
|
||
protected function handleEventEntryAdd(BaseListItem $entry, Model $data): void | ||
{ | ||
$pageNumber = $this->data->getCurrentPage(); | ||
if ($this->lastPageNumber && $this->lastPageNumber !== $pageNumber) { | ||
$this->add(new PageSeparatorItem($pageNumber)); | ||
} elseif ($this->lastPageNumber === null && $pageNumber > 1) { | ||
$this->add(new PageSeparatorItem($pageNumber)); | ||
} | ||
|
||
$this->lastData = $data; | ||
$this->lastPageNumber = $pageNumber; | ||
} | ||
|
||
protected function handleEventAssembled(HtmlDocument $htmlDocument): void { | ||
if ($this->lastPageNumber !== null && $this->loadMoreUrl !== null) { | ||
$showMore = (new ShowMore( | ||
$this->data, | ||
$this->loadMoreUrl->setParam('lnkIdx', $this->lastData[$this->data->getLinkedIndex()]) | ||
->setParam('page', ($this->lastPageNumber + 1)) | ||
->setAnchor('page-' . ($this->lastPageNumber + 1)) | ||
)) | ||
->setLabel(t('Load More')) | ||
->setAttribute('data-no-icinga-ajax', true); | ||
|
||
$this->add($showMore->setTag('li')->addAttributes(['class' => 'list-item'])); | ||
} | ||
} | ||
} |
Oops, something went wrong.