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

docs #490

Merged
merged 4 commits into from
Jun 5, 2024
Merged

docs #490

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 40 additions & 40 deletions public/vendor/moonshine/assets/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/vendor/moonshine/assets/main.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions public/vendor/moonshine/assets/minimalistic.css

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions public/vendor/moonshine/libs/apexcharts/apexcharts.min.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions public/vendor/moonshine/libs/easymde/easymde.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions public/vendor/moonshine/libs/easymde/easymde.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions public/vendor/moonshine/libs/easymde/purify.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions public/vendor/moonshine/libs/easymde/purify.min.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions public/vendor/moonshine/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"isEntry": true,
"src": "resources/css/main.css"
},
"resources/css/minimalistic.css": {
"file": "assets/minimalistic.css",
"isEntry": true,
"src": "resources/css/minimalistic.css"
},
"resources/fonts/Gilroy-Black.woff2": {
"file": "fonts/Gilroy-Black.woff2",
"src": "resources/fonts/Gilroy-Black.woff2"
Expand Down
46 changes: 44 additions & 2 deletions resources/views/pages/en/components/metric_donut_chart.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
:sectionMenu="[
'Sections' => [
['url' => '#make', 'label' => 'Make'],
['url' => '#colors', 'label' => 'Colors'],
['url' => '#column-span', 'label' => 'Block width'],
]
]"
Expand Down Expand Up @@ -46,8 +47,49 @@ public function components(): array
//...
</x-code>

<x-image theme="light" src="{{ asset('screenshots/donut_chart_metric.png') }}"></x-image>
<x-image theme="dark" src="{{ asset('screenshots/donut_chart_metric_dark.png') }}"></x-image>
<x-moonshine::grid>
{!!
\MoonShine\Metrics\DonutChartMetric::make('Subscribers')
->values(['CutCode' => 10000, 'Apple' => 9999])
->columnSpan(4)
!!}
</x-moonshine::grid>

<x-sub-title id="colors">Colors</x-sub-title>

<x-p>
The <code>colors()</code> method allows you to specify colors for the metric.
</x-p>

<x-code language="php">
colors(array|Closure $values)
</x-code>

<x-code language="php">
use MoonShine\Metrics\DonutChartMetric;

//...

public function components(): array
{
return [
DonutChartMetric::make('Subscribers')
->values(['CutCode' => 10000, 'Apple' => 9999])
->colors(['#ffcc00', '#00bb00']) // [tl! focus]
];
}

//...
</x-code>

<x-moonshine::grid>
{!!
\MoonShine\Metrics\DonutChartMetric::make('Subscribers')
->values(['CutCode' => 10000, 'Apple' => 9999])
->colors(['#ffcc00', '#00bb00'])
->columnSpan(4)
!!}
</x-moonshine::grid>

<x-sub-title id="column-span">Block width</x-sub-title>

Expand Down
4 changes: 4 additions & 0 deletions resources/views/pages/en/recipes.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
['url' => '#update-on-preview-pivot', 'label' => 'updateOnPreview for pivot fields'],
['url' => '#hasmany-parent-id', 'label' => 'Parent ID in HasMany'],
['url' => '#tinymce-limit-preview', 'label' => 'TinyMce number of characters in preview'],
['url' => '#change-field-logic', 'label' => 'Changing field logic'],
]
]"
>
Expand All @@ -28,5 +29,8 @@
@include('pages.en.recipes.update-on-preview-pivot', ['title' => 'updateOnPreview for pivot fields'])
@include('pages.en.recipes.hasmany-parent-id', ['title' => 'Parent ID in HasMany'])
@include('pages.en.recipes.tinymce-limit-preview', ['title' => 'TinyMce number of characters in preview'])
@include('pages.en.recipes.change-field-logic', [
'title' => 'An example of changing the behavior logic of the Image field to save paths to images in a separate database table.'
])

</x-page>
73 changes: 73 additions & 0 deletions resources/views/pages/en/recipes/change-field-logic.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<x-recipe id="change-field-logic" title="{{ $title ?? 'Recipe' }}">

<x-ul>
<li>
To solve this problem, you need to block the <code>onApply()</code> method
and moved the logic to <code>onAfterApply()</code>.<br />
This will get the parent model on the creation page.<br />
We will have access to the model and we will be able to work with its relationships.
</li>
<li>
The <code>onAfterApply()</code> method stores and retrieves old and current values,
also cleaning deleted files.
</li>
<li>
After deleting the parent record, the <code>onAfterDestroy()</code> method deletes the downloaded files.
</li>
</x-ul>

<x-code language="php">
use MoonShine\Fields\Image;

//...

Image::make('Images', 'images')
->multiple()
->removable()
->changeFill(function (Model $data, Image $field) {
// return $data->images->pluck('file');
// or raw
return DB::table('images')->pluck('file');
})
->onApply(function (Model $data) {
// block onApply
return $data;
})
->onAfterApply(function (Model $data, false|array $values, Image $field) {
// $field->getRemainingValues(); values that remained in the form taking into account deletions
// $field->toValue(); current images
// $field->toValue()->diff($field->getRemainingValues()) deleted images

if($values !== false) {
foreach ($values as $value) {
DB::table('images')->insert([
'file' => $field->store($value),
]);
}
}

foreach ($field->toValue()->diff($field->getRemainingValues()) as $removed) {
DB::table('images')->where('file', $removed)->delete();
Storage::disk('public')->delete($removed);
}

// or $field->removeExcludedFiles();

return $data;
})
->onAfterDestroy(function (Model $data, mixed $values, Image $field) {
foreach ($values as $value) {
Storage::disk('public')->delete($value);
}

return $data;
})

//...
</x-code>

<x-moonshine::alert type="warning" icon="heroicons.information-circle">
The code comments out the relation option and provides an example of natively obtaining file paths from another table.
</x-moonshine::alert>

</x-recipe>
Loading
Loading