Skip to content

Commit

Permalink
MenuControl: added $levelLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed May 13, 2024
1 parent 87bafdb commit d714fbc
Showing 1 changed file with 54 additions and 13 deletions.
67 changes: 54 additions & 13 deletions src/MenuControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class MenuControl extends BaseControl
/** @var int|NULL */
private $subLevel;

/** @var int|NULL */
private $levelLimit = 1;

/** @var string */
private $templateFile;

Expand Down Expand Up @@ -88,6 +91,17 @@ public function setSubLevel($subLevel)
}


/**
* @param int|NULL $levelLimit
* @return self
*/
public function setLevelLimit($levelLimit)
{
$this->levelLimit = $levelLimit > 0 ? $levelLimit : NULL;
return $this;
}


/**
* @param string[]|NULL $ignoredPages
* @return self
Expand Down Expand Up @@ -122,7 +136,41 @@ public function render()
return;
}

foreach ($this->navigation->getMenuPages() as $pageId => $page) {
$menuPages = $this->navigation->getMenuPages();
$this->prepareItems($items, $menuPages, $subTree);

$template = $this->createTemplate();
assert($template instanceof \Nette\Bridges\ApplicationLatte\Template);

foreach ($this->templateParameters as $paramName => $paramValue) {
$template->{$paramName} = $paramValue;
}

$template->items = $items;
$template->linkGenerator = new DefaultLinkGenerator($this->getPresenter(), $template->basePath);
$template->render($this->templateFile);
}


/**
* @param array<array{
* page: NavigationPage,
* active: bool,
* level: int,
* }> $items
* @param array<string, NavigationPage> $menuPages
* @param string $subTree
* @param int $level
* @return void
*/
private function prepareItems(
array &$items,
array $menuPages,
$subTree,
$level = 0
)
{
foreach ($menuPages as $pageId => $page) {
if (isset($this->ignoredPages[$pageId])) {
continue;
}
Expand All @@ -140,21 +188,14 @@ public function render()
$items[] = [
'page' => $page,
'active' => $active,
'level' => 0,
'level' => $level,
];
}
}

$template = $this->createTemplate();
assert($template instanceof \Nette\Bridges\ApplicationLatte\Template);

foreach ($this->templateParameters as $paramName => $paramValue) {
$template->{$paramName} = $paramValue;
if ($active && ($this->levelLimit === NULL || ($level + 1) < $this->levelLimit)) {
$this->prepareItems($items, $menuPages, $pageId, $level + 1);
}
}
}

$template->items = $items;
$template->linkGenerator = new DefaultLinkGenerator($this->getPresenter(), $template->basePath);
$template->render($this->templateFile);
}


Expand Down

0 comments on commit d714fbc

Please sign in to comment.