-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
306bf94
commit 8f76b06
Showing
12 changed files
with
516 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
|
||
namespace JeroenNoten\LaravelAdminLte\Menu; | ||
|
||
|
||
use Illuminate\Http\Request; | ||
|
||
class ActiveChecker | ||
{ | ||
private $request; | ||
|
||
public function __construct(Request $request) | ||
{ | ||
$this->request = $request; | ||
} | ||
|
||
public function isActive($item) | ||
{ | ||
if (isset($item['active'])) { | ||
return $this->isExplicitActive($item['active']); | ||
} | ||
|
||
if (isset($item['submenu'])) { | ||
return $this->containsActive($item['submenu']); | ||
} | ||
|
||
if (isset($item['url'])) { | ||
return $this->isActiveUrl($item['url']); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected function isActiveUrl($url) | ||
{ | ||
return $this->checkExact($url) || $this->checkSub($url); | ||
} | ||
|
||
protected function checkExact($url) | ||
{ | ||
return $this->request->is($url); | ||
} | ||
|
||
protected function checkSub($url) | ||
{ | ||
return $this->request->is($url . '/*'); | ||
} | ||
|
||
protected function containsActive($items) | ||
{ | ||
foreach ($items as $item) { | ||
if ($this->isActive($item)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function isExplicitActive($active) | ||
{ | ||
foreach ($active as $url) { | ||
if ($this->isActiveUrl($url)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
use JeroenNoten\LaravelAdminLte\Events\BuildingMenu; | ||
|
||
class AdminLteTest extends TestCase | ||
{ | ||
public function testMenu() | ||
{ | ||
$adminLte = $this->makeAdminLte(); | ||
|
||
$this->getDispatcher()->listen(BuildingMenu::class, function (BuildingMenu $event) { | ||
$event->menu->add(['text' => 'Home']); | ||
}); | ||
|
||
$menu = $adminLte->menu(); | ||
|
||
$this->assertEquals('Home', $menu[0]['text']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
|
||
class ActiveCheckerTest extends TestCase | ||
{ | ||
public function testExact() | ||
{ | ||
$checker = $this->makeActiveChecker('http://example.com/about'); | ||
|
||
$this->assertTrue($checker->isActive(['url' => 'about'])); | ||
} | ||
|
||
public function testRoot() | ||
{ | ||
$checker = $this->makeActiveChecker('http://example.com'); | ||
|
||
$this->assertTrue($checker->isActive(['url' => '/'])); | ||
} | ||
|
||
public function testNotActive() | ||
{ | ||
$checker = $this->makeActiveChecker('http://example.com/about'); | ||
|
||
$this->assertFalse($checker->isActive(['url' => 'home'])); | ||
} | ||
|
||
public function testStringNotActive() | ||
{ | ||
$checker = $this->makeActiveChecker(); | ||
|
||
$this->assertFalse($checker->isActive('HEADER')); | ||
} | ||
|
||
public function testSub() | ||
{ | ||
$checker = $this->makeActiveChecker('http://example.com/about/sub'); | ||
|
||
$this->assertTrue($checker->isActive(['url' => 'about'])); | ||
} | ||
|
||
public function testSubmenu() | ||
{ | ||
$checker = $this->makeActiveChecker('http://example.com/home'); | ||
|
||
$isActive = $checker->isActive([ | ||
'submenu' => [ | ||
['url' => 'home'] | ||
] | ||
]); | ||
|
||
$this->assertTrue($isActive); | ||
} | ||
|
||
public function testMultiLevelSubmenu() | ||
{ | ||
$checker = $this->makeActiveChecker('http://example.com/home'); | ||
|
||
$isActive = $checker->isActive([ | ||
'text' => 'Level 0', | ||
'submenu' => [ | ||
[ | ||
'text' => 'Level 1', | ||
'submenu' => [ | ||
['url' => 'home'] | ||
] | ||
] | ||
] | ||
]); | ||
|
||
$this->assertTrue($isActive); | ||
} | ||
|
||
public function testExplicitActive() | ||
{ | ||
$checker = $this->makeActiveChecker('http://example.com/home'); | ||
|
||
$isActive = $checker->isActive([ | ||
'active' => ['home'] | ||
]); | ||
|
||
$this->assertTrue($isActive); | ||
} | ||
|
||
public function testExplicitActiveRegex() | ||
{ | ||
$checker = $this->makeActiveChecker('http://example.com/home/sub'); | ||
|
||
$isActive = $checker->isActive([ | ||
'active' => ['home/*'] | ||
]); | ||
|
||
$this->assertTrue($isActive); | ||
} | ||
|
||
} |
Oops, something went wrong.
8f76b06
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing, thanks for this!