From 94e01d617d5049a51c951856696853d3e581e397 Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Thu, 11 May 2017 07:14:32 -0400 Subject: [PATCH 1/4] Menus can add other Menus as sub items Allow the user to add a menu as a sub item of a menu. --- src/CreatesMenuItems.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/CreatesMenuItems.php b/src/CreatesMenuItems.php index c762381..8ffe1e9 100644 --- a/src/CreatesMenuItems.php +++ b/src/CreatesMenuItems.php @@ -93,6 +93,19 @@ public function &add(string $title, $config = null) return $this; } + + //Is it another Menu + if(starts_with($config, 'menu:')) { + $slug = $item['__slug__'] = snake_case($title); + + foreach(app('menu')->get(str_replace_first('menu:', '', $config)) as $sub_item) { + $item->add($sub_item->title, $sub_item->_items); + } + + $this->_items[$slug] = &$item; + + return $this; + } // Else it's a plain url $this->_items[snake_case($title)] = &$item; @@ -100,4 +113,4 @@ public function &add(string $title, $config = null) return $this; } -} \ No newline at end of file +} From b88535c5808a06d87b46ec375c14df78b7586802 Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Thu, 11 May 2017 07:17:46 -0400 Subject: [PATCH 2/4] Update README.md Add support for sub menu addition --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ee516d2..6492c3b 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ function generate() $this->menu->add('Edit Myself', 'route:users.edit|id:5'); // Method 5 $this->menu->add('Logout')->route('auth.logout'); // Method 6 $this->menu->add('Some Other Link', ['url' => '/link']); // Method 7 + $this->menu->add('Sub Menu', 'menu:sub_menu_name); // Method 8 } ``` @@ -131,6 +132,7 @@ function generate() | 5 | The `$this->menu` instance | The route is associated with arguments, useful to create a quick route with parameters. | | 6 | A new `MenuItem` | The route is assigned using the fluent interface, which makes the code look cleaner. You can also add an array of route parameters as the second argument. | | 7 | The `$this->menu` instance | You can pass an array as the second argument to immediately issue all the variables to the menu item, when you don't want to use the fluent interface. | +| 8 | The `$this->menu` instance | You can pass the name of another menu and the class will pull in that menu as a sub menu. | @@ -285,4 +287,4 @@ This should achieve a menu looking like [the default bootstrap navbar](https://g # Tips -- The `__construct` of your menu class works with dependency injection. Simply type hint the stuff you need in the parameter list and we'll ask Laravel to inject the stuff you need. \ No newline at end of file +- The `__construct` of your menu class works with dependency injection. Simply type hint the stuff you need in the parameter list and we'll ask Laravel to inject the stuff you need. From 676e686e1c197dc660766b9d54a4a6e1340acb0f Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Thu, 11 May 2017 07:50:59 -0400 Subject: [PATCH 3/4] Update CreatesMenuItems.php Allow Menu to be merged with current menu or make a sub menu --- src/CreatesMenuItems.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/CreatesMenuItems.php b/src/CreatesMenuItems.php index 8ffe1e9..7fb1f19 100644 --- a/src/CreatesMenuItems.php +++ b/src/CreatesMenuItems.php @@ -94,15 +94,24 @@ public function &add(string $title, $config = null) return $this; } - //Is it another Menu - if(starts_with($config, 'menu:')) { + + // Is it another Menu? + if(starts_with($config, 'menu:') || starts_with($config, 'sub-menu:')) { $slug = $item['__slug__'] = snake_case($title); - foreach(app('menu')->get(str_replace_first('menu:', '', $config)) as $sub_item) { - $item->add($sub_item->title, $sub_item->_items); + if(starts_with($config, 'menu:')) { + $items =& $this->_items; + $menu = app('menu')->get(str_replace_first('menu:', '', $config)); + } else {// if(starts_with($config, 'sub-menu:')) { + $items =& $item->_items; + $menu = app('menu')->get(str_replace_first('sub-menu:', '', $config)); } - $this->_items[$slug] = &$item; + $items = array_merge($items, $menu->_items); + + if(starts_with($config, 'sub-menu:')) { + $this->_items[$slug] = &$item; + } return $this; } From ec02dda7ac9d0d998681ca10112ce4c1304d9795 Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Thu, 11 May 2017 07:52:10 -0400 Subject: [PATCH 4/4] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6492c3b..f4621d5 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,8 @@ function generate() $this->menu->add('Edit Myself', 'route:users.edit|id:5'); // Method 5 $this->menu->add('Logout')->route('auth.logout'); // Method 6 $this->menu->add('Some Other Link', ['url' => '/link']); // Method 7 - $this->menu->add('Sub Menu', 'menu:sub_menu_name); // Method 8 + $this->menu->add('Sub Menu', 'menu:menu_name); // Method 8 + $this->menu->add('Sub Menu', 'sub-menu:sub_menu_name); // Method 9 } ``` @@ -132,7 +133,8 @@ function generate() | 5 | The `$this->menu` instance | The route is associated with arguments, useful to create a quick route with parameters. | | 6 | A new `MenuItem` | The route is assigned using the fluent interface, which makes the code look cleaner. You can also add an array of route parameters as the second argument. | | 7 | The `$this->menu` instance | You can pass an array as the second argument to immediately issue all the variables to the menu item, when you don't want to use the fluent interface. | -| 8 | The `$this->menu` instance | You can pass the name of another menu and the class will pull in that menu as a sub menu. | +| 8 | The `$this->menu` instance | You can pass the name of another menu and the class will merge that menu with this menu. | +| 9 | The `$this->menu` instance | You can pass the name of another menu and the class will pull in that menu as a sub menu. |