Skip to content

Commit

Permalink
[Feature Request] Add ability to control navigation groups order #15498
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeshan committed Feb 1, 2025
1 parent ac21f7d commit b8c9d9b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 42 deletions.
12 changes: 12 additions & 0 deletions packages/panels/src/Navigation/NavigationGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class NavigationGroup extends Component
protected array | Arrayable $items = [];

protected string | Closure | null $label = null;
protected int|Closure|null $order = null; // NEW ORDER PROPERTY

final public function __construct(string | Closure | null $label = null)
{
Expand Down Expand Up @@ -119,4 +120,15 @@ public function isActive(): bool

return false;
}

public function order(int|Closure|null $order = null): static
{
$this->order = $order;
return $this;
}

public function getOrder(): ?int
{
return $this->evaluate($this->order);
}
}
60 changes: 18 additions & 42 deletions packages/panels/src/Navigation/NavigationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,27 @@ public function get(): array
return $this->panel->buildNavigation();
}

if (! $this->isNavigationMounted) {
if (!$this->isNavigationMounted) {
$this->mountNavigation();
}

$groups = collect($this->getNavigationGroups());
$groups = collect($this->getNavigationGroups())
->sortBy(fn(NavigationGroup $group) => $group->getOrder() ?? PHP_INT_MAX); // Sort groups by their order

return collect($this->getNavigationItems())
->filter(fn (NavigationItem $item): bool => $item->isVisible())
->sortBy(fn (NavigationItem $item): int => $item->getSort())
->groupBy(fn (NavigationItem $item): string => $item->getGroup() ?? '')
->sortBy(fn(NavigationItem $item) => $item->getSort())
->groupBy(fn(NavigationItem $item) => $item->getGroup() ?? '')
->map(function (Collection $items, string $groupIndex) use ($groups): NavigationGroup {
$parentItems = $items->groupBy(fn (NavigationItem $item): string => $item->getParentItem() ?? '');

$parentItems = $items->groupBy(fn(NavigationItem $item) => $item->getParentItem() ?? '');

$items = $parentItems->get('')
->keyBy(fn (NavigationItem $item): string => $item->getLabel());
->keyBy(fn(NavigationItem $item) => $item->getLabel())
->sortBy(fn(NavigationItem $item) => $item->getSort() ?? PHP_INT_MAX); // Sort items by their order

$parentItems->except([''])->each(function (Collection $parentItemItems, string $parentItemLabel) use ($items) {
if (! $items->has($parentItemLabel)) {
if (!$items->has($parentItemLabel)) {
return;
}

Expand All @@ -75,56 +78,29 @@ public function get(): array
}

$registeredGroup = $groups
->first(function (NavigationGroup | string $registeredGroup, string | int $registeredGroupIndex) use ($groupIndex) {
if ($registeredGroupIndex === $groupIndex) {
return true;
}

if ($registeredGroup === $groupIndex) {
->first(function (NavigationGroup|string $registeredGroup) use ($groupIndex) {
if ($registeredGroup instanceof NavigationGroup && $registeredGroup->getLabel() === $groupIndex) {
return true;
}

if (! $registeredGroup instanceof NavigationGroup) {
return false;
}

return $registeredGroup->getLabel() === $groupIndex;
return false;
});

if ($registeredGroup instanceof NavigationGroup) {
return $registeredGroup->items($items);
}

return NavigationGroup::make($registeredGroup ?? $groupIndex)
->items($items);
return NavigationGroup::make($groupIndex)->items($items);
})
->sortBy(function (NavigationGroup $group, ?string $groupIndex): int {
->sortBy(function (NavigationGroup $group) {

if (blank($group->getLabel())) {
return -1;
}
return $group->getOrder() ?? PHP_INT_MAX;

$registeredGroups = $this->getNavigationGroups();

$groupsToSearch = $registeredGroups;

if (Arr::first($registeredGroups) instanceof NavigationGroup) {
$groupsToSearch = [
...array_keys($registeredGroups),
...array_map(fn (NavigationGroup $registeredGroup): string => $registeredGroup->getLabel(), array_values($registeredGroups)),
];
}

$sort = array_search(
$groupIndex,
$groupsToSearch,
);

if ($sort === false) {
return count($registeredGroups);
}

return $sort;
})
->values()
->all();
}

Expand Down

0 comments on commit b8c9d9b

Please sign in to comment.