init
This commit is contained in:
21 files changed
+12224
No files matched your search
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaRowActions;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
use Laravel\Nova\Actions\Action;
|
||||
use Laravel\Nova\Fields\Field;
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
use Stringable;
|
||||
|
||||
class RowActions extends Field
|
||||
{
|
||||
/**
|
||||
* The field's Vue component.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $component = 'row-actions';
|
||||
|
||||
/** @var array<int, RowAction> */
|
||||
private array $rowActions = [];
|
||||
|
||||
public function __construct($name = null, $attribute = null, ?callable $resolveCallback = null)
|
||||
{
|
||||
parent::__construct($name ?? trans('Actions'), $attribute ?? 'row_actions', $resolveCallback);
|
||||
|
||||
$this->onlyOnIndex()->textAlign('center');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Nova action as a visible index-row button.
|
||||
*
|
||||
* Callbacks receive the row model, current Nova request, and action instance.
|
||||
* A null confirmation value preserves the action's native confirmation setting.
|
||||
*
|
||||
* @param Closure(Model, NovaRequest, Action): (Stringable|string|null)|Stringable|string|null $label
|
||||
* @param Closure(Model, NovaRequest, Action): (Stringable|string|null)|Stringable|string|null $tooltip
|
||||
* @param Closure(Model, NovaRequest, Action): bool|bool $activeWhen
|
||||
* @param Closure(Model, NovaRequest, Action): bool|bool $disabledWhen
|
||||
* @param Closure(Model, NovaRequest, Action): bool|bool $visibleWhen
|
||||
*/
|
||||
public function add(
|
||||
Action $action,
|
||||
string $icon = 'play',
|
||||
string $color = 'default',
|
||||
string $variant = 'outline',
|
||||
Closure|Stringable|string|null $label = null,
|
||||
Closure|Stringable|string|null $tooltip = null,
|
||||
Closure|bool $activeWhen = false,
|
||||
Closure|bool $disabledWhen = false,
|
||||
Closure|bool $visibleWhen = true,
|
||||
?int $order = null,
|
||||
?bool $confirm = null,
|
||||
bool $showLabel = false,
|
||||
): static {
|
||||
$sequence = count($this->rowActions);
|
||||
|
||||
$this->rowActions[] = new RowAction(
|
||||
action: $action,
|
||||
icon: $icon,
|
||||
color: $color,
|
||||
variant: $variant,
|
||||
label: $label,
|
||||
tooltip: $tooltip,
|
||||
activeWhen: $activeWhen,
|
||||
disabledWhen: $disabledWhen,
|
||||
visibleWhen: $visibleWhen,
|
||||
order: $order ?? $sequence,
|
||||
confirm: $confirm,
|
||||
showLabel: $showLabel,
|
||||
sequence: $sequence,
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the row-specific state and the native Nova action payload.
|
||||
*/
|
||||
#[\Override]
|
||||
public function resolveForDisplay($resource, ?string $attribute = null): void
|
||||
{
|
||||
parent::resolveForDisplay($resource, $attribute);
|
||||
|
||||
if (! $resource instanceof Model) {
|
||||
$this->withMeta(['actions' => []]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var NovaRequest $request */
|
||||
$request = app(NovaRequest::class);
|
||||
|
||||
$this->withMeta([
|
||||
'actions' => $this->serializedActions($request, $resource),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
private function serializedActions(NovaRequest $request, Model $model): array
|
||||
{
|
||||
return Collection::make($this->rowActions)
|
||||
->sortBy([
|
||||
['order', 'asc'],
|
||||
['sequence', 'asc'],
|
||||
])
|
||||
->filter(fn (RowAction $button) => $this->evaluate($button->visibleWhen, $model, $request, $button->action))
|
||||
->filter(fn (RowAction $button) => $this->authorized($button->action, $request, $model))
|
||||
->values()
|
||||
->map(function (RowAction $button, int $index) use ($request, $model): array {
|
||||
$action = $button->action;
|
||||
$action->resource = $model;
|
||||
$nativeAction = $action->jsonSerialize();
|
||||
|
||||
if ($button->confirm !== null) {
|
||||
$nativeAction['withoutConfirmation'] = ! $button->confirm;
|
||||
}
|
||||
|
||||
$label = $this->text($button->label, $model, $request, $action) ?? (string) $action->name();
|
||||
$tooltip = $this->text($button->tooltip, $model, $request, $action) ?? $label;
|
||||
|
||||
return [
|
||||
'key' => $action->uriKey().'-'.$button->sequence.'-'.$index,
|
||||
'action' => $nativeAction,
|
||||
'icon' => $button->icon,
|
||||
'color' => $button->color,
|
||||
'variant' => $button->variant,
|
||||
'label' => $label,
|
||||
'tooltip' => $tooltip,
|
||||
'active' => $this->evaluate($button->activeWhen, $model, $request, $action),
|
||||
'disabled' => $this->evaluate($button->disabledWhen, $model, $request, $action),
|
||||
'showLabel' => $button->showLabel,
|
||||
'order' => $button->order,
|
||||
];
|
||||
})
|
||||
->all();
|
||||
}
|
||||
|
||||
private function authorized(Action $action, NovaRequest $request, Model $model): bool
|
||||
{
|
||||
if (! $action->authorizedToSee($request) || ! $action->authorizedToRun($request, $model)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Nova treats an explicit canRun callback as the complete execution policy.
|
||||
if ($action->runCallback !== null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $request->newResourceWith($model)->authorizedToRunAction($request, $action);
|
||||
}
|
||||
|
||||
private function evaluate(Closure|bool $value, Model $model, NovaRequest $request, Action $action): bool
|
||||
{
|
||||
return (bool) ($value instanceof Closure ? $value($model, $request, $action) : $value);
|
||||
}
|
||||
|
||||
private function text(
|
||||
Closure|Stringable|string|null $value,
|
||||
Model $model,
|
||||
NovaRequest $request,
|
||||
Action $action,
|
||||
): ?string {
|
||||
$resolved = $value instanceof Closure ? $value($model, $request, $action) : $value;
|
||||
|
||||
return $resolved === null ? null : (string) $resolved;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user