From db67c6f5060a86e8a9b696e0b49ac6904d57db70 Mon Sep 17 00:00:00 2001 From: Owain Date: Wed, 7 Aug 2024 13:33:11 +0100 Subject: [PATCH] allow for closure for most methods --- README.md | 8 ++++---- src/Forms/Components/GazeBanner.php | 22 +++++++++------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 2eda79d..b927790 100644 --- a/README.md +++ b/README.md @@ -97,21 +97,21 @@ GazeBanner::make() #### Arguments `timeInSeconds` - (int) The amount of time in seconds between each poll. Default is 30 seconds. -### `->identifier($name)` +### `->identifier($fnc)` #### Description `identifier` is used as a unique identifier for this gaze banner. Any other gaze banners with the same identifier will share the same list of active users. This can be useful if you want 2 or more difference resources to share the same list of active viewing users. #### Arguments -`name` - (string) The name of the identifier. Default is the resource's model class combines with model Id. +`fnc` - (optional, closure | string) The name of the identifier. Default is the resource's model class combines with model Id. -### `->lock($state)` +### `->lock($fnc)` #### Description `lock` can be used to lock the resource for anyone but the current person editing the form. This can be useful if you want to prevent multiple people from editing the same resource at the same time. The controller is the first person to access the resource, or the person who has taken control of the resource. If you enable this after accessing a resource, you may need to run `php artisan cache:clear` as it is possible that the current cached viewers don't have a marked controller. #### Arguments -`state` - (optional, bool) If the resource is lockable or not. +`fnc` - (optional, closure | bool) If the resource is lockable or not. ### `->canTakeControl($fnc)` diff --git a/src/Forms/Components/GazeBanner.php b/src/Forms/Components/GazeBanner.php index 481b299..59e3396 100644 --- a/src/Forms/Components/GazeBanner.php +++ b/src/Forms/Components/GazeBanner.php @@ -4,12 +4,9 @@ use Carbon\Carbon; use Closure; -use Filament\Actions\Contracts\HasLivewire; use Filament\Facades\Filament; use Filament\Forms\Components\Component; -use Filament\Notifications\Notification; use Illuminate\Support\Facades\Cache; -use Livewire\Livewire; /** * Class GazeBanner @@ -46,7 +43,6 @@ class GazeBanner extends Component */ public bool $canTakeControl = false; - /** * Create a new instance of the GazeBanner component. */ @@ -64,9 +60,9 @@ public static function make(array | Closure $schema = []): static * @param string $identifier * @return $this */ - public function identifier($identifier) + public function identifier(string | Closure $fnc = true): static { - $this->identifier = $identifier; + $this->identifier = (string) $this->evaluate($fnc); return $this; } @@ -87,14 +83,14 @@ public function pollTimer($poll) /** * Set the lock state */ - public function lock($state = true): static + public function lock(bool | Closure $fnc = true): static { - $this->isLockable = $state; + $this->isLockable = (bool) $this->evaluate($fnc); - if ($state) { + if ($this->isLockable) { $this->registerListeners([ 'FilamentGaze::takeControl' => [ - function() { + function () { // Very hacky, maybe a better solution for this? $this->getLivewire()->mount($this->getLivewire()->getForm('form')->getRecord()?->id); $this->takeControl(); @@ -135,7 +131,7 @@ public function takeControl() public function getIdentifier() { - if (!$this->identifier) { + if (! $this->identifier) { $record = $this->getRecord(); if (! $record) { $this->identifier = (string) $this->getModel(); @@ -159,7 +155,7 @@ public function refreshViewers() { $this->registerListeners([ 'FilamentGaze::takeControl' => [ - function() { + function () { $this->refreshViewers(); }, ], @@ -249,7 +245,7 @@ public function render(): \Illuminate\Contracts\View\View $hasControl = isset($lockUser) && $lockUser['id'] == auth()->id(); if ($this->isLockable) { - $this->getLivewire()->getForm('form')->disabled(!$hasControl); + $this->getLivewire()->getForm('form')->disabled(! $hasControl); } return view('filament-gaze::forms.components.gaze-banner', [