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
151
152
153
154
155
156
|
<?php
namespace Lightscale\LaralightTables;
use Lightscale\LaralightTables\Toolbar\Item as ToolbarItem;
use Lightscale\LaralightTables\Toolbar\Search as SearchItem;
use Lightscale\LaralightTables\Toolbar\PageSize as PageSizeItem;
use Lightscale\LaralightTables\Toolbar\Filter as FilterItem;
use Lightscale\LaralightTables\Concerns\Makable;
use Illuminate\View\View;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
class Toolbar
{
use Makable;
protected ?SearchItem $searchItem = null;
protected ?PageSizeItem $pageSizeItem = null;
/**
* @var Collection<int, ToolbarItem>
*/
protected Collection $startItems;
/**
* @var Collection<int, ToolbarItem>
*/
protected Collection $midItems;
/**
* @var Collection<int, ToolbarItem>
*/
protected Collection $endItems;
/**
* @var Collection<int, FilterItem>
*/
protected Collection $filterItems;
/**
* @template TModel of Model
* @param TableComponent<TModel> $table
*/
public function __construct(
private TableComponent $table
)
{
$this->startItems = collect();
$this->midItems = collect();
$this->endItems = collect();
$this->filterItems = collect();
}
private function processItem(ToolbarItem $item): void
{
$item->setToolbar($this);
if ($item instanceof FilterItem) {
$this->filterItems->push($item);
}
else if ($item instanceof PageSizeItem) {
$this->pageSizeItem = $item;
}
else if ($item instanceof SearchItem) {
$this->searchItem = $item;
}
}
/**
* @param Collection<int, ToolbarItem> $list
*/
private function prependItem(Collection $list, ToolbarItem $item): static
{
$this->processItem($item);
$list->prepend($item);
return $this;
}
/**
* @param Collection<int, ToolbarItem> $list
*/
private function appendItem(Collection $list, ToolbarItem $item): static
{
$this->processItem($item);
$list->push($item);
return $this;
}
public function prependStart(ToolbarItem $item): static
{
return $this->prependItem($this->startItems, $item);
}
public function appendStart(ToolbarItem $item): static
{
return $this->appendItem($this->startItems, $item);
}
public function prependMid(ToolbarItem $item): static
{
return $this->prependItem($this->midItems, $item);
}
public function appendMid(ToolbarItem $item): static
{
return $this->appendItem($this->midItems, $item);
}
public function prependEnd(ToolbarItem $item): static
{
return $this->prependItem($this->endItems, $item);
}
public function appendEnd(ToolbarItem $item): static
{
return $this->appendItem($this->endItems, $item);
}
/**
* @return TableComponent<Model>
*/
public function getTable(): TableComponent
{
return $this->table;
}
public function getSearch(): ?SearchItem
{
return $this->searchItem;
}
public function getPageSize(): ?PageSizeItem
{
return $this->pageSizeItem;
}
/**
* @return Collection<int, FilterItem>
*/
public function getFilters(): Collection
{
return $this->filterItems;
}
public function render(): View
{
return view('laralight-tables::toolbar', [
'startItems' => $this->startItems,
'midItems' => $this->midItems,
'endItems' => $this->endItems,
]);
}
}
|