1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Features\Navigation; |
6
|
|
|
|
7
|
|
|
use BadMethodCallException; |
8
|
|
|
use function config; |
9
|
|
|
use Hyde\Pages\DocumentationPage; |
10
|
|
|
use Hyde\Pages\MarkdownPost; |
11
|
|
|
use function in_array; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @see \Hyde\Framework\Testing\Feature\NavigationMenuTest |
15
|
|
|
*/ |
16
|
|
|
class NavigationMenu extends BaseNavigationMenu |
17
|
|
|
{ |
18
|
|
|
public function generate(): static |
19
|
|
|
{ |
20
|
|
|
parent::generate(); |
21
|
|
|
|
22
|
|
|
if ($this->dropdownsEnabled()) { |
23
|
|
|
$this->putGroupedItemsInDropdowns(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function putGroupedItemsInDropdowns(): void |
30
|
|
|
{ |
31
|
|
|
$dropdowns = []; |
32
|
|
|
|
33
|
|
|
/** @var \Hyde\Framework\Features\Navigation\NavItem $item */ |
34
|
|
|
foreach ($this->items as $item) { |
35
|
|
|
if ($this->canBeInDropdown($item)) { |
36
|
|
|
// Buffer the item in the dropdowns array |
37
|
|
|
$dropdowns[$item->getGroup()][] = $item; |
38
|
|
|
|
39
|
|
|
// Remove the item from the main items collection |
40
|
|
|
$this->items->forget($item->route->getRouteKey()); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($dropdowns as $group => $items) { |
45
|
|
|
// Create a new dropdown item containing the buffered items |
46
|
|
|
$this->items->put("dropdown.$group", new DropdownNavItem($group, $items)); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function hasDropdowns(): bool |
51
|
|
|
{ |
52
|
|
|
if (! $this->dropdownsEnabled()) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return count($this->getDropdowns()) >= 1; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** @return array<string, DropdownNavItem> */ |
60
|
|
|
public function getDropdowns(): array |
61
|
|
|
{ |
62
|
|
|
if (! $this->dropdownsEnabled()) { |
63
|
|
|
throw new BadMethodCallException('Dropdowns are not enabled. Enable it by setting `hyde.navigation.subdirectories` to `dropdown`.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->items->filter(function (NavItem $item): bool { |
67
|
|
|
return $item instanceof DropdownNavItem; |
68
|
|
|
})->all(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected static function canBeInDropdown(NavItem $item): bool |
72
|
|
|
{ |
73
|
|
|
return ($item->getGroup() !== null) && ! in_array($item->route->getPageClass(), [DocumentationPage::class, MarkdownPost::class]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected static function dropdownsEnabled(): bool |
77
|
|
|
{ |
78
|
|
|
return config('hyde.navigation.subdirectories', 'hidden') === 'dropdown'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected static function shouldItemBeHidden(NavItem $item): bool |
82
|
|
|
{ |
83
|
|
|
return parent::shouldItemBeHidden($item) || |
84
|
|
|
$item->getRoute()?->getPage() instanceof DocumentationPage && ! $item->getRoute()->is('docs/index'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|