blob: f3fe81158add9be283e0bc629632c215b8f8ea60 (
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
|
<?php
namespace Lightscale\LaralightTables\Columns;
use Lightscale\LaralightTables\TableComponent;
use Illuminate\Database\Eloquent\Model;
use Illuminate\View\ComponentAttributeBag;
use Illuminate\Support\HtmlString;
use Closure;
class Column {
private TableComponent $table;
private bool $showInSelect;
private ?Closure $slotFn = null;
private ?Closure $sortFn = null;
private ?Closure $tdAttributesFn = null;
public function __construct(
public string $name,
public ?string $title = null
) {
$this->showInSelect = $this->title !== null;
}
public static function make(string $name, ?string $title = null) : static
{
return new static($name, $title);
}
public function setTable(TableComponent $table) : void
{
$this->table = $table;
}
private function defaultSlot(Model $row)
{
return $row->{$this->name};
}
public function slot(callable $fn) : static
{
$this->slotFn = Closure::fromCallable($fn);
return $this;
}
public function sortable(callable $fn) : static
{
$this->sortFn = Closure::fromCallable($fn);
return $this;
}
public function tdAttributes(callable $fn) : static
{
$this->tdAttributesFn = Closure::fromCallable($fn);
return $this;
}
public function showInSelect($show = true)
{
$this->showInSelect = $show;
}
public function getShowInSelect()
{
return $this->showInSelect;
}
protected function getContent(Model $row)
{
return $this->slotFn?->call($this, $row, $this) ?? $this->defaultSlot($row);
}
public function view(Model $row)
{
$attributes = $this->tdAttributesFn?->call($this, $row) ?? [];
$attributes = (new ComponentAttributeBag($attributes))->toHtml();
$content = $this->getContent($row);
return new HtmlString("<td {$attributes}>{$content}</td>");
}
}
|