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

[ENHANCEMENT/FIX] Fix hideToggleable, add Action Rules editOnClick(), toggleable(), unless()` condition and others... #1559

Merged
merged 11 commits into from
May 22, 2024
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Livewire PowerGrid creates modern, powerful and easy-to-customize data tables ba
With PowerGrid, you can effortlessly create advanced data tables one in a matter of minutes.

<p>
<a href="https://demo.livewire-powergrid.com" target="_blank"><img src="https://livewire-powergrid.com/screenshot.png" alt="PowerGrid Screenshot" width="805"></a>
<a href="https://demo.livewire-powergrid.com/examples/demo-dish" target="_blank"><img src="https://livewire-powergrid.com/screenshot.png" alt="PowerGrid Screenshot" width="805"></a>
</p>

`🚀` **Minimal Configuration**
Expand Down
3 changes: 2 additions & 1 deletion resources/views/components/checkbox-row.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@use('PowerComponents\LivewirePowerGrid\Components\Rules\RuleManager')
@php
$rulesValues = $actionRulesClass->recoverFromAction($row, 'pg:checkbox');
$rulesValues = $actionRulesClass->recoverFromAction($row, RuleManager::TYPE_CHECKBOX);

$inputAttributes = new \Illuminate\View\ComponentAttributeBag([
'class' => data_get($theme, 'checkbox.inputClass'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<td
class="{{ data_get($theme, 'tdBodyClass') }}"
style="{{ data_get($theme, 'tdBodyStyle') }}"
></td>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@php
$params = [
'id' => data_get($row, $primaryKey),
'isHidden' => !$showToggleable ? 'true' : 'false',
'isHidden' => !$showToggleable,
'tableName' => $tableName,
'field' => $column->field,
'toggle' => (int) $row->{$column->field},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<td
class="{{ data_get($theme, 'tdBodyClass') }}"
style="{{ data_get($theme, 'tdBodyStyle') }}"
></td>
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
@php
$showDefaultToggle = false;
if (str_contains($primaryKey, '.')) {
$showDefaultToggle = true;
}

$value = (int) $row->{$column->field};

$trueValue = $column->toggleable['default'][0];
$falseValue = $column->toggleable['default'][1];

$params = [
'id' => data_get($row, $primaryKey),
'isHidden' => !$showToggleable ? 'true' : 'false',
'isHidden' => !$showToggleable,
'tableName' => $tableName,
'field' => $column->field,
'toggle' => $value,
Expand All @@ -20,8 +15,8 @@
];
@endphp
<div x-data="pgToggleable(@js($params))">
@if ($column->toggleable['enabled'] && !$showDefaultToggle && $showToggleable === true)
<div class="flex">
<div class="flex flex-row justify-center">
@if ($showToggleable === true)
<div
:class="{
'relative rounded-full w-8 h-4 transition duration-200 ease-linear': true,
Expand All @@ -42,16 +37,14 @@ class="appearance-none opacity-0 w-full h-full active:outline-none focus:outline
x-on:click="save"
>
</div>
</div>
@else
<div class="flex flex-row justify-center">
<div @class([
'text-xs px-4 w-auto py-1 text-center rounded-md',
'bg-red-200 text-red-800' => $value === 0,
'bg-blue-200 text-blue-800' => $value === 1,
])>
{{ $value === 0 ? $falseValue : $trueValue }}
</div>
</div>
@endif
</div>
</div>
3 changes: 2 additions & 1 deletion resources/views/components/radio-row.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@use('PowerComponents\LivewirePowerGrid\Components\Rules\RuleManager')
@php
$rulesValues = $actionRulesClass->recoverFromAction($row, 'pg:radio');
$rulesValues = $actionRulesClass->recoverFromAction($row, RuleManager::TYPE_RADIO);

$inputAttributes = new \Illuminate\View\ComponentAttributeBag([
'class' => data_get($theme, 'radio.inputClass'),
Expand Down
99 changes: 93 additions & 6 deletions resources/views/components/row.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@use('PowerComponents\LivewirePowerGrid\Components\Rules\RuleManager')

@props([
'rowIndex' => 0,
'childIndex' => null
Expand All @@ -10,10 +12,32 @@
])

@php
$ruleDetailView = data_get($rulesValues, 'detailView');
// =============* Get Rules *=====================
$rowRules = $actionRulesClass->recoverFromAction($row, RuleManager::TYPE_ROWS);

// =============* Toggle Detail Rules *=====================
$showToggleDetail = data_get($setUp, 'detail.showCollapseIcon');

$toggleDetailVisibilityRowRules = collect(data_get($rowRules, 'ToggleDetailVisibility', []));

if ($toggleDetailVisibilityRowRules) {
// Has permission, but Row Action Rule is changing to hide
if ($showToggleDetail && $toggleDetailVisibilityRowRules->last() == 'hide')
{
$showToggleDetail = false;
}

// No permission, but Row Action Rule is forcing to show
if (!$showToggleDetail && $toggleDetailVisibilityRowRules->last() == 'show')
{
$showToggleDetail = true;
}
}

$toggleDetailView = powerGridThemeRoot() . ($showToggleDetail ? '.toggle-detail' : '.no-toggle-detail');
@endphp

@includeWhen(data_get($setUp, 'detail.showCollapseIcon'), powerGridThemeRoot() . '.toggle-detail', [
@includeWhen(data_get($setUp, 'detail.showCollapseIcon'), $toggleDetailView, [
'theme' => data_get($theme, 'table'),
'view' => data_get($setUp, 'detail.viewIcon') ?? null,
])
Expand Down Expand Up @@ -63,16 +87,79 @@
@endforeach
@endif
</div>
@php
// =============* Get Field Rules *=====================
$hasFieldRules = $actionRulesClass->recoverActionForField($row, $field);

// =============* Edit On Click *=====================

$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;
}

@if (data_get($column->editable, 'hasPermission') && !str_contains($field, '.'))
// No permission, but Row Action Rule is forcing to show
if (!$showEditOnClick && $editOnClickRowRules->last() == 'show')
{
$showEditOnClick = true;
}
}

// 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>

{{-- =============* Toggleable *===================== --}}
@elseif(count($column->toggleable) > 0)
@php
$rules = $actionRulesClass->recoverFromAction($row, 'pg:rows');
$toggleableRules = collect(data_get($rules, 'showHideToggleable', []));
$showToggleable = $toggleableRules->isEmpty() || $toggleableRules->last() == 'show';
//Default Toggle Permission
$showToggleable = data_get($column->toggleable, 'enabled', false);

$toggleableRowRules = collect(data_get($rowRules, 'ToggleableVisibility', []));

// Has permission, but Row Action Rule is changing to hide
if ($showToggleable && $toggleableRowRules->last() == 'hide')
{
$showToggleable = false;
}

// No permission, but Row Action Rule is forcing to show
if (!$showToggleable && $toggleableRowRules->last() == 'show')
{
$showToggleable = true;
}

// Particular Rule for this field
if (isset($hasFieldRules['field_hide_toggleable'])) {
$showToggleable = !$hasFieldRules['field_hide_toggleable'];
}

if (str_contains($field, '.') === true) {
$showToggleable = false;
}

@endphp
@include(data_get($theme, 'toggleable.view'), ['tableName' => $tableName])
@else
Expand Down
3 changes: 2 additions & 1 deletion resources/views/components/table.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@inject('actionRulesClass', 'PowerComponents\LivewirePowerGrid\Components\Rules\RulesController')
@use('PowerComponents\LivewirePowerGrid\Components\Rules\RuleManager')

<x-livewire-powergrid::table-base
:ready-to-load="$readyToLoad"
Expand Down Expand Up @@ -32,7 +33,7 @@

$class = data_get($theme, 'table.trBodyClass');

$rulesValues = $actionRulesClass->recoverFromAction($row, 'pg:rows');
$rulesValues = $actionRulesClass->recoverFromAction($row, RuleManager::TYPE_ROWS);

$applyRulesLoop = true;

Expand Down
4 changes: 3 additions & 1 deletion resources/views/components/table/detail.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@use('PowerComponents\LivewirePowerGrid\Components\Rules\RuleManager')

@if(data_get($setUp, 'detail.state.' . $rowId))
@php
$rulesValues = $actionRulesClass->recoverFromAction($row, 'pg:rows');
$rulesValues = $actionRulesClass->recoverFromAction($row, RuleManager::TYPE_ROWS);
@endphp

<td colspan="999">
Expand Down
3 changes: 2 additions & 1 deletion resources/views/livewire/lazy-child.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@use('PowerComponents\LivewirePowerGrid\Components\Rules\RuleManager')
@inject('actionRulesClass', 'PowerComponents\LivewirePowerGrid\Components\Rules\RulesController')

<tbody>
Expand All @@ -7,7 +8,7 @@

$class = data_get($theme, 'table.trBodyClass');

$rulesValues = $actionRulesClass->recoverFromAction($row, 'pg:rows');
$rulesValues = $actionRulesClass->recoverFromAction($row, RuleManager::TYPE_ROWS);

$applyRulesLoop = true;

Expand Down
4 changes: 2 additions & 2 deletions src/Components/Exports/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Components\Rules\RulesController;
use PowerComponents\LivewirePowerGrid\Components\Rules\{RuleManager, RulesController};

class Export
{
Expand Down Expand Up @@ -60,7 +60,7 @@ public function prepare(Collection $data, array $columns): array
$row = $row->withoutRelations()->toArray();
}

$rules = $actionRulesClass->recoverFromAction($row, 'pg:rows');
$rules = $actionRulesClass->recoverFromAction($row, RuleManager::TYPE_ROWS);
$isExportable = false;

if (filled($rules['hide']) || filled($rules['disable'])) {
Expand Down
85 changes: 85 additions & 0 deletions src/Components/Rules/BaseRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace PowerComponents\LivewirePowerGrid\Components\Rules;

use Closure;

use Livewire\Wireable;
use PowerComponents\LivewirePowerGrid\Contracts\ConditionalRule;

/**
* @codeCoverageIgnore
*/

class BaseRule implements Wireable, ConditionalRule
{
public array $rule = [];

public string $forAction = '';

public string $column = '';

private bool $hasCondition = false;

public function setCondition(string $condition, Closure $closure): self
{
if ($this->hasCondition === true) {
throw new \InvalidArgumentException('A rule must have only one condition.');
}

$this->hasCondition = true;

$this->rule[$condition] = $closure;

return $this;
}

public function isValidModifier(string $modifier): bool
{
return in_array($modifier, RuleManager::applicableModifiers());
}

public function setModifier(string $modifier, mixed $arguments): void
{
if ($this->isValidModifier($modifier) === false) {
throw new \InvalidArgumentException('Invalid Modifier for Row [' . $modifier . ']');
}

$this->rule[$modifier] = $arguments;
}

public function pushModifier(string $modifier, array $argument): void
{
if (isset($this->rule[$modifier]) && is_array($this->rule[$modifier])) {
array_push($this->rule[$modifier], $argument);

return;
}

$this->setModifier($modifier, [$argument]);
}

public function toLivewire(): array
{
return (array) $this;
}

public static function fromLivewire($value)
{
return $value;
}

public function when(Closure $closure): self
{
$this->setCondition('when', $closure);

return $this;
}

public function unless(Closure $closure): self
{
$this->setCondition('unless', $closure);

return $this;
}
}
Loading
Loading