-
Notifications
You must be signed in to change notification settings - Fork 2
Add ViewModeSwitcher control
#392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BastianLedererIcinga
wants to merge
7
commits into
main
Choose a base branch
from
feature/viewmode-switcher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
baef963
Introduce `ViewModeSwitcher`
BastianLedererIcinga 9af4e5e
Introduce `Controls` trait
BastianLedererIcinga d014a88
controls.less: add view-mode-switcher
BastianLedererIcinga 235f4ca
`Controls`: Handle redirects and changes of default limit
BastianLedererIcinga 2395789
`createViewModeSwitcher()`: Throw `InvalidArgumentException` for inva…
BastianLedererIcinga c35e621
remove comma
BastianLedererIcinga 3d73145
Adjust `applyViewModeLimit` to use the tracked `ViewModeSwitcher`
BastianLedererIcinga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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; | ||
| } | ||
| } |
This file contains hidden or 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,157 @@ | ||
| <?php | ||
|
|
||
| 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
|
||
| { | ||
| /** @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); | ||
| } | ||
|
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 { | ||
|
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; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.