Skip to content

Commit ea3d13d

Browse files
authored
Merge pull request #1577 from hydephp/simplify-dropdown-data-handling
[2.x] Simplify dropdown data handling
2 parents 4bcfbeb + e2e0f2d commit ea3d13d

File tree

6 files changed

+90
-36
lines changed

6 files changed

+90
-36
lines changed

packages/framework/src/Framework/Features/Navigation/GeneratesDocumentationSidebarMenu.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Hyde\Framework\Features\Navigation;
66

7+
use Hyde\Hyde;
78
use Hyde\Facades\Config;
89
use Illuminate\Support\Str;
910
use Hyde\Support\Models\Route;
@@ -12,7 +13,9 @@
1213
use Hyde\Foundation\Facades\Routes;
1314
use Hyde\Foundation\Kernel\RouteCollection;
1415

16+
use function filled;
1517
use function collect;
18+
use function strtolower;
1619

1720
/**
1821
* @experimental This class may change significantly before its release.
@@ -121,12 +124,30 @@ protected function getOrCreateGroupItem(string $groupName): NavItem
121124
protected function createGroupItem(string $identifier, string $groupName): NavItem
122125
{
123126
$label = $this->searchForGroupLabelInConfig($identifier) ?? $groupName;
127+
$priority = $this->searchForGroupPriorityInConfig($identifier);
124128

125-
return NavItem::dropdown($label, []);
129+
return NavItem::dropdown(static::normalizeGroupLabel($label), [], $priority);
126130
}
127131

128132
protected function searchForGroupLabelInConfig(string $identifier): ?string
129133
{
130134
return Config::getArray('docs.sidebar_group_labels', [])[$identifier] ?? null;
131135
}
136+
137+
/** Todo: Move into shared class */
138+
protected static function normalizeGroupLabel(string $label): string
139+
{
140+
// If there is no label, and the group is a slug, we can make a title from it
141+
if ($label === strtolower($label)) {
142+
return Hyde::makeTitle($label);
143+
}
144+
145+
return $label;
146+
}
147+
148+
/** Todo: Move into shared class */
149+
protected static function searchForGroupPriorityInConfig(string $groupKey): ?int
150+
{
151+
return Config::getArray('docs.sidebar_order', [])[$groupKey] ?? null;
152+
}
132153
}

packages/framework/src/Framework/Features/Navigation/GeneratesMainNavigationMenu.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace Hyde\Framework\Features\Navigation;
66

7+
use Hyde\Hyde;
78
use Hyde\Facades\Config;
89
use Hyde\Support\Models\Route;
910
use Hyde\Pages\DocumentationPage;
1011
use Illuminate\Support\Collection;
1112
use Hyde\Foundation\Facades\Routes;
1213

1314
use function collect;
15+
use function strtolower;
1416

1517
/**
1618
* @experimental This class may change significantly before its release.
@@ -72,7 +74,7 @@ protected function moveGroupedItemsIntoDropdowns(): void
7274

7375
foreach ($dropdowns as $group => $items) {
7476
// Create a new dropdown item containing the buffered items
75-
$this->items->add(NavItem::dropdown($group, $items));
77+
$this->items->add(NavItem::dropdown(static::normalizeGroupLabel($group), $items, static::searchForDropdownPriorityInConfig($group)));
7678
}
7779
}
7880

@@ -90,4 +92,21 @@ protected function useSubdirectoriesAsDropdowns(): bool
9092
{
9193
return Config::getString('hyde.navigation.subdirectories', 'hidden') === 'dropdown';
9294
}
95+
96+
/** Todo: Move into shared class */
97+
protected static function normalizeGroupLabel(string $label): string
98+
{
99+
// If there is no label, and the group is a slug, we can make a title from it
100+
if ($label === strtolower($label)) {
101+
return Hyde::makeTitle($label);
102+
}
103+
104+
return $label;
105+
}
106+
107+
/** Todo: Move into shared class */
108+
protected static function searchForDropdownPriorityInConfig(string $groupKey): ?int
109+
{
110+
return Config::getArray('hyde.navigation.order', [])[$groupKey] ?? null;
111+
}
93112
}

packages/framework/src/Framework/Features/Navigation/NavItem.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Hyde\Framework\Features\Navigation;
66

7-
use Hyde\Facades\Config;
87
use Hyde\Foundation\Facades\Routes;
98
use Hyde\Hyde;
109
use Hyde\Support\Models\Route;
@@ -13,7 +12,6 @@
1312
use Hyde\Support\Models\ExternalRoute;
1413

1514
use function is_string;
16-
use function strtolower;
1715

1816
/**
1917
* Abstraction for a navigation menu item. Used by the MainNavigationMenu and DocumentationSidebar classes.
@@ -100,7 +98,7 @@ public static function forRoute(Route|string $route, ?string $label = null, ?int
10098
*/
10199
public static function dropdown(string $label, array $items, ?int $priority = null): static
102100
{
103-
return new static('', static::normalizeGroupLabel($label), $priority ?? static::searchForDropdownPriorityInNavigationConfig($label) ?? 999, $label, $items);
101+
return new static('', $label, $priority ?? 999, $label, $items);
104102
}
105103

