From 584933a79628097c3a9d1ff90d3ff7d0f31b81f1 Mon Sep 17 00:00:00 2001 From: David Vincent Date: Sat, 27 Apr 2024 06:01:06 +0200 Subject: [PATCH] Update --- package-lock.json | 8 +- package.json | 3 +- resources/js/filament-nested-list.js | 48 ++++++++++- .../components/nested-list/index.blade.php | 83 +++++++++++++++---- .../Providers/WorkbenchServiceProvider.php | 25 ------ 5 files changed, 124 insertions(+), 43 deletions(-) delete mode 100644 workbench/app/Providers/WorkbenchServiceProvider.php diff --git a/package-lock.json b/package-lock.json index 8d319d6..3dc9af1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,7 +5,8 @@ "packages": { "": { "dependencies": { - "nested-sort": "^5.2.0" + "nested-sort": "^5.2.0", + "sortablejs": "^1.15.2" }, "devDependencies": { "@awcodes/filament-plugin-purge": "^1.1.1", @@ -3963,6 +3964,11 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/sortablejs": { + "version": "1.15.2", + "resolved": "https://registry.npmjs.org/sortablejs/-/sortablejs-1.15.2.tgz", + "integrity": "sha512-FJF5jgdfvoKn1MAKSdGs33bIqLi3LmsgVTliuX6iITj834F+JRQZN90Z93yql8h0K2t0RwDPBmxwlbZfDcxNZA==" + }, "node_modules/source-map-js": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.0.tgz", diff --git a/package.json b/package.json index 883139d..a33158b 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "tailwindcss": "^3.4.3" }, "dependencies": { - "nested-sort": "^5.2.0" + "nested-sort": "^5.2.0", + "sortablejs": "^1.15.2" } } diff --git a/resources/js/filament-nested-list.js b/resources/js/filament-nested-list.js index a3ea15f..a531a5c 100644 --- a/resources/js/filament-nested-list.js +++ b/resources/js/filament-nested-list.js @@ -1,4 +1,4 @@ -import NestedSort from 'nested-sort'; +/*import NestedSort from 'nested-sort'; export default function nestedList({ items = [], @@ -41,4 +41,48 @@ export default function nestedList({ this.$wire.updateNestedList(this.items) }, } -} \ No newline at end of file +}*/ + +import Sortable from "sortablejs" + +export default function nestedList({ + treeId, + id, + maxDepth + }) { + return { + id, + sortable: null, + + init() { + let el = document.getElementById(id); + this.sortable = new Sortable(el, { + 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: () => { + console.log(this.sortable.toArray()); + } + }) + }, + + getDepth(el, depth = 0) { + const parentElement = el.parentElement.closest('[data-sortable-item]'); + + if (parentElement) { + return this.getDepth(parentElement, ++depth); + } + + return depth; + }, + } +} diff --git a/resources/views/components/nested-list/index.blade.php b/resources/views/components/nested-list/index.blade.php index 32ac16d..9cb4ef3 100644 --- a/resources/views/components/nested-list/index.blade.php +++ b/resources/views/components/nested-list/index.blade.php @@ -1,23 +1,26 @@ @php use Filament\Support\Facades\FilamentAsset; + use Illuminate\Support\Js; $containerKey = 'filament_tree_container_' . $this->getId(); $maxDepth = $getMaxDepth() ?? -1; - ray($this->getNestedListData()); + $data = collect($this->getRecords() ?? []) + ->map(function ($record) { + $data = [ + 'id' => $record->id, + 'order' => $record->order, + 'text' => $this->getNestedListRecordTitle($record), + ]; + $parent = $this->getParentKey($record); + if ($parent > 0) { + $data['parent_id'] = $parent; + } + return $data; + }) + ->toArray(); @endphp - -
+
+ x-data="nestedList(JSON.parse('{{ json_encode($data) }}'), {{ $maxDepth }})">
+
+ @php + $action = $this->getMountedNestedListAction(); + @endphp + + + @if ($action) + {{ $action->getModalContent() }} + + @if (count(($infolist = $action->getInfolist())?->getComponents() ?? [])) + {{ $infolist }} + @elseif ($this->mountedNestedListActionHasForm()) + {{ $this->getMountedNestedListActionForm() }} + @endif + + {{ $action->getModalContentFooter() }} + @endif + +
\ No newline at end of file diff --git a/workbench/app/Providers/WorkbenchServiceProvider.php b/workbench/app/Providers/WorkbenchServiceProvider.php deleted file mode 100644 index 6bac32e..0000000 --- a/workbench/app/Providers/WorkbenchServiceProvider.php +++ /dev/null @@ -1,25 +0,0 @@ -