From 564fc034ae4d927abd38b8c6e63cebaf434b12f8 Mon Sep 17 00:00:00 2001 From: Gravitano Date: Mon, 29 Jun 2015 11:42:06 +0700 Subject: [PATCH 01/11] allow to set order value via ->order method --- MenuItem.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/MenuItem.php b/MenuItem.php index f0ca64f..5fa74e9 100644 --- a/MenuItem.php +++ b/MenuItem.php @@ -193,7 +193,7 @@ public function add(array $properties) * Add new divider. * * @param int $order - * + * * @return self */ public function addDivider($order = null) @@ -500,6 +500,19 @@ 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; + } + /** * Get the instance as an array. * From 99dede14a6de18dc86dccf5c1a970ac8cbde3274 Mon Sep 17 00:00:00 2001 From: Gravitano Date: Mon, 29 Jun 2015 11:56:17 +0700 Subject: [PATCH 02/11] improved way to add menu item --- MenuBuilder.php | 22 +++++++++++++++++++++- MenuItem.php | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/MenuBuilder.php b/MenuBuilder.php index 0e896dc..a4dca5e 100644 --- a/MenuBuilder.php +++ b/MenuBuilder.php @@ -299,6 +299,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( @@ -335,8 +345,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; diff --git a/MenuItem.php b/MenuItem.php index 5fa74e9..249d179 100644 --- a/MenuItem.php +++ b/MenuItem.php @@ -156,6 +156,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 +182,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')); } From fab69de52d24e5af31346fe43639e052342381ea Mon Sep 17 00:00:00 2001 From: Gravitano Date: Mon, 29 Jun 2015 12:01:09 +0700 Subject: [PATCH 03/11] return menu item instance --- MenuItem.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/MenuItem.php b/MenuItem.php index 249d179..a676cb6 100644 --- a/MenuItem.php +++ b/MenuItem.php @@ -204,9 +204,11 @@ 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; } /** @@ -218,9 +220,11 @@ public function add(array $properties) */ 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; } /** @@ -242,12 +246,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; } /** From a4002a42be224d4681ccc997f925d74108bf2bb6 Mon Sep 17 00:00:00 2001 From: Gravitano Date: Mon, 29 Jun 2015 12:07:15 +0700 Subject: [PATCH 04/11] allow to create a new menu via 'make' method --- Menu.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Menu.php b/Menu.php index 9ba3655..e2f3f43 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); } /** From d3d5f80f80b23c8a266cf5fce10c672928362b02 Mon Sep 17 00:00:00 2001 From: Gravitano Date: Tue, 30 Jun 2015 11:08:53 +0700 Subject: [PATCH 05/11] set default menu order to null --- MenuBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MenuBuilder.php b/MenuBuilder.php index a4dca5e..b8e9854 100644 --- a/MenuBuilder.php +++ b/MenuBuilder.php @@ -276,7 +276,7 @@ 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); From 51b02a4ed1dc6c6444d6aadda750b2c1584c90e0 Mon Sep 17 00:00:00 2001 From: Gravitano Date: Tue, 30 Jun 2015 11:09:07 +0700 Subject: [PATCH 06/11] return the menu item instance --- MenuBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MenuBuilder.php b/MenuBuilder.php index b8e9854..3d4b49e 100644 --- a/MenuBuilder.php +++ b/MenuBuilder.php @@ -284,7 +284,7 @@ public function dropdown($title, \Closure $callback, $order = null, array $attri $this->items[] = $item; - return $this; + return $item; } /** From dc33a37d27de4d2f99ad953fe962e545ff52f13e Mon Sep 17 00:00:00 2001 From: Gravitano Date: Tue, 30 Jun 2015 11:37:57 +0700 Subject: [PATCH 07/11] allow to set order value by calling ->order() method when registering dropdown menu item --- MenuBuilder.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/MenuBuilder.php b/MenuBuilder.php index 3d4b49e..8f80e57 100644 --- a/MenuBuilder.php +++ b/MenuBuilder.php @@ -278,7 +278,18 @@ public function add(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); From b763cec782c196d924e8253969e1afa4c852b598 Mon Sep 17 00:00:00 2001 From: Gravitano Date: Tue, 30 Jun 2015 11:40:56 +0700 Subject: [PATCH 08/11] allow to set order value by calling ->order() method when registering dropdown child menu item --- MenuItem.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/MenuItem.php b/MenuItem.php index a676cb6..3ed2a34 100644 --- a/MenuItem.php +++ b/MenuItem.php @@ -133,15 +133,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; } /** From 05064e489b8c661b2c4eaebdafc53039616688c0 Mon Sep 17 00:00:00 2001 From: gravitano Date: Sat, 18 Jul 2015 02:03:00 +0700 Subject: [PATCH 09/11] Added new feature: allow to hide menu item via 'hideWhen' method/attribute --- MenuBuilder.php | 4 ++++ MenuItem.php | 36 ++++++++++++++++++++++++++++++++++++ Presenters/Presenter.php | 4 ++++ 3 files changed, 44 insertions(+) diff --git a/MenuBuilder.php b/MenuBuilder.php index 8f80e57..65685aa 100644 --- a/MenuBuilder.php +++ b/MenuBuilder.php @@ -563,6 +563,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 3ed2a34..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. * @@ -550,6 +559,33 @@ public function 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()) { From 98f521e7eb0f95792708a9cdd8ce97ede8504768 Mon Sep 17 00:00:00 2001 From: Maher El Gamil Date: Fri, 20 Nov 2015 11:28:05 +0200 Subject: [PATCH 10/11] fix code style --- src/config/config.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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, -); +]; From d3fcd88420c61e793dba6b23cd8bf501930b1ee9 Mon Sep 17 00:00:00 2001 From: Dennis Oderwald Date: Tue, 22 Dec 2015 02:18:26 +0100 Subject: [PATCH 11/11] Laravel 5.2 availability --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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": {