summaryrefslogtreecommitdiff
path: root/src/TableComponent.php
blob: 266cdc39cd04ee9be5ecf2d28d28cb17b9b36068 (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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php

namespace Lightscale\LaralightTables;

use Lightscale\LaralightTables\Columns\Column;

use Livewire\Component;
use Livewire\WithPagination;
use Livewire\Attributes\Url;

use Illuminate\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;

use Exception;

abstract class TableComponent extends Component
{
    use WithPagination;

    // Config
    protected $paginationTheme = 'bootstrap';
    protected $model = null;
    protected int $defaultPageSize = 10;

    // Properties
    #[Url]
    public string $search = '';

    #[Url]
    public int $pageSize;

    public array $activeColumns = [];

    #[Url]
    public array $filters = [];

    #[Url]
    public ?string $order = null;

    #[Url]
    public ?string $orderDirection = null;

    public function mount(): void
    {
        $this->setDefaultActiveColumns();
        $this->setDefaultPageSize();
    }

    protected function setDefaultPageSize(): void
    {
        if (!isset($this->pageSize)) {
            $this->pageSize = $this->defaultPageSize;
        }
    }

    protected function setDefaultActiveColumns(): void
    {
        foreach($this->getColumns() as $column) {
            $this->activeColumns[] = $column->name;
        }
    }

    protected function isSearching(): bool
    {
        $search = $this->getToolbar()->getSearch();
        return $search !== null && Str::length($this->search) >= $search->getMinLength();
    }

    public function updatedSearch(): void
    {
        if ($this->isSearching()) {
            $this->resetPage();
        }
    }

    public function updatedFilters(): void
    {
        $this->resetPage();
    }

    public function orderBy(string $column): void
    {
        if ($column === $this->order) {
            if ($this->orderDirection === 'desc') {
                $this->order = null;
                $this->orderDirection = null;
            }
            else {
                $this->orderDirection = 'desc';
            }
        }
        else {
            $this->order = $column;
            $this->orderDirection = 'asc';
        }
    }

    protected function toolbar(): ?Toolbar
    {
        return null;
    }

    private ?Toolbar $toolbarCache;

    protected function getToolbar(): ?Toolbar
    {
        if (!isset($this->_toolbar)) {
            $this->toolbar = $this->toolbar();
        }
        return $this->toolbar;
    }

    protected function query() : Builder
    {
        if($this->model === null) {
            throw new Exception('Requires $model to be set or query() method to be overridden');
        }

        return $this->model::query();
    }

    abstract protected function columns() : array;

    protected function search(Builder $builder, string $search) : void {}

    protected function getFilters(): Collection
    {
        return $this->getToolbar()?->getFilters() ?? collect();
    }


    protected function getOrderColumn(): ?Column
    {
        return ($name = $this->order) === null ? null : $this->getColumns()[$name] ?? null;
    }

    protected function applyOrder(Builder $query): void
    {
        $column = $this->getOrderColumn();
        if ($column) {
            $column->applySort($query, $this->orderDirection);
        }
    }

    protected function buildQuery() : Builder
    {
        $query = $this->query();

        if ($this->isSearching()) {
            $this->search($query, $this->search);
        }

        foreach ($this->getFilters() as $filter) {
            $filter->applyFilter($query);
        }

        $this->applyOrder($query);

        return $query;
    }

    private ?Collection $columnsCache = null;
    public function getColumns(): Collection
    {
        if($this->columnsCache === null) {
            $this->columnsCache = collect($this->columns())->each(
                fn($c) => $c->setTable($this)
            )->keyBy('name');
        }

        return $this->columnsCache;
    }

    public function render()
    {
        $data = $this->buildQuery()->paginate($this->pageSize);
        $allColumns = $this->getColumns();
        $columns = $allColumns->filter(fn($c) => in_array($c->name,$this->activeColumns));
        $toolbar = $this->toolbar();

        Paginator::defaultView('laralight-tables::pagination');

        return view('laralight-tables::table', compact(
            'data', 'allColumns', 'columns', 'toolbar',
        ));
    }
}