Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
71c068c
Update todo abstract
emmadesilva Mar 24, 2024
cd791af
Convert todo into deprecation
emmadesilva Mar 24, 2024
f45eb19
Deprecate accessor for deprecated property
emmadesilva Mar 24, 2024
48d945d
Deprecate the group paramters
emmadesilva Mar 24, 2024
51f5620
Remove tests for deprecated group accessor
emmadesilva Mar 24, 2024
9a893d1
Replace ternary expression with if/else block
emmadesilva Mar 24, 2024
769744f
Introduce local variable
emmadesilva Mar 24, 2024
2fafd0f
Get group key through page instance
emmadesilva Mar 24, 2024
ba03fb8
Replace conditional with ternary expression
emmadesilva Mar 24, 2024
2527648
Add clarifying parentheses
emmadesilva Mar 24, 2024
0eda65f
Use helper method with essentially the same semantics
emmadesilva Mar 24, 2024
1343543
Get group through the connected page
emmadesilva Mar 24, 2024
d362701
Remove the group property
emmadesilva Mar 24, 2024
35c2cd8
Remove group parameter documentation
emmadesilva Mar 24, 2024
fbb7b3b
No longer construct the group
emmadesilva Mar 24, 2024
daef828
Remove the group parameter from item constructor
emmadesilva Mar 24, 2024
91d9cac
Remove coalesce assignment for the removed group
emmadesilva Mar 24, 2024
8cad7f2
Remove the group parameter from item create method
emmadesilva Mar 24, 2024
8c37e94
Fix wrong order of parameter annotations
emmadesilva Mar 24, 2024
13298c4
Introduce local variable
emmadesilva Mar 24, 2024
81f2e03
Add type annotation
emmadesilva Mar 24, 2024
8ecc783
Simplify local variable name
emmadesilva Mar 24, 2024
4f3ba21
Add some spacing
emmadesilva Mar 24, 2024
be0820e
Use assert same
emmadesilva Mar 24, 2024
80f9a5c
Inline method usage
emmadesilva Mar 24, 2024
660edfc
Extract testing helper method for data access
emmadesilva Mar 24, 2024
c2254b0
Extract testing helper method for data access
emmadesilva Mar 24, 2024
524e1ea
Update DocumentationSidebarGetActiveGroupUnitTest.php
emmadesilva Mar 24, 2024
c52a912
Move up test in source
emmadesilva Mar 24, 2024
09b909f
Use fluent method call
emmadesilva Mar 24, 2024
749d41c
Remove `getGroupKey` method from `NavigationElement` contract
emmadesilva Mar 24, 2024
6b67552
Remove `getGroupKey` method from `NavigationItem` class
emmadesilva Mar 24, 2024
c359bca
Move group key normalization method to navigation group class
emmadesilva Mar 24, 2024
74933b8
Remove null support from group key normalization method
emmadesilva Mar 24, 2024
eb346e9
Add null type check
emmadesilva Mar 24, 2024
de6993b
Mark group key normalization method as experimental
emmadesilva Mar 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function getActiveGroup(): ?NavigationGroup

return $this->items->first(function (NavigationGroup $group) use ($currentPage): bool {
// A group is active when it contains the current page being rendered.
return $group->getGroupKey() === NavigationItem::normalizeGroupKey($currentPage->navigationMenuGroup());
return $currentPage->navigationMenuGroup() && $group->getGroupKey() === NavigationGroup::normalizeGroupKey($currentPage->navigationMenuGroup());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,4 @@ public function getLabel(): string;
* Get the priority to determine the order of the navigation item.
*/
public function getPriority(): int;

/**
* Get the group identifier key of the navigation item, if any.
*/
public function getGroupKey(): ?string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ protected function containsOnlyDocumentationPages(): bool
return $child->getPage() instanceof DocumentationPage;
});
}

