summaryrefslogtreecommitdiff
path: root/src/Toolbar.php
blob: 10082ae95903b53cebaae79b2d87a5b8461d7153 (plain)
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
<?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;

class Toolbar
{
    use Makable;

    protected ?SearchItem $searchItem = null;
    protected ?PageSizeItem $pageSizeItem = null;

    protected Collection $startItems;
    protected Collection $midItems;
    protected Collection $endItems;
    protected Collection $filterItems;

    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;
        }
    }

    private function prependItem(Collection $list, ToolbarItem $item): static
    {
        $this->processItem($item);
        $list->prepend($item);
        return $this;
    }

    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);
    }

    public function getTable(): TableComponent
    {
        return $this->table;
    }

    public function getSearch(): ?SearchItem
    {
        return $this->searchItem;
    }

    public function getPageSize(): ?PageSizeItem
    {
        return $this->pageSizeItem;
    }

    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,
        ]);
    }

}