Repository navigation
Expand file tree
/
Copy pathInactiveRow.php
More file actions
301 lines (256 loc) · 10.8 KB
/
Copy pathInactiveRow.php
File metadata and controls
301 lines (256 loc) · 10.8 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
declare(strict_types=1);
namespace NyonCode\WireTable\Support;
use NyonCode\WireTable\Concerns\InteractsWithRecordDisabledState;
use NyonCode\WireTable\Exceptions\TableConfigurationException;
/**
* What an *inactive* record looks like and what it still permits — the single
* owner of that decision for the row, the stacked card and the server alike.
*
* A record that has been cancelled, voided, archived or closed is still part of
* the list: it is read, searched and counted, and hiding it would be a lie about
* the data. What it is not is *writable*, and a table that keeps offering its
* editors invites a write the domain has already refused. So the state is
* declared once on the table — `Table::rowInactive()` — and everything that
* follows from it is resolved here:
*
* $table->rowInactive(
* fn (Order $order) => $order->status === 'cancelled',
* fn (InactiveRow $row) => $row->strikethrough()->color('danger'),
* );
*
* **The lock is the point, the styling is the hint.** Inline editing is off for
* an inactive record by default and is refused *server-side* — the table pushes
* this state into every editable column's own per-record disabled state
* ({@see InteractsWithRecordDisabledState}), which
* `Column::canEdit()` already enforces in `CellEditPipeline::commit()`, so a
* forged request and a fill-handle drag are refused on the same rule the cell
* renders from. Nothing here is cosmetic-only.
*
* **What stays live, by default.** Row actions, the selection checkbox and a
* record click. A cancelled order still has to be openable, and "un-cancel" is
* itself a row action — locking the whole row would take away the one control
* that undoes the state. Both are switchable ({@see selectable()},
* {@see actions()}) for a table where an inactive record really is inert.
*
* **The two looks are separate on purpose.** {@see dim()} is the state (the row
* has stopped being live) and is on by default; {@see strikethrough()} is the
* *claim* that the value itself was struck out, which is right for a cancelled
* invoice line and wrong for an archived customer, so it waits to be asked for.
* {@see color()} feeds the canonical row tint — the same resolver
* `Table::rowColor()` uses, so an inactive row and a coloured row cannot drift
* apart — and an explicit `rowColor()` on the table always wins over it.
*
* The project-wide default lives in `config('wire-table.defaults.inactive_rows')`
* and is read through {@see fromConfig()}, so a back office that wants one house
* style for every cancelled row says so once.
*/
final class InactiveRow
{
/** Setter name keyed by its normalized config key. */
private const OPTIONS = [
'strikethrough' => 'strikethrough',
'dim' => 'dim',
'color' => 'color',
'editing' => 'editing',
'selectable' => 'selectable',
'actions' => 'actions',
];
/**
* The strike's weight and colour on a form control. A 1px line in the text's
* own colour disappears into dimmed glyphs, so the line is 2px and one step
* darker than the text it crosses. A control draws its own decoration
* rather than inheriting the cell's, so it is named here as well.
*/
private const CONTROL_STRIKE = '[&_:is(input,select,textarea)]:decoration-2 '
.'[&_:is(input,select,textarea)]:decoration-gray-800 dark:[&_:is(input,select,textarea)]:decoration-gray-200';
private bool $strikethrough = false;
private bool $dim = true;
private ?string $color = 'gray';
private bool $editing = false;
private bool $selectable = true;
private bool $actions = true;
/** The shipped look: dimmed, tinted gray, not struck through, editing locked. */
public static function make(): self
{
return new self;
}
/**
* Build from a config value: `null` (or a missing key) for the shipped
* defaults, or a map of option => value applied on top of them. An unknown
* option is a typo that would otherwise silently do nothing, so it throws.
*
* @param array<string, bool|string|null>|null $config
*/
public static function fromConfig(?array $config): self
{
$row = new self;
if ($config === null) {
return $row;
}
foreach ($config as $option => $value) {
$setter = self::OPTIONS[strtolower(str_replace(['_', '-'], '', (string) $option))]
?? throw TableConfigurationException::unknownInactiveRowOption((string) $option, array_values(self::OPTIONS));
if ($setter === 'color') {
$row->color($value === null ? null : (string) $value);
continue;
}
$row->{$setter}((bool) $value);
}
return $row;
}
/**
* Strike the row's text through — the value was voided, not merely closed.
*
* Off by default. It reads as a statement about the *content* ("this line is
* not part of the total"), which is true of a cancelled order line and false
* of an archived customer, so the table says which one it means.
*/
public function strikethrough(bool $condition = true): static
{
$this->strikethrough = $condition;
return $this;
}
public function isStrikethrough(): bool
{
return $this->strikethrough;
}
/**
* Mute the row's text. On by default: it is the quiet half of the state and
* the only one that says "this row has stopped being live" without also
* claiming the values were struck out.
*/
public function dim(bool $condition = true): static
{
$this->dim = $condition;
return $this;
}
public function isDimmed(): bool
{
return $this->dim;
}
/**
* Tint an inactive row with a semantic role or raw hue (`'danger'`,
* `'gray'`, …), resolved by the canonical row-tint owner. Gray is the
* default: it sets the row apart without the claim a hue makes, and stays
* quiet on a table where most rows are cancelled. Null for no tint.
*/
public function color(?string $color): static
{
$this->color = $color === '' ? null : $color;
return $this;
}
public function getColor(): ?string
{
return $this->color;
}
/**
* Whether inline editing survives the state. Off by default — this is the
* reason the state exists.
*
* `editing(true)` re-opens the editors on an inactive row, for a table where
* the state is a label rather than a lock.
*/
public function editing(bool $allowed = true): static
{
$this->editing = $allowed;
return $this;
}
public function allowsEditing(): bool
{
return $this->editing;
}
/**
* Whether an inactive row may be ticked. On by default: bulk actions are how
* a hundred cancelled rows get archived, and the checkbox is how they are
* chosen.
*
* With `selectable(false)` the row's checkbox goes inert, "select page"
* skips the row and a forged toggle is refused. A **"select all matching"**
* selection is a query rather than a list (see `Table::bulkMaxRecords()`) and
* still covers inactive rows — a bulk action that must not touch them checks
* the record, exactly as it would for any other rule the database cannot
* express.
*/
public function selectable(bool $allowed = true): static
{
$this->selectable = $allowed;
return $this;
}
public function allowsSelection(): bool
{
return $this->selectable;
}
/**
* Whether the row's own actions stay operable. On by default, because the
* action that undoes the state lives there.
*
* With `actions(false)` the action cell goes inert, the right-click menu is
* not built for that record, and `executeTableAction()` / `openActionModal()`
* refuse it — which covers a *bound* record action too (`onClick()`,
* `onDoubleClick()`), since those execute through the same two methods.
*
* `recordUrl()` is the one row-wide click this does not touch: it is a plain
* link, and opening a cancelled record is reading rather than writing.
*/
public function actions(bool $allowed = true): static
{
$this->actions = $allowed;
return $this;
}
public function allowsActions(): bool
{
return $this->actions;
}
/**
* The `<tr>`'s share of the look.
*
* Both utilities are written as child/descendant variants rather than plain
* inherited ones, and that is load-bearing twice over: a body cell carries
* its own `dark:text-white`, which an inherited colour on the row would lose
* to in dark mode (the variant raises specificity past it), and a form
* control does not inherit `text-decoration` from its ancestors at all, so
* the strike has to be applied to the inputs themselves — which is precisely
* where an inactive row has to show it, since those are the values a reader
* would otherwise take for editable.
*
* The strike skips the actions cell (`data-row-actions`). A struck button
* reads as cancelled, and the actions are exactly what stays live on an
* inactive row, "Reactivate" first among them.
*/
public function rowClasses(): string
{
$classes = [];
if ($this->strikethrough) {
$classes[] = '[&>td:not([data-row-actions])]:line-through [&_input]:line-through [&_select]:line-through [&_textarea]:line-through '
.'[&>td:not([data-row-actions])]:decoration-2 [&>td:not([data-row-actions])]:decoration-gray-800 '
.'dark:[&>td:not([data-row-actions])]:decoration-gray-200 '
.self::CONTROL_STRIKE;
}
if ($this->dim) {
$classes[] = '[&>td]:text-gray-600 dark:[&>td]:text-gray-400';
}
return implode(' ', $classes);
}
/**
* The same look for the stacked card. Text dims by inheriting from the card,
* but the strike cannot sit on the card itself: a decoration carries into
* every block inside it and no descendant can take it off, the action
* buttons included. It goes on the regions marked `data-card-content`
* instead, which hold the record's values and none of its actions.
*/
public function cardClasses(): string
{
$classes = [];
if ($this->strikethrough) {
$classes[] = '[&_[data-card-content]]:line-through [&_input]:line-through [&_select]:line-through [&_textarea]:line-through '
.'[&_[data-card-content]]:decoration-2 [&_[data-card-content]]:decoration-gray-800 '
.'dark:[&_[data-card-content]]:decoration-gray-200 '
.self::CONTROL_STRIKE;
}
if ($this->dim) {
$classes[] = 'text-gray-600 dark:text-gray-400';
}
return implode(' ', $classes);
}
}