Skip to content

Commit

Permalink
Finalising the overhaul of the actions
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-helies committed Aug 2, 2024
1 parent 1a7901a commit 3c054a3
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 291 deletions.
6 changes: 3 additions & 3 deletions src/Crud/AbstractCrudConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,23 @@ public function getShowActions(): array
$this->getPath(CrudConfigInterface::INDEX),
Icon::new('list')
)
->setCssClass('btn btn-secondary btn-sm me-1')
->setCssClass('btn btn-secondary btn-sm ms-1')
->setRole(sprintf('ROLE_%s_%s', $this->getName(), CrudConfigInterface::INDEX));

$actions[CrudConfigInterface::ACTION_EDIT] = EditAction::new(
'action.edit',
$this->getPath(CrudConfigInterface::EDIT),
Icon::new('edit')
)
->setCssClass('btn btn-secondary btn-sm me-1')
->setCssClass('btn btn-secondary btn-sm ms-1')
->setRole(sprintf('ROLE_%s_%s', $this->getName(), CrudConfigInterface::EDIT));

$actions[CrudConfigInterface::ACTION_DELETE] = DeleteAction::new(
'action.delete',
$this->getPath(CrudConfigInterface::DELETE),
Icon::new('trash-alt')
)
->setCssClass('btn btn-danger btn-sm me-1')
->setCssClass('btn btn-danger btn-sm ms-1')
->setConfirmModal(true)
->setRole(sprintf('ROLE_%s_%s', $this->getName(), CrudConfigInterface::DELETE));

Expand Down
8 changes: 5 additions & 3 deletions src/Dto/Action/DropdownAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
namespace Lle\CruditBundle\Dto\Action;

use Lle\CruditBundle\Contracts\ActionInterface;
use Lle\CruditBundle\Dto\Icon;
use Lle\CruditBundle\Dto\Path;

