Skip to content

Commit

Permalink
MenuItem: Add support for common MenuItem interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Aug 8, 2021
1 parent 1436656 commit 76a45fb
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function render(): void
'user' => $this->user,
'panels' => $this->panels,
'plugins' => $this->plugins,
'menuLinks' => $this->menu->getItems(),
'menuItems' => $this->menu->getItems(),
'debugMode' => $this->debugMode,
'enableVue' => $this->enableVue,
];
Expand Down
18 changes: 15 additions & 3 deletions src/Menu/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@

final class Menu
{
/** @var array<string, array<int, MenuLink|null>> */
/** @var array<string, array<int, MenuItem|null>> */
private array $items = [];


public function addLink(string $label, string $url, ?string $group = null): void
{
$this->addItem(new MenuLink($label, $url), $group);
}


public function addEvent(string $label, string $event, ?string $group = null): void
{
$this->addItem(new MenuEvent($label, $event), $group);
}


public function addItem(MenuItem $item, ?string $group = null): void
{
try {
$this->items[$this->registerGroup($group)][] = new MenuLink($label, $url);
$this->items[$this->registerGroup($group)][] = $item;
} catch (\InvalidArgumentException $e) {
trigger_error(__METHOD__ . ': ' . $e->getMessage());
}
Expand All @@ -28,7 +40,7 @@ public function addSeparator(?string $group = null): void


/**
* @return array<string, array<int, MenuLink|null>>
* @return array<string, array<int, MenuItem|null>>
*/
public function getItems(): array
{
Expand Down
52 changes: 52 additions & 0 deletions src/Menu/MenuEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Baraja\AdminBar\Menu;


use Baraja\AdminBar\Helpers;
use Nette\Utils\Strings;

final class MenuEvent extends MenuItem
{
private string $label;

private string $event;


public function __construct(string $label, string $event)
{
if ($event === '') {
throw new \InvalidArgumentException('Event is required.');
}
if (preg_match('/^[a-zA-Z0-9-.]+$/', $event) !== 1) {
throw new \InvalidArgumentException(
'Event format is not valid, because haystack "' . $event . '" given.',
);
}

$this->label = Strings::firstUpper($label);
$this->event = $event;
}


public function render(): string
{
return '<a onclick="eventBus.$emit(\'' . Helpers::escapeHtml($this->getEvent()) . '\')">'
. Helpers::escapeHtml($this->getLabel())
. '</a>';
}


public function getLabel(): string
{
return $this->label;
}


public function getEvent(): string
{
return $this->event;
}
}
20 changes: 20 additions & 0 deletions src/Menu/MenuItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Baraja\AdminBar\Menu;


abstract class MenuItem implements \Stringable
{
final public function __toString(): string
{
return $this->render();
}


/**
* Safe render of HTML content.
*/
abstract public function render(): string;
}
9 changes: 8 additions & 1 deletion src/Menu/MenuLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace Baraja\AdminBar\Menu;


use Baraja\AdminBar\Helpers;
use Nette\Utils\Strings;
use Nette\Utils\Validators;

final class MenuLink
final class MenuLink extends MenuItem
{
private string $label;

Expand All @@ -30,6 +31,12 @@ public function __construct(string $label, string $url)
}


public function render(): string
{
return '<a href="' . $this->getUrl() . '">' . Helpers::escapeHtml($this->getLabel()) . '</a>';
}


public function getLabel(): string
{
return $this->label;
Expand Down
16 changes: 8 additions & 8 deletions src/assets/content.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ declare(strict_types=1);
namespace Baraja\AdminBar;


use Baraja\AdminBar\Menu\MenuLink;
use Baraja\AdminBar\Menu\MenuItem;
use Baraja\AdminBar\Panel\Panel;
use Baraja\AdminBar\Plugin\Plugin;
use Baraja\AdminBar\User\User;

/**
* @var array<string, array<int, MenuLink|null>> $menuLinks
* @var array<string, array<int, MenuItem|null>> $menuItems
* @var string $basePath
* @var User|null $user
* @var Panel[] $panels
Expand Down Expand Up @@ -77,17 +77,17 @@ use Baraja\AdminBar\User\User;
echo '<hr class="w-100 my-2">';
}

$menuLinksIterator = 0;
foreach ($menuLinks as $menuLinkGroup) {
if ($menuLinksIterator++ > 0) {
$menuItemsIterator = 0;
foreach ($menuItems as $menuItemGroup) {
if ($menuItemsIterator++ > 0) {
echo '<hr class="w-100 my-2">';
}
foreach ($menuLinkGroup as $menuLink) {
if ($menuLink === null) {
foreach ($menuItemGroup as $menuItem) {
if ($menuItem === null) {
echo '<hr class="w-100 my-2">';
} else {
echo '<li class="text-left">';
echo '<a href="' . $menuLink->getUrl() . '" class="ml-2">' . Helpers::escapeHtml($menuLink->getLabel()) . '</a>';
echo '<div class="ml-2">' . $menuItem->render() . '</div>';
echo '</li>';
}
}
Expand Down

0 comments on commit 76a45fb

Please sign in to comment.