Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 7, 2023
1 parent b65b2a6 commit 3d75e40
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
4 changes: 2 additions & 2 deletions resources/views/fields/file-option.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="file-list-item">
<div class="file-list-item__column">
@if($isImage)
<img class="file-list-item__thumbnail" src="{{ $url }}" alt="{{ $label }}">
<img class="file-list-item__thumbnail" src="{{ $url }}" alt="{{ $fileName }}">
@else
<span class="file-list-item__icon">
<x-root::icon name="document" class="media-item__icon" />
</span>
@endif
<span id="{{ $uuid }}" class="file-list-item__name">{{ $label }}</span>
<span id="{{ $uuid }}" class="file-list-item__name">{{ $fileName }}</span>
<input type="hidden" name="{{ $attrs->get('name') }}" value="{{ $attrs->get('value') }}">
</div>
<div class="file-list-item__actions">
Expand Down
6 changes: 3 additions & 3 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasAttributes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Redirect;
Expand Down Expand Up @@ -213,12 +214,11 @@ public function toArray(): array
/**
* {@inheritdoc}
*/
public function toForm(Request $request): array
public function toForm(Request $request, Model $model): array
{
return array_merge($this->toArray(), [
'open' => $this->errors($request)->isNotEmpty(),
'fields' => $this->resolveFields($request)
->mapToInputs($request, $this->query->getModel()),
'fields' => $this->resolveFields($request)->mapToInputs($request, $model),
]);
}
}
6 changes: 3 additions & 3 deletions src/Actions/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public function visible(string|array $context): static
}

/**
* Map the action to table components.
* Map the action to forms.
*/
public function mapToForms(Request $request): array
public function mapToForms(Request $request, Model $model): array
{
return $this->map->toForm($request)->all();
return $this->map->toForm($request, $model)->all();
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class File extends MorphToMany
*/
protected string $disk;

/**
* The displayable conversion name.
*/
protected ?string $displayConversion = 'original';

/**
* Create a new field instance.
*/
Expand Down Expand Up @@ -94,7 +99,11 @@ public function collection(string $value): static
public function resolveDisplay(Model $related): mixed
{
if (is_null($this->displayResolver)) {
$this->display('file_name');
$this->display(function (Medium $related): string {
return $related->isImage
? sprintf('<img src="%s" width="30" height="30">', $related->getUrl($this->displayConversion))
: sprintf('<a href="%s">%s</a>', $related->getUrl(), $related->file_name);
});
}

return parent::resolveDisplay($related);
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function getPath(string $conversion = null, bool $absolute = false): stri
{
$path = sprintf('%s/%s', $this->uuid, $this->file_name);

if (! is_null($conversion)) {
if (! is_null($conversion) && $conversion !== 'original') {
$path = substr_replace(
$path, "-{$conversion}", -(mb_strlen(Str::afterLast($path, '.')) + 1), -mb_strlen("-{$conversion}")
);
Expand Down
12 changes: 10 additions & 2 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ public function toIndex(Request $request): array
return array_merge($this->toArray(), [
'title' => $this->getName(),
'actions' => $this->resolveActions($request)
->authorized($request)
->authorized($request, $this->getModelInstance())
->visible('index')
->mapToForms($request),
->mapToForms($request, $this->getModelInstance()),
'data' => $this->paginate($request),
'widgets' => $this->resolveWidgets($request)
->authorized($request)
Expand Down Expand Up @@ -385,6 +385,14 @@ public function toShow(Request $request, Model $model): array
->authorized($request, $model)
->visible('show')
->mapToDisplay($request, $model),
'actions' => $this->resolveActions($request)
->authorized($request, $model)
->visible('show')
->mapToForms($request, $model),
'widgets' => $this->resolveWidgets($request)
->authorized($request, $model)
->visible('show')
->toArray(),
]);
}

Expand Down

0 comments on commit 3d75e40

Please sign in to comment.