summaryrefslogtreecommitdiff
path: root/src/Toolbar.php
blob: 2db39f5671f619cc134436af113d7a736ba5f5fb (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
<?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,
        ]);
    }

}