class DropdownAction extends AbstractAction
{
protected array $actions;

/**
* @param array|AbstractAction[] $actions
* @param array<AbstractAction[]> $actions
*/
public static function new(string $label, array $actions): static
public static function new(string $label, array $actions, ?Icon $icon = null): static
{
return (new static($label))

Check failure on line 18 in src/Dto/Action/DropdownAction.php

View workflow job for this annotation

GitHub Actions / PHPStan

Unsafe usage of new static().
->setActions($actions);
->setActions($actions)
->setIcon($icon);
}

public function __construct(string $label)
Expand Down
53 changes: 53 additions & 0 deletions src/Resources/views/brick/links/actions/_actions.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{% set params = action.path.params %}
{% if view.data.resource and action.setResource is defined%}
{% do action.setResource(view.data.resource) %}
{% endif %}

{% set id = app.request.attributes.get('id') %}
{% if id %}
{% set params = action.path.params|merge({id: id}) %}
{% endif %}

{% if action.modal %}
{% include action.modal with {action: action, id: 'modal_' ~ action.id, params: params} %}
{% endif %}

{% set disabled = action.disabled or not
((action.path.role is null or is_granted(action.path.role, view.data.resource))
and (action.role is null or is_granted(action.role, view.data.resource)))
%}

{% if not (disabled and crudit_hide_if_disabled(action)) %}
{% block actions_item %}
<span class="btn-wrapper" title="{{ action.title|trans(domain=view.config.translation_domain) }}">
<a
class="
{{ action.cssClass ?? 'btn btn-sm btn-primary mt-2 ms-1 mt-md-0' }}
{% if disabled %} disabled{% endif %}
"
{% if action.modal %}
data-bs-toggle="modal"
data-bs-target="#modal_{{ action.id }}"
{% elseif action.confirmModal %}
data-bs-toggle="modal"
data-bs-target="#modal-confirm"
data-confirm-link="{{ path(action.path.route, params) }}"
data-confirm-text="{{ ('modal.confirm.' ~ action.label)|trans }}"
{% elseif not action.disabled %}
href="{{ action.url ?? path(action.path.route, params) }}"
{% endif %}

{% if action.target is defined and action.target %}
target="{{ action.target }}"
{% endif %}
>
{% if action.icon %}
<i class="{{ action.icon.cssClass }}"></i>
{% endif %}
{% if not action.hideLabel %}
{{ action.label|trans(domain=view.config.translation_domain) }}
{% endif %}
</a>
</span>
{% endblock %}
{% endif %}
128 changes: 76 additions & 52 deletions src/Resources/views/brick/links/actions/_dropdown_actions.html.twig
Original file line number Diff line number Diff line change
@@ -1,58 +1,82 @@
<div class="dropdown">
<button
id="dropdown-actions-{{ action.label|replace({'.': '-'}) }}"
class="btn btn-primary dropdown-toggle py-1"
data-bs-toggle="dropdown"
>
{{ dropdown_action.label|trans(domain=view.config.translation_domain) }}
</button>
<ul
class="dropdown-menu dropdown-menu-end"
aria-labelledby="#dropdown-actions-{{ dropdown_action.label|replace({'.': '-'}) }}"
>
{% for group_name, group in dropdown_action.actions %}
{% if crudit_is_dropdown_group_name(group_name) %}
<li><h6 class="dropdown-header">{{ group_name|trans(domain=view.config.translation_domain) }}</h6></li>
{% elseif loop.index0 != 0 and not crudit_is_dropdown_group_name(group_name) %}
<li><hr class="dropdown-divider"></li>
{% endif %}
{% set disabled =
dropdown_action.disabled
or not(dropdown_action.role is null or is_granted(dropdown_action.role, view.data.resource))
%}

{% for action in group %}
{% set params = action.path.params %}
{% if view.data.resource and action.setResource is defined%}
{% do action.setResource(view.data.resource) %}
{% if not (disabled and crudit_hide_if_disabled(dropdown_action)) %}
<div class="dropdown">
{% block dropdown_actions_button %}
<button
{# Replace . & _ in label to have only - #}
id="dropdown-actions-{{ action.label|replace({'.': '-', '_': '-'}) }}"
class="
{{ dropdown_action.cssClass ?? 'btn btn-sm btn-primary dropdown-toggle mt-2 ms-1 mt-md-0' }}
{% if disabled %} disabled{% endif %}
"
data-bs-toggle="dropdown"
>
{% if dropdown_action.icon %}
<i class="{{ dropdown_action.icon.cssClass }}"></i>
{% endif %}
{{ dropdown_action.label|trans(domain=view.config.translation_domain) }}
</button>
{% endblock %}
{% block dropdown_actions_menu %}
<ul
class="dropdown-menu dropdown-menu-end"
aria-labelledby="#dropdown-actions-{{ dropdown_action.label|replace({'.': '-'}) }}"
>
{% for group_name, group in dropdown_action.actions %}
{% if crudit_is_dropdown_group_name(group_name) %}
<li><h6 class="dropdown-header">{{ group_name|trans(domain=view.config.translation_domain) }}</h6></li>
{% elseif loop.index0 != 0 and not crudit_is_dropdown_group_name(group_name) %}
<li><hr class="dropdown-divider"></li>
{% endif %}

{% set id = app.request.attributes.get('id') %}
{% if id %}
{% set params = action.path.params|merge({id: id}) %}
{% endif %}
{% for action in group %}
{% set params = action.path.params %}
{% if view.data.resource and action.setResource is defined%}
{% do action.setResource(view.data.resource) %}
{% endif %}

{% if action.modal %}
{% include action.modal with {action: action, id: 'modal_' ~ action.id, params: params} %}
{% endif %}
{% set id = app.request.attributes.get('id') %}
{% if id %}
{% set params = action.path.params|merge({id: id}) %}
{% endif %}

{% set disabled = action.disabled or not
((action.path.role is null or is_granted(action.path.role, view.data.resource))
and (action.role is null or is_granted(action.role, view.data.resource)))
%}
{% if action.modal %}
{% include action.modal with {action: action, id: 'modal_' ~ action.id, params: params} %}
{% endif %}

{% if not (disabled and crudit_hide_if_disabled(action)) %}
<li>
<a
class="{{ action.cssClass ?? 'dropdown-item' }} {% if disabled %}disabled{% endif %}"
{% if action.modal %}
data-bs-toggle="modal" data-bs-target="#modal_{{ action.id }}"
{% elseif not action.disabled %}
href="{{ action.url ?? path(action.path.route, params) }}"
{% endif %}
>
{% if action.icon %}<i class="{{ action.icon.cssClass }}"></i>{% endif %}
{% if not action.hideLabel %}{{ action.label|trans(domain=view.config.translation_domain) }}{% endif %}
</a>
</li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
</div>
{% set disabled = action.disabled or not
((action.path.role is null or is_granted(action.path.role, view.data.resource))
and (action.role is null or is_granted(action.role, view.data.resource)))
%}

{% if not (disabled and crudit_hide_if_disabled(action)) %}
{% block dropdown_actions_item %}
<li>
<a
class="{{ action.cssClass ?? 'dropdown-item' }} {% if disabled %}disabled{% endif %}"
{% if action.modal %}
data-bs-toggle="modal" data-bs-target="#modal_{{ action.id }}"
{% elseif not action.disabled %}
href="{{ action.url ?? path(action.path.route, params) }}"
{% endif %}
>
{% if action.icon %}
<i class="{{ action.icon.cssClass }}"></i>
{% endif %}
{% if not action.hideLabel %}
{{ action.label|trans(domain=view.config.translation_domain) }}
{% endif %}
</a>
</li>
{% endblock %}
{% endif %}
{% endfor %}
{% endfor %}
</ul>
{% endblock %}
</div>
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<div class="dropdown dropdown--no-arrow">
{% block dropdown_actions_button %}
<button id="actions_{{ view.id }}" class="btn dropdown-toggle py-1" data-bs-toggle="dropdown">
{% block kebab_dropdown_actions_button %}
<button id="actions_{{ view.id }}" class="btn btn-sm dropdown-toggle mt-2 ms-1 mt-md-0" data-bs-toggle="dropdown">
<i class="fas fa-ellipsis-v"></i>
</button>
{% endblock %}
{% block dropdown_actions_menu %}
{% block kebab_dropdown_actions_menu %}
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="#actions_{{ view.id }}">
{% for action in view.data.actions %}
{% if action.dropdown %}
{% set params = action.path.params %}
{% if view.data.resource and action.setResource is defined%}
{% do action.setResource(view.data.resource) %}
{% endif %}

{% set id = app.request.attributes.get('id') %}
{% if id %}
{% set params = action.path.params|merge({id: id}) %}
Expand All @@ -20,27 +21,32 @@
{% if action.modal %}
{% include action.modal with {action: action, id: 'modal_' ~ action.id, params: params} %}
{% endif %}

{% set disabled = action.disabled or not
((action.path.role is null or is_granted(action.path.role, view.data.resource))
and (action.role is null or is_granted(action.role, view.data.resource)))
%}

{% block dropdown_actions_item %}
{% if not (disabled and crudit_hide_if_disabled(action)) %}
{% if not (disabled and crudit_hide_if_disabled(action)) %}
{% block kebab_dropdown_actions_item %}
<li>
<a class="dropdown-item {% if disabled %}disabled{% endif %}"
<a class="{{ action.cssClass ?? 'dropdown-item' }} {% if disabled %}disabled{% endif %}"
{% if action.modal %}
data-bs-toggle="modal" data-bs-target="#modal_{{ action.id }}"
{% elseif not action.disabled %}
href="{{ action.url ?? path(action.path.route, params) }}"
{% endif %}
>
{% if action.icon %}<i class="{{ action.icon.cssClass }}"></i>{% endif %}
{% if not action.hideLabel %}{{ action.label|trans(domain=view.config.translation_domain) }}{% endif %}
{% if action.icon %}
<i class="{{ action.icon.cssClass }}"></i>
{% endif %}
{% if not action.hideLabel %}
{{ action.label|trans(domain=view.config.translation_domain) }}
{% endif %}
</a>
</li>
{% endif %}
{% endblock %}
{% endblock %}
{% endif %}
{% endif %}
{% endfor %}
</ul>
Expand Down
69 changes: 1 addition & 68 deletions src/Resources/views/brick/links/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
{% block dropdown_actions %}
{{ include('@LleCrudit/brick/links/actions/_dropdown_actions.html.twig', {dropdown_action: action, view: view}) }}
{% endblock %}
{% endif %}

{% if not action.dropdown %}
{% elseif not action.dropdown %}
{% block actions %}
{{ include('@LleCrudit/brick/links/actions/_actions.html.twig', {action: action, view: view}) }}
{% endblock %}
Expand All @@ -27,70 +25,5 @@
{% endblock %}
{% endif %}
{% endblock %}














{# {% set hasDropdown = false %}#}
{# {% for action in view.data.actions %}#}
{# {% if not action.dropdown %}#}
{# {% set params = action.path.params %}#}
{# {% if view.data.resource and action.setResource is defined%}#}
{# {% do action.setResource(view.data.resource) %}#}
{# {% endif %}#}
{# {% set id = app.request.attributes.get('id') %}#}
{# {% if id %}#}
{# {% set params = action.path.params|merge({id: id}) %}#}
{# {% endif %}#}

{# {% if action.modal %}#}
{# {% include action.modal with {action: action, id: 'modal_' ~ action.id, params: params} %}#}
{# {% endif %}#}
{# {% set disabled = action.disabled or not#}
{# ((action.path.role is null or is_granted(action.path.role, view.data.resource))#}
{# and (action.role is null or is_granted(action.role, view.data.resource)))#}
{# %}#}

{# {% if not (disabled and crudit_hide_if_disabled(action)) %}#}
{# <span class="btn-wrapper" title="{{ action.title|trans(domain=view.config.translation_domain) }}">#}
{# <a#}
{# class="{{ action.cssClass ?? 'btn btn-sm btn-primary mt-2 ms-1 mt-md-0' }} {% if disabled %}disabled{% endif %}"#}
{# {% if action.modal %}#}
{# data-bs-toggle="modal" data-bs-target="#modal_{{ action.id }}"#}
{# {% elseif action.confirmModal %}#}
{# data-bs-toggle="modal" data-bs-target="#modal-confirm" data-confirm-link="{{ path(action.path.route, params) }}" data-confirm-text="{{ ('modal.confirm.' ~ action.label)|trans }}"#}
{# {% elseif not action.disabled %}#}
{# href="{{ action.url ?? path(action.path.route, params) }}"#}
{# {% endif %}#}

{# {% if action.target is defined and action.target %}#}
{# target="{{ action.target }}"#}
{# {% endif %}#}
{# >#}
{# {% if action.icon %}<i class="{{ action.icon.cssClass }}"></i>{% endif %}#}
{# {% if not action.hideLabel %}{{ action.label|trans(domain=view.config.translation_domain) }}{% endif %}#}
{# </a>#}
{# </span>#}
{# {% endif %}#}
{# {% else %}#}
{# {% set hasDropdown = true %}#}
{# {% endif %}#}
{# {% endfor %}#}

{# {% block dropdown_actions %}#}
{# {% if hasDropdown %}#}
{# {{ include('@LleCrudit/brick/links/_dropdown_actions.html.twig', {view: view}) }}#}
{# {% endif %}#}
{# {% endblock %}#}
</div>
</nav>
Loading

0 comments on commit 3c054a3

Please sign in to comment.