diff --git a/demo/app/Sharp/Dashboard/PostDashboard.php b/demo/app/Sharp/Dashboard/PostDashboard.php
new file mode 100644
index 000000000..4852162df
--- /dev/null
+++ b/demo/app/Sharp/Dashboard/PostDashboard.php
@@ -0,0 +1,87 @@
+addWidget(
+ SharpLineGraphWidget::make('visits_line')
+ ->setTitle('Visits')
+ ->setHeight(200)
+ ->setShowLegend(false)
+ ->setDisplayHorizontalAxisAsTimeline()
+ )
+ ->addWidget(
+ SharpFigureWidget::make('visit_count')
+ ->setTitle('Total visits'),
+ )
+ ->addWidget(
+ SharpFigureWidget::make('page_count')
+ ->setTitle('Total pageviews'),
+ );
+ }
+
+ protected function buildDashboardLayout(DashboardLayout $dashboardLayout): void
+ {
+ $dashboardLayout
+ ->addFullWidthWidget('visits_line')
+ ->addRow(fn (DashboardLayoutRow $row) => $row
+ ->addWidget(6, 'visit_count')
+ ->addWidget(6, 'page_count')
+ );
+ }
+
+ public function getFilters(): ?array
+ {
+ return [
+ PeriodRequiredFilter::class,
+ HiddenFilter::make('post'),
+ ];
+ }
+
+ protected function buildWidgetsData(): void
+ {
+ $visitCount = $this->getStartDate()->diffInDays($this->getEndDate()) * rand(10, 100);
+
+ $this
+ ->setFigureData('visit_count', figure: $visitCount)
+ ->setFigureData('page_count', figure: $visitCount * rand(2, 10))
+ ->addGraphDataSet(
+ 'visits_line',
+ SharpGraphWidgetDataSet::make(collect(CarbonPeriod::create($this->getStartDate(), $this->getEndDate()))
+ ->mapWithKeys(fn (Carbon $day, $k) => [
+ $day->isoFormat('L') => rand(10, 100),
+ ]))
+ ->setLabel('Visits')
+ ->setColor('#274754'),
+ );
+ }
+
+ protected function getStartDate(): Carbon
+ {
+ return $this->queryParams->filterFor(PeriodRequiredFilter::class)->getStart();
+ }
+
+ protected function getEndDate(): Carbon
+ {
+ return min(
+ $this->queryParams->filterFor(PeriodRequiredFilter::class)->getEnd(),
+ today()->subDay(),
+ );
+ }
+}
diff --git a/demo/app/Sharp/Entities/PostDashboardEntity.php b/demo/app/Sharp/Entities/PostDashboardEntity.php
new file mode 100644
index 000000000..ee868abdb
--- /dev/null
+++ b/demo/app/Sharp/Entities/PostDashboardEntity.php
@@ -0,0 +1,23 @@
+isAdmin();
+ }
+ };
+ }
+}
diff --git a/demo/app/Sharp/Posts/PostShow.php b/demo/app/Sharp/Posts/PostShow.php
index a2bcbc759..af8e10dbe 100644
--- a/demo/app/Sharp/Posts/PostShow.php
+++ b/demo/app/Sharp/Posts/PostShow.php
@@ -5,6 +5,7 @@
use App\Models\Post;
use App\Sharp\Entities\AuthorEntity;
use App\Sharp\Entities\PostBlockEntity;
+use App\Sharp\Entities\PostDashboardEntity;
use App\Sharp\Entities\PostEntity;
use App\Sharp\Posts\Commands\EvaluateDraftPostWizardCommand;
use App\Sharp\Posts\Commands\PreviewPostCommand;
@@ -14,6 +15,7 @@
use App\Sharp\Utils\Embeds\TableOfContentsEmbed;
use App\Sharp\Utils\Filters\CategoryFilter;
use Code16\Sharp\Form\Eloquent\Uploads\Transformers\SharpUploadModelFormAttributeTransformer;
+use Code16\Sharp\Show\Fields\SharpShowDashboardField;
use Code16\Sharp\Show\Fields\SharpShowEntityListField;
use Code16\Sharp\Show\Fields\SharpShowFileField;
use Code16\Sharp\Show\Fields\SharpShowListField;
@@ -64,6 +66,11 @@ protected function buildShowFields(FieldsContainer $showFields): void
->setLabel('File')
)
)
+ ->addField(
+ SharpShowDashboardField::make(PostDashboardEntity::class)
+ ->setLabel('Analytics')
+ ->hideFilterWithValue('post', fn ($instanceId) => $instanceId)
+ )
->addField(
SharpShowEntityListField::make(PostBlockEntity::class)
->setLabel('Blocks')
@@ -94,6 +101,7 @@ protected function buildShowLayout(ShowLayout $showLayout): void
$column->withField('content');
});
})
+ ->addDashboardSection(PostDashboardEntity::class)
->addEntityListSection(PostBlockEntity::class);
}
diff --git a/docs/.vitepress/sidebar.ts b/docs/.vitepress/sidebar.ts
index a8dae5976..855dfc670 100644
--- a/docs/.vitepress/sidebar.ts
+++ b/docs/.vitepress/sidebar.ts
@@ -57,7 +57,8 @@ export function sidebar(): DefaultTheme.SidebarItem[] {
{ text: 'Picture', link: '/guide/show-fields/picture.md' },
{ text: 'List', link: '/guide/show-fields/list.md' },
{ text: 'File', link: '/guide/show-fields/file.md' },
- { text: 'Embedded EntityList', link: '/guide/show-fields/embedded-entity-list.md' },
+ { text: 'Entity List', link: '/guide/show-fields/entity-list.md' },
+ { text: 'Dashboard', link: '/guide/show-fields/dashboard.md' },
// { text: 'Custom show field', link: '/guide/custom-show-fields.md' }
]
},
diff --git a/docs/guide/building-show-page.md b/docs/guide/building-show-page.md
index bef0cef03..987c63845 100644
--- a/docs/guide/building-show-page.md
+++ b/docs/guide/building-show-page.md
@@ -56,7 +56,7 @@ class MyShow extends SharpShow
Each available Show field is detailed below; here are the attributes they all share :
-- `setShowIfEmpty(bool $show = true): self`: by default, an empty field (meaning: with null or empty data) is not displayed at all in the Show UI. You can change this behaviour with this attribute. This method has no impact for [embedded EntityList](show-fields/embedded-entity-list.md).
+- `setShowIfEmpty(bool $show = true): self`: by default, an empty field (meaning: with null or empty data) is not displayed at all in the Show UI. You can change this behaviour with this attribute. This method has no impact for the [Entity List field](show-fields/entity-list.md).
#### Available simple Show fields
@@ -95,9 +95,9 @@ Notice that you have three possibilities for the actual code of this Entity List
As always with Sharp, implementation is up to you.
-The next thing to do is to scope the data of the embedded Entity List. In our case, we want to display and interact only with the products for this order... For this and more on personalization, refer to the detailed documentation of this field:
+The next thing to do is to scope the data of the Entity List field. In our case, we want to display and interact only with the products for this order... For this and more on personalization, refer to the detailed documentation of this field:
-- [embedded EntityList](show-fields/embedded-entity-list.md)
+- [Entity List field](show-fields/entity-list.md)
### `buildShowLayout(ShowLayout $showLayout): void`
diff --git a/docs/guide/entity-class.md b/docs/guide/entity-class.md
index ef5fa3f6a..60be6b019 100644
--- a/docs/guide/entity-class.md
+++ b/docs/guide/entity-class.md
@@ -202,5 +202,5 @@ You must remove all `->declareEntity()` calls in order to use `->declareEntityRe
:::
::: warning
-If you are using a custom entity resolver, you won’t be able to use the `SharpEntity` classes in the [menu](building-menu.md), or in [`LinkTo` links](link-to.md), or for [embedded entity lists](show-fields/embedded-entity-list.md): you will have to use the entity key instead. For instance: `LinkToForm::make('products', $id)`.
+If you are using a custom entity resolver, you won’t be able to use the `SharpEntity` classes in the [menu](building-menu.md), or in [`LinkTo` links](link-to.md), or for [Entity List fields](show-fields/entity-list.md): you will have to use the entity key instead. For instance: `LinkToForm::make('products', $id)`.
:::
diff --git a/docs/guide/show-fields/dashboard.md b/docs/guide/show-fields/dashboard.md
new file mode 100644
index 000000000..da25ea443
--- /dev/null
+++ b/docs/guide/show-fields/dashboard.md
@@ -0,0 +1,124 @@
+# Dashboard
+
+Class: `Code16\Sharp\Show\Fields\SharpShowDashboardField`.
+
+The field allows you to integrate a [Dashboard](../building-dashboard.md) into your Show Page.
+
+## Constructor
+
+This field needs, as first parameter, either the entity key or the `SharpDashboardEntity` class that declares the dashboard which will be included in the Show Page.
+
+For instance:
+
+```php
+SharpShowDashboardField::make('posts_dashboard')
+```
+
+or
+
+```php
+SharpShowDashboardField::make(PostDashboardEntity::class)
+```
+::: warning
+This last syntax is better in terms of DX (since it allows using the IDE to navigate to the Entity List implementation), but it won’t work in two specific cases: if you use a custom `SharpEntityResolver` or if you your Entity is declared with multiple keys.
+:::
+
+## Configuration
+
+### `hideFilterWithValue(string $filterName, $value)`
+This is the most important method of the field, since it will not only hide a filter but also set its value. The purpose is to allow to **scope the data to the instance** of the Show Page. For example, let’s say we display a Post and that we want to embed a dashboard with the post's statistics:
+
+
+```php
+class PostShow extends SharpShow
+{
+ // ...
+
+ public function buildShowFields(FieldsContainer $showFields): void
+ {
+ $showFields->addField(
+ SharpShowDashboardField::make(PostDashboardEntity::class)
+ ->hideFilterWithValue(PostFilter::class, 64)
+ );
+ }
+}
+```
+
+Here we're scoping the `PostDashboard` declared in the `PostDashboardEntity` to the instance of the `Post` with id 64.
+
+
+You can pass a closure as the value, and it will contain the current Show instance id. In most cases, you'll have to write this:
+
+```php
+SharpShowDashboardField::make(PostDashboardEntity::class)
+ ->hideFilterWithValue(PostFilter::class, fn ($instanceId) => $instanceId);
+```
+
+**One final note**: sometimes the linked filter is really just a scope, never displayed to the user. In this case, it can be tedious to write a full implementation in the Dashboard. In this situation, you can use the `HiddenFiler` class for the filter, passing a key:
+
+```php
+class PostShow extends SharpShow
+{
+ // ...
+
+ public function buildShowFields(FieldsContainer $showFields): void
+ {
+ $showFields->addField(
+ SharpShowDashboardField::make(PostDashboardEntity::class)
+ ->hideFilterWithValue('post', fn ($instanceId) => $instanceId);
+ );
+ }
+}
+```
+
+```php
+use \Code16\Sharp\EntityList\Filters\HiddenFilter;
+
+class PostDashboard extends SharpDashboard
+{
+ // ...
+
+ protected function getFilters(): ?array
+ {
+ return [
+ HiddenFilter::make('post')
+ ];
+ }
+
+ protected function buildWidgetsData(): void
+ {
+ return $this->setFigureData('visit_count',
+ figure: Post::query()
+ ->findOrFail($this->queryParams->filterFor('post'))
+ ->get()?->visit_count
+ );
+ }
+}
+```
+
+### `hideDashboardCommand(array|string $commands): self`
+
+Use it to hide any dashboard command in this particular Dashboard (useful when reusing a Dashboard). This will apply before looking at authorizations.
+
+## Display in layout
+
+To display your dashboard in your show page's layout, you have to use the `addDashboardSection()` method in your Show Page's `buildShowLayout()` method.
+
+```php
+protected function buildShowLayout(ShowLayout $showLayout): void
+ {
+ $showLayout
+ ->addSection(function (ShowLayoutSection $section) {
+ $section
+ ->addColumn(7, function (ShowLayoutColumn $column) {
+ $column
+ ->withFields(categories: 5, author: 7)
+ // ...
+ })
+ ->addColumn(5, function (ShowLayoutColumn $column) {
+ // ...
+ });
+ })
+ ->addDashboardSection(PostDashboardEntity::class);
+ }
+```
diff --git a/docs/guide/show-fields/embedded-entity-list.md b/docs/guide/show-fields/entity-list.md
similarity index 52%
rename from docs/guide/show-fields/embedded-entity-list.md
rename to docs/guide/show-fields/entity-list.md
index 89151f153..85b5e75e6 100644
--- a/docs/guide/show-fields/embedded-entity-list.md
+++ b/docs/guide/show-fields/entity-list.md
@@ -1,10 +1,12 @@
-# Embedded EntityList
+# Entity List
Class: `Code16\Sharp\Show\Fields\SharpShowEntityListField`
+The field allows you to integrate an [Entity List](../building-entity-list.md) into your Show Page.
+
## Constructor
-This field needs, as first parameter, either the entity key or the `SharpEntity` class that declares the Entity List which will be embedded.
+This field needs, as first parameter, either the entity key or the `SharpEntity` class that declares the Entity List which will be included in the Show Page.
For instance:
@@ -19,26 +21,26 @@ SharpShowEntityListField::make(ProductEntity::class)
```
::: warning
-This last syntax is better in terms of DX (since it allows to use the IDE to navigate to the Entity List implementation), but it won’t work in two specific cases: if you use a custom `SharpEntityResolver` or if you your Entity is declared with multiple keys.
+This last syntax is better in terms of DX (since it allows using the IDE to navigate to the Entity List implementation), but it won’t work in two specific cases: if you use a custom `SharpEntityResolver` or if you your Entity is declared with multiple keys.
:::
-To handle special (rare) cases you can provide a second string argument: in this case the first argument is the field key (as referred in the layout), and the second argument is the related Entity List key:
+To handle special cases, you can provide a second string argument: the first argument is the field key (as referred in the layout), and the second argument is the related Entity List key:
```php
SharpShowEntityListField::make('order_products', 'products')
```
::: tip
-Note that unlike every other Show Field, the `instance` of the Show don't have to expose an attribute named like that, since the EntityList data is gathered with a dedicated request.
+Note that unlike every other Show Field, the `instance` of the Show don't have to expose an attribute named like that, since the Entity List data is gathered with a dedicated request.
:::
-Embedded Entity List are really just regular Entity List presented in a Show page: we therefore need a full Entity List implementation, declared in an Entity. To scope the data to the `instance` of the Show, you can use `hideFilterWithValue()` (see below).
+Entity List fields really are just regular Entity List presented in a Show page: we therefore need a full Entity List implementation, declared in an Entity. To scope the data to the `instance` of the Show, you can use `hideFilterWithValue()` (see below).
## Configuration
### `hideFilterWithValue(string $filterName, $value)`
-This is the most important method of the field, since it will not only hide a filter, but also set its value. The purpose is to allow to **scope the data to the instance** of the Show Page. For example let’s say we display an Order and that we want to embed a list of its products:
+This is the most important method of the field, since it will not only hide a filter but also set its value. The purpose is to allow to **scope the data to the instance** of the Show Page. For example, let’s say we display an Order and that we want to embed a list of its products:
```php
class OrderShow extends SharpShow
@@ -55,7 +57,7 @@ class OrderShow extends SharpShow
}
```
-We defined here that we want to embed the Entity List defined in the `ProductEntity`, with its `OrderFilter` filter (which must be declared as usual in the Entity List implementation) hidden AND valued to `64` when gathering the data. In short: we want the products for the order of id `64`.
+We defined here that we want to display the Entity List defined in the `ProductEntity`, with its `OrderFilter` filter (which must be declared as usual in the Entity List implementation) hidden AND valued to `64` when gathering the data. In short: we want the products for the order of id `64`.
::: tip
Note on the filter name: passing its full classname will always work, but you can also directly pass its `key`, in case you defined one.
@@ -112,11 +114,11 @@ class OrderProductList extends SharpEntityList
### `hideEntityCommand(array|string $commands): self`
-Use it to hide any entity command in this particular embedded Entity List (useful when reusing an EntityList). This will apply before looking at authorizations.
+Use it to hide any entity command in this particular Entity List (useful when reusing an Entity List displayed elsewhere). This will apply before looking at authorizations.
### `hideInstanceCommand(array|string $commands): self`
-Use it to hide any instance command in this particular embedded Entity List (useful when reusing an Entity List). This will apply before looking at authorizations.
+Use it to hide any instance command in this particular Entity List (useful when reusing an Entity List). This will apply before looking at authorizations.
### `showEntityState(bool $showEntityState = true): self`
@@ -124,20 +126,43 @@ Use it to show or hide the EntityState label and command (useful when reusing an
### `showCreateButton(bool $showCreateButton = true): self`
-Use it to show or hide the create button in this particular embedded Entity List (useful when reusing an Entity List). This will apply before looking at authorizations.
+Use it to show or hide the "create" button in this particular Entity List (useful when reusing an Entity List). This will apply before looking at authorizations.
### `showReorderButton(bool $showReorderButton = true): self`
-Use it to show or hide the reorder button in this particular embedded Entity List (useful when reusing an Entity List). This will apply before looking at authorizations.
+Use it to show or hide the reorder button in this particular Entity List (useful when reusing an Entity List). This will apply before looking at authorizations.
### `showSearchField(bool $showSearchField = true): self`
-Use it to show or hide the search field in this particular embedded Entity List (useful when reusing an Entity List).
+Use it to show or hide the search field in this particular Entity List (useful when reusing an Entity List).
### `showCount(bool $showCount = true): self`
-Use it to show or hide the count of items in the embedded Entity List.
+Use it to show or hide the count of items in the Entity List.
+
+## Display in layout
+
+To display your entity list in your show page's layout, you have to use the `addEntityListSection()` method in your Show Page's `buildShowLayout()` method.
+
+```php
+protected function buildShowLayout(ShowLayout $showLayout): void
+ {
+ $showLayout
+ ->addSection(function (ShowLayoutSection $section) {
+ $section
+ ->addColumn(7, function (ShowLayoutColumn $column) {
+ $column
+ ->withFields(categories: 5, author: 7)
+ // ...
+ })
+ ->addColumn(5, function (ShowLayoutColumn $column) {
+ // ...
+ });
+ })
+ ->addEntityListSection(ProductEntity::class);
+ }
+```
## Transformer
-There is no transformer, since Sharp will NOT look for an attribute in the instance sent. The data of the Entity List is brought by a distinct XHR call, the same Sharp will use for a non embedded Entity List.
+There is no transformer, since Sharp will NOT look for an attribute in the instance sent. The data of the Entity List is brought by a distinct XHR call, the same Sharp will use for any Entity List.
diff --git a/resources/js/Pages/Dashboard/Dashboard.vue b/resources/js/Pages/Dashboard/Dashboard.vue
index 93a8b9caf..d2b919134 100644
--- a/resources/js/Pages/Dashboard/Dashboard.vue
+++ b/resources/js/Pages/Dashboard/Dashboard.vue
@@ -1,35 +1,17 @@
@@ -88,217 +61,18 @@
-
-