Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 2, 2023
1 parent 3d6aec3 commit e95f2db
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 62 deletions.
2 changes: 1 addition & 1 deletion resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@section('content')
<div class="l-row l-row--column:sm:2 l-row--column:lg:3">
@foreach($widgets as $widget)
{!! $widget !!}
@include($widget['template'], $widget)
@endforeach
</div>
@endsection
2 changes: 1 addition & 1 deletion resources/views/resources/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@if(! empty($widgets))
<div class="l-row l-row--column:sm:2 l-row--column:lg:3">
@foreach($widgets as $widget)
{!! $widget !!}
@include($widget['template'], $widget)
@endforeach
</div>
@endif
Expand Down
10 changes: 1 addition & 9 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ public function getModalKey(): string
return sprintf('action-%s', $this->getKey());
}

/**
* Get the Blade template.
*/
public function getTemplate(): string
{
return $this->template;
}

/**
* Set the Eloquent query.
*/
Expand Down Expand Up @@ -213,7 +205,7 @@ public function toArray(): array
'key' => $this->getKey(),
'modalKey' => $this->getModalKey(),
'name' => $this->getName(),
'template' => $this->getTemplate(),
'template' => $this->template,
'url' => $this->getUri(),
];
}
Expand Down
3 changes: 2 additions & 1 deletion src/Fields/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

class Editor extends Field
{
use ResolvesFields;
use RegistersRoutes {
RegistersRoutes::registerRoutes as __registerRoutes;
}
use ResolvesFields;

/**
* The Blade template.
Expand All @@ -43,6 +43,7 @@ public function __construct(string $label, string $modelAttribute = null)

$this->config = Config::get('root.editor', []);
$this->height('350px');
$this->hiddenOn(['index']);
}

/**
Expand Down
10 changes: 1 addition & 9 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,6 @@ public function __construct(string $label, string $modelAttribute = null)
$this->setAttribute('class', 'form-control');
}

/**
* Get the template.
*/
public function getTemplate(): string
{
return $this->template;
}

/**
* Get the model attribute.
*/
Expand Down Expand Up @@ -538,7 +530,7 @@ public function toArray(): array
'label' => $this->label,
'prefix' => $this->prefix,
'suffix' => $this->suffix,
'template' => $this->getTemplate(),
'template' => $this->template,
'searchable' => $this->isSearchable(),
'sortable' => $this->isSortable(),
];
Expand Down
2 changes: 2 additions & 0 deletions src/Fields/ID.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ class ID extends Field
public function __construct(string $label = 'ID', string $modelAttribute = 'id')
{
parent::__construct($label, $modelAttribute);

$this->hiddenOn(['crate', 'update']);
}
}
16 changes: 12 additions & 4 deletions src/Fields/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public function as(string $field, Closure $callback = null): static
{
$this->field = new $field($this->label, $this->getModelAttribute());

if (! is_null($callback)) {
call_user_func_array($callback, [$this->field]);
}

$this->field->value(function (Request $request, Model $model): mixed {
return $this->resolveValue($request, $model);
});

if (! is_null($callback)) {
call_user_func_array($callback, [$this->field]);
}

return $this;
}

Expand Down Expand Up @@ -249,6 +249,14 @@ public function toArray(): array
return $this->field->toArray();
}

/**
* {@inheritdoc}
*/
public function toDisplay(Request $request, Model $model): array
{
return $this->field->toDisplay($request, $model);
}

/**
* {@inheritdoc}
*/
Expand Down
24 changes: 24 additions & 0 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,30 @@ public function getValue(Model $model): mixed
return $model->getAttribute($name);
}

/**
* {@inheritdoc}
*/
public function resolveFormat(Request $request, Model $model): mixed
{
if (is_null($this->formatResolver)) {
$this->formatResolver = function (Request $request, Model $model): mixed {
$default = $this->getValue($model);

if ($default instanceof Model) {
return $this->resolveDisplay($default);
} elseif ($default instanceof Collection) {
return $default->map(function (Model $related): mixed {
return $this->resolveDisplay($related);
})->join(', ');
}

return $default;
};
}

return parent::resolveFormat($request, $model);
}

