Skip to content

Commit ab3c019

Browse files
authored
Replaced chartable function by inbuilt resolve method (#30)
* Replaced Chartable function by inbuilt resolve method * Add hideFromIndex to InlinePanel * Removed Chartable from MetricValue Resource * Updated Readme * StyleCI fix * Added deprication warning to chartable readme * Style CI fix * Added deprecated annotation * Updated changelog * Spelling Fixes
1 parent c926e26 commit ab3c019

File tree

5 files changed

+46
-19
lines changed

5 files changed

+46
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ This document is meant for tracking notable changes to `nova-chartjs`
44
## 0.1.0
55
1. Chart metric values can now directly be added while creating new Chartable model.
66
2. Deprecated `RedirectsOnCreate` Trait
7+
8+
# 0.2.0
9+
1. Deprecated `chartable` method on `NovaChartjs`
10+
2. Added `hideFromIndex` option to `InlinePanel`

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class Employee extends Resource
167167
```
168168
**_NOTE:_** You must pass the `Resource` (i.e. `$this`) and `$request` to the `InlinePanel` component.
169169

170-
As an optional argument you can pass a chart name as the third argument, `showLabel` as fourth argument and `notEditable` as the fifth argument.
170+
As an optional argument you can pass a chart name as the third argument, `showLabel` as fourth argument, `notEditable` as the fifth argument and `hideFromIndex` as the sixth argument.
171171

172172
![Panel with Label](screenshots/PanelWithLabel.jpg "Panel with Label")
173173

@@ -189,7 +189,7 @@ class Employee extends Resource
189189

190190
NovaChartjs::make('Panel Name', 'novaChartjsMetricValue', function () {
191191
return $this->novaChartjsMetricValue->metric_values;
192-
})->chartable($this->resource ?? App::make($request->viaResource()::$model)),
192+
}),
193193
];
194194
}
195195
}

src/InlinePanel.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Laravel\Nova\Panel;
66
use Illuminate\Http\Request;
7-
use Illuminate\Support\Facades\App;
87
use App\Nova\Resource as NovaResource;
98
use KirschbaumDevelopment\NovaChartjs\Contracts\Chartable;
109

@@ -18,12 +17,13 @@ class InlinePanel extends Panel
1817
* @param string $panelTitle
1918
* @param bool $showLabel
2019
* @param bool $notEditable
20+
* @param bool $hideFromIndex
2121
*/
22-
public function __construct(NovaResource $resource, Request $request, $panelTitle = 'Chart Metric Values', $showLabel = false, $notEditable = false)
22+
public function __construct(NovaResource $resource, Request $request, $panelTitle = 'Chart Metric Values', $showLabel = false, $notEditable = false, $hideFromIndex = false)
2323
{
2424
parent::__construct(
2525
$panelTitle,
26-
$this->prepareFields($this->fields($resource->resource, $request, $panelTitle, $showLabel, $notEditable))
26+
$this->prepareFields($this->fields($resource->resource, $request, $panelTitle, $showLabel, $notEditable, $hideFromIndex))
2727
);
2828
}
2929

@@ -35,14 +35,15 @@ public function __construct(NovaResource $resource, Request $request, $panelTitl
3535
* @param mixed $panelTitle
3636
* @param bool $showLabel
3737
* @param bool $notEditable
38+
* @param bool $hideFromIndex
3839
*
3940
* @return array
4041
*/
41-
protected function fields(Chartable $chartable, Request $request, $panelTitle = 'Chart Metric Values', $showLabel = false, $notEditable = false): array
42+
protected function fields(Chartable $chartable, Request $request, $panelTitle = 'Chart Metric Values', $showLabel = false, $notEditable = false, $hideFromIndex = false): array
4243
{
4344
$field = NovaChartjs::make($panelTitle, 'novaChartjsMetricValue', function () use ($chartable) {
4445
return $chartable->novaChartjsMetricValue->metric_values ?? [];
45-
})->chartable($chartable ?? App::make($request->viaResource()::$model));
46+
});
4647

4748
if ($showLabel) {
4849
$field->showLabel();
@@ -52,6 +53,10 @@ protected function fields(Chartable $chartable, Request $request, $panelTitle =
5253
$field->notEditable();
5354
}
5455

56+
if ($hideFromIndex) {
57+
$field->hideFromIndex();
58+
}
59+
5560
return [
5661
$field,
5762
];

src/Nova/MetricValue.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use App\Nova\Resource;
66
use Illuminate\Http\Request;
77
use Laravel\Nova\Fields\MorphTo;
8-
use Illuminate\Support\Facades\App;
98
use KirschbaumDevelopment\NovaChartjs\NovaChartjs;
109
use KirschbaumDevelopment\NovaChartjs\Models\NovaChartjsMetricValue;
1110

@@ -42,7 +41,6 @@ class MetricValue extends Resource
4241
public function fields(Request $request)
4342
{
4443
$field = NovaChartjs::make('Metric Values', 'metric_values')
45-
->chartable($this->resource->chartable ?? App::make($request->viaResource()::$model))
4644
->rules('required', 'json');
4745
$field->showOnCreation = true;
4846

src/NovaChartjs.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Laravel\Nova\Fields\Field;
77
use Laravel\Nova\Http\Requests\NovaRequest;
88
use KirschbaumDevelopment\NovaChartjs\Contracts\Chartable;
9+
use KirschbaumDevelopment\NovaChartjs\Models\NovaChartjsMetricValue;
910

1011
class NovaChartjs extends Field
1112
{
@@ -34,6 +35,8 @@ public function __construct($name, $attribute = null, callable $resolveCallback
3435
}
3536

3637
/**
38+
* @deprecated This method has been deprecated and will be removed in next major update.
39+
*
3740
* Pass chartable model to NovaChartjs to fetch settings.
3841
*
3942
* @param Chartable|null $chartable
@@ -42,18 +45,35 @@ public function __construct($name, $attribute = null, callable $resolveCallback
4245
*/
4346
public function chartable(Chartable $chartable): self
4447
{
45-
$chartableClass = get_class($chartable);
48+
return $this;
49+
}
50+
51+
/**
52+
* Resolve the field's value.
53+
*
54+
* @param mixed $resource
55+
* @param string|null $attribute
56+
*/
57+
public function resolve($resource, $attribute = null)
58+
{
59+
parent::resolve($resource, $attribute);
4660

47-
$settings = $chartableClass::getNovaChartjsSettings();
61+
if ($resource instanceof NovaChartjsMetricValue) {
62+
$resource = $resource->chartable;
63+
}
4864

49-
return $this->withMeta([
50-
'settings' => $settings,
51-
'comparison' => $chartableClass::getNovaChartjsComparisonData(),
52-
'additionalDatasets' => $chartable->getAdditionalDatasets() ?? [],
53-
'model' => Str::singular(Str::title(Str::snake(class_basename($chartableClass), ' '))),
54-
'title' => $this->getChartableProp($chartable, $settings['titleProp'] ?? $chartable->getKeyName()),
55-
'ident' => $this->getChartableProp($chartable, $settings['identProp'] ?? $chartable->getKeyName()),
56-
]);
65+
if (! empty($resource)) {
66+
$settings = $resource::getNovaChartjsSettings();
67+
68+
$this->withMeta([
69+
'settings' => $settings,
70+
'comparison' => $resource::getNovaChartjsComparisonData(),
71+
'additionalDatasets' => $resource->getAdditionalDatasets() ?? [],
72+
'model' => Str::singular(Str::title(Str::snake(class_basename($resource), ' '))),
73+
'title' => $this->getChartableProp($resource, $settings['titleProp'] ?? $resource->getKeyName()),
74+
'ident' => $this->getChartableProp($resource, $settings['identProp'] ?? $resource->getKeyName()),
75+
]);
76+
}
5777
}
5878

5979
/**

0 commit comments

Comments
 (0)