Skip to content

Commit 12e4b1b

Browse files
committed
Made MenuBuilder items also hideable in the event an entire section needed to be hidden
1 parent 4803637 commit 12e4b1b

File tree

4 files changed

+92
-40
lines changed

4 files changed

+92
-40
lines changed

Tests/MenuTest.php

+43
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,47 @@ public function it_can_destroy_all_menus()
133133
$this->menu->destroy();
134134
$this->assertCount(0, $this->menu->all());
135135
}
136+
137+
/** @test */
138+
public function it_still_generates_empty_menu_after_adding_dropdown()
139+
{
140+
$this->menu->create('test', function (MenuBuilder $menu) {
141+
$menu->dropdown('Test', function($sub) {
142+
143+
})->hideWhen(function() {
144+
return true;
145+
});
146+
});
147+
148+
$expected = <<<TEXT
149+
150+
<ul class="nav navbar-nav">
151+
152+
</ul>
153+
154+
TEXT;
155+
156+
self::assertEquals($expected, $this->menu->get('test'));
157+
}
158+
159+
/** @test */
160+
public function it_still_generates_empty_menu_after_adding_item()
161+
{
162+
$this->menu->create('test', function (MenuBuilder $menu) {
163+
$menu->url('/', 'Test')
164+
->hideWhen(function() {
165+
return true;
166+
});
167+
});
168+
169+
$expected = <<<TEXT
170+
171+
<ul class="nav navbar-nav">
172+
173+
</ul>
174+
175+
TEXT;
176+
177+
self::assertEquals($expected, $this->menu->get('test'));
178+
}
136179
}

src/MenuBuilder.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace Nwidart\Menus;
44

55
use Countable;
6-
use Illuminate\Contracts\Config\Repository;
76
use Illuminate\Support\Arr;
7+
use Nwidart\Menus\Traits\CanHide;
8+
use Illuminate\Contracts\Config\Repository;
89
use Illuminate\View\Factory as ViewFactory;
910

1011
class MenuBuilder implements Countable
1112
{
13+
use CanHide;
1214
/**
1315
* Menu name.
1416
*
@@ -634,7 +636,7 @@ protected function renderMenu()
634636
$menu = $presenter->getOpenTagWrapper();
635637

636638
foreach ($this->getOrderedItems() as $item) {
637-
if ($item->hidden()) {
639+
if ($item->hidden() || $this->hidden()) {
638640
continue;
639641
}
640642

src/MenuItem.php

+5-38
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Nwidart\Menus;
44

5-
use Closure;
6-
use Collective\Html\HtmlFacade as HTML;
7-
use Illuminate\Contracts\Support\Arrayable as ArrayableContract;
85
use Illuminate\Support\Arr;
9-
use Illuminate\Support\Facades\Request;
106
use Illuminate\Support\Str;
7+
use Nwidart\Menus\Traits\CanHide;
8+
use Collective\Html\HtmlFacade as HTML;
9+
use Illuminate\Support\Facades\Request;
10+
use Illuminate\Contracts\Support\Arrayable as ArrayableContract;
1111

1212
/**
1313
* @property string url
@@ -22,6 +22,7 @@
2222
*/
2323
class MenuItem implements ArrayableContract
2424
{
25+
use CanHide;
2526
/**
2627
* Array properties.
2728
*
@@ -54,13 +55,6 @@ class MenuItem implements ArrayableContract
5455
'hideWhen',
5556
);
5657

57-
/**
58-
* The hideWhen callback.
59-
*
60-
* @var Closure
61-
*/
62-
protected $hideWhen;
63-
6458
/**
6559
* Constructor.
6660
*
@@ -592,33 +586,6 @@ public function order($order)
592586
return $this;
593587
}
594588

595-
/**
596-
* Set hide condition for current menu item.
597-
*
598-
* @param Closure
599-
* @return boolean
600-
*/
601-
public function hideWhen(Closure $callback)
602-
{
603-
$this->hideWhen = $callback;
604-
605-
return $this;
606-
}
607-
608-
/**
609-
* Determine whether the menu item is hidden.
610-
*
611-
* @return boolean
612-
*/
613-
public function hidden()
614-
{
615-
if (is_null($this->hideWhen)) {
616-
return false;
617-
}
618-
619-
return call_user_func($this->hideWhen) == true;
620-
}
621-
622589
/**
623590
* Get the instance as an array.
624591
*

src/Traits/CanHide.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Nwidart\Menus\Traits;
4+
5+
use Closure;
6+
7+
trait CanHide
8+
{
9+
/**
10+
* @var Closure
11+
*/
12+
protected $hideWhen;
13+
14+
/**
15+
* Set hide condition for current menu item.
16+
*
17+
* @param Closure
18+
* @return boolean
19+
*/
20+
public function hideWhen(Closure $callback)
21+
{
22+
$this->hideWhen = $callback;
23+
24+
return $this;
25+
}
26+
27+
/**
28+
* Determine whether the menu item is hidden.
29+
*
30+
* @return boolean
31+
*/
32+
public function hidden()
33+
{
34+
if (is_null($this->hideWhen)) {
35+
return false;
36+
}
37+
38+
return call_user_func($this->hideWhen) == true;
39+
}
40+
}

0 commit comments

Comments
 (0)