Skip to content

Commit

Permalink
Merge pull request #72 from BBS-Lab/develop
Browse files Browse the repository at this point in the history
feat(flexible): add flexible content support
  • Loading branch information
mikaelpopowicz authored Sep 9, 2022
2 parents 7f83d7e + a340c17 commit 49b0410
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 8 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
],
"require": {
"php": "^8.0",
"ext-json": "*",
"james-heinrich/getid3": "^1.9",
"pion/laravel-chunk-upload": "^1.5",
"laravel/nova": "^4.0",
"nova-kit/nova-packages-tool": "^1.3.1",
"ext-json": "*"
"pion/laravel-chunk-upload": "^1.5",
"spatie/invade": "^1.1"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.0.1",
Expand Down
68 changes: 67 additions & 1 deletion composer.lock

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

2 changes: 1 addition & 1 deletion dist/js/tool.js

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion resources/js/fields/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ export default {
drag: false,
displayModal: false,
value: [],
flexibleGroup: [],
}),
mounted() {
this.init()
this.value = (this.field.value || []).map(file => this.mapEntity(file))
this.flexibleGroup = this.resolveFlexible(this)
},
computed: {
Expand Down Expand Up @@ -167,9 +169,10 @@ export default {
limit: this.field.limit ?? null,
resource: this.resourceName,
resourceId: this.resourceId,
attribute: this.field.attribute,
attribute: this.flexibleGroup.length ? this.field.sortableUriKey : this.field.attribute,
customDisk: this.field.customDisk,
permissions: this.field.permissions,
flexibleGroup: this.flexibleGroup,
callback: selection => {
this.value = selection.map(f => this.mapEntity(f))
},
Expand Down Expand Up @@ -199,6 +202,20 @@ export default {
file.exists,
file.disk
),
resolveFlexible(component) {
let elements = []
let group = component.$parent
let parent = component.$parent?.$parent?.$parent?.$parent
if (parent?.field?.component === 'nova-flexible-content') {
elements.unshift(...this.resolveFlexible(parent))
elements.push(`${group?.group?.name}:${parent.field.sortableUriKey}`)
}
return elements
},
},
watch: {
Expand Down
7 changes: 7 additions & 0 deletions resources/js/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const buildPayload = (state, params) => {
...(!state.customDisk && {
disk: state.disk,
}),
...(state.isFieldMode &&
state.flexibleGroup?.length && {
flexible: state.flexibleGroup.join('.'),
}),
}
}

Expand Down Expand Up @@ -424,6 +428,7 @@ const actions = {
attribute,
customDisk,
permissions,
flexibleGroup,
callback,
}
) => {
Expand All @@ -434,6 +439,7 @@ const actions = {
commit('setResourceId', resourceId)
commit('setAttribute', attribute)
commit('setCustomDisk', customDisk)
commit('setFlexibleGroup', flexibleGroup)
commit('setCallback', callback)
commit('setSelection', [...initialFiles])
dispatch('setPermissions', permissions)
Expand All @@ -456,6 +462,7 @@ const actions = {
commit('setResourceId', null)
commit('setAttribute', null)
commit('setCustomDisk', false)
commit('setFlexibleGroup', [])
commit('setDisk', null)
commit('setSelection', null)

Expand Down
4 changes: 4 additions & 0 deletions resources/js/store/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ const mutations = {
state.customDisk = customDisk
},

setFlexibleGroup(state, flexibleGroup) {
state.flexibleGroup = flexibleGroup
},

/*
|--------------------------------------------------------------------------
| Setters
Expand Down
1 change: 1 addition & 0 deletions resources/js/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const store = {
resourceId: null,
attribute: null,
customDisk: false,
flexibleGroup: [],

// permissions
showCreateFolder: true,
Expand Down
8 changes: 8 additions & 0 deletions src/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ protected function resolveAttribute($resource, $attribute = null): ?array
$value = collect([$value]);
}

if (is_array($value)) {
if ($this->multiple) {
$value = collect($value)->map(fn (array $asset) => new Asset(...$asset));
} else {
$value = collect([new Asset(...$value)]);
}
}

return $value
->map(function (Asset $asset) {
$disk = $this->resolveFilesystem(app(NovaRequest::class)) ?? $asset->disk;
Expand Down
4 changes: 2 additions & 2 deletions src/Filesystem/Upload/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Uploader implements UploaderContract
*/
public function handle(UploadFileRequest $request, string $index = 'file'): array
{
if (! $request->validateUpload()) {
if (!$request->validateUpload()) {
throw ValidationException::withMessages([
'file' => [__('nova-file-manager::errors.file.upload_validation')],
]);
Expand Down Expand Up @@ -49,7 +49,7 @@ public function handle(UploadFileRequest $request, string $index = 'file'): arra

public function saveFile(UploadFileRequest $request, UploadedFile $file): array
{
if (! $request->validateUpload($file, true)) {
if (!$request->validateUpload($file, true)) {
throw ValidationException::withMessages([
'file' => [__('nova-file-manager::errors.file.upload_validation')],
]);
Expand Down
52 changes: 51 additions & 1 deletion src/Http/Requests/BaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use BBSLab\NovaFileManager\FileManager;
use BBSLab\NovaFileManager\NovaFileManager;
use Illuminate\Validation\ValidationException;
use Laravel\Nova\Fields\FieldCollection;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Nova;
use Laravel\Nova\Resource;
use Laravel\Nova\Tool;

/**
Expand Down Expand Up @@ -51,13 +53,61 @@ public function resolveField(): ?InteractsWithFilesystem
{
$resource = $this->resourceId ? $this->findResourceOrFail() : $this->newResource();

return $resource->availableFields($this)
$fields = $this->has('flexible')
? $this->flexibleAvailableFields($resource)
: $resource->availableFields($this);

return $fields
->whereInstanceOf(FileManager::class)
->findFieldByAttribute($this->attribute, function () {
abort(404);
});
}

public function flexibleAvailableFields(Resource $resource): FieldCollection
{
$path = $this->input('flexible');

abort_if(blank($path), 404);

$tree = collect(explode('.', $path))
->map(function (string $item) {
[$layout, $attribute] = explode(':', $item);

return ['attribute' => $attribute, 'layout' => $layout];
});

$fields = $resource->availableFields($this);

while ($tree->isNotEmpty()) {
$current = $tree->shift();

$fields = $this->flexibleFieldCollection($fields, $current['attribute'], $current['layout']);
}

return $fields;
}

public function flexibleFieldCollection(FieldCollection $fields, string $attribute, string $name): FieldCollection
{
/** @var \Whitecube\NovaFlexibleContent\Flexible $field */
$field = $fields
->whereInstanceOf('Whitecube\NovaFlexibleContent\Flexible')
->findFieldByAttribute($attribute, function () {
abort(404);
});

/** @var \Whitecube\NovaFlexibleContent\Layouts\Collection $layouts */
abort_unless($layouts = invade($field)->layouts, 404);

/** @var \Whitecube\NovaFlexibleContent\Layouts\Layout $layout */
$layout = $layouts->first(fn ($layout) => $layout->name() === $name);

abort_if($layout === null, 404);

return new FieldCollection($layout->fields());
}

public function resolveTool(): ?InteractsWithFilesystem
{
return tap(once(function () {
Expand Down

0 comments on commit 49b0410

Please sign in to comment.