Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into prepare-phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
szepeviktor committed Nov 11, 2023
2 parents 1d12b19 + cbe165f commit 0c9702d
Show file tree
Hide file tree
Showing 27 changed files with 199 additions and 70 deletions.
1 change: 0 additions & 1 deletion public/build/assets/app-5cfcf402.js

This file was deleted.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions public/build/assets/app-e9d1dd12.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions public/build/assets/dropdown-73a613e0.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion public/build/assets/dropdown-7dc1f3ea.js

This file was deleted.

1 change: 1 addition & 0 deletions public/build/assets/editor-73a613e0.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion public/build/assets/editor-7dc1f3ea.js

This file was deleted.

1 change: 1 addition & 0 deletions public/build/assets/media-manager-73a613e0.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion public/build/assets/media-manager-7dc1f3ea.js

This file was deleted.

1 change: 1 addition & 0 deletions public/build/assets/repeater-73a613e0.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion public/build/assets/repeater-7dc1f3ea.js

This file was deleted.

12 changes: 6 additions & 6 deletions public/build/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,44 @@
"css": [
"assets/app-4d04387c.css"
],
"file": "assets/app-c6af0dee.js",
"file": "assets/app-c8ab9151.js",
"isEntry": true,
"src": "resources/js/app.js"
},
"resources/js/dropdown.js": {
"file": "assets/dropdown-7dc1f3ea.js",
"file": "assets/dropdown-73a613e0.js",
"imports": [
"resources/js/app.js"
],
"isEntry": true,
"src": "resources/js/dropdown.js"
},
"resources/js/editor.js": {
"file": "assets/editor-7dc1f3ea.js",
"file": "assets/editor-73a613e0.js",
"imports": [
"resources/js/app.js"
],
"isEntry": true,
"src": "resources/js/editor.js"
},
"resources/js/media-manager.js": {
"file": "assets/media-manager-7dc1f3ea.js",
"file": "assets/media-manager-73a613e0.js",
"imports": [
"resources/js/app.js"
],
"isEntry": true,
"src": "resources/js/media-manager.js"
},
"resources/js/repeater.js": {
"file": "assets/repeater-7dc1f3ea.js",
"file": "assets/repeater-73a613e0.js",
"imports": [
"resources/js/app.js"
],
"isEntry": true,
"src": "resources/js/repeater.js"
},
"resources/sass/app.scss": {
"file": "assets/app-5cfcf402.js",
"file": "assets/app-e9d1dd12.js",
"imports": [
"resources/js/app.js"
],
Expand Down
18 changes: 16 additions & 2 deletions resources/js/media-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ document.addEventListener('alpine:init', () => {
queue: [],
items: [],
nextPageUrl: url,
query: config.query,
init() {
this.$root.querySelector('.modal__body').addEventListener('scroll', throttle((event) => {
if (this.shouldPaginate(event)) {
this.fetch();
this.paginate();
}
}));

this.$watch('query', () => this.fetch());
},
fetch() {
this.processing = true;

window.$http.get(this.nextPageUrl).then((response) => {
window.$http.get(url, { params: this.query }).then((response) => {
this.items = response.data.data;
}).catch((error) => {
//
}).finally(() => {
this.processing = false;
});
},
paginate() {
this.processing = true;

window.$http.get(this.nextPageUrl, { params: this.query }).then((response) => {
this.items.push(...response.data.data);
this.nextPageUrl = response.data.next_page_url;
}).catch((error) => {
Expand Down
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
5 changes: 1 addition & 4 deletions resources/views/fields/media.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<div
class="form-group--row"
x-data="{ selection: {{ json_encode($selection) }} }"
>
<div class="form-group--row" x-data="{ selection: {{ json_encode($selection) }} }">
<span class="form-label">{{ $label }}</span>
<div class="file-list">
<button
Expand Down
12 changes: 4 additions & 8 deletions resources/views/media/filters.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<div class="modal__filter">
<div class="search-form">
<input class="form-control search-form__control" type="text" placeholder="Search..." title="Search" />
<button type="button" class="search-form__submit">
<span class="sr-only">Search</span>
<x-root::icon name="search" class="search-form__icon" />
</button>
</div>
<div class="modal__filter" x-data="query">
@foreach($filters as $filter)
@include($filter['template'], $filter)
@endforeach
</div>
1 change: 0 additions & 1 deletion resources/views/media/manager.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
class="modal--media"
data-dropzone="{{ __('Drag & drop your images and files here') }}"
x-data="mediaManager('{{ $url }}', {{ json_encode($config) }})"
x-model="selection"
x-bind:class="{ 'modal--dropzone': dragging }"
x-on:dragend.prevent="dragging = false"
x-on:dragover.prevent="dragging = true"
Expand Down
4 changes: 2 additions & 2 deletions resources/views/media/queued-medium.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class="media-item"
<div
class="progressbar__inner"
role="progressbar"
x-bind:aria-labelledby="item.hash"
aria-valuemin="0"
aria-valuenow="68"
aria-valuemax="100"
aria-labelledby="progress-caption"
x-bind:aria-valuenow="item.progress"
x-bind:aria-labelledby="item.hash"
>
<div class="progressbar__indicator" x-bind:style="item.progress && { inlineSize: item.progress + '%' }"></div>
</div>
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
71 changes: 70 additions & 1 deletion src/Fields/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Filters\Filter;
use Cone\Root\Filters\Filters;
use Cone\Root\Filters\MediaSearch;
use Cone\Root\Filters\RenderableFilter;
use Cone\Root\Http\Controllers\MediaController;
use Cone\Root\Models\Medium;
use Cone\Root\Traits\HasMedia;
use Cone\Root\Traits\RegistersRoutes;
use Cone\Root\Traits\ResolvesFilters;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
Expand All @@ -21,6 +27,7 @@ class Media extends File
use RegistersRoutes {
RegistersRoutes::registerRoutes as __registerRoutes;
}
use ResolvesFilters;

/**
* Indicates if the component is multiple.
Expand All @@ -32,6 +39,11 @@ class Media extends File
*/
protected string $template = 'root::fields.media';

/**
* The filters resolver callback.
*/
protected ?Closure $filtersResolver = null;

/**
* Get the URI key.
*/
Expand Down Expand Up @@ -77,12 +89,55 @@ public function getModel(): Model
};
}

/**
* Define the filters for the object.
*/
public function filters(Request $request): array
{
return [
new MediaSearch(),
];
}

/**
* Set the filters resolver callback.
*/
public function withFilters(Closure $callback): static
{
$this->filtersResolver = $callback;

return $this;
}

/**
* Resolve the filters collection.
*/
public function resolveFilters(Request $request): Filters
{
if (is_null($this->filters)) {
$this->filters = new Filters($this->filters($request));

if (! is_null($this->filtersResolver)) {
$this->fields->register(
Arr::wrap(call_user_func_array($this->filtersResolver, [$request]))
);
}

$this->filters->each(function (Filter $filter) use ($request): void {
$this->resolveFilter($request, $filter);
});
}

return $this->filters;
}

/**
* Paginate the results.
*/
public function paginate(Request $request, Model $model): array
{
return $this->resolveRelatableQuery($request, $model)
return $this->resolveFilters($request)
->apply($request, $this->resolveRelatableQuery($request, $model))
->latest()
->paginate($request->input('per_page'))
->withQueryString()
Expand Down Expand Up @@ -190,19 +245,33 @@ public function toInput(Request $request, Model $model): array
{
$data = parent::toInput($request, $model);

$filters = $this->resolveFilters($request)->authorized($request);

return array_merge($data, [
'modalKey' => $this->getModalKey(),
'config' => [
'accept' => $this->getAttribute('accept', '*'),
'multiple' => $this->multiple,
'chunk_size' => Config::get('root.media.chunk_size'),
'query' => $filters->mapToData($request),
],
'selection' => array_map(static function (array $option): array {
return array_merge($option, [
'html' => View::make('root::fields.file-option', $option)->render(),
]);
}, $data['options'] ?? []),
'url' => $this->getUri() ? $this->buildUri($request, $model) : null,
'filters' => $filters->renderable()
->map(function (RenderableFilter $filter) use ($request, $model): array {
return $filter->toField()
->removeAttribute('name')
->setAttributes([
'x-model.debounce.300ms' => $filter->getKey(),
'x-bind:readonly' => 'processing',
])
->toInput($request, $this->getRelation($model)->getRelated());
})
->all(),
]);
}
}
29 changes: 29 additions & 0 deletions src/Filters/MediaSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Cone\Root\Filters;

class MediaSearch extends Search
{
/**
* The searchable attributes
*/
protected array $attributes = [
'file_name',
];

/**
* Create a new filter instance.
*/
public function __construct(array $attributes = ['file_name'])
{
$this->attributes = $attributes;
}

/**
* {@inheritdoc}
*/
public function getSearchableAttributes(): array
{
return array_fill_keys($this->attributes, null);
}
}
Loading

0 comments on commit 0c9702d

Please sign in to comment.