Skip to content

Commit

Permalink
refs #2875 Fixed collapsing behavior for nested menu
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Aug 13, 2024
1 parent 55b33c0 commit 1a0496c
Showing 1 changed file with 55 additions and 20 deletions.
75 changes: 55 additions & 20 deletions src/Screen/Actions/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,27 @@
class Menu extends Link
{
/**
* The view associated with this menu item.
*
* @var string
*/
protected $view = 'platform::actions.menu';

/**
* Determines whether the menu item should be displayed based on permissions.
*
* @var bool
*/
protected $permit = true;

/**
* Default attributes value.
* 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',
'class' => 'nav-link d-flex align-items-center collapsed icon-link',
'title' => null,
'icon' => null,
'href' => null,
Expand Down Expand Up @@ -74,6 +79,7 @@ class Menu extends Link

/**
* Menu constructor.
* Initializes the menu and sets default behaviors for rendering.
*/
public function __construct()
{
Expand All @@ -88,6 +94,7 @@ public function __construct()
$slug = $this->getSlug();

$this
->set('slug', $slug)
->set('data-bs-toggle', 'collapse')
->set('href', '#menu-'.$slug);
})
Expand All @@ -96,7 +103,7 @@ public function __construct()
return;
}

$active = collect([])
$active = collect()
->merge($this->get('list'))
->map(fn (Menu $menu) => $menu->get('href'))
->push($this->get('href'))
Expand All @@ -112,15 +119,21 @@ public function __construct()
});
}

/**
* 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'))));
return $this->get('slug', Str::slug($this->get('name')));
}

/**
* @param Actionable[] $list
* Sets a list of sub-menu items for this menu item.
*
* @return $this
* @param Actionable[] $list The array of sub-menu items.
* @return $this The current Menu instance for method chaining.
*/
public function list(array $list): self
{
Expand All @@ -135,19 +148,23 @@ public function list(array $list): self
}

/**
* @throws \Throwable
* Builds and renders the menu view.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
* @param Repository|null $repository The data repository to use for rendering.
* @throws \Throwable If rendering fails.
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed The rendered view.
*/
public function build(?Repository $repository = null)
{
$this->set('source', $repository);

return $this->render();
}

/**
* @return $this
* 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
{
Expand All @@ -160,26 +177,30 @@ public function badge(\Closure $badge, Color $color = Color::PRIMARY): self
}

/**
* @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);
}

/**
* @param string|string[] $permission
* Sets the permission(s) required to see the menu item.
*
* @return $this
* @param string|string[]|null $permission The required permission(s).
* @return $this The current Menu instance for method chaining.
*/
public function permission($permission = null): self
public function permission(string|iterable $permission = null): self
{
$user = Auth::user();

if ($permission !== null) {
$this->permit = false;
}

$user = Auth::user();

if ($user === null) {
return $this;
}
Expand All @@ -189,29 +210,43 @@ public function permission($permission = null): self
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;
}

/**
* @return $this
* 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);
}

/**
* @return $this
* 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);
}

/**
* @return $this
* 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
{
Expand Down

0 comments on commit 1a0496c

Please sign in to comment.