diff options
author | Sam Light <samlight1994@gmail.com> | 2025-03-27 10:53:34 +0000 |
---|---|---|
committer | Sam Light <samlight1994@gmail.com> | 2025-03-27 10:53:34 +0000 |
commit | 1f81ce361cb454b1655d6e2a7ac031bc1e3b2ede (patch) | |
tree | 1307ebad02e2a9644613d496e3d3ec87ed2cdfb9 /src/Toolbar.php | |
parent | 56a180f139699b3ee1eec566b6a98105576f804a (diff) |
Seperating toolbar into its own class and views
Diffstat (limited to 'src/Toolbar.php')
-rw-r--r-- | src/Toolbar.php | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/Toolbar.php b/src/Toolbar.php new file mode 100644 index 0000000..2db39f5 --- /dev/null +++ b/src/Toolbar.php @@ -0,0 +1,88 @@ +<?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 FitlerItem; + +use Illuminate\View\View; +use Illuminate\Support\Collection; + +class Toolbar +{ + protected ?SearchItem $searchItem = null; + protected ?PageSizeItem $pageSizeItem = null; + + protected Collection $startItems; + protected Collection $midItems; + protected Collection $endItems; + + public function __construct( + private TableComponent $table + ) + { + $this->startItems = collect(); + $this->midItems = collect(); + $this->endItems = collect(); + } + + private function addItem(Collection $list, ToolbarItem $item): static + { + $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; + } + + $list->push($item); + return $this; + } + + public function addStartItem(ToolbarItem $item): static + { + return $this->addItem($this->startItems, $item); + } + + public function addMidItem(ToolbarItem $item): static + { + return $this->addItem($this->midItems, $item); + } + + public function addEndItem(ToolbarItem $item): static + { + return $this->addItem($this->endItems, $item); + } + + public function getTable(): TableComponent + { + return $this->table; + } + + public function getSearch(): ?SearchItem + { + return $this->searchItem; + } + + public function getPageSize(): ?PageSizeItem + { + return $this->pageSizeItem; + } + + public function render(): View + { + return view('laralight-tables::toolbar', [ + 'startItems' => $this->startItems, + 'midItems' => $this->midItems, + 'endItems' => $this->endItems, + ]); + } + +} |