/** @experimental This method is subject to change before its release. */
public static function normalizeGroupKey(string $group): string
{
return Str::slug($group);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Hyde\Foundation\Facades\Routes;
use Hyde\Hyde;
use Hyde\Support\Models\Route;
use Illuminate\Support\Str;
use Stringable;

use function is_string;
Expand All @@ -27,36 +26,29 @@ class NavigationItem implements NavigationElement, Stringable
protected string $label;
protected int $priority;

// TODO: Do we actually need this? We should just care if it's physically stored in a group.
protected ?string $group = null;

/**
* Create a new navigation menu item with your own properties.
*
* @param \Hyde\Support\Models\Route|string $destination Route instance, route key, or external URI.
* @param string $label The label of the navigation item.
* @param int $priority The priority to determine the order of the navigation item.
* @param string|null $group The dropdown/group key of the navigation item, if any.
*/
public function __construct(Route|string $destination, string $label, int $priority = NavigationMenu::DEFAULT, ?string $group = null)
public function __construct(Route|string $destination, string $label, int $priority = NavigationMenu::DEFAULT)
{
$this->destination = $destination;

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

$this->group = static::normalizeGroupKey($group);
}

/**
* Create a new navigation menu item, automatically filling in the properties from a Route instance if provided.
*
* @param \Hyde\Support\Models\Route|string<\Hyde\Support\Models\RouteKey>|string $destination Route instance or route key, or external URI.
* @param int|null $priority Leave blank to use the priority of the route's corresponding page, if there is one tied to the route.
* @param string|null $label Leave blank to use the label of the route's corresponding page, if there is one tied to the route.
* @param string|null $group Leave blank to use the group of the route's corresponding page, if there is one tied to the route.
* @param int|null $priority Leave blank to use the priority of the route's corresponding page, if there is one tied to the route.
*/
public static function create(Route|string $destination, ?string $label = null, ?int $priority = null, ?string $group = null): static
public static function create(Route|string $destination, ?string $label = null, ?int $priority = null): static
{
if (is_string($destination) && Routes::has($destination)) {
$destination = Routes::get($destination);
Expand All @@ -65,10 +57,9 @@ public static function create(Route|string $destination, ?string $label = null,
if ($destination instanceof Route) {
$label ??= $destination->getPage()->navigationMenuLabel();
$priority ??= $destination->getPage()->navigationMenuPriority();
$group ??= $destination->getPage()->navigationMenuGroup();
}

return new static($destination, $label ?? $destination, $priority ?? NavigationMenu::DEFAULT, $group);
return new static($destination, $label ?? $destination, $priority ?? NavigationMenu::DEFAULT);
}

/**
Expand Down Expand Up @@ -103,19 +94,6 @@ public function getPriority(): int
return $this->priority;
}

/**
* Get the group identifier key of the navigation item, if any.
*
* For sidebars this is the category key, for navigation menus this is the dropdown key.
*
* When using automatic subdirectory based groups, the subdirectory name is the group key.
* Otherwise, the group key is a 'slugified' version of the group's label.
*/
public function getGroupKey(): ?string
{
return $this->group;
}

/**
* If the navigation item is a link to a routed page, get the corresponding page instance.
*/
Expand All @@ -131,10 +109,4 @@ public function isActive(): bool
{
return Hyde::currentRoute()->getLink() === $this->getLink();
}

/** @return ($group is null ? null : string) */
public static function normalizeGroupKey(?string $group): ?string
{
return $group ? Str::slug($group) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Hyde\Hyde;
use Hyde\Facades\Config;
use Illuminate\Support\Str;
use Hyde\Support\Models\Route;
use Hyde\Pages\DocumentationPage;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -135,7 +134,8 @@ protected function addRouteToGroup(Route $route): void
{
$item = NavigationItem::create($route);

$groupName = $this->generatesSidebar ? ($item->getGroupKey() ?? 'Other') : $item->getGroupKey();
$groupKey = $item->getPage()->navigationMenuGroup();
$groupName = $this->generatesSidebar ? ($groupKey ?? 'Other') : $groupKey;

$groupItem = $this->getOrCreateGroupItem($groupName);

Expand All @@ -148,7 +148,7 @@ protected function addRouteToGroup(Route $route): void

protected function getOrCreateGroupItem(string $groupName): NavigationGroup
{
$groupKey = Str::slug($groupName);
$groupKey = NavigationGroup::normalizeGroupKey($groupName);
$group = $this->items->get($groupKey);

if ($group instanceof NavigationGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1284,21 +1284,19 @@ protected function sidebar(?array $withPages = null): AssertableNavigationMenu
class TestNavigationItem
{
public readonly string $label;
public readonly ?string $group;
public readonly int $priority;
public readonly array $children;

public function __construct(string $label, ?string $group, int $priority, array $children)
public function __construct(string $label, int $priority, array $children)
{
$this->label = $label;
$this->group = $group;
$this->priority = $priority;
$this->children = collect($children)->map(fn (NavigationItem|NavigationGroup $child) => $child->getLabel())->toArray();
}

public static function properties(): array
{
return ['label', 'group', 'priority', 'children'];
return ['label', 'priority', 'children'];
}
}

Expand All @@ -1320,7 +1318,7 @@ public function __construct(TestCase $test, $sidebar = false)
public function state(): array
{
return $this->items->map(function (NavigationItem|NavigationGroup $item): TestNavigationItem {
return new TestNavigationItem($item->getLabel(), $item->getGroupKey(), $item->getPriority(), $item instanceof NavigationGroup ? $item->getItems() : []);
return new TestNavigationItem($item->getLabel(), $item->getPriority(), $item instanceof NavigationGroup ? $item->getItems() : []);
})->toArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ public function testSidebarPrioritiesCanBeSetInBothFrontMatterAndConfig()
public function testGroupCanBeSetInFrontMatter()
{
$this->makePage('foo', ['navigation.group' => 'bar']);
$this->assertEquals('bar', collect(NavigationMenuGenerator::handle(DocumentationSidebar::class)->getItems()->first()->getItems())->first()->getGroupKey());

/** @var NavigationItem $item */
$item = collect(NavigationMenuGenerator::handle(DocumentationSidebar::class)->getItems()->first()->getItems())->first();
$this->assertSame('bar', $item->getPage()->navigationMenuGroup());
}

public function testHasGroupsReturnsFalseWhenThereAreNoGroups()
Expand Down Expand Up @@ -222,23 +225,23 @@ public function testIsGroupActiveReturnsFalseWhenSuppliedGroupIsNotActive()
{
Render::setPage(new DocumentationPage(matter: ['navigation.group' => 'foo']));
$mainNavigationMenu = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertFalse('bar' === $mainNavigationMenu->getActiveGroup()?->getGroupKey());
$this->assertFalse('bar' === $this->getGroupKey($mainNavigationMenu));
}

public function testIsGroupActiveReturnsTrueWhenSuppliedGroupIsActive()
{
$this->makePage('foo', ['navigation.group' => 'foo']);
Render::setPage(new DocumentationPage(matter: ['navigation.group' => 'foo']));
$mainNavigationMenu = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertTrue('foo' === $mainNavigationMenu->getActiveGroup()?->getGroupKey());
$this->assertTrue('foo' === $this->getGroupKey($mainNavigationMenu));
}

public function testIsGroupActiveReturnsTrueForDifferingCasing()
{
$this->makePage('foo', ['navigation.group' => 'Foo Bar']);
Render::setPage(new DocumentationPage(matter: ['navigation.group' => 'Foo Bar']));
$mainNavigationMenu = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertTrue('foo-bar' === $mainNavigationMenu->getActiveGroup()?->getGroupKey());
$this->assertTrue('foo-bar' === $this->getGroupKey($mainNavigationMenu));
}

public function testIsGroupActiveReturnsTrueFirstGroupOfIndexPage()
Expand All @@ -250,11 +253,11 @@ public function testIsGroupActiveReturnsTrueFirstGroupOfIndexPage()

Render::setPage(DocumentationPage::get('index'));
$mainNavigationMenu2 = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertTrue('bar' === $mainNavigationMenu2->getActiveGroup()?->getGroupKey());
$this->assertTrue('bar' === $this->getGroupKey($mainNavigationMenu2));
$mainNavigationMenu1 = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertFalse('foo' === $mainNavigationMenu1->getActiveGroup()?->getGroupKey());
$this->assertFalse('foo' === $this->getGroupKey($mainNavigationMenu1));
$mainNavigationMenu = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertFalse('baz' === $mainNavigationMenu->getActiveGroup()?->getGroupKey());
$this->assertFalse('baz' === $this->getGroupKey($mainNavigationMenu));
}

public function testIsGroupActiveReturnsTrueFirstSortedGroupOfIndexPage()
Expand All @@ -266,11 +269,11 @@ public function testIsGroupActiveReturnsTrueFirstSortedGroupOfIndexPage()

Render::setPage(DocumentationPage::get('index'));
$mainNavigationMenu2 = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertTrue('foo' === $mainNavigationMenu2->getActiveGroup()?->getGroupKey());
$this->assertTrue('foo' === $this->getGroupKey($mainNavigationMenu2));
$mainNavigationMenu1 = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertFalse('bar' === $mainNavigationMenu1->getActiveGroup()?->getGroupKey());
$this->assertFalse('bar' === $this->getGroupKey($mainNavigationMenu1));
$mainNavigationMenu = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertFalse('baz' === $mainNavigationMenu->getActiveGroup()?->getGroupKey());
$this->assertFalse('baz' === $this->getGroupKey($mainNavigationMenu));
}

public function testAutomaticIndexPageGroupExpansionRespectsCustomNavigationMenuSettings()
Expand All @@ -282,11 +285,11 @@ public function testAutomaticIndexPageGroupExpansionRespectsCustomNavigationMenu

Render::setPage(DocumentationPage::get('index'));
$mainNavigationMenu2 = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertFalse('foo' === $mainNavigationMenu2->getActiveGroup()?->getGroupKey());
$this->assertFalse('foo' === $this->getGroupKey($mainNavigationMenu2));
$mainNavigationMenu1 = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertFalse('bar' === $mainNavigationMenu1->getActiveGroup()?->getGroupKey());
$this->assertFalse('bar' === $this->getGroupKey($mainNavigationMenu1));
$mainNavigationMenu = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertTrue('baz' === $mainNavigationMenu->getActiveGroup()?->getGroupKey());
$this->assertTrue('baz' === $this->getGroupKey($mainNavigationMenu));
}

public function testCanHaveMultipleGroupedPagesWithTheSameNameLabels()
Expand Down Expand Up @@ -335,7 +338,7 @@ public function testIsGroupActiveForIndexPageWithNoGroups()

Render::setPage(DocumentationPage::get('index'));
$mainNavigationMenu = NavigationMenuGenerator::handle(DocumentationSidebar::class);
$this->assertFalse('foo' === $mainNavigationMenu->getActiveGroup()?->getGroupKey());
$this->assertFalse('foo' === $this->getGroupKey($mainNavigationMenu));
}

public function testIndexPageAddedToSidebarWhenItIsTheOnlyPage()
Expand Down Expand Up @@ -377,4 +380,9 @@ protected function makePage(string $name, ?array $matter = null): void
(new ConvertsArrayToFrontMatter)->execute($matter ?? [])
);
}

protected function getGroupKey(DocumentationSidebar $menu): ?string
{
return $menu->getActiveGroup()?->getGroupKey();
}
}
Loading