Skip to content

Commit

Permalink
Merge pull request #115 from Icinga/add-selector
Browse files Browse the repository at this point in the history
Add selector
  • Loading branch information
lippserd authored Dec 19, 2024
2 parents 48fcb37 + 486cb24 commit c96ccd7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
92 changes: 92 additions & 0 deletions library/Kubernetes/Web/Selectors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Icinga\Module\Kubernetes\Web;

use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\I18n\Translation;
use ipl\Stdlib\Str;
use ipl\Web\Widget\EmptyState;
use ipl\Web\Widget\HorizontalKeyValue;

use function Icinga\Module\Kubernetes\yield_iterable;

class Selectors extends BaseHtmlElement
{
use Translation;

protected iterable $selectors;

protected $tag = 'section';

protected $defaultAttributes = ['class' => 'selectors'];

public function __construct(iterable $selectors)
{
$this->selectors = $selectors;
}

protected function assemble(): void
{
$this->addHtml(new HtmlElement('h2', null, new Text($this->translate('Selectors'))));

$selectors = yield_iterable($this->selectors);
$content = [];
if ($selectors->valid()) {
foreach ($this->categorizeSelectors($selectors) as $title => $selectors) {
$content[] = new HtmlElement(
'li',
null,
new HtmlElement(
'span',
new Attributes(['class' => 'title']),
new Text($title)
),
new HtmlElement(
'ul',
null,
...array_map(
fn($name, $value) => new HtmlElement(
'li',
null,
new HorizontalKeyValue($name, $value)
),
array_keys($selectors),
$selectors
)
)
);
}

$this->addHtml(
new HtmlElement(
'div',
new Attributes([
'class' => 'collapsible',
'data-visible-height' => 100
]),
new HtmlElement('ul', null, ...$content)
)
);
} else {
$this->addHtml(new EmptyState($this->translate('No items to display.')));
}
}

private function categorizeSelectors(iterable $selectors): array
{
$categorized = [];

foreach ($selectors as $selector) {
[$prefix, $name] = Str::symmetricSplit($selector->name, '/', 2);
$group = $name ? $prefix : '-';
$categorized[$group][$name ?: $prefix] = $selector->value;
}

uksort($categorized, fn($a, $b) => $a === '-' ? 1 : ($b === '-' ? -1 : strcmp($a, $b)));

return $categorized;
}
}
1 change: 1 addition & 0 deletions library/Kubernetes/Web/ServiceDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ protected function assemble(): void
])),
new Labels($this->service->label),
new Annotations($this->service->annotation),
new Selectors($this->service->selector),
new PortTable($this->service->port, (new ServicePort())->getColumnDefinitions()),
new EndpointTable($endpointSlices->endpoint, (new Endpoint())->getColumnDefinitions()),
new ServiceEnvironment($this->service)
Expand Down
2 changes: 1 addition & 1 deletion public/css/labels.less
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.labels {
.labels, .selectors {
ul {
list-style-type: none;
margin: 0;
Expand Down
1 change: 1 addition & 0 deletions public/css/widgets.less
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
.details,
.labels,
.annotations,
.selectors,
.state {
.horizontal-key-value > .key {
width: 25em;
Expand Down

0 comments on commit c96ccd7

Please sign in to comment.