Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions asset/css/controls.less
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@
}

> .sort-control,
> .view-mode-switcher,
> .limit-control {
float: right;
}

> .view-mode-switcher,
> .limit-control {
margin-right: .5em;
}
Expand Down
42 changes: 42 additions & 0 deletions asset/css/view-mode-switcher.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.view-mode-switcher {
display: flex;

input {
display: none;
}

label {
color: var(--control-color, @control-color);
line-height: 1;
background: var(--low-sat-blue, @low-sat-blue);
padding: 14/16*.25em 14/16*.5em;
font-size: 16/12em;
height: 24/16em; // desired pixel height / font-size
cursor: pointer;

&:first-of-type {
border-top-left-radius: 0.25em;
border-bottom-left-radius: 0.25em;
}

&:last-of-type {
border-top-right-radius: 0.25em;
border-bottom-right-radius: 0.25em;
}

&:not(:last-of-type) {
border-right: 1px solid var(--low-sat-blue-dark, @low-sat-blue-dark);
}

i {
// fix height for Chrome
display: block;
}
}

input[checked] + label {
background-color: var(--control-color, @control-color);
color: var(--text-color-on-icinga-blue, @text-color-on-icinga-blue);
cursor: default;
}
}
157 changes: 157 additions & 0 deletions src/Common/Controls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
Comment thread
sukhwinder33445 marked this conversation as resolved.

namespace ipl\Web\Common;

use Icinga\Web\UrlParams;
use InvalidArgumentException;
use ipl\Html\Form;
use ipl\Web\Compat\CompatController;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\PaginationControl;
use ipl\Web\Control\ViewModeSwitcher;
use ipl\Web\Url;
use Psr\Http\Message\ServerRequestInterface;

/**
* Provides factory methods to prepare reusable web controls and allows to handle their requests
*
* @phpstan-require-extends CompatController
*/
trait Controls

Check failure on line 20 in src/Common/Controls.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.3) / PHPStan 8.3

Trait ipl\Web\Common\Controls is used zero times and is not analysed.

Check failure on line 20 in src/Common/Controls.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.5) / PHPStan 8.5

Trait ipl\Web\Common\Controls is used zero times and is not analysed.

Check failure on line 20 in src/Common/Controls.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.2) / PHPStan 8.2

Trait ipl\Web\Common\Controls is used zero times and is not analysed.

Check failure on line 20 in src/Common/Controls.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.4) / PHPStan 8.4

Trait ipl\Web\Common\Controls is used zero times and is not analysed.
{
/** @var Form[] Controls registered for handling by {@see self::handleControls()} */
private array $trackedControls = [];

/** @var ?Url Url to redirect to in {@see self::handleControls()} */
private ?Url $redirectUrl = null;

/**
* Create a {@see ViewModeSwitcher} control
*
* The control is registered via {@see self::trackControl()} and gets a default {@see Form::ON_SUBMIT} handler
* that writes the chosen view mode to the {@see self::getRedirectUrl()}.
*
* @param UrlParams $params The url params; the view mode param is shifted out of them
* @param ?string $viewModeParam Custom view mode param, null uses the default of the given class
* @param class-string<ViewModeSwitcher> $viewModeSwitcherClass
*
* @return ViewModeSwitcher
*
* @throws InvalidArgumentException
*/
public function createViewModeSwitcher(
UrlParams $params,
?string $viewModeParam = null,
string $viewModeSwitcherClass = ViewModeSwitcher::class
): ViewModeSwitcher {
if (! is_a($viewModeSwitcherClass, ViewModeSwitcher::class, true)) {
throw new InvalidArgumentException(
sprintf('%s is not a subclass of ViewModeSwitcher', $viewModeSwitcherClass)
);
}

$viewModeSwitcher = new $viewModeSwitcherClass();
if ($viewModeParam !== null) {
$viewModeSwitcher->setViewModeParam($viewModeParam);
}
Comment thread
sukhwinder33445 marked this conversation as resolved.

$viewModeSwitcher->populate([
$viewModeSwitcher->getViewModeParam() => $params->shift($viewModeSwitcher->getViewModeParam())
]);

$this->trackControl($viewModeSwitcher);

$viewModeSwitcher->on(ViewModeSwitcher::ON_SUBMIT, function (ViewModeSwitcher $switcher): void {
$this->getRedirectUrl()->setParam($switcher->getViewModeParam(), $switcher->getViewMode());
});

return $viewModeSwitcher;
}

/**
* Double the default item limit and page size for `minimal` view mode
*
* @param LimitControl $limitControl
* @param ?PaginationControl $paginationControl
*
* @return void
*/
protected function applyViewModeLimit(
LimitControl $limitControl,
?PaginationControl $paginationControl = null
): void {
Comment thread
sukhwinder33445 marked this conversation as resolved.
$viewModeSwitcher = $this->getTrackedControl(ViewModeSwitcher::class);
if ($viewModeSwitcher?->getViewMode() === 'minimal') {
$limitControl->setDefaultLimit($limitControl->getDefaultLimit() * 2);

$paginationControl
?->setDefaultPageSize($paginationControl->getDefaultPageSize() * 2)
->apply();
}
}

/**
* Register the given control to be handled by {@see self::handleControls()}
*
* @param Form $control
*
* @return $this
*/
protected function trackControl(Form $control): static
{
$this->trackedControls[] = $control;

return $this;
}

/**
* Get the tracked control of the given type
*
* @template TForm of Form
*
* @param class-string<TForm> $type
*
* @return ?TForm
*/
protected function getTrackedControl(string $type): ?Form
{
foreach ($this->trackedControls as $control) {
if ($control instanceof $type) {
return $control;
}
}

return null;
}

/**
* Get the Url {@see self::handleControls()} redirects to
*
* @return Url
*/
protected function getRedirectUrl(): Url
{
return $this->redirectUrl ??= Url::fromRequest();
}

/**
* Call {@see Form::handleRequest()} on every control in {@see self::$trackedControls}. If {@see self::$redirectUrl}
* has been set, a redirect is performed afterwards.
*
* @param ServerRequestInterface $request
*
* @return $this
*/
protected function handleControls(ServerRequestInterface $request): static
{
foreach ($this->trackedControls as $control) {
$control->handleRequest($request);
}

if ($this->redirectUrl !== null) {
$this->redirectNow($this->redirectUrl);
}

return $this;
}
}
Loading
Loading