Skip to content

Commit

Permalink
Add additional flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
adambinnersley committed Oct 7, 2024
1 parent 3bc4cc4 commit c065fb7
Showing 1 changed file with 62 additions and 6 deletions.
68 changes: 62 additions & 6 deletions src/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ class Navigation
*/
public $navigationClass = 'nav navbar-nav';

/**
* The class a li item should have
* @var string
*/
public $itemClass = '';

/**
* The class that should be given to all a elements
* @var string
*/
public $linkClass = '';

/**
* The HTML ID assigned to the menu item
* @var string
Expand Down Expand Up @@ -169,6 +181,50 @@ public function getActiveClass()
return $this->activeClass;
}

/**
* Returns the class given to link items
* @return string
*/
public function getItemClass()
{
return $this->itemClass;
}

/**
* Sets a class to assign to all link items
* @param string $className
* @return $this
*/
public function setItemClass($className)
{
if (Check::checkIfStringSet($className)) {
$this->itemClass = trim($className);
}
return $this;
}

/**
* Gets the class assigned to all link elements
* @return string
*/
public function getLinkClass()
{
return $this->linkClass;
}

/**
* Sets the class to assign to all link elements
* @param string $className
* @return $this
*/
public function setLinkClass($className)
{
if (Check::checkIfStringSet($className)) {
$this->linkClass = trim($className);
}
return $this;
}

/**
* Sets the class(es) for the HTML navigation item
* @param string $classes This should be the class or lasses that you want to give to the navigation item
Expand Down Expand Up @@ -357,7 +413,7 @@ public function createNavigation($levels = 0, $startLevel = 0)
*/
protected function buildMenu($it, $text, $link, $levels, $start)
{
$current = Check::checkIfSet($this->checkLinkOffsetMatch($link, $it->getDepth()), ' class="'.$this->getActiveClass().'"');
$current = Check::checkIfSet($this->checkLinkOffsetMatch($link, $it->getDepth()), ' '.$this->getActiveClass());

if ($start === 0 || $this->checkLinkOffsetMatch($link, ($start - 1)) || $this->sub === true) {
if ($start !== 0 && $this->checkLinkOffsetMatch($link, ($start - 1))) {
Expand All @@ -380,7 +436,7 @@ protected function nextItem($it, $start, $current)
if ($this->sub === true && ($start - 1) == $it->getDepth()) {
$this->sub = false;
} else {
$this->navItem.= Check::greaterThanOrEqual($this->linkCount, 1, '</li>').'<li'.$current.'>';
$this->navItem.= Check::greaterThanOrEqual($this->linkCount, 1, '</li>').'<li class="'.$this->getItemClass().$current.'">';
}
}

Expand All @@ -402,7 +458,7 @@ protected function buildItem($it, $text, $link, $start, $current)
$this->nextItem($it, $start, $current);
} elseif ($this->currentLevel < $it->getDepth()) {
for ($i = $this->currentLevel; $i < $it->getDepth(); $i++) {
$this->navItem.= '<ul'.Check::checkIfSet($this->getDropDownClass(), ' class="'.$this->getDropDownClass().'"').'><li'.$current.'>';
$this->navItem.= '<ul'.Check::checkIfSet($this->getDropDownClass(), ' class="'.$this->getDropDownClass().'"').'><li class="'.$this->getItemClass().$current.'">';
}
} else {
for ($i = $it->getDepth(); $i < $this->currentLevel; $i++) {
Expand All @@ -411,7 +467,7 @@ protected function buildItem($it, $text, $link, $start, $current)
$this->nextItem($it, $start, $current);
}
} elseif ($start === 0 || ($this->sub === true && $it->getDepth() >= $start)) {
$this->navItem.= '<li'.$current.'>';
$this->navItem.= '<li class="'.$this->getItemClass().$current.'">';
}

$this->currentLevel = $it->getDepth();
Expand All @@ -437,7 +493,7 @@ protected function closeLevel()
protected function createLinkItem($link, $text, $start, $depth)
{
if ($this->checkLinkOffsetMatch($link, 0) || $this->checkLinkOffsetMatch($link, 1) || $this->checkLinkOffsetMatch($link, 2) || $this->checkLinkOffsetMatch($link, 3)) {
$current = (($start >= 1 && $this->checkLinkOffsetMatch($link, 0) && isset($this->current[1]['link'])) ? '' : ' class="'.$this->getActiveClass().'"');
$current = (($start >= 1 && $this->checkLinkOffsetMatch($link, 0) && isset($this->current[1]['link'])) ? '' : ' '.$this->getActiveClass().'');
} else {
$current = '';
}
Expand All @@ -447,7 +503,7 @@ protected function createLinkItem($link, $text, $start, $depth)
$href = ' href="'.$link.'"';
}
$this->linkCount++;
return '<a'.$href.' title="'.$text.'"'.$current.'>'.$this->textWrap($text, $depth).'</a>';
return '<a'.$href.' title="'.$text.'" class="'.$this->getLinkClass().$current.'">'.$this->textWrap($text, $depth).'</a>';
}

/**
Expand Down

0 comments on commit c065fb7

Please sign in to comment.