/**
* Set the query resolver.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DashboardController extends Controller
public function __invoke(Request $request, Root $root): Response
{
return ResponseFactory::view('root::dashboard', [
'widgets' => $root->widgets->all(),
'widgets' => $root->widgets->toArray(),
]);
}
}
28 changes: 22 additions & 6 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ public function paginate(Request $request): LengthAwarePaginator
'id' => $model->getKey(),
'url' => $this->modelUrl($model),
'model' => $model,
'fields' => $this->resolveFields($request)->mapToDisplay($request, $model),
'fields' => $this->resolveFields($request)
->authorized($request, $model)
->visible('index')
->mapToDisplay($request, $model),
];
});
}
Expand Down Expand Up @@ -315,11 +318,18 @@ public function toIndex(Request $request): array
{
return array_merge($this->toArray(), [
'title' => $this->getName(),
'actions' => $this->resolveActions($request)->mapToForms($request),
'actions' => $this->resolveActions($request)
->authorized($request)
->visible('index')
->mapToForms($request),
'data' => $this->paginate($request),
'widgets' => $this->resolveWidgets($request)->all(),
'widgets' => $this->resolveWidgets($request)
->authorized($request)
->visible('index')
->toArray(),
'perPageOptions' => $this->getPerPageOptions(),
'filters' => $this->resolveFilters($request)
->authorized($request)
->renderable()
->map(function (RenderableFilter $filter) use ($request): array {
return $filter->toField()->toInput($request, $this->getModelInstance());
Expand All @@ -339,7 +349,10 @@ public function toCreate(Request $request): array
'model' => $model = $this->getModelInstance(),
'action' => $this->getUri(),
'method' => 'POST',
'fields' => $this->resolveFields($request)->mapToInputs($request, $model),
'fields' => $this->resolveFields($request)
->authorized($request, $model)
->visible('update')
->mapToInputs($request, $model),
]);
}

Expand All @@ -349,11 +362,14 @@ public function toCreate(Request $request): array
public function toEdit(Request $request, Model $model): array
{
return array_merge($this->toArray(), [
'title' => '',
'title' => __('Edit :model', ['model' => sprintf('%s #%s', $this->getModelName(), $model->getKey())]),
'model' => $model,
'action' => $this->modelUrl($model),
'method' => 'PATCH',
'fields' => $this->resolveFields($request)->mapToInputs($request, $model),
'fields' => $this->resolveFields($request)
->authorized($request, $model)
->visible('update')
->mapToInputs($request, $model),
]);
}
}
19 changes: 17 additions & 2 deletions src/Widgets/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

namespace Cone\Root\Widgets;

use Cone\Root\Support\Element;
use Cone\Root\Traits\Authorizable;
use Cone\Root\Traits\HasAttributes;
use Cone\Root\Traits\Makeable;
use Cone\Root\Traits\ResolvesVisibility;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Str;
use JsonSerializable;

abstract class Widget extends Element
abstract class Widget implements Arrayable, JsonSerializable
{
use Authorizable;
use HasAttributes;
use Makeable;
use ResolvesVisibility;

/**
* The Blade template.
Expand All @@ -33,14 +38,24 @@ public function getName(): string
return __(Str::of(static::class)->classBasename()->headline()->value());
}

/**
* Convert the element to a JSON serializable format.
*/
public function jsonSerialize(): mixed
{
return $this->toArray();
}

/**
* Convert the widget to an array.
*/
public function toArray(): array
{
return [
'attrs' => $this->newAttributeBag(),
'key' => $this->getKey(),
'name' => $this->getName(),
'template' => $this->template,
];
}
}
38 changes: 10 additions & 28 deletions src/Widgets/Widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,38 @@

namespace Cone\Root\Widgets;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\ForwardsCalls;

class Widgets
class Widgets extends Collection
{
use ForwardsCalls;

/**
* The widgets collection.
*/
protected Collection $widgets;

/**
* Create a new widgets instance.
*/
public function __construct(array $widgets = [])
{
$this->widgets = new Collection($widgets);
}

/**
* Register the given widgets.
*/
public function register(array|Widget $widgets): static
{
foreach (Arr::wrap($widgets) as $widget) {
$this->widgets->push($widget);
$this->push($widget);
}

return $this;
}

/**
* Make a new widget instance.
* Filter the fields that are available for the current request and model.
*/
public function widget(string $widget, ...$params): Widget
public function authorized(Request $request, Model $model = null): static
{
$instance = new $widget(...$params);

$this->register($instance);

return $instance;
return $this->filter->authorized($request, $model)->values();
}

/**
* Handle the dynamic method call.
* Filter the fields that are visible in the given context.
*/
public function __call($method, $parameters): mixed
public function visible(string|array $context): static
{
return $this->forwardCallTo($this->widgets, $method, $parameters);
return $this->filter->visible($context)->values();
}
}

0 comments on commit e95f2db

Please sign in to comment.