Skip to content
Merged
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Hyde\Framework\Features\Navigation;

use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Hyde\Pages\DocumentationPage;

use function min;
Expand All @@ -14,20 +13,17 @@
/**
* Abstraction for a grouped navigation menu item, like a dropdown or a sidebar group.
*/
class NavigationGroup
class NavigationGroup extends NavigationMenu
{
use HasNavigationItems;

protected string $label;
protected int $priority;

public function __construct(string $label, array $items = [], int $priority = NavigationMenu::LAST)
{
$this->items = new Collection();
parent::__construct($items);

$this->label = $label;
$this->priority = $priority;

$this->add($items);
}

public static function create(string $label, array $items = [], int $priority = NavigationMenu::LAST): static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Features\Navigation;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Support\Arrayable;

Expand All @@ -19,15 +20,51 @@
*/
class NavigationMenu
{
use HasNavigationItems;

public const DEFAULT = 500;
public const LAST = 999;

/** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> */
protected Collection $items;

public function __construct(Arrayable|array $items = [])
{
$this->items = new Collection();

$this->add(evaluate_arrayable($items));
}

/**
* Get the navigation items in the menu.
*
* Items are automatically sorted by their priority, falling back to the order they were added.
*
* @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup>
*/
public function getItems(): Collection
{
// The reason we sort them here is that navigation items can be added from different sources,
// so any sorting we do in generator actions will only be partial. This way, we can ensure
// that the items are always freshly sorted by their priorities when they are retrieved.

return $this->items->sortBy(fn (NavigationItem|NavigationGroup $item) => $item->getPriority())->values();
}

/**
* Add one or more navigation items to the navigation menu.
*
* @param \Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup|array<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> $items
*/
public function add(NavigationItem|NavigationGroup|array $items): static
{
foreach (Arr::wrap($items) as $item) {
$this->addItem($item);
}

return $this;
}

protected function addItem(NavigationItem|NavigationGroup $item): void
{
$this->items->push($item);
}
}