Repository navigation
Expand file tree
/
Copy pathShellRenderPlan.php
More file actions
121 lines (115 loc) · 6.23 KB
/
Copy pathShellRenderPlan.php
File metadata and controls
121 lines (115 loc) · 6.23 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
<?php
declare(strict_types=1);
namespace NyonCode\WireTable\Support;
use NyonCode\WireTable\Contracts\HostsTableShell;
use NyonCode\WireTable\Filters\Filter;
use NyonCode\WireTable\Table;
/**
* The frame around the rows: whether it renders yet, how it stays current, and
* which of its optional regions exist.
*
* Part of {@see TableRenderPlan}, and the last group the head block gave up.
* Three concerns that share one property — none of them is about a row:
*
* - **arrival** — a `lazy()` table renders a placeholder until the host says it
* is ready, and the placeholder is the only thing in the document until then;
* - **freshness** — `poll()` produces the `wire:poll` attribute and the config
* the Stop control reads; `live(broadcast: true)` adds a channel to listen on.
* The channel is null without the opt-in, so a table that did not ask for it
* ships no listener and needs no channel authorization;
* - **regions** — filters, sub-rows, grouping and the view menu each earn their
* markup or do not. These are the conditions the view branches on, hoisted out
* of it.
*
* The three `&&` pairs are the reason this is worth a class rather than four
* `$table->…()` calls in Blade. `isSubRowsExpandable`, `allRowsExpanded` and
* `hasGroupSummaries` are each only meaningful when their feature is on, and the
* guard is not decoration: a table with no sub-rows still answers
* {@see Table::isSubRowsExpandable()} from a default, and rendering an expand-all
* control for rows that cannot expand is exactly the bug the guard prevents.
*
* `$hasViewMenu` is the one value with an edge outside this group: the menu
* holds column toggles, sub-row expansion, saved views, or any mix, so the
* columns group has to have been resolved first. It is passed in rather than reached for — the same
* shape {@see PagingRenderPlan} uses — which keeps the group's inputs visible in
* its signature instead of hidden in its body.
*/
final class ShellRenderPlan
{
/**
* @param ?string $lazyPlaceholder Custom placeholder markup, or null for the default.
* @param array<string, mixed> $pollingConfig `['enabled' => false]`, or the interval plus whether it is running.
* @param ?string $pollingAttribute The literal `wire:poll…` value, or null when not polling.
* @param ?string $liveChannel The broadcast channel to listen on, or null without `live(broadcast: true)`.
* @param array<int, Filter> $filters The table-level filters (column filters live on the columns group).
* @param array<int, string> $savedViews Names of this user's saved views, empty when the feature is off.
* @param array<int, string> $sharedViews Names of the views shared with everyone (Table::shareViews()).
* @param bool $canShareViews Whether this user may publish and delete shared views.
* @param string $viewMenuLabel Narrows to "toggle columns" when that is all the menu holds.
*/
private function __construct(
public readonly bool $isLazy,
public readonly bool $isTableReady,
public readonly ?string $lazyPlaceholder,
public readonly array $pollingConfig,
public readonly ?string $pollingAttribute,
public readonly ?string $liveChannel,
public readonly array $filters,
public readonly bool $hasFilters,
public readonly bool $hasSubRows,
public readonly bool $isSubRowsExpandable,
public readonly bool $allRowsExpanded,
public readonly bool $hasGrouping,
public readonly bool $hasGroupSummaries,
public readonly bool $hasSavedViews,
/** @var array<int, string> */
public readonly array $savedViews,
public readonly bool $hasViewMenu,
public readonly string $viewMenuLabel,
/** @var array<int, string> */
public readonly array $sharedViews = [],
public readonly bool $canShareViews = false,
/**
* Whether this page draws the expander column. Narrower than
* $hasSubRows: a page where no record gets sub-rows drops it.
*/
public readonly bool $hasExpanderColumn = false,
) {}
/**
* @param HostsTableShell|mixed $component The host. Typed `mixed` so a
* consumer's component needs no `implements`; the contract is what it must
* be able to answer, and what a test double implements.
*/
public static function resolve(Table $table, mixed $component, bool $hasColumnToggles, ?bool $hasExpanderColumn = null): self
{
$filters = $table->getFilters();
$hasSubRows = $table->hasSubRows();
$hasGrouping = $table->hasGrouping();
$isSubRowsExpandable = $hasSubRows && $table->isSubRowsExpandable();
$hasSavedViews = $table->getSavedViewsKey() !== null;
return new self(
isLazy: $table->isLazy(),
isTableReady: $component->isTableReady(),
lazyPlaceholder: $table->getLazyPlaceholder(),
pollingConfig: $component->getTablePollingConfig(),
pollingAttribute: $component->getTablePollingAttribute(),
liveChannel: $component->getTableLiveChannel(),
filters: $filters,
hasFilters: $filters !== [],
hasSubRows: $hasSubRows,
isSubRowsExpandable: $isSubRowsExpandable,
allRowsExpanded: $hasSubRows && $component->expandsSubRowsByDefault(),
hasGrouping: $hasGrouping,
hasGroupSummaries: $hasGrouping && $component->tableHasGroupSummaries(),
hasSavedViews: $hasSavedViews,
savedViews: $hasSavedViews ? $component->getTableViews() : [],
sharedViews: $hasSavedViews ? $component->getSharedTableViews() : [],
canShareViews: $hasSavedViews && $component->canShareTableViews(),
hasExpanderColumn: $hasExpanderColumn ?? $hasSubRows,
hasViewMenu: $hasColumnToggles || $isSubRowsExpandable || $hasSavedViews || $table->hasPinnableColumns(),
viewMenuLabel: $hasColumnToggles && ! $isSubRowsExpandable && ! $hasSavedViews
? __('wire-table::messages.toggle_columns')
: __('wire-table::messages.view_options'),
);
}
}