Skip to content

Commit 564c2f0

Browse files
authored
Columns() Setup Improvements (rappasoft#2089)
* Initial Commit * Fix styling * Clean Up Commented Code * Fix styling --------- Co-authored-by: lrljoe <[email protected]>
1 parent b026e05 commit 564c2f0

File tree

3 files changed

+41
-113
lines changed

3 files changed

+41
-113
lines changed

src/Traits/Configuration/ColumnConfiguration.php

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,14 @@ trait ColumnConfiguration
88
{
99
public function setPrependedColumns(array $prependedColumns): void
1010
{
11-
$this->prependedColumns = collect($prependedColumns)
12-
->filter(fn ($column) => $column instanceof Column)
13-
->map(function (Column $column) {
14-
$column->setTheme($this->getTheme());
15-
$column->setHasTableRowUrl($this->hasTableRowUrl());
16-
$column->setIsReorderColumn($this->getDefaultReorderColumn() == $column->getField());
17-
18-
if ($column->hasField()) {
19-
if ($column->isBaseColumn()) {
20-
$column->setTable($this->getBuilder()->getModel()->getTable());
21-
} else {
22-
$column->setTable($this->getTableForColumn($column));
23-
}
24-
}
25-
26-
return $column;
27-
});
11+
$this->prependedColumns = collect($prependedColumns);
12+
$this->hasRunColumnSetup = false;
2813
}
2914

3015
public function setAppendedColumns(array $appendedColumns): void
3116
{
32-
$this->appendedColumns = collect($appendedColumns)
33-
->filter(fn ($column) => $column instanceof Column)
34-
->map(function (Column $column) {
35-
$column->setTheme($this->getTheme());
36-
$column->setHasTableRowUrl($this->hasTableRowUrl());
37-
$column->setIsReorderColumn($this->getDefaultReorderColumn() == $column->getField());
38-
39-
if ($column->hasField()) {
40-
if ($column->isBaseColumn()) {
41-
$column->setTable($this->getBuilder()->getModel()->getTable());
42-
} else {
43-
$column->setTable($this->getTableForColumn($column));
44-
}
45-
}
46-
47-
return $column;
48-
});
17+
$this->appendedColumns = collect($appendedColumns);
18+
$this->hasRunColumnSetup = false;
4919
}
5020

5121
public function unsetCollapsedStatuses(): void

src/Traits/Helpers/ColumnHelpers.php

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@ trait ColumnHelpers
1313
*/
1414
public function setColumns(): void
1515
{
16-
$this->prependedColumns = $this->getPrependedColumns();
16+
$columns = collect($this->getPrependedColumns())->concat($this->columns())->concat(collect($this->getAppendedColumns()));
17+
$this->columns = $columns->filter(fn ($column) => $column instanceof Column);
18+
}
1719

18-
$columns = collect($this->columns())
20+
protected function setupColumns(): void
21+
{
22+
$this->columns = $this->columns
1923
->filter(fn ($column) => $column instanceof Column)
2024
->map(function (Column $column) {
21-
$column->setTheme($this->getTheme());
22-
$column->setHasTableRowUrl($this->hasTableRowUrl());
23-
$column->setIsReorderColumn($this->getDefaultReorderColumn() == $column->getField());
25+
$column->setTheme($this->getTheme())
26+
->setHasTableRowUrl($this->hasTableRowUrl())
27+
->setIsReorderColumn($this->getDefaultReorderColumn() == $column->getField());
28+
2429
if ($column instanceof AggregateColumn) {
2530
if ($column->getAggregateMethod() == 'count' && $column->hasDataSource()) {
2631
$this->addExtraWithCount($column->getDataSource());
@@ -42,13 +47,15 @@ public function setColumns(): void
4247
return $column;
4348
});
4449

45-
$this->appendedColumns = $this->getAppendedColumns();
46-
47-
$this->columns = collect([...$this->prependedColumns, ...$columns, ...$this->appendedColumns]);
50+
$this->hasRunColumnSetup = true;
4851
}
4952

5053
public function getColumns(): Collection
5154
{
55+
if (! $this->hasRunColumnSetup) {
56+
$this->setupColumns();
57+
}
58+
5259
return $this->columns;
5360
}
5461

@@ -206,63 +213,12 @@ public function getColspanCount(): int
206213

207214
public function getPrependedColumns(): Collection
208215
{
209-
return collect($this->prependedColumns ?? $this->prependColumns())
210-
->filter(fn ($column) => $column instanceof Column)
211-
->map(function (Column $column) {
212-
$column->setTheme($this->getTheme());
213-
$column->setHasTableRowUrl($this->hasTableRowUrl());
214-
$column->setIsReorderColumn($this->getDefaultReorderColumn() == $column->getField());
215-
if ($column instanceof AggregateColumn) {
216-
if ($column->getAggregateMethod() == 'count' && $column->hasDataSource()) {
217-
$this->addExtraWithCount($column->getDataSource());
218-
} elseif ($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) {
219-
$this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn());
220-
} elseif ($column->getAggregateMethod() == 'avg' && $column->hasDataSource() && $column->hasForeignColumn()) {
221-
$this->addExtraWithAvg($column->getDataSource(), $column->getForeignColumn());
222-
}
223-
}
224-
225-
if ($column->hasField()) {
226-
if ($column->isBaseColumn()) {
227-
$column->setTable($this->getBuilder()->getModel()->getTable());
228-
} else {
229-
$column->setTable($this->getTableForColumn($column));
230-
}
231-
}
232-
233-
return $column;
234-
});
216+
return $this->prependedColumns ?? collect($this->prependColumns());
235217
}
236218

237219
public function getAppendedColumns(): Collection
238220
{
239-
return collect($this->appendedColumns ?? $this->appendColumns())
240-
->filter(fn ($column) => $column instanceof Column)
241-
->map(function (Column $column) {
242-
$column->setTheme($this->getTheme());
243-
$column->setHasTableRowUrl($this->hasTableRowUrl());
244-
$column->setIsReorderColumn($this->getDefaultReorderColumn() == $column->getField());
245-
if ($column instanceof AggregateColumn) {
246-
if ($column->getAggregateMethod() == 'count' && $column->hasDataSource()) {
247-
$this->addExtraWithCount($column->getDataSource());
248-
} elseif ($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) {
249-
$this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn());
250-
} elseif ($column->getAggregateMethod() == 'avg' && $column->hasDataSource() && $column->hasForeignColumn()) {
251-
$this->addExtraWithAvg($column->getDataSource(), $column->getForeignColumn());
252-
}
253-
}
254-
255-
if ($column->hasField()) {
256-
if ($column->isBaseColumn()) {
257-
$column->setTable($this->getBuilder()->getModel()->getTable());
258-
} else {
259-
$column->setTable($this->getTableForColumn($column));
260-
}
261-
}
262-
263-
return $column;
264-
});
265-
221+
return $this->appendedColumns ?? collect($this->appendColumns());
266222
}
267223

268224
public function getCollapsedAlwaysColumns(): Collection
@@ -287,4 +243,20 @@ public function shouldCollapseAlways(): bool
287243

288244
return $this->shouldAlwaysCollapse;
289245
}
246+
247+
/**
248+
* Prepend columns.
249+
*/
250+
public function prependColumns(): array
251+
{
252+
return [];
253+
}
254+
255+
/**
256+
* Append columns.
257+
*/
258+
public function appendColumns(): array
259+
{
260+
return [];
261+
}
290262
}

src/Traits/WithColumns.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ trait WithColumns
1515

1616
protected Collection $columns;
1717

18-
protected Collection $prependedColumns;
18+
protected ?Collection $prependedColumns;
1919

20-
protected Collection $appendedColumns;
20+
protected ?Collection $appendedColumns;
2121

2222
protected ?bool $shouldAlwaysCollapse;
2323

2424
protected ?bool $shouldMobileCollapse;
2525

2626
protected ?bool $shouldTabletCollapse;
2727

28+
protected bool $hasRunColumnSetup = false;
29+
2830
/**
2931
* Sets up Columns
3032
*/
@@ -55,22 +57,6 @@ public function bootedWithColumns(): void
5557
*/
5658
abstract public function columns(): array;
5759

58-
/**
59-
* Prepend columns.
60-
*/
61-
public function prependColumns(): array
62-
{
63-
return [];
64-
}
65-
66-
/**
67-
* Append columns.
68-
*/
69-
public function appendColumns(): array
70-
{
71-
return [];
72-
}
73-
7460
/**
7561
* Add Columns to View
7662
*/

0 commit comments

Comments
 (0)