-
-
Notifications
You must be signed in to change notification settings - Fork 671
Expand file tree
/
Copy pathMenu.php
More file actions
266 lines (236 loc) · 6.81 KB
/
Menu.php
File metadata and controls
266 lines (236 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
declare(strict_types=1);
namespace Orchid\Screen\Actions;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Orchid\Screen\Contracts\Actionable;
use Orchid\Screen\Repository;
use Orchid\Support\Color;
/**
* Class Menu
*
* @method Menu divider(bool $enabled = true)
* @method Menu icon(string $icon = null)
* @method Menu class(string $classes = null)
* @method Menu parameters(array|object $name)
* @method Menu target(string $target = null)
* @method Menu download($download = true)
* @method Menu href(string $url = true)
* @method Menu sort(int $weight = 1)
* @method Menu active(string|array $active = null)
*/
class Menu extends Link
{
/**
* The view associated with this menu item.
*
* @var string
*/
protected $view = 'orchid::actions.menu';
/**
* Determines whether the menu item should be displayed based on permissions.
*
* @var bool
*/
protected $permit = true;
/**
* Default attributes for the menu item.
* This includes CSS classes, title, icon, URL, and other options.
*
* @var array
*/
protected $attributes = [
'class' => 'nav-link d-flex align-items-center collapsed icon-link gap-3',
'title' => null,
'icon' => null,
'href' => null,
'turbo' => true,
'list' => [],
'source' => null,
'divider' => false,
'active' => null,
'data-bs-toggle' => null,
'parent' => null,
'sort' => 0,
'slug' => null,
];
/**
* Attributes available for a particular tag.
*
* @var array
*/
public $inlineAttributes = [
'autofocus',
'disabled',
'tabindex',
'href',
'target',
'title',
'download',
'data-bs-toggle',
];
/**
* Menu constructor.
* Initializes the menu and sets default behaviors for rendering.
*/
public function __construct()
{
$this
->addBeforeRender(function () {
$href = $this->get('href');
if ($href !== null) {
return;
}
$slug = $this->getSlug();
$this
->set('slug', $slug)
->set('data-bs-toggle', 'collapse')
->set('href', '#menu-'.$slug);
})
->addBeforeRender(function () {
if ($this->get('active') !== null) {
return;
}
$active = collect()
->merge($this->get('list'))
->map(fn (Menu $menu) => $menu->get('href'))
->push($this->get('href'))
->filter()
->map(fn ($href) => [
$href,
$href.'?*',
$href.'/*',
])
->flatten();
$this->set('active', $active->toArray());
});
}
/**
* Generates a slug for the menu item based on its name.
*
* @return string The generated slug.
*/
protected function getSlug(): string
{
return $this->get('slug', Str::slug($this->get('name')));
}
/**
* Sets a list of sub-menu items for this menu item.
*
* @param Actionable[] $list The array of sub-menu items.
*
* @return $this The current Menu instance for method chaining.
*/
public function list(array $list): self
{
$default = $this->get('list', []);
$subMenu = collect()
->merge($default)
->merge($list)
->sort(fn (Menu $menu) => $menu->get('sort', 0));
return $this->set('list', $subMenu);
}
/**
* Builds and renders the menu view.
*
* @param Repository|null $repository The data repository to use for rendering.
*
* @throws \Throwable If rendering fails.
*
* @return Factory|View|mixed The rendered view.
*/
public function build(?Repository $repository = null)
{
return $this->render();
}
/**
* Adds a badge to the menu item with a specific color.
*
* @param \Closure $badge The closure to generate the badge content.
* @param Color $color The color of the badge.
*
* @return $this The current Menu instance for method chaining.
*/
public function badge(\Closure $badge, Color $color = Color::PRIMARY): self
{
$this->set('badge', [
'class' => $color->name(),
'data' => $badge,
]);
return $this;
}
/**
* Sets the URL (href attribute) for the menu item.
*
* @param string $url The URL to set.
*
* @return $this The current Menu instance for method chaining.
*/
public function url(string $url): self
{
return $this->set('href', $url);
}
/**
* Sets the permission(s) required to see the menu item.
*
* @param string|string[]|null $permission The required permission(s).
*
* @return $this The current Menu instance for method chaining.
*/
public function permission(string|iterable|null $permission = null): self
{
if ($permission !== null) {
$this->permit = false;
}
$user = Auth::user();
if ($user === null) {
return $this;
}
$this->permit = $user->hasAnyAccess($permission);
return $this;
}
/**
* Determines whether the menu item should be displayed based on permissions and visibility conditions.
*
* @return bool True if the menu item should be displayed, otherwise false.
*/
public function isSee(): bool
{
return parent::isSee() && $this->permit;
}
/**
* Sets the title for the menu item.
*
* @param string|null $title The title to set.
*
* @return $this The current Menu instance for method chaining.
*/
public function title(?string $title = null): self
{
return $this->set('title', $title);
}
/**
* Sets the slug for the menu item.
*
* @param string $slug The slug to set.
*
* @return $this The current Menu instance for method chaining.
*/
public function slug(string $slug): self
{
return $this->set('slug', $slug);
}
/**
* Sets the parent menu item for this menu item.
*
* @param string $parent The parent menu item slug or identifier.
*
* @return $this The current Menu instance for method chaining.
*/
public function parent(string $parent): self
{
return $this->set('parent', $parent);
}
}