From 9599b3a3ff19d2c2f94c3e1b4bba0135a31e895e Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Wed, 8 Jan 2025 03:29:00 +0000 Subject: [PATCH 1/2] Split ComponentUtilities into traits --- src/Traits/ComponentUtilities.php | 33 +++--------- .../Configuration/ComponentConfiguration.php | 48 ----------------- .../Component/HandlesComputedProperties.php | 27 ++++++++++ .../Core/Component/HandlesEmptyMessage.php | 32 ++++++++++++ .../Core/Component/HandlesFingerprint.php | 34 ++++++++++++ .../Component/HandlesOfflineIndicator.php | 44 ++++++++++++++++ .../Core/Component/HandlesTableName.php | 28 ++++++++++ src/Traits/Helpers/ComponentHelpers.php | 52 ------------------- 8 files changed, 173 insertions(+), 125 deletions(-) create mode 100644 src/Traits/Core/Component/HandlesComputedProperties.php create mode 100644 src/Traits/Core/Component/HandlesEmptyMessage.php create mode 100644 src/Traits/Core/Component/HandlesFingerprint.php create mode 100644 src/Traits/Core/Component/HandlesOfflineIndicator.php create mode 100644 src/Traits/Core/Component/HandlesTableName.php diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index d2121205f..18ce29c33 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -7,28 +7,22 @@ use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Traits\Configuration\ComponentConfiguration; use Rappasoft\LaravelLivewireTables\Traits\Helpers\ComponentHelpers; +use Rappasoft\LaravelLivewireTables\Traits\Core\Component\{HandlesComputedProperties,HandlesEmptyMessage, HandlesFingerprint, HandlesOfflineIndicator,HandlesTableName}; trait ComponentUtilities { - use ComponentConfiguration, + use HandlesTableName, + HandlesFingerprint, + HandlesEmptyMessage, + HandlesComputedProperties, + HandlesOfflineIndicator, + ComponentConfiguration, ComponentHelpers; - + public array $table = []; protected $model; - #[Locked] - public string $tableName = 'table'; - - #[Locked] - public ?string $dataTableFingerprint; - - protected bool $offlineIndicatorStatus = true; - - protected string $emptyMessage = 'No items found, try to broaden your search'; - - protected bool $useComputedProperties = true; - protected bool $hasRunConfigure = false; /** @@ -82,17 +76,6 @@ protected function runCoreConfiguration(): void } } - /** - * Returns a unique id for the table, used as an alias to identify one table from another session and query string to prevent conflicts - */ - protected function generateDataTableFingerprint(): string - { - $className = str_split(static::class); - $crc32 = sprintf('%u', crc32(serialize($className))); - - return base_convert($crc32, 10, 36); - } - /** * 1. After the sorting method is hit we need to tell the table to go back into reordering mode */ diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index fee44c87d..35bd142a6 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -4,55 +4,7 @@ trait ComponentConfiguration { - /** - * Set the empty message - */ - public function setEmptyMessage(string $message): self - { - $this->emptyMessage = $message; - return $this; - } - public function setOfflineIndicatorStatus(bool $status): self - { - $this->offlineIndicatorStatus = $status; - return $this; - } - - public function setOfflineIndicatorEnabled(): self - { - $this->setOfflineIndicatorStatus(true); - - return $this; - } - - public function setOfflineIndicatorDisabled(): self - { - $this->setOfflineIndicatorStatus(false); - - return $this; - } - - public function setDataTableFingerprint(string $dataTableFingerprint): self - { - $this->dataTableFingerprint = $dataTableFingerprint; - - return $this; - } - - public function useComputedPropertiesEnabled(): self - { - $this->useComputedProperties = true; - - return $this; - } - - public function useComputedPropertiesDisabled(): self - { - $this->useComputedProperties = false; - - return $this; - } } diff --git a/src/Traits/Core/Component/HandlesComputedProperties.php b/src/Traits/Core/Component/HandlesComputedProperties.php new file mode 100644 index 000000000..450cb4e95 --- /dev/null +++ b/src/Traits/Core/Component/HandlesComputedProperties.php @@ -0,0 +1,27 @@ +useComputedProperties = true; + + return $this; + } + + public function useComputedPropertiesDisabled(): self + { + $this->useComputedProperties = false; + + return $this; + } + + public function getComputedPropertiesStatus(): bool + { + return $this->useComputedProperties ?? false; + } +} \ No newline at end of file diff --git a/src/Traits/Core/Component/HandlesEmptyMessage.php b/src/Traits/Core/Component/HandlesEmptyMessage.php new file mode 100644 index 000000000..23c09b053 --- /dev/null +++ b/src/Traits/Core/Component/HandlesEmptyMessage.php @@ -0,0 +1,32 @@ +emptyMessage == 'No items found, try to broaden your search') { + return __($this->getLocalisationPath().'No items found, try to broaden your search'); + } + + return $this->emptyMessage; + } + + /** + * Set the empty message + */ + public function setEmptyMessage(string $message): self + { + $this->emptyMessage = $message; + + return $this; + } + +} diff --git a/src/Traits/Core/Component/HandlesFingerprint.php b/src/Traits/Core/Component/HandlesFingerprint.php new file mode 100644 index 000000000..87bd71dbc --- /dev/null +++ b/src/Traits/Core/Component/HandlesFingerprint.php @@ -0,0 +1,34 @@ +dataTableFingerprint ?? ($this->dataTableFingerprint = $this->generateDataTableFingerprint()); + } + + public function setDataTableFingerprint(string $dataTableFingerprint): self + { + $this->dataTableFingerprint = $dataTableFingerprint; + + return $this; + } + + /** + * Returns a unique id for the table, used as an alias to identify one table from another session and query string to prevent conflicts + */ + protected function generateDataTableFingerprint(): string + { + $className = str_split(static::class); + $crc32 = sprintf('%u', crc32(serialize($className))); + + return base_convert($crc32, 10, 36); + } +} \ No newline at end of file diff --git a/src/Traits/Core/Component/HandlesOfflineIndicator.php b/src/Traits/Core/Component/HandlesOfflineIndicator.php new file mode 100644 index 000000000..1db217b36 --- /dev/null +++ b/src/Traits/Core/Component/HandlesOfflineIndicator.php @@ -0,0 +1,44 @@ +offlineIndicatorStatus; + } + + public function offlineIndicatorIsEnabled(): bool + { + return $this->getOfflineIndicatorStatus() === true; + } + + public function offlineIndicatorIsDisabled(): bool + { + return $this->getOfflineIndicatorStatus() === false; + } + + public function setOfflineIndicatorStatus(bool $status): self + { + $this->offlineIndicatorStatus = $status; + + return $this; + } + + public function setOfflineIndicatorEnabled(): self + { + $this->setOfflineIndicatorStatus(true); + + return $this; + } + + public function setOfflineIndicatorDisabled(): self + { + $this->setOfflineIndicatorStatus(false); + + return $this; + } +} \ No newline at end of file diff --git a/src/Traits/Core/Component/HandlesTableName.php b/src/Traits/Core/Component/HandlesTableName.php new file mode 100644 index 000000000..f936d0f90 --- /dev/null +++ b/src/Traits/Core/Component/HandlesTableName.php @@ -0,0 +1,28 @@ +tableName = $name; + } + + #[Computed] + public function getTableName(): string + { + return $this->tableName; + } + + public function isTableNamed(string $name): bool + { + return $this->tableName === $name; + } + +} \ No newline at end of file diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index 06cf2ee16..f9587c372 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -7,11 +7,6 @@ trait ComponentHelpers { - public function getDataTableFingerprint(): string - { - return $this->dataTableFingerprint ?? ($this->dataTableFingerprint = $this->generateDataTableFingerprint()); - } - public function hasModel(): bool { return $this->model !== null; @@ -25,57 +20,10 @@ public function getModel() return $this->model; } - /** - * Get the translated empty message of the table - */ - public function getEmptyMessage(): string - { - if ($this->emptyMessage == 'No items found, try to broaden your search') { - return __($this->getLocalisationPath().'No items found, try to broaden your search'); - } - - return $this->emptyMessage; - } - - public function getOfflineIndicatorStatus(): bool - { - return $this->offlineIndicatorStatus; - } - - public function offlineIndicatorIsEnabled(): bool - { - return $this->getOfflineIndicatorStatus() === true; - } - - public function offlineIndicatorIsDisabled(): bool - { - return $this->getOfflineIndicatorStatus() === false; - } - - public function setTableName(string $name): string - { - return $this->tableName = $name; - } - - #[Computed] - public function getTableName(): string - { - return $this->tableName; - } - #[Computed] public function getTableId(): string { return $this->getTableAttributes()['id'] ?? 'table-'.$this->getTableName(); } - public function isTableNamed(string $name): bool - { - return $this->tableName === $name; - } - - public function getComputedPropertiesStatus(): bool - { - return $this->useComputedProperties ?? false; - } } From 27a43b51e6e16b31d1ffa022890d9e09136a8e12 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 8 Jan 2025 03:29:26 +0000 Subject: [PATCH 2/2] Fix styling --- src/Traits/ComponentUtilities.php | 4 +- .../Configuration/ComponentConfiguration.php | 7 +- .../Component/HandlesComputedProperties.php | 54 ++++++------ .../Core/Component/HandlesEmptyMessage.php | 62 +++++++------ .../Core/Component/HandlesFingerprint.php | 68 +++++++------- .../Component/HandlesOfflineIndicator.php | 88 +++++++++---------- .../Core/Component/HandlesTableName.php | 55 ++++++------ src/Traits/Helpers/ComponentHelpers.php | 1 - 8 files changed, 165 insertions(+), 174 deletions(-) diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index 18ce29c33..617266776 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -6,8 +6,8 @@ use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Traits\Configuration\ComponentConfiguration; -use Rappasoft\LaravelLivewireTables\Traits\Helpers\ComponentHelpers; use Rappasoft\LaravelLivewireTables\Traits\Core\Component\{HandlesComputedProperties,HandlesEmptyMessage, HandlesFingerprint, HandlesOfflineIndicator,HandlesTableName}; +use Rappasoft\LaravelLivewireTables\Traits\Helpers\ComponentHelpers; trait ComponentUtilities { @@ -18,7 +18,7 @@ trait ComponentUtilities HandlesOfflineIndicator, ComponentConfiguration, ComponentHelpers; - + public array $table = []; protected $model; diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index 35bd142a6..3587b03af 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -2,9 +2,4 @@ namespace Rappasoft\LaravelLivewireTables\Traits\Configuration; -trait ComponentConfiguration -{ - - - -} +trait ComponentConfiguration {} diff --git a/src/Traits/Core/Component/HandlesComputedProperties.php b/src/Traits/Core/Component/HandlesComputedProperties.php index 450cb4e95..d278ecae8 100644 --- a/src/Traits/Core/Component/HandlesComputedProperties.php +++ b/src/Traits/Core/Component/HandlesComputedProperties.php @@ -1,27 +1,27 @@ -useComputedProperties = true; - - return $this; - } - - public function useComputedPropertiesDisabled(): self - { - $this->useComputedProperties = false; - - return $this; - } - - public function getComputedPropertiesStatus(): bool - { - return $this->useComputedProperties ?? false; - } -} \ No newline at end of file +useComputedProperties = true; + + return $this; + } + + public function useComputedPropertiesDisabled(): self + { + $this->useComputedProperties = false; + + return $this; + } + + public function getComputedPropertiesStatus(): bool + { + return $this->useComputedProperties ?? false; + } +} diff --git a/src/Traits/Core/Component/HandlesEmptyMessage.php b/src/Traits/Core/Component/HandlesEmptyMessage.php index 23c09b053..972b7cb41 100644 --- a/src/Traits/Core/Component/HandlesEmptyMessage.php +++ b/src/Traits/Core/Component/HandlesEmptyMessage.php @@ -1,32 +1,30 @@ -emptyMessage == 'No items found, try to broaden your search') { - return __($this->getLocalisationPath().'No items found, try to broaden your search'); - } - - return $this->emptyMessage; - } - - /** - * Set the empty message - */ - public function setEmptyMessage(string $message): self - { - $this->emptyMessage = $message; - - return $this; - } - -} +emptyMessage == 'No items found, try to broaden your search') { + return __($this->getLocalisationPath().'No items found, try to broaden your search'); + } + + return $this->emptyMessage; + } + + /** + * Set the empty message + */ + public function setEmptyMessage(string $message): self + { + $this->emptyMessage = $message; + + return $this; + } +} diff --git a/src/Traits/Core/Component/HandlesFingerprint.php b/src/Traits/Core/Component/HandlesFingerprint.php index 87bd71dbc..b8ff168dd 100644 --- a/src/Traits/Core/Component/HandlesFingerprint.php +++ b/src/Traits/Core/Component/HandlesFingerprint.php @@ -1,34 +1,34 @@ -dataTableFingerprint ?? ($this->dataTableFingerprint = $this->generateDataTableFingerprint()); - } - - public function setDataTableFingerprint(string $dataTableFingerprint): self - { - $this->dataTableFingerprint = $dataTableFingerprint; - - return $this; - } - - /** - * Returns a unique id for the table, used as an alias to identify one table from another session and query string to prevent conflicts - */ - protected function generateDataTableFingerprint(): string - { - $className = str_split(static::class); - $crc32 = sprintf('%u', crc32(serialize($className))); - - return base_convert($crc32, 10, 36); - } -} \ No newline at end of file +dataTableFingerprint ?? ($this->dataTableFingerprint = $this->generateDataTableFingerprint()); + } + + public function setDataTableFingerprint(string $dataTableFingerprint): self + { + $this->dataTableFingerprint = $dataTableFingerprint; + + return $this; + } + + /** + * Returns a unique id for the table, used as an alias to identify one table from another session and query string to prevent conflicts + */ + protected function generateDataTableFingerprint(): string + { + $className = str_split(static::class); + $crc32 = sprintf('%u', crc32(serialize($className))); + + return base_convert($crc32, 10, 36); + } +} diff --git a/src/Traits/Core/Component/HandlesOfflineIndicator.php b/src/Traits/Core/Component/HandlesOfflineIndicator.php index 1db217b36..47104cb58 100644 --- a/src/Traits/Core/Component/HandlesOfflineIndicator.php +++ b/src/Traits/Core/Component/HandlesOfflineIndicator.php @@ -1,44 +1,44 @@ -offlineIndicatorStatus; - } - - public function offlineIndicatorIsEnabled(): bool - { - return $this->getOfflineIndicatorStatus() === true; - } - - public function offlineIndicatorIsDisabled(): bool - { - return $this->getOfflineIndicatorStatus() === false; - } - - public function setOfflineIndicatorStatus(bool $status): self - { - $this->offlineIndicatorStatus = $status; - - return $this; - } - - public function setOfflineIndicatorEnabled(): self - { - $this->setOfflineIndicatorStatus(true); - - return $this; - } - - public function setOfflineIndicatorDisabled(): self - { - $this->setOfflineIndicatorStatus(false); - - return $this; - } -} \ No newline at end of file +offlineIndicatorStatus; + } + + public function offlineIndicatorIsEnabled(): bool + { + return $this->getOfflineIndicatorStatus() === true; + } + + public function offlineIndicatorIsDisabled(): bool + { + return $this->getOfflineIndicatorStatus() === false; + } + + public function setOfflineIndicatorStatus(bool $status): self + { + $this->offlineIndicatorStatus = $status; + + return $this; + } + + public function setOfflineIndicatorEnabled(): self + { + $this->setOfflineIndicatorStatus(true); + + return $this; + } + + public function setOfflineIndicatorDisabled(): self + { + $this->setOfflineIndicatorStatus(false); + + return $this; + } +} diff --git a/src/Traits/Core/Component/HandlesTableName.php b/src/Traits/Core/Component/HandlesTableName.php index f936d0f90..ce9c4a70d 100644 --- a/src/Traits/Core/Component/HandlesTableName.php +++ b/src/Traits/Core/Component/HandlesTableName.php @@ -1,28 +1,27 @@ -tableName = $name; - } - - #[Computed] - public function getTableName(): string - { - return $this->tableName; - } - - public function isTableNamed(string $name): bool - { - return $this->tableName === $name; - } - -} \ No newline at end of file +tableName = $name; + } + + #[Computed] + public function getTableName(): string + { + return $this->tableName; + } + + public function isTableNamed(string $name): bool + { + return $this->tableName === $name; + } +} diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index f9587c372..88cd1906f 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -25,5 +25,4 @@ public function getTableId(): string { return $this->getTableAttributes()['id'] ?? 'table-'.$this->getTableName(); } - }