Skip to content

Commit

Permalink
Merge pull request #10 from vanhooff/3.x
Browse files Browse the repository at this point in the history
Added maxDepth config option
  • Loading branch information
saade authored Sep 13, 2023
2 parents 4101397 + 72cb73a commit 9297ca4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ AdjacencyList::make('subjects')
->childrenKey('subitems') // defaults to 'children'
```

### Customizing the `MaxDepth` of the tree.
```php
AdjacencyList::make('subjects')
->maxDepth(2) // defaults to -1 (unlimited depth)
```

### Creating items without a modal.
```php
AdjacencyList::make('subjects')
Expand Down
2 changes: 1 addition & 1 deletion resources/dist/filament-adjacency-list.js

Large diffs are not rendered by default.

23 changes: 19 additions & 4 deletions resources/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import Sortable from "sortablejs"

export default ({
statePath,
disabled
disabled,
maxDepth
}) => ({
statePath,
sortable: null,
maxDepth,

init() {
this.sortable = new Sortable(this.$el, {
Expand All @@ -16,9 +18,22 @@ export default ({
swapThreshold: 0.50,
draggable: "[data-sortable-item]",
handle: "[data-sortable-handle]",
onSort: () => {
onMove: (evt) => {
if (this.maxDepth >= 0 && this.getDepth(evt.related) > this.maxDepth) {
return false; // Prevent sorting
}
},
onSort: (evt) => {
this.$wire.dispatchFormEvent('builder::sort', this.statePath, this.sortable.toArray())
}
})
}
})
},

getDepth(el, depth = 0) {
let parentEl = el.parentElement.closest('[data-sortable-item]');
if (parentEl) {
return this.getDepth(parentEl, ++depth);
}
return depth;
},
})
5 changes: 4 additions & 1 deletion resources/views/builder.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class="filament-navigation"
$isDisabled = $isDisabled();
$isEditable = $isEditable();
$isReorderable = $isReorderable();
$maxDepth = $getMaxDepth();
$addAction = $getAction('add');
Expand All @@ -35,7 +36,8 @@ class="space-y-2"
ax-load-css="{{ \Filament\Support\Facades\FilamentAsset::getStyleHref('filament-adjacency-list-styles', 'saade/filament-adjacency-list') }}"
x-data="tree({
statePath: @js($getStatePath()),
disabled: @js($isDisabled)
disabled: @js($isDisabled),
maxDepth: @js($maxDepth)
})"
>
@forelse($getState() as $uuid => $item)
Expand All @@ -51,6 +53,7 @@ class="space-y-2"
:label-key="$getLabelKey()"
:reorderable="$isReorderable"
:state-path="$getStatePath()"
:max-depth="$maxDepth"
/>
@empty
<div @class([
Expand Down
4 changes: 3 additions & 1 deletion resources/views/components/item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class="space-y-2"
wire:key="{{ $itemStatePath }}-children"
x-data="tree({
statePath: @js($itemStatePath . ".$childrenKey"),
disabled: @js($disabled)
disabled: @js($disabled),
maxDepth: @js($maxDepth)
})"
>
@foreach ($item[$childrenKey] as $uuid => $child)
Expand All @@ -86,6 +87,7 @@ class="space-y-2"
:label-key="$labelKey"
:reorderable="$reorderable"
:state-path="$statePath"
:max-depth="$maxDepth"
/>
@endforeach
</div>
Expand Down
14 changes: 14 additions & 0 deletions src/Forms/Components/AdjacencyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class AdjacencyList extends Forms\Components\Field

protected string | Closure $childrenKey = 'children';

protected int $maxDepth = -1;

protected function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -90,6 +92,18 @@ public function getChildrenKey(): string
return $this->evaluate($this->childrenKey);
}

public function maxDepth(int | Closure $maxDepth): static
{
$this->maxDepth = $maxDepth;

return $this;
}

public function getMaxDepth(): int
{
return $this->evaluate($this->maxDepth);
}

public function getRelativeStatePath(string $path): string
{
return str($path)->after($this->getStatePath())->trim('.')->toString();
Expand Down

0 comments on commit 9297ca4

Please sign in to comment.