diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php
index f60a89b72..714136238 100644
--- a/resources/views/dashboard.blade.php
+++ b/resources/views/dashboard.blade.php
@@ -6,53 +6,8 @@
{{-- Content --}}
@section('content')
-
-
-
+ @foreach($widgets as $widget)
+ {!! $widget !!}
+ @endforeach
@endsection
diff --git a/resources/views/resources/index.blade.php b/resources/views/resources/index.blade.php
index df23c308b..2d372c7f3 100644
--- a/resources/views/resources/index.blade.php
+++ b/resources/views/resources/index.blade.php
@@ -5,13 +5,13 @@
{{-- Content --}}
@section('content')
-{{-- @if($widgets->isNotEmpty())
+@if(! empty($widgets))
- @foreach($widgets->all() as $widget)
+ @foreach($widgets as $widget)
{!! $widget !!}
@endforeach
-@endif --}}
+@endif
{!! $table !!}
@endsection
diff --git a/resources/views/widgets/trend.blade.php b/resources/views/widgets/trend.blade.php
new file mode 100644
index 000000000..e499053ea
--- /dev/null
+++ b/resources/views/widgets/trend.blade.php
@@ -0,0 +1,13 @@
+
diff --git a/resources/views/widgets/widget.blade.php b/resources/views/widgets/widget.blade.php
index dcfa88f8a..dead391ff 100644
--- a/resources/views/widgets/widget.blade.php
+++ b/resources/views/widgets/widget.blade.php
@@ -1,22 +1 @@
-
+
diff --git a/src/Form/Fields/Fields.php b/src/Form/Fields/Fields.php
index b3f9a0be8..107036e46 100644
--- a/src/Form/Fields/Fields.php
+++ b/src/Form/Fields/Fields.php
@@ -5,6 +5,7 @@
use Closure;
use Cone\Root\Form\Form;
use Illuminate\Http\Request;
+use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\ForwardsCalls;
@@ -31,6 +32,18 @@ public function __construct(Form $form, array $fields = [])
$this->fields = new Collection($fields);
}
+ /**
+ * Register the given fields.
+ */
+ public function register(array|Field $fields): static
+ {
+ foreach (Arr::wrap($fields) as $field) {
+ $this->fields->push($field);
+ }
+
+ return $this;
+ }
+
/**
* Add a new field to the collection.
*/
@@ -38,7 +51,7 @@ public function field(string $field, string $label, string $modelAttribute = nul
{
$instance = new $field($this->form, $label, $modelAttribute, ...$params);
- $this->push($instance);
+ $this->register($field);
return $instance;
}
diff --git a/src/Http/Controllers/DashboardController.php b/src/Http/Controllers/DashboardController.php
index d9853d061..5026c6404 100644
--- a/src/Http/Controllers/DashboardController.php
+++ b/src/Http/Controllers/DashboardController.php
@@ -15,7 +15,7 @@ class DashboardController extends Controller
public function __invoke(Request $request, Root $root): Response
{
return ResponseFactory::View('root::dashboard', [
- 'widgets' => $root->widgets,
+ 'widgets' => $root->widgets->all(),
]);
}
}
diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php
index 1b140c73d..9466b9750 100644
--- a/src/Resources/Resource.php
+++ b/src/Resources/Resource.php
@@ -243,6 +243,7 @@ public function toIndex(Request $request): array
return array_merge($this->toArray(), [
'title' => $this->getName(),
'table' => $this->toTable($request, $this->resolveQuery($request)),
+ 'widgets' => $this->resolveWidgets($request)->all(),
]);
}
diff --git a/src/Traits/ResolvesWidgets.php b/src/Traits/ResolvesWidgets.php
index 939f06ca2..0e0ff9f54 100644
--- a/src/Traits/ResolvesWidgets.php
+++ b/src/Traits/ResolvesWidgets.php
@@ -2,31 +2,46 @@
namespace Cone\Root\Traits;
-use Closure;
+use Cone\Root\Widgets\Widget;
use Cone\Root\Widgets\Widgets;
+use Illuminate\Http\Request;
trait ResolvesWidgets
{
/**
- * The resolved widgets.
+ * The widgets collection.
*/
- public readonly Widgets $widgets;
+ protected ?Widgets $widgets = null;
/**
* Define the widgets.
*/
- public function widgets(): array
+ public function widgets(Request $request): array
{
return [];
}
/**
- * Apply the callback on the widgets.
+ * Resolve the widgets collection.
*/
- public function withWidgets(Closure $callback): static
+ public function resolveWidgets(Request $request): Widgets
{
- call_user_func_array($callback, [$this->widgets]);
+ if (is_null($this->widgets)) {
+ $this->widgets = new Widgets($this->widgets($request));
- return $this;
+ $this->widgets->each(function (Widget $widget) use ($request): void {
+ $this->resolveWidget($request, $widget);
+ });
+ }
+
+ return $this->widgets;
+ }
+
+ /**
+ * Handle the callback for the widget resolution.
+ */
+ protected function resolveWidget(Request $request, Widget $widget): void
+ {
+ //
}
}
diff --git a/src/Widgets/Metric.php b/src/Widgets/Metric.php
index bff06bfd4..cd3089263 100644
--- a/src/Widgets/Metric.php
+++ b/src/Widgets/Metric.php
@@ -2,7 +2,29 @@
namespace Cone\Root\Widgets;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\App;
+use Illuminate\Support\Facades\Redis;
+
abstract class Metric extends Widget
{
- //
+ /**
+ * Calculate the metric data.
+ */
+ abstract public function calculate(Request $request): array;
+
+ /**
+ * Get the data.
+ */
+ public function toArray(): array
+ {
+ return array_merge(
+ parent::toArray(),
+ App::call(function (Request $request): array {
+ return [
+ 'data' => $this->calculate($request),
+ ];
+ }),
+ );
+ }
}
diff --git a/src/Widgets/Trend.php b/src/Widgets/Trend.php
new file mode 100644
index 000000000..8fc67fa94
--- /dev/null
+++ b/src/Widgets/Trend.php
@@ -0,0 +1,21 @@
+