Skip to content

Commit

Permalink
Add Rule EditOnClick and Row Enable/Disable EditOnClick
Browse files Browse the repository at this point in the history
  • Loading branch information
dansysanalyst committed May 19, 2024
1 parent d281750 commit 38443fd
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
38 changes: 37 additions & 1 deletion resources/views/components/row.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,44 @@
$rowRules = $actionRulesClass->recoverFromAction($row, RuleManager::TYPE_ROWS);
$hasFieldRules = $actionRulesClass->recoverActionForField($row, $field);
@endphp

{{-- =============* Edit On Click *===================== --}}
@php
$showEditOnClick = false;
if (data_get($column->editable, 'hasPermission')) {
$showEditOnClick = true;
}
// Check if there is any Role Row for Edit on click
$editOnClickRowRules = collect(data_get($rowRules, 'EditOnClickVisibility', []));
if ($editOnClickRowRules) {
// Has permission, but Row Action Rule is changing to hide
if ($showEditOnClick && $editOnClickRowRules->last() == 'hide')
{
$showEditOnClick = false;
}
// No permission, but Row Action Rule is forcing to show
if (!$showEditOnClick && $editOnClickRowRules->last() == 'show')
{
$showEditOnClick = true;
}
}
@if (data_get($column->editable, 'hasPermission') && !str_contains($field, '.'))
// Particular Rule for this field
if (isset($hasFieldRules['field_hide_editonclick'])) {
$showEditOnClick = !$hasFieldRules['field_hide_editonclick'];
}
if (str_contains($field, '.') === true) {
$showEditOnClick = false;
}
@endphp

@if($showEditOnClick === true)
<span @class([$contentClassField, $contentClass])>
@include(data_get($theme, 'editable.view') ?? null, ['editable' => $column->editable])
</span>
Expand Down
33 changes: 33 additions & 0 deletions src/Components/Rules/RuleEditOnClick.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace PowerComponents\LivewirePowerGrid\Components\Rules;

class RuleEditOnClick extends BaseRule
{
public string $forAction = RuleManager::TYPE_EDIT_ON_CLICK;

public function __construct(public string|null $column)
{
$this->forAction = $column;
}

/**
* Disable the Edit On Click.
*/
public function disable(): self
{
$this->setModifier('field_hide_editonclick', true);

return $this;
}

/**
* Enable the Edit On Click.
*/
public function enable(): self
{
$this->setModifier('field_hide_editonclick', false);

return $this;
}
}
10 changes: 9 additions & 1 deletion src/Components/Rules/RuleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class RuleManager

public const TYPE_TOGGLEABLE = 'pg:toggleable';

public const TYPE_EDIT_ON_CLICK = 'pg:editOnClick';

public const TYPE_CHECKBOX = 'pg:checkbox';

public const TYPE_RADIO = 'pg:radio';
Expand All @@ -18,9 +20,10 @@ class RuleManager

public static function applicableModifiers(): array
{
return ['bladeComponent', 'detailView', 'disable', 'dispatch', 'dispatchTo', 'emit', 'hide', 'loop', 'redirect', 'rowClasses', 'setAttribute', 'slot', 'ToggleableVisibility', 'DetailButtonVisibility', 'field_hide_toggleable'];
return ['bladeComponent', 'detailView', 'disable', 'dispatch', 'dispatchTo', 'emit', 'hide', 'loop', 'redirect', 'rowClasses', 'setAttribute', 'slot', 'ToggleableVisibility', 'DetailButtonVisibility', 'EditOnClickVisibility', 'field_hide_editonclick', 'field_hide_toggleable'];
}

//@TODO DetailButtonVisibility
public function button(string $button): RuleActions
{
return new RuleActions($button);
Expand All @@ -36,6 +39,11 @@ public function toggleable(string $column): RuleToggleable
return new RuleToggleable($column);
}

public function editOnClick(string $column): RuleEditOnClick
{
return new RuleEditOnClick($column);
}

public function checkbox(): RuleCheckbox
{
return new RuleCheckbox();
Expand Down
20 changes: 20 additions & 0 deletions src/Components/Rules/RuleRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ public function hideToggleable(): self
return $this;
}

/**
* Enable all edit on click in current row.
*/
public function enableEditOnClick(): self
{
$this->setModifier('EditOnClickVisibility', 'show');

return $this;
}

/**
* Disable all edit on click in current row.
*/
public function disableEditOnClick(): self
{
$this->setModifier('EditOnClickVisibility', 'hide');

return $this;
}

/**
* Show the Detail button in current row.
*/
Expand Down

0 comments on commit 38443fd

Please sign in to comment.