1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Giuga\LaravelNovaSidebar; |
4
|
|
|
|
5
|
|
|
use Giuga\LaravelNovaSidebar\Traits\Makeable; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Laravel\Nova\Tool; |
8
|
|
|
|
9
|
|
|
class NovaSidebar extends Tool |
10
|
|
|
{ |
11
|
|
|
use Makeable; |
12
|
|
|
|
13
|
|
|
public Collection $linkGroups; |
|
|
|
|
14
|
|
|
public Collection $links; |
15
|
|
|
|
16
|
|
|
public function __construct($component = null) |
17
|
|
|
{ |
18
|
|
|
parent::__construct($component); |
19
|
|
|
$this->links = collect([]); |
20
|
|
|
$this->linkGroups = collect([]); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Perform any tasks that need to happen when the tool is booted. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function boot() |
29
|
|
|
{ |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Build the view that renders the navigation links for the tool. |
34
|
|
|
* |
35
|
|
|
* @return \Illuminate\View\View |
36
|
|
|
*/ |
37
|
|
|
public function renderNavigation() |
38
|
|
|
{ |
39
|
|
|
return view('nova-sidebar::navigation', ['groups' => $this->linkGroups, 'links' => $this->links]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Add a new sidebar group. |
44
|
|
|
* @param SidebarGroup $group |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function addGroup(SidebarGroup $group): self |
48
|
|
|
{ |
49
|
|
|
$this->linkGroups->add($group); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Add a standalone link to the sidebar. |
56
|
|
|
* @param SidebarLink $link |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function addLink(SidebarLink $link): self |
60
|
|
|
{ |
61
|
|
|
$this->links->add($link); |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Hydrate the menu without creation of classes manually. |
68
|
|
|
* @param array $data |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function hydrate(array $data): self |
72
|
|
|
{ |
73
|
|
|
if (count($data)) { |
74
|
|
|
foreach ($data as $key => $links) { |
75
|
|
|
if (is_array($links)) { |
76
|
|
|
$group = new SidebarGroup(); |
77
|
|
|
$group->setName($key); |
78
|
|
|
foreach ($links as $link) { |
79
|
|
|
$type = '_blank'; |
80
|
|
|
if (count($link) == 3) { |
81
|
|
|
[$name, $url, $type] = $link; |
82
|
|
|
} else { |
83
|
|
|
[$name, $url] = $link; |
84
|
|
|
} |
85
|
|
|
$group->addLink((new SidebarLink())->setName($name)->setUrl($url)->setType($type)); |
86
|
|
|
} |
87
|
|
|
$this->addGroup($group); |
88
|
|
|
} else { |
89
|
|
|
$this->addLink((new SidebarLink())->setName($key)->setUrl($links)); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|