Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Oct 25, 2023
1 parent 2508e7a commit 8a60a38
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 69 deletions.
18 changes: 7 additions & 11 deletions resources/views/fields/file-option.blade.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<div class="file-list-item">
<div class="file-list-item__column">
@if($medium->isImage)
<img
class="file-list-item__thumbnail"
src="{{ $medium->getUrl('thumbnail') }}"
alt="{{ $label }}"
>
@if($isImage)
<img class="file-list-item__thumbnail" src="{{ $url }}" alt="{{ $label }}">
@else
<span class="file-list-item__icon">
<x-root::icon name="document" class="media-item__icon" />
</span>
@endif
<span id="{{ $medium->uuid }}" class="file-list-item__name">{{ $label }}</span>
<span id="{{ $uuid }}" class="file-list-item__name">{{ $label }}</span>
<input type="hidden" name="{{ $attrs->get('name') }}" value="{{ $attrs->get('value') }}">
</div>
<div class="file-list-item__actions">
Expand All @@ -20,8 +16,8 @@ class="file-list-item__thumbnail"
type="button"
class="btn btn--light btn--sm btn--icon"
aria-label="{{ __('Edit') }}"
aria-describedby="{{ $medium->uuid }}"
x-on:click="$dispatch('open-{{ $medium->uuid }}')"
aria-describedby="{{ $uuid }}"
x-on:click="$dispatch('open-{{ $uuid }}')"
>
<x-root::icon name="edit" class="btn__icon" />
</a>
Expand All @@ -30,7 +26,7 @@ class="btn btn--light btn--sm btn--icon"
type="button"
class="btn btn--delete btn--sm btn--icon"
aria-label="{{ __('Remove') }}"
aria-describedby="{{ $medium->uuid }}"
aria-describedby="{{ $uuid }}"
x-on:click="selection.splice(index, 1)"
>
<x-root::icon name="close" class="btn__icon" />
Expand All @@ -39,7 +35,7 @@ class="btn btn--delete btn--sm btn--icon"
</div>

@if(! empty($fields))
<x-root::modal :title="$label" :key="$medium->uuid">
<x-root::modal :title="$label" :key="$uuid">
@foreach($fields as $field)
@include($field['template'], $field)
@endforeach
Expand Down
4 changes: 2 additions & 2 deletions resources/views/media/medium.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class="media-item"
<span x-text="item.name" class="media-item__name"></span>
</span>
</template>
<template x-if="! item.processing && item.is_image">
<template x-if="! item.processing && item.isImage">
<img x-bind:src="item.url" x-bind:alt="item.label">
</template>
<template x-if="! item.processing && ! item.is_image">
<template x-if="! item.processing && ! item.isImage">
<span class="media-item__background">
<x-root::icon name="document" class="media-item__icon" />
<span x-text="item.label" class="media-item__name"></span>
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ResourceMake.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function buildClass($name): string
}

