diff --git a/Menu.php b/Menu.php index 72f3a24..53376f9 100644 --- a/Menu.php +++ b/Menu.php @@ -32,18 +32,13 @@ public function __construct(Factory $views, Repository $config) * Make new menu. * * @param string $name + * @param Closure $callback * * @return \Pingpong\Menus\MenuBuilder */ - public function make($name) + public function make($name, \Closure $callback) { - $builder = new MenuBuilder($name, $this->config); - - $builder->setViewFactory($this->views); - - $this->menus[$name] = $builder; - - return $builder; + return $this->create($name, $callback); } /** @@ -56,9 +51,13 @@ public function make($name) */ public function create($name, Closure $resolver) { - $menus = $this->make($name); + $builder = new MenuBuilder($name, $this->config); + + $builder->setViewFactory($this->views); + + $this->menus[$name] = $builder; - return $resolver($menus); + return $resolver($builder); } /** diff --git a/MenuBuilder.php b/MenuBuilder.php index d5c790f..eeb5a19 100644 --- a/MenuBuilder.php +++ b/MenuBuilder.php @@ -335,15 +335,26 @@ public function add(array $attributes = array()) * * @return $this */ - public function dropdown($title, \Closure $callback, $order = 0, array $attributes = array()) + public function dropdown($title, \Closure $callback, $order = null, array $attributes = array()) { - $item = MenuItem::make(compact('title', 'order') + $attributes); + $properties = compact('title', 'order', 'attributes'); + + if (func_num_args() == 3) { + $arguments = func_get_args(); + + $title = array_get($arguments, 0); + $attributes = array_get($arguments, 2); + + $properties = compact('title', 'attributes'); + } + + $item = MenuItem::make($properties); call_user_func($callback, $item); $this->items[] = $item; - return $this; + return $item; } /** @@ -358,6 +369,16 @@ public function dropdown($title, \Closure $callback, $order = 0, array $attribut */ public function route($route, $title, $parameters = array(), $order = null, $attributes = array()) { + if (func_num_args() == 4) { + $arguments = func_get_args(); + + return $this->add([ + 'route' => [array_get($arguments, 0), array_get($arguments, 2)], + 'title' => array_get($arguments, 1), + 'attributes' => array_get($arguments, 3) + ]); + } + $route = array($route, $parameters); $item = MenuItem::make( @@ -394,8 +415,18 @@ protected function formatUrl($url) */ public function url($url, $title, $order = 0, $attributes = array()) { - $url = $this->formatUrl($url); + if (func_num_args() == 3) { + $arguments = func_get_args(); + + return $this->add([ + 'url' => $this->formatUrl(array_get($arguments, 0)), + 'title' => array_get($arguments, 1), + 'attributes' => array_get($arguments, 2) + ]); + } + $url = $this->formatUrl($url); + $item = MenuItem::make(compact('url', 'title', 'order', 'attributes')); $this->items[] = $item; @@ -593,6 +624,10 @@ protected function renderMenu() $menu = $presenter->getOpenTagWrapper(); foreach ($this->getOrderedItems() as $item) { + if ($item->hidden()) { + continue; + } + if ($item->hasSubMenu()) { $menu .= $presenter->getMenuWithDropDownWrapper($item); } elseif ($item->isHeader()) { diff --git a/MenuItem.php b/MenuItem.php index f0ca64f..89c77d9 100644 --- a/MenuItem.php +++ b/MenuItem.php @@ -2,6 +2,7 @@ namespace Pingpong\Menus; +use Closure; use Illuminate\Contracts\Support\Arrayable as ArrayableContract; use Collective\Html\HtmlFacade as HTML; use Illuminate\Support\Facades\Request; @@ -37,8 +38,16 @@ class MenuItem implements ArrayableContract 'attributes', 'active', 'order', + 'hideWhen' ); + /** + * The hideWhen callback. + * + * @var Closure + */ + protected $hideWhen; + /** * Constructor. * @@ -133,15 +142,26 @@ public function child($attributes) * * @return $this */ - public function dropdown($title, $order = 0, \Closure $callback) + public function dropdown($title, \Closure $callback, $order = 0, array $attributes = array()) { - $child = static::make(compact('title', 'order')); + $properties = compact('title', 'order', 'attributes'); + + if (func_num_args() == 3) { + $arguments = func_get_args(); + + $title = array_get($arguments, 0); + $attributes = array_get($arguments, 2); + + $properties = compact('title', 'attributes'); + } + + $child = static::make($properties); call_user_func($callback, $child); $this->childs[] = $child; - return $this; + return $child; } /** @@ -156,6 +176,16 @@ public function dropdown($title, $order = 0, \Closure $callback) */ public function route($route, $title, $parameters = array(), $order = 0, $attributes = array()) { + if (func_num_args() == 4) { + $arguments = func_get_args(); + + return $this->add([ + 'route' => [array_get($arguments, 0), array_get($arguments, 2)], + 'title' => array_get($arguments, 1), + 'attributes' => array_get($arguments, 3) + ]); + } + $route = array($route, $parameters); return $this->add(compact('route', 'title', 'order', 'attributes')); @@ -172,6 +202,16 @@ public function route($route, $title, $parameters = array(), $order = 0, $attrib */ public function url($url, $title, $order = 0, $attributes = array()) { + if (func_num_args() == 3) { + $arguments = func_get_args(); + + return $this->add([ + 'url' => array_get($arguments, 0), + 'title' => array_get($arguments, 1), + 'attributes' => array_get($arguments, 2) + ]); + } + return $this->add(compact('url', 'title', 'order', 'attributes')); } @@ -184,23 +224,27 @@ public function url($url, $title, $order = 0, $attributes = array()) */ public function add(array $properties) { - $this->childs[] = static::make($properties); + $item = static::make($properties); - return $this; + $this->childs[] = $item; + + return $item; } /** * Add new divider. * * @param int $order - * + * * @return self */ public function addDivider($order = null) { - $this->childs[] = static::make(array('name' => 'divider', 'order' => $order)); + $item = static::make(array('name' => 'divider', 'order' => $order)); - return $this; + $this->childs[] = $item; + + return $item; } /** @@ -222,12 +266,14 @@ public function divider() */ public function addHeader($title) { - $this->childs[] = static::make(array( + $item = static::make(array( 'name' => 'header', 'title' => $title, )); - return $this; + $this->childs[] = $item; + + return $item; } /** @@ -500,6 +546,46 @@ protected function getActiveStateFromUrl() return Request::is($this->url); } + /** + * Set order value. + * + * @param int $order + * @return self + */ + public function order($order) + { + $this->order = $order; + + return $this; + } + + /** + * Set hide condition for current menu item. + * + * @param Closure + * @return boolean + */ + public function hideWhen(Closure $callback) + { + $this->hideWhen = $callback; + + return $this; + } + + /** + * Determine whether the menu item is hidden. + * + * @return boolean + */ + public function hidden() + { + if (is_null($this->hideWhen)) { + return false; + } + + return call_user_func($this->hideWhen) == true; + } + /** * Get the instance as an array. * diff --git a/Presenters/Presenter.php b/Presenters/Presenter.php index b79d99a..4262e7b 100644 --- a/Presenters/Presenter.php +++ b/Presenters/Presenter.php @@ -88,6 +88,10 @@ public function getChildMenuItems(MenuItem $item) { $results = ''; foreach ($item->getChilds() as $child) { + if ($child->hidden()) { + continue; + } + if ($child->hasSubMenu()) { $results .= $this->getMultiLevelDropdownWrapper($child); } elseif ($child->isHeader()) { diff --git a/composer.json b/composer.json index 8986ece..6148cb8 100644 --- a/composer.json +++ b/composer.json @@ -17,10 +17,10 @@ } ], "require": { - "php": ">=5.3.0", - "illuminate/support": "5.1.*", - "illuminate/config": "5.1.*", - "illuminate/view": "5.1.*", + "php": ">=5.5.9", + "illuminate/support": "5.2.*", + "illuminate/config": "5.2.*", + "illuminate/view": "5.2.*", "laravelcollective/html": "~5.0" }, "require-dev": { diff --git a/src/config/config.php b/src/config/config.php index fce1fa4..70d950b 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -1,16 +1,16 @@ array( - 'navbar' => 'Pingpong\Menus\Presenters\Bootstrap\NavbarPresenter', + 'styles' => [ + 'navbar' => 'Pingpong\Menus\Presenters\Bootstrap\NavbarPresenter', 'navbar-right' => 'Pingpong\Menus\Presenters\Bootstrap\NavbarRightPresenter', - 'nav-pills' => 'Pingpong\Menus\Presenters\Bootstrap\NavPillsPresenter', - 'nav-tab' => 'Pingpong\Menus\Presenters\Bootstrap\NavTabPresenter', - 'sidebar' => 'Pingpong\Menus\Presenters\Bootstrap\SidebarMenuPresenter', - 'navmenu' => 'Pingpong\Menus\Presenters\Bootstrap\NavMenuPresenter', - ), + 'nav-pills' => 'Pingpong\Menus\Presenters\Bootstrap\NavPillsPresenter', + 'nav-tab' => 'Pingpong\Menus\Presenters\Bootstrap\NavTabPresenter', + 'sidebar' => 'Pingpong\Menus\Presenters\Bootstrap\SidebarMenuPresenter', + 'navmenu' => 'Pingpong\Menus\Presenters\Bootstrap\NavMenuPresenter', + ], 'ordering' => false, -); +];