Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy computed properties behaviour #2160

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions src/Traits/ComponentUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
*/
Expand Down
55 changes: 1 addition & 54 deletions src/Traits/Configuration/ComponentConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
27 changes: 27 additions & 0 deletions src/Traits/Core/Component/HandlesComputedProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Core\Component;

trait HandlesComputedProperties
{
protected bool $useComputedProperties = true;

public function useComputedPropertiesEnabled(): self

Check warning on line 9 in src/Traits/Core/Component/HandlesComputedProperties.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Core/Component/HandlesComputedProperties.php#L9

Added line #L9 was not covered by tests
{
$this->useComputedProperties = true;

Check warning on line 11 in src/Traits/Core/Component/HandlesComputedProperties.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Core/Component/HandlesComputedProperties.php#L11

Added line #L11 was not covered by tests

return $this;

Check warning on line 13 in src/Traits/Core/Component/HandlesComputedProperties.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Core/Component/HandlesComputedProperties.php#L13

Added line #L13 was not covered by tests
}

public function useComputedPropertiesDisabled(): self

Check warning on line 16 in src/Traits/Core/Component/HandlesComputedProperties.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Core/Component/HandlesComputedProperties.php#L16

Added line #L16 was not covered by tests
{
$this->useComputedProperties = false;

Check warning on line 18 in src/Traits/Core/Component/HandlesComputedProperties.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Core/Component/HandlesComputedProperties.php#L18

Added line #L18 was not covered by tests

return $this;

Check warning on line 20 in src/Traits/Core/Component/HandlesComputedProperties.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Core/Component/HandlesComputedProperties.php#L20

Added line #L20 was not covered by tests
}

public function getComputedPropertiesStatus(): bool
{
return $this->useComputedProperties ?? false;
}
}
30 changes: 30 additions & 0 deletions src/Traits/Core/Component/HandlesEmptyMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Core\Component;

trait HandlesEmptyMessage
{
protected string $emptyMessage = 'No items found, try to broaden your search';

/**
* 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;
}

/**
* Set the empty message
*/
public function setEmptyMessage(string $message): self
{
$this->emptyMessage = $message;

return $this;
}
}
34 changes: 34 additions & 0 deletions src/Traits/Core/Component/HandlesFingerprint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Core\Component;

use Livewire\Attributes\Locked;

trait HandlesFingerprint
{
#[Locked]
public ?string $dataTableFingerprint;

public function getDataTableFingerprint(): string
{
return $this->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);
}
}
44 changes: 44 additions & 0 deletions src/Traits/Core/Component/HandlesOfflineIndicator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Core\Component;

trait HandlesOfflineIndicator
{
protected bool $offlineIndicatorStatus = true;

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 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;
}
}
27 changes: 27 additions & 0 deletions src/Traits/Core/Component/HandlesTableName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Core\Component;

use Livewire\Attributes\{Computed,Locked};

trait HandlesTableName
{
#[Locked]
public string $tableName = 'table';

public function setTableName(string $name): string
{
return $this->tableName = $name;
}

#[Computed]
public function getTableName(): string
{
return $this->tableName;
}

public function isTableNamed(string $name): bool
{
return $this->tableName === $name;
}
}
53 changes: 0 additions & 53 deletions src/Traits/Helpers/ComponentHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Loading