Skip to content

Commit

Permalink
re-upgrade menus
Browse files Browse the repository at this point in the history
  • Loading branch information
Gravitano committed May 9, 2015
1 parent 85aeeac commit 741e6d4
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 22 deletions.
37 changes: 34 additions & 3 deletions MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class MenuBuilder implements Countable {
*/
protected $styles = array();

/**
* Prefix URL.
*
* @var string|null
*/
protected $prefixUrl = null;

/**
* Constructor.
*
Expand All @@ -44,6 +51,19 @@ public function __construct($menu, Repository $config)
$this->config = $config;
}

/**
* Set Prefix URL.
*
* @param string $prefixUrl
* @return $this
*/
public function setPrefixUrl($prefixUrl)
{
$this->prefixUrl = $prefixUrl;

return $this;
}

/**
* Set styles.
*
Expand Down Expand Up @@ -191,6 +211,17 @@ public function route($route, $title, $parameters = array(), $attributes = array
return $item;
}

/**
* Format URL.
*
* @param string $url
* @return string
*/
protected function formatUrl($url)
{
return ! is_null($this->prefixUrl) ? $this->prefixUrl . $url : $url;
}

/**
* Register new menu item using url.
*
Expand All @@ -202,7 +233,7 @@ public function route($route, $title, $parameters = array(), $attributes = array
public function url($url, $title, $attributes = array())
{
$item = MenuItem::make(array(
'url' => $url,
'url' => $this->formatUrl($url),
'title' => $title,
'attributes' => $attributes
));
Expand Down Expand Up @@ -321,7 +352,7 @@ protected function renderMenu()
}
elseif ($item->isHeader())
{
$menu .= $this->getHeaderWrapper($item);
$menu .= $presenter->getHeaderWrapper($item);
}
elseif ($item->isDivider())
{
Expand All @@ -337,4 +368,4 @@ protected function renderMenu()

return $menu;
}
}
}
101 changes: 82 additions & 19 deletions MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Support\Facades\Request;

class MenuItem implements ArrayableContract {

/**
* Array properties.
*
Expand All @@ -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 @@ -55,6 +55,7 @@ protected static function setIconIfDefinedInAttributes(array $properties)

return $properties;
}

return $properties;
}

Expand Down Expand Up @@ -85,14 +86,14 @@ public static function make(array $properties)
/**
* Fill the attributes.
*
* @param array $attributes
* @param array $attributes
* @return void
*/
public function fill($attributes)
{
foreach ($attributes as $key => $value)
{
if(in_array($key, $this->fillable))
if (in_array($key, $this->fillable))
{
$this->{$key} = $value;
}
Expand Down Expand Up @@ -142,9 +143,9 @@ public function dropdown($title, \Closure $callback)
public function route($route, $title, $parameters = array(), $attributes = array())
{
$item = array(
'route' => array($route, $parameters),
'title' => $title,
'attributes' => $attributes,
'route' => array($route, $parameters),
'title' => $title,
'attributes' => $attributes,
);

$this->childs[] = static::make($item);
Expand All @@ -163,9 +164,9 @@ public function route($route, $title, $parameters = array(), $attributes = array
public function url($url, $title, $attributes = array())
{
$item = array(
'url' => $url,
'title' => $title,
'attributes' => $attributes
'url' => $url,
'title' => $title,
'attributes' => $attributes
);

$this->childs[] = static::make($item);
Expand Down Expand Up @@ -204,8 +205,8 @@ public function divider()
public function addHeader($title)
{
$this->childs[] = static::make(array(
'name' => 'header',
'title' => $title
'name' => 'header',
'title' => $title
));

return $this;
Expand Down Expand Up @@ -260,7 +261,7 @@ public function getRequest()
*/
public function getIcon($default = null)
{
return ! is_null($this->icon) ? '<i class="'. $this->icon .'"></i>' : $default;
return ! is_null($this->icon) ? '<i class="' . $this->icon . '"></i>' : $default;
}

/**
Expand All @@ -280,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 @@ -341,17 +344,27 @@ public function hasChilds()
*/
public function hasActiveOnChild()
{
if ($this->inactive()) return false;

$isActive = false;

if($this->hasChilds())
if ($this->hasChilds())
{
foreach($this->getChilds() as $child)
foreach ($this->getChilds() as $child)
{
if($child->hasRoute() && $child->getActiveStateFromRoute())
if ($child->inactive())
{
$isActive = false;
}
elseif ($child->isActive())
{
$isActive = true;
}
elseif($child->getActiveStateFromUrl())
elseif ($child->hasRoute() && $child->getActiveStateFromRoute())
{
$isActive = true;
}
elseif ($child->getActiveStateFromUrl())
{
$isActive = true;
}
Expand All @@ -361,13 +374,63 @@ public function hasActiveOnChild()
return $isActive;
}

/**
* Get inactive state.
*
* @return boolean
*/
public function inactive()
{
$inactive = $this->getInactiveAttribute();

if (is_bool($inactive)) return $inactive;

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

return false;
}

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

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

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

$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 Expand Up @@ -421,7 +484,7 @@ public function toArray()
/**
* Get property.
*
* @param string $key
* @param string $key
* @return string|null
*/
public function __get($key)
Expand Down

0 comments on commit 741e6d4

Please sign in to comment.