Repository navigation
Expand file tree
/
Copy pathWireSortableServiceProvider.php
More file actions
145 lines (122 loc) · 4.98 KB
/
Copy pathWireSortableServiceProvider.php
File metadata and controls
145 lines (122 loc) · 4.98 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
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
<?php
declare(strict_types=1);
namespace NyonCode\WireSortable;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\HtmlString;
use NyonCode\LaravelPackageToolkit\Commands\InstallCommand;
use NyonCode\LaravelPackageToolkit\Packager;
use NyonCode\LaravelPackageToolkit\PackageServiceProvider;
use NyonCode\WireCore\Core\Plugin\PluginManager;
use NyonCode\WireCore\Foundation\Assets\Bundle;
use NyonCode\WireCore\Foundation\Icons\IconManager;
use NyonCode\WireTable\Table;
class WireSortableServiceProvider extends PackageServiceProvider
{
/** Absolute path to the pre-bundled, self-registering sortable assets. */
public const ASSETS_PATH = __DIR__.'/../dist';
/**
* @throws \Exception
*/
public function configure(Packager $packager): void
{
$packager
->name('WireSortable')
->hasShortName('wire-sortable')
->registeredPackage(function ($packager) {
$this->app->resolving(PluginManager::class, function (PluginManager $manager) {
if (! $manager->has('sortable')) {
$manager->register(new SortablePlugin);
}
});
})
->bootedPackage(function ($packager) {
$this->registerTableMacros();
$this->registerIcons();
Bundle::serve('wire-sortable', self::ASSETS_PATH);
})
->hasConfig()
->hasViews()
->hasAssets('dist', entries: [
Bundle::make('wire-sortable.js'),
])
->hasAssetFallback(Bundle::servedByRoute('wire-sortable'))
->hasTranslations()
->hasMigrations()
->hasAbout()
->hasInstallCommand(function (InstallCommand $command) {
$command
->publishConfig()
->publishMigrations()
->publishViews()
->publishTranslations();
});
}
/**
* The grip this package draws its handle with, through the canonical owner.
*
* A six-dot grip is in no icon set the framework ships, and the alternative
* to registering one is an inline `<svg>` in the handle partial — which is
* the thing the Icons rule exists to prevent. Registered from a `.svg` file
* rather than a PHP string so the markup stays markup, and prefixed so it
* cannot collide with a consumer's own `grip`.
*/
protected function registerIcons(): void
{
app(IconManager::class)->registerIconsFromDirectory(
__DIR__.'/../resources/icons',
'sortable',
);
}
protected function registerTableMacros(): void
{
Table::macro('reorderable', function (?string $orderColumn = null, bool $condition = true): static {
$this->sortableReorderable = $condition;
if ($orderColumn !== null) {
$this->sortableOrderColumn = $orderColumn;
}
return $this;
});
Table::macro('isReorderable', function (): bool {
return $this->sortableReorderable ?? false;
});
Table::macro('alwaysReorderable', function (?string $orderColumn = null): static {
$this->sortableReorderable = true;
$this->sortableAlwaysReorderable = true;
if ($orderColumn !== null) {
$this->sortableOrderColumn = $orderColumn;
}
return $this;
});
Table::macro('isAlwaysReorderable', function (): bool {
return $this->sortableAlwaysReorderable ?? false;
});
Table::macro('getOrderColumn', function (): string {
if (isset($this->sortableOrderColumn)) {
return $this->sortableOrderColumn;
}
return app()->bound('config')
? config('wire-sortable.order_column', 'sort_order')
: 'sort_order';
});
Table::macro('paginatedWhileReordering', function (bool $enabled = true): static {
$this->sortablePaginatedWhileReordering = $enabled;
return $this;
});
Table::macro('isPaginatedWhileReordering', function (): bool {
return $this->sortablePaginatedWhileReordering ?? false;
});
Table::macro('columnReorderable', function (bool $enabled = true): static {
$this->sortableColumnReorderable = $enabled;
return $this;
});
Table::macro('isColumnReorderable', function (): bool {
return $this->sortableColumnReorderable ?? false;
});
// Canonical owner of the drag-handle markup. The handle SVG lives in a
// Blade partial instead of a hand-built JS string; the rendered HTML is
// injected into the sortable Alpine component as config.
Table::macro('getDragHandleHtml', function (): Htmlable {
return new HtmlString(view('wire-sortable::partials.drag-handle')->render());
});
}
}