Skip to content

Commit 5b12206

Browse files
committed
Fix empty slots and floating widgets
1 parent 3fcb469 commit 5b12206

File tree

6 files changed

+100
-36
lines changed

6 files changed

+100
-36
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
![invaders-xx-gridstack-dashboard](https://github.com/invaders-xx/filament-gridstack-dashboard/assets/604907/7b94f470-9e83-4cc5-95af-e5794db76feb)
32

43
# Create and manage filament Dashboards using gridstack js
@@ -129,6 +128,21 @@ public function panel(Panel $panel): Panel
129128
}
130129
```
131130

131+
You can enable/disable floating widgets (default: true).
132+
133+
```php
134+
use InvadersXX\FilamentGridstackDashboard\GridstackDashboardPlugin;
135+
136+
public function panel(Panel $panel): Panel
137+
{
138+
return $panel
139+
->plugins([
140+
GridstackDashboardPlugin::make()
141+
->float(false),
142+
])
143+
}
144+
```
145+
132146
You can configure the navigationIcon, the navigationGroup and the navigationSort
133147

134148
```php

resources/dist/components/filament-gridstack-dashboard.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {GridStack} from 'gridstack';
33
export default function gridStackDashboard({
44
columns = 12,
55
rows = 0,
6+
float = true
67
}) {
78
return {
89
grid: null,
@@ -20,6 +21,7 @@ export default function gridStackDashboard({
2021
cellHeight: 80,
2122
column: columns,
2223
row: rows,
24+
float: float,
2325
acceptWidgets: true,
2426
removable: '#trash',
2527
alwaysShowResizeHandle: true,

resources/views/pages/dashboard.blade.php

+23-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Filament\Widgets\WidgetConfiguration;
44
$columns = $this->getColumns();
55
$rows = $this->getRows();
6+
$float = $this->getFloat();
67
@endphp
78

89
<x-filament-panels::page class="fi-dashboard-page">
@@ -14,7 +15,8 @@
1415
ax-load-src="{{ FilamentAsset::getAlpineComponentSrc('filament-gridstack-dashboard-script', 'invaders-xx/filament-gridstack-dashboard') }}"
1516
x-data="gridStackDashboard({
1617
columns:{{ $columns }},
17-
rows: {{ $rows }}
18+
rows: {{ $rows }},
19+
float: {{ $float }}
1820
})"
1921
x-load-css="[@js(FilamentAsset::getStyleHref('filament-gridstack-dashboard-styles', package: 'invaders-xx/filament-gridstack-dashboard'))]"
2022
class="text-center"
@@ -94,19 +96,26 @@ class="border-danger-500 bg-danger-500 flex items-center border p-6 text-center"
9496
@foreach ($this->buildGridItemsForDesign() as $row => $widgets)
9597
<x-filament::grid :default="$columns" class="gap-6">
9698
@foreach ($widgets as $widgetKey => $widget)
97-
@php
98-
$widgetClass = $normalizeWidgetClass($widget['id']);
99-
@endphp
100-
<x-filament::grid.column
101-
class="fi-wi-widget"
102-
:default="$widget['w']">
103-
@livewire($widgetClass,
104-
[...$widget['id'] instanceof \Filament\Widgets\WidgetConfiguration?
105-
[...$widget['id']->widget::getDefaultProperties(), ...$widget['id']->getProperties()]:
106-
$widget['id']::getDefaultProperties(),...$data,],
107-
key("{$widgetClass}-{$widgetKey}")
108-
)
109-
</x-filament::grid.column>
99+
@if($widget['id']===null)
100+
<x-filament::grid.column
101+
class="fi-wi-widget"
102+
:default="$widget['w']">
103+
</x-filament::grid.column>
104+
@else
105+
@php
106+
$widgetClass = $normalizeWidgetClass($widget['id']);
107+
@endphp
108+
<x-filament::grid.column
109+
class="fi-wi-widget"
110+
:default="$widget['w']">
111+
@livewire($widgetClass,
112+
[...$widget['id'] instanceof \Filament\Widgets\WidgetConfiguration?
113+
[...$widget['id']->widget::getDefaultProperties(), ...$widget['id']->getProperties()]:
114+
$widget['id']::getDefaultProperties(),...$data,],
115+
key("{$widgetClass}-{$widgetKey}")
116+
)
117+
</x-filament::grid.column>
118+
@endif
110119
@endforeach
111120
</x-filament::grid>
112121
@endforeach

src/Filament/Pages/Dashboard.php

+43-18
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function getRows(): int
4343
return GridstackDashboardPlugin::get()->getRows() ?? 0;
4444
}
4545

46+
public function getFloat(): bool
47+
{
48+
return GridstackDashboardPlugin::get()->getFloat() ?? false;
49+
}
50+
4651
public function saveLayout(): void
4752
{
4853
$data = collect($this->gridItems)->sortBy([
@@ -90,34 +95,54 @@ public function getFilteredWidgets(): array
9095

9196
public function buildGridItemsForDesign(): array
9297
{
93-
$return = [];
98+
$data = [];
9499
$this->gridItems = [];
95100
foreach ($this->getVisibleWidgetsForGrid() as $widget) {
96-
$widgetInstance = app()->make($widget['widget']);
97-
98-
$label = match (true) {
99-
$widgetInstance instanceof TableWidget => (string) invade($widgetInstance)->makeTable()->getHeading(),
100-
! ($widgetInstance instanceof TableWidget) && $widgetInstance instanceof Widget && method_exists(
101-
$widgetInstance,
102-
'getHeading'
103-
) => (string) invade($widgetInstance)->getHeading(),
104-
default => str($widget['widget'])
105-
->afterLast('\\')
106-
->headline()
107-
->toString()
108-
};
101+
if (! isset($data[$widget['y']])) {
102+
$data[$widget['y']] = [];
103+
}
104+
$label = null;
105+
if ($widget['widget']) {
106+
$widgetInstance = app()->make($widget['widget']);
107+
108+
$label = match (true) {
109+
$widgetInstance instanceof TableWidget => (string) invade($widgetInstance)->makeTable()->getHeading(),
110+
! ($widgetInstance instanceof TableWidget) && $widgetInstance instanceof Widget && method_exists(
111+
$widgetInstance,
112+
'getHeading'
113+
) => (string) invade($widgetInstance)->getHeading(),
114+
default => str($widget['widget'])
115+
->afterLast('\\')
116+
->headline()
117+
->toString()
118+
};
119+
}
109120
$item['id'] = $widget['widget'];
110121
$item['w'] = $widget['w'];
111122
$item['x'] = $widget['x'];
112123
$item['y'] = $widget['y'];
113124
$item['content'] = $label;
114125
$item['resizeHandles'] = 'e,w';
115-
if (! isset($return[$item['y']])) {
116-
$return[$item['y']] = [];
117-
}
118-
$return[$item['y']][] = $item;
126+
$data[$item['y']][] = $item;
119127
$this->gridItems[] = $item;
120128
}
129+
$return = [];
130+
foreach ($data as $row => $widgets) {
131+
$pos = 0;
132+
foreach ($widgets as $widget) {
133+
if ($pos !== $widget['x']) {
134+
$size = abs($widget['x'] - $pos);
135+
$return[$row][] = [
136+
'id' => null,
137+
'y' => $row,
138+
'x' => $pos,
139+
'w' => $size,
140+
];
141+
}
142+
$pos = $widget['x'] + $widget['w'];
143+
$return[$row][] = $widget;
144+
}
145+
}
121146

122147
return $return;
123148
}

src/GridstackDashboardPlugin.php

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class GridstackDashboardPlugin implements Plugin
2727

2828
protected int|Closure|null $rows = 0;
2929

30+
protected bool|Closure|null $float = true;
31+
3032
public static function make(): static
3133
{
3234
return app(static::class);
@@ -81,6 +83,13 @@ public function rows(int|Closure $rows): static
8183
return $this;
8284
}
8385

86+
public function float(bool|Closure $float): static
87+
{
88+
$this->float = $float;
89+
90+
return $this;
91+
}
92+
8493
public function navigationSort(int|Closure $navigationSort): static
8594
{
8695
$this->navigationSort = $navigationSort;
@@ -137,6 +146,11 @@ public function getRows(): int
137146
return $this->evaluate($this->rows);
138147
}
139148

149+
public function getFloat(): bool
150+
{
151+
return $this->evaluate($this->float);
152+
}
153+
140154
public function boot(Panel $panel): void
141155
{
142156
}

0 commit comments

Comments
 (0)