-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseItemList.php
110 lines (90 loc) · 2.86 KB
/
BaseItemList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/* Icinga for Kubernetes Web | (c) 2023 Icinga GmbH | AGPLv3 */
namespace Icinga\Module\Kubernetes\Common;
use ipl\Html\BaseHtmlElement;
use ipl\I18n\Translation;
use ipl\Stdlib\BaseFilter;
use ipl\Stdlib\Filter;
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use ipl\Web\Widget\EmptyState;
use Ramsey\Uuid\Uuid;
abstract class BaseItemList extends BaseHtmlElement
{
use BaseFilter;
use Translation;
use ViewMode;
/**
* Indicates whether the item list should be treated as an action list.
*
* @var bool $actionList
*/
protected bool $actionList = true;
protected iterable $query;
protected array $baseAttributes = [
'class' => 'item-list',
'data-base-target' => '_next',
'data-pdfexport-page-breaks-at' => '.list-item'
];
protected $tag = 'ul';
public function __construct(iterable $query)
{
$this->query = $query;
$this->init();
}
/**
* Enable or disable the action list functionality by setting the $actionList
* property.
*
* @param bool $actionList
*
* @return static
*/
public function setActionList(bool $actionList): static
{
$this->actionList = $actionList;
return $this;
}
/**
* Initialize the item list
* If you want to adjust the item list after construction, override this method.
*/
protected function init(): void
{
}
protected function assemble(): void
{
$detailUrlAdded = ! $this->actionList;
$itemClass = $this->getItemClass();
$this->addAttributes($this->baseAttributes);
$this->addAttributes(['class' => $this->viewMode]);
foreach ($this->query as $item) {
if (! $detailUrlAdded) {
$this->addAttributes(['class' => 'action-list'] + [
'data-icinga-detail-url' => Url::fromPath(
'kubernetes/' . str_replace('_', '-', $item->getTableAlias())
)
]);
$detailUrlAdded = true;
}
$listItem = (new $itemClass($item, $this))
->addAttributes([
'data-action-item' => true,
'data-icinga-detail-filter' => QueryString::render(
Filter::equal('id', Uuid::fromBytes($item->uuid)->toString())
)
]);
if ($this->viewMode !== null) {
$listItem->setViewMode($this->viewMode);
}
$this->addHtml(
$listItem
);
}
if ($this->isEmpty()) {
$this->setTag('div');
$this->addHtml(new EmptyState($this->translate('No items to display.')));
}
}
abstract protected function getItemClass(): string;
}