Repository navigation
Expand file tree
/
Copy pathTablePartials.php
More file actions
187 lines (162 loc) · 6.73 KB
/
Copy pathTablePartials.php
File metadata and controls
187 lines (162 loc) · 6.73 KB
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
<?php
declare(strict_types=1);
namespace NyonCode\WireTable\Support;
use Closure;
use NyonCode\WireCore\Foundation\Support\IslandViewScope;
use NyonCode\WireTable\Contracts\SummarisesTable;
use NyonCode\WireTable\Table;
/**
* Everything a set of changed rows moves, as a map of partial name to markup.
*
* A write to one cell does not only move that row. It moves the row's card on
* the width where the table is hidden, the group subtotal the row belongs to,
* and the grand totals — each of which lives outside the row and has to be
* re-rendered on its own or go stale while the row beside it updates.
*
* Deciding *which* of those a change touches is a question about the table, not
* about Livewire, and this answers it. Handing the answers back as a map rather
* than queueing them is what keeps it that way: the host loops the map through
* its own `renderPartial()`, and everything up to that point can be exercised
* without a browser — which is where this logic used to be reachable only.
*
* The markup is closures, so a partial the host decides not to send costs
* nothing to have been offered.
*
* A stacked table whose response carried only one half
* ({@see Table::getClientLayout()}) is answered for that half alone: the rows,
* group subtotals and desktop totals only where the table is in the document,
* the cards and the card totals only where the cards are. The other half has no
* anchor in that browser, so rendering it would be work the client discards.
*/
final class TablePartials
{
/**
* @param SummarisesTable $host The Livewire component; typed `object` natively
* so a consumer's component needs no `implements`.
*/
private function __construct(
private readonly Table $table,
private readonly object $host,
private readonly TableRenderPlan $plan,
) {}
/**
* Render one region the way a partial of it is rendered, wherever it is
* rendered from.
*
* A partial is morphed over what the full render left on the page, pairing
* children by position, so the two have to be the same nodes. They were not:
* the full render runs inside Livewire's pass, where every block `@if` of a
* cell view emits `[if BLOCK]` comments, and the partial runs after it, where
* none do. A comment meeting the input's wrapper made the morph swap the
* wrapper — and the input the user had just clicked into lost the focus the
* moment the previous cell's save came back. Rendering both outside the pass
* makes them one render rather than two that are kept in step.
*
* Only on a table that sends partials: one that does not has nothing to keep
* in step, and keeps rendering exactly as it did.
*
* @param Closure(): string $render
*/
public static function region(Table $table, Closure $render): string
{
return $table->usesRowPartials()
? IslandViewScope::outsideLivewireRender($render)
: $render();
}
/** @param SummarisesTable $host */
public static function for(Table $table, object $host, TableRenderPlan $plan): self
{
return new self($table, $host, $plan);
}
/**
* The rows themselves, keyed `row-{key}`.
*
* Position matters and travels with the record: a row renders differently
* depending on where it sits — striping, and the first/last row's chrome —
* so it is the position on the page, not the position among the changed.
*
* @param array<array-key, mixed> $page Every record on the page, in order.
* @param array<int, array-key> $changed Keys whose contents moved.
* @return array<string, callable(): string>
*/
public function rows(array $page, array $changed): array
{
if ($this->table->getClientLayout() === 'cards') {
return [];
}
$rows = RowRenderer::for($this->table, $this->host, $this->plan);
$partials = [];
$position = 0;
foreach ($page as $recordKey => $record) {
$index = $position++;
if (! in_array($recordKey, $changed, true)) {
continue;
}
$partials['row-'.$recordKey] = fn (): string => $rows->render($record, $index);
}
return $partials;
}
/**
* Everything else the changed records move: their cards, their groups'
* subtotals, and the table's totals.
*
* @param array<array-key, mixed> $records The changed records only.
* @return array<string, callable(): string>
*/
public function satellites(array $records): array
{
if ($records === []) {
return [];
}
$partials = [];
$layout = $this->table->getClientLayout();
$cards = $this->table->isStackedOnMobile() && $layout !== 'table';
$desktop = $layout !== 'cards';
if ($cards) {
$renderer = CardRenderer::for($this->table, $this->host, $this->plan);
foreach ($records as $recordKey => $record) {
$partials['card-'.$recordKey] = fn (): string => $renderer->render($record);
}
}
$summaries = SummaryRenderer::for($this->table, $this->host, $this->plan);
// A group's subtotal is moved by a write to any of its members, and it
// is a sibling row rather than part of one — so each changed record's
// group is re-rendered, once however many of its rows moved.
if ($desktop && $this->host->tableHasGroupSummaries()) {
foreach ($this->changedGroups($records) as $groupValue) {
foreach ($summaries->group($groupValue) as $name => $html) {
$partials[$name] = fn (): string => $html;
}
}
}
if (! $this->host->tableHasSummaries()) {
return $partials;
}
if ($desktop) {
$partials['summary'] = fn (): string => $summaries->desktop();
}
if ($cards) {
$partials['summary-mobile'] = fn (): string => $summaries->mobile();
}
return $partials;
}
/**
* The distinct groups the changed records belong to.
*
* Keyed by the string form so two records of one group collapse to one
* subtotal render, while the value keeps its original type — the renderer
* matches groups by that, not by its string.
*
* @param array<array-key, mixed> $records
* @return array<string, mixed>
*/
private function changedGroups(array $records): array
{
$groups = [];
foreach ($records as $record) {
$value = $this->table->getGroupComparisonKey($record);
$groups[(string) $value] ??= $value;
}
return $groups;
}
}