Skip to content

Commit

Permalink
Merge pull request #13 from tattersoftware/styles
Browse files Browse the repository at this point in the history
Styles & Fixes
  • Loading branch information
MGatner authored Apr 29, 2021
2 parents 055d865 + 4e0ef5f commit e79b465
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 44 deletions.
9 changes: 7 additions & 2 deletions examples/Menus.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php namespace Config;

use Tatter\Menus\Config\Menus as BaseMenus;
use Tatter\Menus\Menus\BreadcrumbsMenu;

/***
*
* This file contains example values to alter default library behavior.
Expand All @@ -10,12 +13,14 @@
*
***/

class Menus extends \Tatter\Menus\Config\Menus
class Menus extends BaseMenus
{
/**
* Menu class aliases.
*
* @var array<string, string>
*/
public $aliases = [];
public $aliases = [
'breadcrumbs' => BreadcrumbsMenu::class,
];
}
5 changes: 4 additions & 1 deletion src/Config/Menus.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Tatter\Menus\Config;

use CodeIgniter\Config\BaseConfig;
use Tatter\Menus\Menus\BreadcrumbsMenu;

class Menus extends BaseConfig
{
Expand All @@ -9,5 +10,7 @@ class Menus extends BaseConfig
*
* @var array<string, string>
*/
public $aliases = [];
public $aliases = [
'breadcrumbs' => BreadcrumbsMenu::class,
];
}
11 changes: 9 additions & 2 deletions src/Filters/MenusFilter.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php namespace Tatter\Menus\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Tatter\Menus\Menu;
use InvalidArgumentException;
use RuntimeException;

/**
Expand Down Expand Up @@ -32,10 +34,15 @@ public function before(RequestInterface $request, $arguments = null)
*/
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): ?ResponseInterface
{
// Check a few short-circuit conditions
// Verify menu conditions
if (empty($arguments))
{
throw new RuntimeException('No arguments supplied to Menus filter.');
throw new InvalidArgumentException('No arguments supplied to Menus filter.');
}
// Ignore Redirects
if ($response instanceof RedirectResponse)
{
return null;
}
if (empty($response->getBody()))
{
Expand Down
7 changes: 4 additions & 3 deletions src/Menus/BreadcrumbsMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use CodeIgniter\HTTP\URI;
use Tatter\Menus\Breadcrumb;
use Tatter\Menus\Menu;
use Tatter\Menus\Traits\BreadcrumbsStyle;
use Tatter\Menus\Styles\BreadcrumbsStyle;

/**
* Breadcrumbs Menu
Expand Down Expand Up @@ -124,7 +124,8 @@ public function __toString(): string
}

return $this->builder
->html($last->display, ['class' => 'breadcrumb-item active'])
->render();
->html($last->display, [
'class' => 'breadcrumb-item active'
])->render();
}
}
28 changes: 28 additions & 0 deletions src/Styles/AdminLTEStyle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php namespace Tatter\Menus\Styles;

use Spatie\Menu\Link;

/**
* AdminLTE Styler Trait
*
* Applies CSS classes & styles
* to start a Menu for AdminLTE.
*
* @mixin \Tatter\Menus\Menu
*/
trait AdminLTEStyle
{
protected function applyAdminLTEStyle(): void
{
$this->builder
->addClass('nav nav-pills nav-sidebar flex-column')
->setActiveClass('active menu-open')
->setAttribute('data-widget', 'treeview')
->setAttribute('role', 'menu')
->setAttribute('data-accordion', 'false')
->registerFilter(function (Link $link) {
$link->addParentClass('nav-item');
$link->addClass('nav-link');
});
}
}
12 changes: 7 additions & 5 deletions src/Traits/BootstrapStyle.php → src/Styles/BootstrapStyle.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php namespace Tatter\Menus\Traits;
<?php namespace Tatter\Menus\Styles;

use Spatie\Menu\Link;

Expand All @@ -14,9 +14,11 @@ trait BootstrapStyle
{
protected function applyBootstrapStyle(): void
{
$this->builder->registerFilter(function (Link $link) {
$link->addParentClass('nav-item');
$link->addClass('nav-link');
});
$this->builder
->addClass('navbar-nav mr-auto')
->registerFilter(function (Link $link) {
$link->addParentClass('nav-item');
$link->addClass('nav-link');
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php namespace Tatter\Menus\Traits;
<?php namespace Tatter\Menus\Styles;

use Spatie\Menu\Link;

Expand Down
13 changes: 12 additions & 1 deletion tests/filter/MenusFilterTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Tests\Support;

use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Test\FilterTestTrait;
use Tatter\Menus\Filters\MenusFilter;
Expand All @@ -25,7 +26,7 @@ protected function setUp(): void

public function testEmptyArguments()
{
$this->expectException('RuntimeException');
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('No arguments supplied to Menus filter.');

($this->caller)();
Expand Down Expand Up @@ -53,6 +54,16 @@ public function testEmptyBody()
$caller(['test']);
}

public function testRedirects()
{
$this->response = new RedirectResponse(config('App'));
$caller = $this->getFilterCaller(MenusFilter::class, 'after');

$result = $caller(['test']);

$this->assertNull($result);
}

public function testUnknownAlias()
{
$this->expectException('RuntimeException');
Expand Down
52 changes: 52 additions & 0 deletions tests/menu/StylesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php namespace Tests\Support;

use Tatter\Menus\Menu;
use Tatter\Menus\Styles\BootstrapStyle;
use Tatter\Menus\Styles\AdminLTEStyle;
use Tests\Support\MenusTestCase;

class StylesTest extends MenusTestCase
{
public function testBootstrapStyleAppliesClasses()
{
$menu = new class extends Menu {

use BootstrapStyle;

public function __toString(): string
{
return $this->builder
->link(site_url('/'), 'Home')
->link(site_url('/current'), 'Grain')
->render();
}
};

$result = $menu->__toString();

$this->assertStringContainsString('li class="nav-item"', $result);
$this->assertStringContainsString('class="nav-link"', $result);
}

public function testAdminLTEStyleAppliesClasses()
{
$menu = new class extends Menu {

use AdminLTEStyle;

public function __toString(): string
{
return $this->builder
->link(site_url('/'), 'Home')
->link(site_url('/current'), 'Grain')
->render();
}
};

$result = $menu->__toString();

$this->assertStringContainsString('data-widget="treeview"', $result);
$this->assertStringContainsString('class="nav nav-pills', $result);
$this->assertStringContainsString('class="nav-link"', $result);
}
}
29 changes: 0 additions & 29 deletions tests/menu/TraitsTest.php

This file was deleted.

0 comments on commit e79b465

Please sign in to comment.