From 741e6d4661842af3e4495c4ad0d71b31c8279ab2 Mon Sep 17 00:00:00 2001 From: Gravitano Date: Sun, 10 May 2015 01:12:51 +0700 Subject: [PATCH] re-upgrade menus --- MenuBuilder.php | 37 ++++++++++++++++-- MenuItem.php | 101 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 116 insertions(+), 22 deletions(-) diff --git a/MenuBuilder.php b/MenuBuilder.php index 1aee609..ba6f844 100644 --- a/MenuBuilder.php +++ b/MenuBuilder.php @@ -33,6 +33,13 @@ class MenuBuilder implements Countable { */ protected $styles = array(); + /** + * Prefix URL. + * + * @var string|null + */ + protected $prefixUrl = null; + /** * Constructor. * @@ -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. * @@ -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. * @@ -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 )); @@ -321,7 +352,7 @@ protected function renderMenu() } elseif ($item->isHeader()) { - $menu .= $this->getHeaderWrapper($item); + $menu .= $presenter->getHeaderWrapper($item); } elseif ($item->isDivider()) { @@ -337,4 +368,4 @@ protected function renderMenu() return $menu; } -} +} \ No newline at end of file diff --git a/MenuItem.php b/MenuItem.php index c0eba4c..3ce452b 100644 --- a/MenuItem.php +++ b/MenuItem.php @@ -5,7 +5,7 @@ use Illuminate\Support\Facades\Request; class MenuItem implements ArrayableContract { - + /** * Array properties. * @@ -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. @@ -55,6 +55,7 @@ protected static function setIconIfDefinedInAttributes(array $properties) return $properties; } + return $properties; } @@ -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; } @@ -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); @@ -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); @@ -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; @@ -260,7 +261,7 @@ public function getRequest() */ public function getIcon($default = null) { - return ! is_null($this->icon) ? '' : $default; + return ! is_null($this->icon) ? '' : $default; } /** @@ -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); } /** @@ -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; } @@ -361,6 +374,45 @@ 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. * @@ -368,6 +420,17 @@ public function hasActiveOnChild() */ 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(); @@ -421,7 +484,7 @@ public function toArray() /** * Get property. * - * @param string $key + * @param string $key * @return string|null */ public function __get($key)