Repository navigation
Expand file tree
/
Copy pathSortablePlugin.php
More file actions
64 lines (51 loc) · 1.66 KB
/
Copy pathSortablePlugin.php
File metadata and controls
64 lines (51 loc) · 1.66 KB
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
<?php
declare(strict_types=1);
namespace NyonCode\WireSortable;
use NyonCode\WireCore\Core\Plugin\Contracts\HasDependencies;
use NyonCode\WireCore\Core\Plugin\Contracts\Plugin;
use NyonCode\WireCore\Core\Plugin\PluginManager;
use NyonCode\WireTable\Table;
/**
* Plugin for reorderable tables.
*
* Registers a table.querying hook that forces ordering by the sort column
* when the table is in reorder mode (isReordering on the component).
*/
class SortablePlugin implements HasDependencies, Plugin
{
public function getId(): string
{
return 'sortable';
}
/**
* @return array<int, string>
*/
public function dependencies(): array
{
return [];
}
public function register(PluginManager $manager): void
{
$manager->hook('table.querying', function (array $payload) {
$table = $payload['table'] ?? null;
if (! $table instanceof Table) {
return $payload;
}
$hasMethod = $table instanceof SortableTable || Table::hasMacro('isReorderable');
if (! $hasMethod || ! $table->isReorderable()) {
return $payload;
}
// Check if the component is in reorder mode
$component = $table->getLivewireComponent();
if ($component !== null && method_exists($component, 'isTableReordering') && $component->isTableReordering()) {
$payload['force_sort_column'] = $table->getOrderColumn();
$payload['force_sort_direction'] = 'asc';
}
return $payload;
});
}
public function boot(PluginManager $manager): void
{
//
}
}