generated from filamentphp/plugin-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
HandlesNavigationBuilder.php
150 lines (121 loc) · 5.24 KB
/
HandlesNavigationBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace RyanChandler\FilamentNavigation\Filament\Resources\NavigationResource\Pages\Concerns;
use Closure;
use Filament\Forms\ComponentContainer;
use Filament\Forms\Components\Component;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Pages\Actions\Action;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use RyanChandler\FilamentNavigation\Facades\FilamentNavigation;
trait HandlesNavigationBuilder
{
public $mountedItem;
public $mountedItemData = [];
public $mountedChildTarget;
public function sortNavigation(string $targetStatePath, array $targetItemsStatePaths)
{
$items = [];
foreach ($targetItemsStatePaths as $targetItemStatePath) {
$item = data_get($this, $targetItemStatePath);
$uuid = Str::afterLast($targetItemStatePath, '.');
$items[$uuid] = $item;
}
data_set($this, $targetStatePath, $items);
}
public function addChild(string $statePath)
{
$this->mountedChildTarget = $statePath;
$this->mountAction('item');
}
public function removeItem(string $statePath)
{
$uuid = Str::afterLast($statePath, '.');
$parentPath = Str::beforeLast($statePath, '.');
$parent = data_get($this, $parentPath);
data_set($this, $parentPath, Arr::except($parent, $uuid));
}
public function editItem(string $statePath)
{
$this->mountedItem = $statePath;
$this->mountedItemData = Arr::except(data_get($this, $statePath), 'children');
$this->mountAction('item');
}
public function createItem()
{
$this->mountedItem = null;
$this->mountedItemData = [];
$this->mountedActionData = [];
$this->mountAction('item');
}
protected function getActions(): array
{
return [
Action::make('item')
->mountUsing(function (ComponentContainer $form) {
if (! $this->mountedItem) {
return;
}
$form->fill($this->mountedItemData);
})
->view('filament-navigation::hidden-action')
->form([
TextInput::make('label')
->label(__('filament-navigation::filament-navigation.items-modal.label'))
->required(),
Select::make('type')
->label(__('filament-navigation::filament-navigation.items-modal.type'))
->options(function () {
$types = FilamentNavigation::getItemTypes();
return array_combine(array_keys($types), Arr::pluck($types, 'name'));
})
->afterStateUpdated(function ($state, Select $component): void {
if (! $state) {
return;
}
// NOTE: This chunk of code is a workaround for Livewire not letting
// you entangle to non-existent array keys, which wire:model
// would normally let you do.
$component
->getContainer()
->getComponent(fn (Component $component) => $component instanceof Group)
->getChildComponentContainer()
->fill();
})
->reactive(),
Group::make()
->statePath('data')
->schema(function (Closure $get) {
$type = $get('type');
return FilamentNavigation::getItemTypes()[$type]['fields'] ?? [];
}),
])
->modalWidth('md')
->action(function (array $data) {
if ($this->mountedItem) {
data_set($this, $this->mountedItem, array_merge(data_get($this, $this->mountedItem), $data));
$this->mountedItem = null;
$this->mountedItemData = [];
} elseif ($this->mountedChildTarget) {
$children = data_get($this, $this->mountedChildTarget . '.children', []);
$children[(string) Str::uuid()] = [
...$data,
...['children' => []],
];
data_set($this, $this->mountedChildTarget . '.children', $children);
$this->mountedChildTarget = null;
} else {
$this->data['items'][(string) Str::uuid()] = [
...$data,
...['children' => []],
];
}
$this->mountedActionData = [];
})
->modalButton(__('filament-navigation::filament-navigation.items-modal.btn'))
->label(__('filament-navigation::filament-navigation.items-modal.title')),
];
}
}