Skip to content

Commit

Permalink
Allow to define active state using 'active' attribute. ref: pingpong-…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gravitano committed Apr 1, 2015
1 parent 03549c8 commit 09ca1ac
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/Pingpong/Menus/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MenuItem implements ArrayableContract {
*
* @var array
*/
protected $fillable = array('url', 'route', 'title', 'name', 'icon', 'parent', 'attributes');
protected $fillable = array('url', 'route', 'title', 'name', 'icon', 'parent', 'attributes', 'active');

/**
* Constructor.
Expand Down Expand Up @@ -281,7 +281,9 @@ public function getProperties()
*/
public function getAttributes()
{
return HTML::attributes($this->attributes);
$attributes = array_forget($this->attributes, 'active');

return HTML::attributes($attributes);
}

/**
Expand Down Expand Up @@ -348,7 +350,11 @@ public function hasActiveOnChild()
{
foreach ($this->getChilds() as $child)
{
if ($child->hasRoute() && $child->getActiveStateFromRoute())
if ($child->isActive())
{
$isActive = true;
}
elseif ($child->hasRoute() && $child->getActiveStateFromRoute())
{
$isActive = true;
}
Expand All @@ -362,13 +368,32 @@ public function hasActiveOnChild()
return $isActive;
}

/**
* Get active attribute.
*
* @return string
*/
public function getActiveAttribute()
{
return array_get($this->attributes, 'active');
}

/**
* Get active state for current item.
*
* @return mixed
*/
public function isActive()
{
$active = $this->getActiveAttribute();

if (is_bool($active)) return $active;

if ($active instanceof \Closure)
{
return call_user_func($active);
}

if ($this->hasRoute())
{
return $this->getActiveStateFromRoute();
Expand Down

0 comments on commit 09ca1ac

Please sign in to comment.