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

Enhance Widget Layout #4559

Merged
35 changes: 21 additions & 14 deletions resources/views/dimmers.blade.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
@php
$dimmers = Voyager::dimmers();
$count = $dimmers->count();
$classes = [
'col-xs-12',
'col-sm-'.($count >= 2 ? '6' : '12'),
'col-md-'.($count >= 3 ? '4' : ($count >= 2 ? '6' : '12')),
];
$class = implode(' ', $classes);
$prefix = "<div class='{$class}'>";
$surfix = '</div>';
$dimmerGroups = Voyager::dimmers();
@endphp
@if ($dimmers->any())
<div class="clearfix container-fluid row">
{!! $prefix.$dimmers->setSeparator($surfix.$prefix)->display().$surfix !!}
</div>

@if (count($dimmerGroups))
@foreach($dimmerGroups as $dimmerGroup)
@php
$count = $dimmerGroup->count();
$classes = [
'col-xs-12',
'col-sm-'.($count >= 2 ? '6' : '12'),
'col-md-'.($count >= 3 ? '4' : ($count >= 2 ? '6' : '12')),
];
$class = implode(' ', $classes);
$prefix = "<div class='{$class}'>";
$surfix = '</div>';
@endphp
@if ($dimmerGroup->any())
<div class="clearfix container-fluid row">
{!! $prefix.$dimmerGroup->setSeparator($surfix.$prefix)->display().$surfix !!}
</div>
@endif
@endforeach
@endif
24 changes: 20 additions & 4 deletions src/Voyager.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,40 @@ public function actions()
}

/**
* Get a collection of the dashboard widgets.
* Get a collection of dashboard widgets.
* Each of our widget groups contain a max of three widgets.
* After that, we will switch to a new widget group.
*
* @return \Arrilot\Widgets\WidgetGroup
* @return array - Array consisting of \Arrilot\Widget\WidgetGroup objects
*/
public function dimmers()
{
$widgetClasses = config('voyager.dashboard.widgets');
$dimmers = Widget::group('voyager::dimmers');
$dimmerGroups = [];
$dimmerCount = 0;
$dimmers = Widget::group("voyager::dimmers-{$dimmerCount}");

foreach ($widgetClasses as $widgetClass) {
$widget = app($widgetClass);

if ($widget->shouldBeDisplayed()) {

// Every third dimmer, we consider out WidgetGroup filled.
// We switch that out with another WidgetGroup.
if ($dimmerCount % 3 === 0 && $dimmerCount !== 0) {
$dimmerGroups[] = $dimmers;
$dimmerGroupTag = ceil($dimmerCount / 3);
$dimmers = Widget::group("voyager::dimmers-{$dimmerGroupTag}");
}

$dimmers->addWidget($widgetClass);
$dimmerCount++;
}
}

return $dimmers;
$dimmerGroups[] = $dimmers;

return $dimmerGroups;
}

public function setting($key, $default = null)
Expand Down
35 changes: 29 additions & 6 deletions tests/Unit/VoyagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
class VoyagerTest extends TestCase
{
/**
* Dimmers returns collection of widgets.
* Dimmers returns an array filled with widget collections.
*
* This test will make sure that the dimmers method will give us a
* collection of the configured widgets.
* This test will make sure that the dimmers method will give us an array
* of the collection of the configured widgets.
*/
public function testDimmersReturnsCollectionOfConfiguredWidgets()
{
Expand All @@ -23,11 +23,11 @@ public function testDimmersReturnsCollectionOfConfiguredWidgets()

$dimmers = Voyager::dimmers();

$this->assertEquals(2, $dimmers->count());
$this->assertEquals(2, $dimmers[0]->count());
}

/**
* Dimmers returns collection of widgets which should be displayed.
* Dimmers returns an array filled with widget collections.
*
* This test will make sure that the dimmers method will give us a
* collection of the configured widgets which also should be displayed.
Expand All @@ -42,6 +42,29 @@ public function testDimmersReturnsCollectionOfConfiguredWidgetsWhichShouldBeDisp

$dimmers = Voyager::dimmers();

$this->assertEquals(1, $dimmers->count());
$this->assertEquals(1, $dimmers[0]->count());
}

/**
* Dimmers returns an array filled with widget collections.
*
* Tests that we build N / 3 (rounded up) widget collections where
* N is the total amount of widgets set in configuration
*/
public function testCreateEnoughDimmerCollectionsToContainAllAvailableDimmers()
{
Config::set('voyager.dashboard.widgets', [
'TCG\\Voyager\\Tests\\Stubs\\Widgets\\AccessibleDimmer',
'TCG\\Voyager\\Tests\\Stubs\\Widgets\\AccessibleDimmer',
'TCG\\Voyager\\Tests\\Stubs\\Widgets\\AccessibleDimmer',
'TCG\\Voyager\\Tests\\Stubs\\Widgets\\AccessibleDimmer',
'TCG\\Voyager\\Tests\\Stubs\\Widgets\\AccessibleDimmer',
]);

$dimmers = Voyager::dimmers();

$this->assertEquals(2, count($dimmers));
$this->assertEquals(3, $dimmers[0]->count());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion proves what you were trying to prove in the previous test, so I think the other one can be removed.

$this->assertEquals(2, $dimmers[1]->count());
}
}