Skip to content

Commit 83e9387

Browse files
committed
- adds 'selection' ability for global controls
- selection works only on selectable table, and will send to the BE a selection payload with the selected rows - buttons that work with selection will be rendered only when a selection is made
1 parent 8a912f1 commit 83e9387

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

src/Attributes/Button.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Button
99
public const Optional = [
1010
'action', 'confirmation', 'event', 'fullRoute', 'label', 'message',
1111
'method', 'params', 'postEvent', 'routeSuffix', 'tooltip', 'slot',
12-
'class', 'name',
12+
'class', 'name', 'selection',
1313
];
1414

1515
public const Actions = ['ajax', 'export', 'href', 'router'];

src/Exceptions/Button.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,18 @@ public static function invalidMethod(string $method)
8080
['method' => $method]
8181
));
8282
}
83+
84+
public static function noSelectable()
85+
{
86+
return new static(__(
87+
"You can't have an action with selection when the table is not selectable",
88+
));
89+
}
90+
91+
public static function rowSelection()
92+
{
93+
return new static(__(
94+
'Selection works only on global buttons',
95+
));
96+
}
8397
}

src/Services/Template/Validators/Buttons/Button.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ class Button
1414
{
1515
private const Validations = [
1616
'mandatory', 'optional', 'complementary',
17-
'actions', 'method', 'name', 'route',
17+
'actions', 'method', 'name', 'route', 'selection',
1818
];
1919

2020
private Obj $button;
21+
private Obj $template;
2122
private Table $table;
2223
private ?string $routePrefix;
2324

24-
public function __construct(Obj $button, Table $table, ?string $routePrefix)
25+
public function __construct(Obj $button, Table $table, Obj $template)
2526
{
2627
$this->button = $button;
2728
$this->table = $table;
28-
$this->routePrefix = $routePrefix;
29+
$this->template = $template;
2930
}
3031

3132
public function validate(): void
@@ -94,7 +95,7 @@ private function route(): void
9495
$route = $this->button->get('fullRoute');
9596

9697
$route ??= $this->button->has('routeSuffix')
97-
? "{$this->routePrefix}.{$this->button->get('routeSuffix')}"
98+
? "{$this->template->get('routePrefix')}.{$this->button->get('routeSuffix')}"
9899
: null;
99100

100101
if ($route !== null && ! Route::has($route)) {
@@ -121,4 +122,19 @@ private function name(): void
121122
throw Exception::missingName();
122123
}
123124
}
125+
126+
private function selection(): void
127+
{
128+
if (! $this->button->get('selection')) {
129+
return;
130+
}
131+
132+
if ($this->button->get('type') === 'row') {
133+
throw Exception::rowSelection();
134+
}
135+
136+
if (! $this->template->get('selectable')) {
137+
throw Exception::noSelectable();
138+
}
139+
}
124140
}

src/Services/Template/Validators/Buttons/Buttons.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ class Buttons
1313
private const Validations = ['format', 'defaults', 'structure'];
1414

1515
private Obj $buttons;
16-
private string $routePrefix;
1716
private Obj $defaults;
1817
private Table $table;
1918

2019
public function __construct(Obj $template, Table $table)
2120
{
22-
$this->buttons = $template->get('buttons');
23-
$this->routePrefix = $template->get('routePrefix');
21+
$this->template = $template;
2422
$this->table = $table;
2523
$this->defaults = $this->configButtons();
2624
}
@@ -33,7 +31,7 @@ public function validate(): void
3331

3432
private function format(): void
3533
{
36-
$invalid = $this->buttons
34+
$invalid = $this->template->get('buttons')
3735
->filter(fn ($button) => ! is_string($button) && ! $button instanceof Obj);
3836

3937
if ($invalid->isNotEmpty()) {
@@ -43,7 +41,8 @@ private function format(): void
4341

4442
private function defaults(): void
4543
{
46-
$diff = $this->buttons->filter(fn ($button) => is_string($button))
44+
$diff = $this->template->get('buttons')
45+
->filter(fn ($button) => is_string($button))
4746
->diff($this->defaults->keys());
4847

4948
if ($diff->isNotEmpty()) {
@@ -53,8 +52,9 @@ private function defaults(): void
5352

5453
private function structure(): void
5554
{
56-
$this->buttons->map(fn ($button) => $this->map($button))
57-
->each(fn ($button) => (new Button($button, $this->table, $this->routePrefix))
55+
$this->template->get('buttons')
56+
->map(fn ($button) => $this->map($button))
57+
->each(fn ($button) => (new Button($button, $this->table, $this->template))
5858
->validate());
5959
}
6060

0 commit comments

Comments
 (0)