106104
/**
@@ -208,20 +206,4 @@ protected static function makeIdentifier(string $label): string
208206
{
209207
return Str::slug($label); // Todo: If it's a dropdown based on a subdirectory, we should use the subdirectory as the identifier
210208
}
211-
212-
// TODO: Consider moving all of these to a dropdown factory
213-
protected static function searchForDropdownPriorityInNavigationConfig(string $groupKey): ?int
214-
{
215-
return Config::getArray('hyde.navigation.order', [])[$groupKey] ?? null;
216-
}
217-
218-
protected static function normalizeGroupLabel(string $label): string
219-
{
220-
// If there is no label, and the group is a slug, we can make a title from it
221-
if ($label === strtolower($label)) {
222-
return Hyde::makeTitle($label);
223-
}
224-
225-
return $label;
226-
}
227209
}

packages/framework/tests/Feature/AutomaticNavigationConfigurationsTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,50 @@ public function testAllSidebarItemsArePlacedInGroupsWhenAtLeastOneItemIsGrouped(
10381038
]);
10391039
}
10401040

1041+
// Priority tests
1042+
1043+
public function testMainNavigationDropdownPriorityCanBeSetInConfig()
1044+
{
1045+
config(['hyde.navigation.subdirectories' => 'dropdown']);
1046+
config(['hyde.navigation.order' => ['foo' => 500]]);
1047+
1048+
$this->assertMenuEquals(
1049+
[['label' => 'Foo', 'priority' => 500]],
1050+
[new MarkdownPage('foo/bar')]
1051+
);
1052+
}
1053+
1054+
public function testMainNavigationDropdownPriorityCanBeSetInConfigUsingDifferingCases()
1055+
{
1056+
config(['hyde.navigation.subdirectories' => 'dropdown']);
1057+
config(['hyde.navigation.order' => ['hello-world' => 500]]);
1058+
1059+
$expected = [['label' => 'Hello World', 'priority' => 500]];
1060+
$this->assertMenuEquals($expected, [new MarkdownPage('Hello World/bar')]);
1061+
$this->assertMenuEquals($expected, [new MarkdownPage('hello-world/bar')]);
1062+
$this->assertMenuEquals($expected, [new MarkdownPage('hello world/bar')]);
1063+
}
1064+
1065+
public function testSidebarGroupPriorityCanBeSetInConfig()
1066+
{
1067+
config(['docs.sidebar_order' => ['foo' => 500]]);
1068+
1069+
$this->assertSidebarEquals(
1070+
[['label' => 'Foo', 'priority' => 500]],
1071+
[new DocumentationPage('foo/bar')]
1072+
);
1073+
}
1074+
1075+
public function testSidebarGroupPriorityCanBeSetInConfigUsingDifferingCases()
1076+
{
1077+
config(['docs.sidebar_order' => ['hello-world' => 500]]);
1078+
1079+
$expected = [['label' => 'Hello World', 'priority' => 500]];
1080+
$this->assertSidebarEquals($expected, [new DocumentationPage('Hello World/bar')]);
1081+
$this->assertSidebarEquals($expected, [new DocumentationPage('hello-world/bar')]);
1082+
$this->assertSidebarEquals($expected, [new DocumentationPage('hello world/bar')]);
1083+
}
1084+
10411085
// Label casing tests
10421086

10431087
public function testMainMenuNavigationItemCasing()

packages/framework/tests/Feature/NavigationMenuTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public function testPagesInSubdirectoriesAreNotAddedToTheNavigationMenuWithConfi
228228
$menu = $this->createNavigationMenu();
229229
$expected = collect([
230230
NavItem::fromRoute(Routes::get('index')),
231-
NavItem::dropdown('foo', [
231+
NavItem::dropdown('Foo', [
232232
NavItem::fromRoute(Routes::get('foo/bar')),
233233
]),
234234
]);
@@ -249,7 +249,7 @@ public function testPagesInDropdownsDoNotGetAddedToTheMainNavigation()
249249
$this->assertEquals([
250250
NavItem::fromRoute(Routes::get('index')),
251251
NavItem::fromRoute((new MarkdownPage('foo'))->getRoute()),
252-
NavItem::dropdown('bar', [
252+
NavItem::dropdown('Bar', [
253253
NavItem::fromRoute((new MarkdownPage('bar/baz'))->getRoute()),
254254
]),
255255
], $menu->getItems()->all());

packages/framework/tests/Unit/NavItemTest.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function testDropdownFacade()
265265
{
266266
$item = NavItem::dropdown('foo', []);
267267

268-
$this->assertSame('Foo', $item->getLabel());
268+
$this->assertSame('foo', $item->getLabel());
269269
$this->assertSame([], $item->getChildren());
270270
$this->assertSame(999, $item->getPriority());
271271
}
@@ -396,16 +396,4 @@ public function testCanAddItemToDropdown()
396396

397397
$this->assertSame([$child], $parent->getChildren());
398398
}
399-
400-
public function testDefaultDropdownItemPriority()
401-
{
402-
$this->assertSame(999, NavItem::dropdown('foo', [])->getPriority());
403-
}
404-
405-
public function testCanResolveDropdownItemPriorityFromConfig()
406-
{
407-
$this->mockConfig(['hyde.navigation.order' => ['foo' => 500]]);
408-
409-
$this->assertSame(500, NavItem::dropdown('foo', [])->getPriority());
410-
}
411399
}

0 commit comments

Comments
 (0)