diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index d2121205f..617266776 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -6,29 +6,23 @@ use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Traits\Configuration\ComponentConfiguration; +use Rappasoft\LaravelLivewireTables\Traits\Core\Component\{HandlesComputedProperties,HandlesEmptyMessage, HandlesFingerprint, HandlesOfflineIndicator,HandlesTableName}; use Rappasoft\LaravelLivewireTables\Traits\Helpers\ComponentHelpers; 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..3587b03af 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -2,57 +2,4 @@ namespace Rappasoft\LaravelLivewireTables\Traits\Configuration; -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; - } -} +trait ComponentConfiguration {} diff --git a/src/Traits/Core/Component/HandlesComputedProperties.php b/src/Traits/Core/Component/HandlesComputedProperties.php new file mode 100644 index 000000000..d278ecae8 --- /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; + } +} diff --git a/src/Traits/Core/Component/HandlesEmptyMessage.php b/src/Traits/Core/Component/HandlesEmptyMessage.php new file mode 100644 index 000000000..972b7cb41 --- /dev/null +++ b/src/Traits/Core/Component/HandlesEmptyMessage.php @@ -0,0 +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; + } +} diff --git a/src/Traits/Core/Component/HandlesFingerprint.php b/src/Traits/Core/Component/HandlesFingerprint.php new file mode 100644 index 000000000..b8ff168dd --- /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); + } +} diff --git a/src/Traits/Core/Component/HandlesOfflineIndicator.php b/src/Traits/Core/Component/HandlesOfflineIndicator.php new file mode 100644 index 000000000..47104cb58 --- /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; + } +} diff --git a/src/Traits/Core/Component/HandlesTableName.php b/src/Traits/Core/Component/HandlesTableName.php new file mode 100644 index 000000000..ce9c4a70d --- /dev/null +++ b/src/Traits/Core/Component/HandlesTableName.php @@ -0,0 +1,27 @@ +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 06cf2ee16..88cd1906f 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,9 @@ 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; - } }