Skip to content

Commit f17a0d8

Browse files
authored
Tidy computed properties behaviour (rappasoft#2160)
* Split ComponentUtilities into traits * Fix styling --------- Co-authored-by: lrljoe <[email protected]>
1 parent 26f094b commit f17a0d8

File tree

8 files changed

+170
-131
lines changed

8 files changed

+170
-131
lines changed

src/Traits/ComponentUtilities.php

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,23 @@
66
use Livewire\Attributes\Locked;
77
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
88
use Rappasoft\LaravelLivewireTables\Traits\Configuration\ComponentConfiguration;
9+
use Rappasoft\LaravelLivewireTables\Traits\Core\Component\{HandlesComputedProperties,HandlesEmptyMessage, HandlesFingerprint, HandlesOfflineIndicator,HandlesTableName};
910
use Rappasoft\LaravelLivewireTables\Traits\Helpers\ComponentHelpers;
1011

1112
trait ComponentUtilities
1213
{
13-
use ComponentConfiguration,
14+
use HandlesTableName,
15+
HandlesFingerprint,
16+
HandlesEmptyMessage,
17+
HandlesComputedProperties,
18+
HandlesOfflineIndicator,
19+
ComponentConfiguration,
1420
ComponentHelpers;
1521

1622
public array $table = [];
1723

1824
protected $model;
1925

20-
#[Locked]
21-
public string $tableName = 'table';
22-
23-
#[Locked]
24-
public ?string $dataTableFingerprint;
25-
26-
protected bool $offlineIndicatorStatus = true;
27-
28-
protected string $emptyMessage = 'No items found, try to broaden your search';
29-
30-
protected bool $useComputedProperties = true;
31-
3226
protected bool $hasRunConfigure = false;
3327

3428
/**
@@ -82,17 +76,6 @@ protected function runCoreConfiguration(): void
8276
}
8377
}
8478

85-
/**
86-
* Returns a unique id for the table, used as an alias to identify one table from another session and query string to prevent conflicts
87-
*/
88-
protected function generateDataTableFingerprint(): string
89-
{
90-
$className = str_split(static::class);
91-
$crc32 = sprintf('%u', crc32(serialize($className)));
92-
93-
return base_convert($crc32, 10, 36);
94-
}
95-
9679
/**
9780
* 1. After the sorting method is hit we need to tell the table to go back into reordering mode
9881
*/

src/Traits/Configuration/ComponentConfiguration.php

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,4 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Traits\Configuration;
44

5-
trait ComponentConfiguration
6-
{
7-
/**
8-
* Set the empty message
9-
*/
10-
public function setEmptyMessage(string $message): self
11-
{
12-
$this->emptyMessage = $message;
13-
14-
return $this;
15-
}
16-
17-
public function setOfflineIndicatorStatus(bool $status): self
18-
{
19-
$this->offlineIndicatorStatus = $status;
20-
21-
return $this;
22-
}
23-
24-
public function setOfflineIndicatorEnabled(): self
25-
{
26-
$this->setOfflineIndicatorStatus(true);
27-
28-
return $this;
29-
}
30-
31-
public function setOfflineIndicatorDisabled(): self
32-
{
33-
$this->setOfflineIndicatorStatus(false);
34-
35-
return $this;
36-
}
37-
38-
public function setDataTableFingerprint(string $dataTableFingerprint): self
39-
{
40-
$this->dataTableFingerprint = $dataTableFingerprint;
41-
42-
return $this;
43-
}
44-
45-
public function useComputedPropertiesEnabled(): self
46-
{
47-
$this->useComputedProperties = true;
48-
49-
return $this;
50-
}
51-
52-
public function useComputedPropertiesDisabled(): self
53-
{
54-
$this->useComputedProperties = false;
55-
56-
return $this;
57-
}
58-
}
5+
trait ComponentConfiguration {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Component;
4+
5+
trait HandlesComputedProperties
6+
{
7+
protected bool $useComputedProperties = true;
8+
9+
public function useComputedPropertiesEnabled(): self
10+
{
11+
$this->useComputedProperties = true;
12+
13+
return $this;
14+
}
15+
16+
public function useComputedPropertiesDisabled(): self
17+
{
18+
$this->useComputedProperties = false;
19+
20+
return $this;
21+
}
22+
23+
public function getComputedPropertiesStatus(): bool
24+
{
25+
return $this->useComputedProperties ?? false;
26+
}
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Component;
4+
5+
trait HandlesEmptyMessage
6+
{
7+
protected string $emptyMessage = 'No items found, try to broaden your search';
8+
9+
/**
10+
* Get the translated empty message of the table
11+
*/
12+
public function getEmptyMessage(): string
13+
{
14+
if ($this->emptyMessage == 'No items found, try to broaden your search') {
15+
return __($this->getLocalisationPath().'No items found, try to broaden your search');
16+
}
17+
18+
return $this->emptyMessage;
19+
}
20+
21+
/**
22+
* Set the empty message
23+
*/
24+
public function setEmptyMessage(string $message): self
25+
{
26+
$this->emptyMessage = $message;
27+
28+
return $this;
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Component;
4+
5+
use Livewire\Attributes\Locked;
6+
7+
trait HandlesFingerprint
8+
{
9+
#[Locked]
10+
public ?string $dataTableFingerprint;
11+
12+
public function getDataTableFingerprint(): string
13+
{
14+
return $this->dataTableFingerprint ?? ($this->dataTableFingerprint = $this->generateDataTableFingerprint());
15+
}
16+
17+
public function setDataTableFingerprint(string $dataTableFingerprint): self
18+
{
19+
$this->dataTableFingerprint = $dataTableFingerprint;
20+
21+
return $this;
22+
}
23+
24+
/**
25+
* Returns a unique id for the table, used as an alias to identify one table from another session and query string to prevent conflicts
26+
*/
27+
protected function generateDataTableFingerprint(): string
28+
{
29+
$className = str_split(static::class);
30+
$crc32 = sprintf('%u', crc32(serialize($className)));
31+
32+
return base_convert($crc32, 10, 36);
33+
}
34+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Component;
4+
5+
trait HandlesOfflineIndicator
6+
{
7+
protected bool $offlineIndicatorStatus = true;
8+
9+
public function getOfflineIndicatorStatus(): bool
10+
{
11+
return $this->offlineIndicatorStatus;
12+
}
13+
14+
public function offlineIndicatorIsEnabled(): bool
15+
{
16+
return $this->getOfflineIndicatorStatus() === true;
17+
}
18+
19+
public function offlineIndicatorIsDisabled(): bool
20+
{
21+
return $this->getOfflineIndicatorStatus() === false;
22+
}
23+
24+
public function setOfflineIndicatorStatus(bool $status): self
25+
{
26+
$this->offlineIndicatorStatus = $status;
27+
28+
return $this;
29+
}
30+
31+
public function setOfflineIndicatorEnabled(): self
32+
{
33+
$this->setOfflineIndicatorStatus(true);
34+
35+
return $this;
36+
}
37+
38+
public function setOfflineIndicatorDisabled(): self
39+
{
40+
$this->setOfflineIndicatorStatus(false);
41+
42+
return $this;
43+
}
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Component;
4+
5+
use Livewire\Attributes\{Computed,Locked};
6+
7+
trait HandlesTableName
8+
{
9+
#[Locked]
10+
public string $tableName = 'table';
11+
12+
public function setTableName(string $name): string
13+
{
14+
return $this->tableName = $name;
15+
}
16+
17+
#[Computed]
18+
public function getTableName(): string
19+
{
20+
return $this->tableName;
21+
}
22+
23+
public function isTableNamed(string $name): bool
24+
{
25+
return $this->tableName === $name;
26+
}
27+
}

src/Traits/Helpers/ComponentHelpers.php

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77

88
trait ComponentHelpers
99
{
10-
public function getDataTableFingerprint(): string
11-
{
12-
return $this->dataTableFingerprint ?? ($this->dataTableFingerprint = $this->generateDataTableFingerprint());
13-
}
14-
1510
public function hasModel(): bool
1611
{
1712
return $this->model !== null;
@@ -25,57 +20,9 @@ public function getModel()
2520
return $this->model;
2621
}
2722

28-
/**
29-
* Get the translated empty message of the table
30-
*/
31-
public function getEmptyMessage(): string
32-
{
33-
if ($this->emptyMessage == 'No items found, try to broaden your search') {
34-
return __($this->getLocalisationPath().'No items found, try to broaden your search');
35-
}
36-
37-
return $this->emptyMessage;
38-
}
39-
40-
public function getOfflineIndicatorStatus(): bool
41-
{
42-
return $this->offlineIndicatorStatus;
43-
}
44-
45-
public function offlineIndicatorIsEnabled(): bool
46-
{
47-
return $this->getOfflineIndicatorStatus() === true;
48-
}
49-
50-
public function offlineIndicatorIsDisabled(): bool
51-
{
52-
return $this->getOfflineIndicatorStatus() === false;
53-
}
54-
55-
public function setTableName(string $name): string
56-
{
57-
return $this->tableName = $name;
58-
}
59-
60-
#[Computed]
61-
public function getTableName(): string
62-
{
63-
return $this->tableName;
64-
}
65-
6623
#[Computed]
6724
public function getTableId(): string
6825
{
6926
return $this->getTableAttributes()['id'] ?? 'table-'.$this->getTableName();
7027
}
71-
72-
public function isTableNamed(string $name): bool
73-
{
74-
return $this->tableName === $name;
75-
}
76-
77-
public function getComputedPropertiesStatus(): bool
78-
{
79-
return $this->useComputedProperties ?? false;
80-
}
8128
}

0 commit comments

Comments
 (0)