/**
* Create a new method.
* Replace the model class.
*/
public function replaceModel(string $class): string
{
Expand Down
77 changes: 38 additions & 39 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class BelongsToMany extends Relation
*/
protected ?Closure $pivotFieldsResolver = null;

/**
* The default pivot values that should be saved.
*/
protected array $pivotValues = [];

/**
* Create a new relation field instance.
*/
Expand Down Expand Up @@ -87,28 +92,15 @@ public function withPivotFields(Closure $callback): static
/**
* {@inheritdoc}
*/
public function toOption(Request $request, Model $model, Model $related): array
public function getValueForHydrate(Request $request): mixed
{
$relation = $this->getRelation($model);

if (! $related->relationLoaded($relation->getPivotAccessor())) {
$related->setRelation($relation->getPivotAccessor(), $relation->newPivot());
}

$option = parent::toOption($request, $model, $related);

$option['attrs']['name'] = sprintf(
'%s[%s][%s]',
$this->getAttribute('name'),
$related->getKey(),
$this->getRelation($model)->getRelatedPivotKeyName()
);
$value = (array) parent::getValueForHydrate($request);

$option['fields'] = is_null($this->pivotFieldsResolver)
? new Fields()
: call_user_func_array($this->pivotFieldsResolver, [$request, $model, $related]);
$value = Arr::isList($value) ? array_fill_keys($value, []) : $value;

return $option;
return array_map(function (array $pivot): array {
return array_merge($this->pivotValues, $pivot);
}, $value);
}

/**
Expand All @@ -130,15 +122,11 @@ public function resolveHydrate(Request $request, Model $model, mixed $value): vo
{
if (is_null($this->hydrateResolver)) {
$this->hydrateResolver = function (Request $request, Model $model, mixed $value): void {
$value = (array) $value;

$value = Arr::isList($value) ? array_fill_keys($value, []) : $value;

$relation = $this->getRelation($model);

$results = $this->resolveRelatableQuery($request, $model)
->findMany(array_keys($value))
->each(static function (Model $related) use ($relation, $value): void {
->each(function (Model $related) use ($relation, $value): void {
$related->setRelation(
$relation->getPivotAccessor(),
$relation->newPivot($value[$related->getKey()])
Expand All @@ -155,27 +143,38 @@ public function resolveHydrate(Request $request, Model $model, mixed $value): vo
/**
* {@inheritdoc}
*/
public function toArray(): array
public function toOption(Request $request, Model $model, Model $related): array
{
return array_merge(parent::toArray(), [
'relatedName' => $this->getRelatedName(),
]);
$relation = $this->getRelation($model);

if (! $related->relationLoaded($relation->getPivotAccessor())) {
$related->setRelation($relation->getPivotAccessor(), $relation->newPivot());
}

$option = parent::toOption($request, $model, $related);

$option['attrs']['name'] = sprintf(
'%s[%s][%s]',
$this->getAttribute('name'),
$related->getKey(),
$this->getRelation($model)->getRelatedPivotKeyName()
);

$option['fields'] = is_null($this->pivotFieldsResolver)
? []
: call_user_func_array($this->pivotFieldsResolver, [$request, $model, $related])->mapToFormComponents($request, $model);

return $option;
}

/**
* Create a new method.
* {@inheritdoc}
*/
public function toFormComponent(Request $request, Model $model): array
public function toArray(): array
{
$data = parent::toFormComponent($request, $model);

$data['options'] = array_map(static function (array $option) use ($request, $model): array {
return array_merge($option, [
'fields' => $option['fields']->mapToFormComponents($request, $model),
]);
}, $data['options']);

return $data;
return array_merge(parent::toArray(), [
'relatedName' => $this->getRelatedName(),
]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function checked(bool $value = true): static
}

/**
* Create a new method.
* {@inheritdoc}
*/
public function resolveValue(Request $request, Model $model): mixed
{
Expand Down
19 changes: 14 additions & 5 deletions src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public function disk(string $value): static
return $this;
}

/**
* Set the collection pivot value.
*/
public function collection(string $value): static
{
$this->pivotValues['collection'] = $value;

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -210,7 +220,7 @@ protected function prune(Request $request, Model $model, array $keys): int
}

/**
* Create a new method.
* {@inheritdoc}
*/
public function toOption(Request $request, Model $model, Model $related): array
{
Expand All @@ -226,11 +236,10 @@ public function toOption(Request $request, Model $model, Model $related): array
$option['attrs']->merge(['name' => $name]);

return array_merge($option, [
'file_name' => $related->file_name,
'is_image' => $related->isImage,
'medium' => $related,
'fileName' => $related->file_name,
'isImage' => $related->isImage,
'processing' => false,
'url' => $related->getUrl('thumbnail') ?: $related->getUrl('original'),
'url' => $related->hasConversion('thumbnail') ? $related->getUrl('thumbnail') : $related->getUrl(),
'uuid' => $related->uuid,
]);
}
Expand Down
8 changes: 2 additions & 6 deletions src/Fields/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ public function paginate(Request $request, Model $model): array
->through(function (Medium $related) use ($request, $model): array {
$option = $this->toOption($request, $model, $related);

$option['fields'] = $option['fields']->mapToFormComponents($request, $model);

return array_merge($option, [
'html' => View::make('root::fields.file-option', $option)->render(),
]);
Expand Down Expand Up @@ -146,7 +144,7 @@ public function store(Request $request, Model $model, UploadedFile $file): array
if ($request->header('X-Chunk-Index') !== $request->header('X-Chunk-Total')) {
return array_merge($this->toOption($request, $model, new Medium()), [
'processing' => true,
'file_name' => null,
'fileName' => null,
]);
}

Expand Down Expand Up @@ -199,9 +197,7 @@ public function toFormComponent(Request $request, Model $model): array
'multiple' => $this->multiple,
'chunk_size' => Config::get('root.media.chunk_size'),
],
'selection' => array_map(static function (array $option) use ($request, $model): array {
$option['fields'] = $option['fields']->mapToFormComponents($request, $model);

'selection' => array_map(static function (array $option): array {
return array_merge($option, [
'html' => View::make('root::fields.file-option', $option)->render(),
]);
Expand Down
14 changes: 10 additions & 4 deletions src/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

class Attachment extends MorphPivot
{
protected $attributes = [
'collection' => 'foo',
];

/**
* The attributes that should be cast to native types.
*
Expand All @@ -18,4 +14,14 @@ class Attachment extends MorphPivot
protected $casts = [
'meta' => 'json',
];

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'collection',
'meta',
];
}
8 changes: 8 additions & 0 deletions src/Models/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ public function getUrl(string $conversion = null): string
return URL::to(Storage::disk($this->disk)->url($this->getPath($conversion)));
}

/**
* Check if the medium has the given conversion.
*/
public function hasConversion(string $conversion): bool
{
return in_array($conversion, $this->properties['conversions'] ?? []);
}

/**
* Scope the query only to the given search term.
*/
Expand Down

0 comments on commit 8a60a38

Please sign in to comment.