Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve menu filters (stage 1) #1266

Merged
merged 2 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/Menu/Filters/ClassesFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@
class ClassesFilter implements FilterInterface
{
/**
* Transforms a menu item. Add particular classes when suitable.
* Transforms a menu item. Add some particular HTML classes when suitable.
*
* @param array $item A menu item
* @return array The transformed menu item
* @return array
*/
public function transform($item)
{
$item['class'] = implode(' ', $this->makeClasses($item));
$item['class'] = $this->makeClasses($item);

if (MenuItemHelper::isSubmenu($item)) {
$item['submenu_class'] = implode(' ', $this->makeSubmenuClasses($item));
$item['submenu_class'] = $this->makeSubmenuClasses($item);
}

return $item;
}

/**
* Make classes related to the components of a menu item.
* Make the HTML classes string attribute for a menu item.
*
* @param array $item A menu item
* @return array The array of classes
* @return string
*/
protected function makeClasses($item)
{
Expand All @@ -40,32 +40,32 @@ protected function makeClasses($item)
$classes[] = $item['classes'];
}

// Add the active class when the item is active.
// When the item is active, add the "active" class too.

if (! empty($item['active'])) {
$classes[] = 'active';
}

return $classes;
return implode(' ', $classes);
}

/**
* Make classes related to the components of a submenu item.
* Make the HTML classes string for the submenu of an item.
*
* @param array $item A menu item
* @return array The array of classes
* @return string
*/
protected function makeSubmenuClasses($item)
{
$classes = [];

// Add the menu-open class when a sidebar submenu is active. Note we
// Add the "menu-open" class when a sidebar submenu is active. Note we
// need to add the class to sidebar submenu items only.

if (SidebarItemHelper::isValidItem($item) && $item['active']) {
$classes[] = 'menu-open';
}

return $classes;
return implode(' ', $classes);
}
}
13 changes: 7 additions & 6 deletions src/Menu/Filters/DataFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
class DataFilter implements FilterInterface
{
/**
* Transforms a menu item. Adds the compiled data attributes when suitable.
* Transforms a menu item. Adds the compiled version of HTML data
* attributes to an item when suitable.
*
* @param array $item A menu item
* @return array The transformed menu item
* @return array
*/
public function transform($item)
{
Expand All @@ -20,17 +21,17 @@ public function transform($item)
}

/**
* Compile an array of data attributes into a data string.
* Compile an array of HTML data attributes into a data string.
*
* @param array $dataArray Array of html data attributes
* @return string The compiled version of data attributes
* @param array $dataArray Array of HTML data attributes
* @return string
*/
protected function compileData($dataArray)
{
$compiled = [];

foreach ($dataArray as $key => $value) {
$compiled[] = 'data-'.$key.'="'.$value.'"';
$compiled[] = "data-{$key}=\"{$value}\"";
}

return implode(' ', $compiled);
Expand Down
5 changes: 3 additions & 2 deletions src/Menu/Filters/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
interface FilterInterface
{
/**
* Transforms a menu item in some way.
* Transforms a menu item somehow, by changing properties or adding new
* ones, and return the new version of the menu item.
*
* @param array $item A menu item
* @return array The transformed menu item
* @return array
*/
public function transform($item);
}
34 changes: 19 additions & 15 deletions src/Menu/Filters/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,50 @@
class SearchFilter implements FilterInterface
{
/**
* The default name attribute to be used on the search input.
* The default HTML name attribute to be used on the search input.
*
* @var string
*/
protected $defInputName = 'adminlteSearch';
protected $defName = 'adminlteSearch';

/**
* The default method attribute to be used on the search input.
* The default HTML method attribute to be used on the search input.
*
* @var string
*/
protected $defMethod = 'get';

/**
* Transforms a menu item. Makes the proper search bar configuration.
* Transforms a menu item. Makes the proper configuration for a search bar
* item.
*
* @param array $item A menu item
* @return array The transformed menu item
* @return array
*/
public function transform($item)
{
$isSearch = NavbarItemHelper::isSearch($item) ||
SidebarItemHelper::isSearch($item);
// Menu items that aren't a search bar should be ignored.

if (! $isSearch) {
$isSearchBar = NavbarItemHelper::isSearch($item)
|| SidebarItemHelper::isSearch($item);

if (! $isSearchBar) {
return $item;
}

// Configure search bar method.
// Setup the search bar method attribute.

if (! isset($item['method'])) {
$item['method'] = $this->defMethod;
} elseif (! in_array(strtolower($item['method']), ['post', 'get'])) {
$isValidMethod = isset($item['method'])
&& in_array(strtolower($item['method']), ['post', 'get']);

if (! $isValidMethod) {
$item['method'] = $this->defMethod;
}

// Configure search bar input name.
// Setup the search bar input name attribute.

if (! isset($item['input_name'])) {
$item['input_name'] = $this->defInputName;
if (empty($item['input_name'])) {
$item['input_name'] = $this->defName;
}

return $item;
Expand Down
185 changes: 0 additions & 185 deletions tests/Menu/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,125 +513,6 @@ public function testRouteHref()
$this->assertEquals('http://example.com/profile?user=data', $builder->menu[1]['href']);
}

public function testActiveClass()
{
$builder = $this->makeMenuBuilder('http://example.com/about');

$builder->add(['text' => 'About', 'url' => 'about']);
$builder->add(['text' => 'Profile', 'url' => 'profile']);

$this->assertStringContainsString('active', $builder->menu[0]['class']);
$this->assertStringNotContainsString('active', $builder->menu[1]['class']);
}

public function testActiveClassWithRoute()
{
$builder = $this->makeMenuBuilder('http://example.com/about');
$this->getRouteCollection()->add(new Route('GET', 'about', ['as' => 'pages.about']));

$builder->add(['text' => 'About', 'route' => 'pages.about']);
$builder->add(['text' => 'Profile', 'url' => 'profile']);

$this->assertStringContainsString('active', $builder->menu[0]['class']);
$this->assertStringNotContainsString('active', $builder->menu[1]['class']);
}

public function testActiveClassWithSubmenuAndUrl()
{
$builder = $this->makeMenuBuilder('http://example.com/about');

$builder->add(
[
'text' => 'Menu',
'submenu' => [
[
'text' => 'About',
'url' => '/about',
],
],
]
);

$this->assertStringContainsString('active', $builder->menu[0]['class']);
$this->assertStringContainsString('active', $builder->menu[0]['submenu'][0]['class']);
}

public function testActiveClassWithSubmenuAndRoute()
{
$builder = $this->makeMenuBuilder('http://example.com/about');
$this->getRouteCollection()->add(new Route('GET', 'about', ['as' => 'pages.about']));

$builder->add(
[
'text' => 'Menu',
'submenu' => [
[
'text' => 'About',
'route' => 'pages.about',
],
],
]
);

$this->assertStringContainsString('active', $builder->menu[0]['class']);
$this->assertStringContainsString('active', $builder->menu[0]['submenu'][0]['class']);
}

public function testSubmenuActiveWithHash()
{
$builder = $this->makeMenuBuilder('http://example.com/home');

$builder->add(
[
'text' => 'Menu',
'url' => '#',
'submenu' => [
['url' => 'home'],
],
]
);

$this->assertTrue($builder->menu[0]['active']);
$this->assertEquals('active', $builder->menu[0]['class']);
$this->assertEquals('menu-open', $builder->menu[0]['submenu_class']);
}

public function testTopNavActiveClass()
{
$builder = $this->makeMenuBuilder('http://example.com/about');

$builder->add(['text' => 'About', 'url' => 'about', 'topnav' => true]);

$this->assertEquals('active', $builder->menu[0]['class']);
}

public function testTopNavRightActiveClass()
{
$builder = $this->makeMenuBuilder('http://example.com/about');

$builder->add(['text' => 'About', 'url' => 'about', 'topnav_right' => true]);

$this->assertEquals('active', $builder->menu[0]['class']);
}

public function testSubmenuClassWhenAddInMultipleItems()
{
$builder = $this->makeMenuBuilder();

// Add a new link item.

$builder->add(['text' => 'Home', 'url' => '/', 'key' => 'home']);

// Add elements inside the previous one, now it will be a submenu item.

$builder->addIn('home', ['text' => 'Profile', 'url' => '/profile']);
$builder->addIn('home', ['text' => 'About', 'url' => '/about']);

// Check the "submenu_class" attribute is added.

$this->assertTrue(isset($builder->menu[0]['submenu_class']));
}

public function testCan()
{
$gate = $this->makeGate();
Expand Down Expand Up @@ -959,70 +840,4 @@ public function testLangTranslateWithExtraParams()
$this->assertEquals('MENU :cat / :subcat', $builder->menu[3]['header']);
$this->assertEquals('MENU :cat / :subcat', $builder->menu[4]['header']);
}

public function testDataAttributes()
{
$builder = $this->makeMenuBuilder();

$builder->add(['text' => 'About', 'data' => [
'test-one' => 'content-one',
'test-two' => 'content-two',
]]);

$this->assertEquals(
'data-test-one="content-one" data-test-two="content-two"',
$builder->menu[0]['data-compiled']
);
}

public function testSearchBarDefaultMethod()
{
$builder = $this->makeMenuBuilder();

$builder->add(['text' => 'search', 'search' => true]);
$builder->add(['text' => 'Search', 'search' => true, 'method' => 'foo']);
$builder->add(['text' => 'Search', 'search' => true, 'method' => 'post']);

$this->assertEquals('get', $builder->menu[0]['method']);
$this->assertEquals('get', $builder->menu[1]['method']);
$this->assertEquals('post', $builder->menu[2]['method']);
}

public function testSearchBarDefaultName()
{
$builder = $this->makeMenuBuilder();

$builder->add(['text' => 'search', 'search' => true]);
$builder->add(['text' => 'Search', 'search' => true, 'input_name' => 'foo']);

$this->assertEquals('adminlteSearch', $builder->menu[0]['input_name']);
$this->assertEquals('foo', $builder->menu[1]['input_name']);
}

public function testClassesAttribute()
{
$builder = $this->makeMenuBuilder();

$builder->add([
'text' => 'About',
'classes' => 'foo-class',
]);

$this->assertStringContainsString('foo-class', $builder->menu[0]['class']);
}

public function testClassesAttributeWithActiveClass()
{
$builder = $this->makeMenuBuilder('http://example.com/about');

$builder->add([
'text' => 'About',
'url' => 'about',
'classes' => 'foo-class bar-class',
]);

$this->assertStringContainsString('active', $builder->menu[0]['class']);
$this->assertStringContainsString('foo-class', $builder->menu[0]['class']);
$this->assertStringContainsString('bar-class', $builder->menu[0]['class']);
}
}
Loading