Skip to content

Commit

Permalink
fix: use a less generic name for the alpine component to not clash wi…
Browse files Browse the repository at this point in the history
…th other plugins
  • Loading branch information
saade committed Feb 15, 2024
1 parent dc054a3 commit 48d7197
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion resources/dist/filament-adjacency-list.js

Large diffs are not rendered by default.

64 changes: 33 additions & 31 deletions resources/js/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
import Sortable from "sortablejs"

export default ({
export default function adjacencyList({
treeId,
statePath,
disabled,
maxDepth
}) => ({
statePath,
sortable: null,
}) {
return {
statePath,
sortable: null,

init() {
this.sortable = new Sortable(this.$el, {
disabled,
group: treeId,
animation: 150,
fallbackOnBody: true,
swapThreshold: 0.25,
invertSwap: true,
draggable: "[data-sortable-item]",
handle: "[data-sortable-handle]",
onMove: (evt) => {
if (maxDepth && maxDepth >= 0 && this.getDepth(evt.related) > maxDepth) {
return false; // Prevent dragging items to a depth greater than maxDepth
init() {
this.sortable = new Sortable(this.$el, {
disabled,
group: treeId,
animation: 150,
fallbackOnBody: true,
swapThreshold: 0.25,
invertSwap: true,
draggable: "[data-sortable-item]",
handle: "[data-sortable-handle]",
onMove: (evt) => {
if (maxDepth && maxDepth >= 0 && this.getDepth(evt.related) > maxDepth) {
return false; // Prevent dragging items to a depth greater than maxDepth
}
},
onSort: () => {
this.$wire.dispatchFormEvent('builder::sort', this.statePath, this.sortable.toArray())
}
},
onSort: () => {
this.$wire.dispatchFormEvent('builder::sort', this.statePath, this.sortable.toArray())
}
})
},
})
},

getDepth(el, depth = 0) {
const parentElement = el.parentElement.closest('[data-sortable-item]');
getDepth(el, depth = 0) {
const parentElement = el.parentElement.closest('[data-sortable-item]');

if (parentElement) {
return this.getDepth(parentElement, ++depth);
}
if (parentElement) {
return this.getDepth(parentElement, ++depth);
}

return depth;
},
})
return depth;
},
}
}
2 changes: 1 addition & 1 deletion resources/views/builder.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class="fi-adjacency-list-tree"
ax-load
ax-load-css="{{ \Filament\Support\Facades\FilamentAsset::getStyleHref('filament-adjacency-list-styles', 'saade/filament-adjacency-list') }}"
ax-load-src="{{ \Filament\Support\Facades\FilamentAsset::getAlpineComponentSrc('filament-adjacency-list', 'saade/filament-adjacency-list') }}"
x-data="tree({
x-data="adjacencyList({
treeId: @js($treeId),
statePath: @js($getStatePath()),
disabled: @js($isDisabled),
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class="px-2 text-gray-500 appearance-none"
'pb-1' => !$hasChildren,
'border-l border-l-gray-100 dark:border-l-white/10 ps-5 pt-2' => $hasRulers,
])
x-data="tree({
x-data="adjacencyList({
treeId: @js($treeId),
statePath: @js($itemStatePath . ".$childrenKey"),
disabled: @js($disabled),
Expand Down

0 comments on commit 48d7197

Please sign in to comment.