canaille-globuzma/canaille/templates/macro/form.html

208 lines
7 KiB
HTML
Raw Normal View History

2023-06-19 16:10:14 +00:00
{% macro render_input(
2021-12-20 22:57:27 +00:00
field,
label_visible=true,
icon=none,
container=true,
noindicator=false,
indicator_icon=none,
indicator_text=none,
2023-06-22 09:39:50 +00:00
display=true,
add_button=false,
del_button=false
2021-12-20 22:57:27 +00:00
) -%}
2021-12-23 18:21:29 +00:00
{% set field_visible = field.type != 'HiddenField' and field.type !='CSRFTokenField' %}
2023-06-28 11:26:15 +00:00
{% set disabled = kwargs.get("disabled") or (field.render_kw and "disabled" in field.render_kw) %}
{% set readonly = kwargs.get("readonly") or (field.render_kw and "readonly" in field.render_kw) %}
{% set required = "required" in field.flags %}
{% set lock_indicator = readonly or disabled %}
{% set corner_indicator = not noindicator and (indicator_icon or lock_indicator or required) %}
{% set inline_validation = field.validators and field.type not in ("FileField", "MultipleFileField") %}
{% if inline_validation %}
{% set ignore_me = kwargs.update({"hx-post": ""}) %}
{% set ignore_me = kwargs.update({"hx-indicator": "closest .input"}) %}
{% endif %}
2021-12-23 18:21:29 +00:00
{% if container and field_visible %}
2020-11-02 14:03:41 +00:00
<div class="field {{ kwargs.pop('class_', '') }}
2023-03-14 11:08:37 +00:00
{%- if field.errors %} error{% endif -%}
2023-06-28 11:26:15 +00:00
{%- if disabled %} disabled{% endif -%}"
2023-03-14 11:08:37 +00:00
{% if not display %}style="display: none"{% endif %}
2023-06-28 11:26:15 +00:00
{% if inline_validation %}hx-target="this" hx-swap="outerHTML"{% endif %}
2021-12-08 17:06:50 +00:00
>
2020-11-02 10:28:52 +00:00
{% endif %}
2021-12-23 18:21:29 +00:00
{% if field_visible and label_visible %}
2020-08-17 13:49:48 +00:00
{{ field.label() }}
{% endif %}
2021-12-23 18:21:29 +00:00
{% if field_visible %}
<div class="ui
2023-03-29 21:29:25 +00:00
{%- if corner_indicator %} corner labeled{% endif -%}
{%- if icon or field.description %} left icon{% endif -%}
{%- if field_visible and (add_button or del_button) %} action{% endif -%}
{%- if field.type not in ("BooleanField", "RadioField") %} input{% endif -%}
2023-03-14 11:08:37 +00:00
">
2021-12-23 18:21:29 +00:00
{% endif %}
{% if icon %}<i class="{{ icon }} icon"></i>{% endif %}
{% if field.description %}<i class="question circle link icon" title="{{ field.description }}"></i>{% endif %}
2021-12-23 18:21:29 +00:00
{% if field.type not in ("SelectField", "SelectMultipleField") %}
{{ field(**kwargs) }}
2023-06-28 11:26:15 +00:00
{% elif field.type == "SelectMultipleField" and readonly %}
2021-12-23 18:21:29 +00:00
{{ field(class_="ui fluid dropdown multiple read-only", **kwargs) }}
{% elif field.type == "SelectMultipleField" %}
2021-12-23 18:21:29 +00:00
{{ field(class_="ui fluid dropdown multiple", **kwargs) }}
2023-06-28 11:26:15 +00:00
{% elif field.type == "SelectField" and readonly %}
{{ field(class_="ui fluid dropdown read-only", **kwargs) }}
{% elif field.type == "SelectField" %}
{{ field(class_="ui fluid dropdown", **kwargs) }}
2021-12-23 18:21:29 +00:00
{% endif %}
2021-12-23 18:21:29 +00:00
{% if not noindicator %}
{% if indicator_icon %}
<div class="ui corner label"{% if indicator_text %} title="{{ indicator_text }}"{% endif %}>
<i class="{{ indicator_icon }} icon"></i>
</div>
{% elif lock_indicator %}
<div class="ui corner label" title="{{ _("This field is not editable") }}">
<i class="lock icon"></i>
</div>
2023-06-28 11:26:15 +00:00
{% elif required %}
2021-12-23 18:21:29 +00:00
<div class="ui corner label" title="{{ _("This field is required") }}">
<i class="asterisk icon"></i>
</div>
2021-12-20 22:57:27 +00:00
{% endif %}
2021-12-23 18:21:29 +00:00
{% endif %}
2023-06-22 09:39:50 +00:00
2021-12-23 18:21:29 +00:00
{% if field_visible %}
{% if del_button %}
2023-06-22 09:39:50 +00:00
<button
class="ui teal icon button"
title="{{ _("Remove this field") }}"
type="submit"
name="fieldlist_remove"
value="{{ field.name }}"
hx-post=""
{# Workaround for https://github.com/bigskysoftware/htmx/issues/1506 #}
hx-vals='{"fieldlist_remove": "{{ field.name }}"}'
hx-target="closest .fieldlist"
formnovalidate>
<i class="minus icon"></i>
</button>
{% endif %}
{% if add_button %}
2023-06-22 09:39:50 +00:00
<button
class="ui teal icon button"
title="{{ _("Add another field") }}"
type="submit"
name="fieldlist_add"
value="{{ field.name }}"
hx-post=""
{# Workaround for https://github.com/bigskysoftware/htmx/issues/1506 #}
hx-vals='{"fieldlist_add": "{{ field.name }}"}'
hx-target="closest .fieldlist"
formnovalidate>
<i class="plus icon"></i>
</button>
{% endif %}
2021-12-23 18:21:29 +00:00
</div>
{% endif %}
2021-12-20 22:57:27 +00:00
{% if field.errors %}
2020-11-01 11:46:32 +00:00
{% for error in field.errors %}
2021-12-20 22:57:27 +00:00
<div class="ui pointing red basic label">
<p>{{ error }}</p>
</div>
2020-11-01 11:46:32 +00:00
{% endfor %}
2021-12-20 22:57:27 +00:00
{% endif %}
2021-12-23 18:21:29 +00:00
{% if container and field_visible %}
2021-12-20 22:57:27 +00:00
</div>
2020-11-02 10:28:52 +00:00
{% endif %}
2020-08-17 13:49:48 +00:00
{%- endmacro %}
2020-10-31 16:53:33 +00:00
{% macro render_fields(form) %}
2023-03-28 21:50:18 +00:00
{% for field in form %}
2023-06-19 16:10:14 +00:00
{{ render_field(field) }}
2023-03-28 21:50:18 +00:00
{% endfor %}
2020-10-31 16:53:33 +00:00
{% endmacro %}
2023-06-22 09:39:50 +00:00
{% macro render_field(field, parent_list=none, parent_indice=none) -%}
{% if parent_list %}
{% set last = parent_indice >= parent_list.entries|len -1 %}
{% set ignore_me = kwargs.update({
"label_visible": false,
"add_button": (last and (not parent_list.max_entries or parent_indice < parent_list.max_entries)),
"del_button": (last and parent_list.min_entries and parent_indice >= parent_list.min_entries),
}) %}
{% endif %}
{% if field.type == "FieldList" %}
{{ render_list(field, **kwargs) }}
{% elif field.type == "BooleanField" %}
{{ render_checkbox(field, **kwargs) }}
2023-06-19 16:10:14 +00:00
{% else %}
{{ render_input(field, **kwargs) }}
2023-06-19 16:10:14 +00:00
{% endif %}
{%- endmacro %}
{% macro render_list(field, render_func=none) -%}
2023-06-22 09:39:50 +00:00
<div class="field fieldlist" id="{{ field.name }}">
{# Strangely enough, translations are not rendered when using field.label() #}
{{ field[0].label() }}
{% for subfield in field %}
2023-06-28 11:26:15 +00:00
{% set ignore_me = kwargs.update(**field.render_kw or {}) %}
{{ (render_func or render_field)(
2023-06-22 09:39:50 +00:00
subfield,
parent_list=field,
parent_indice=loop.index0,
**kwargs
) }}
{% endfor %}
</div>
{%- endmacro %}
{% macro render_checkbox(field, display=true) -%}
<div class="field"
{% if not display %}style="display: none"{% endif %}
>
2022-01-01 17:41:04 +00:00
<div class="ui toggle checkbox">
{{ field(**kwargs) }}
{{ field.label() }}
</div>
</div>
{%- endmacro %}
2020-08-17 13:49:48 +00:00
{% macro render_form(
2021-12-20 22:57:27 +00:00
form,
2021-12-23 18:21:29 +00:00
action_text=none,
2021-12-20 22:57:27 +00:00
class_='',
btn_class='ui right floated primary button',
action=none,
id=none,
csrf=true) -%}
2020-08-17 13:49:48 +00:00
<form method="POST"
2023-03-14 11:08:37 +00:00
id="{{ id or form.__class__.__name__|lower }}"
action="{% if action %}{{ action }}{% elif form.action is defined %}{{ form.action }}{% endif %}"
2023-03-14 11:08:37 +00:00
role="form"
enctype="multipart/form-data"
class="ui form {{ class_ }}"
2021-12-20 22:57:27 +00:00
>
{% if caller is defined %}
{% if csrf %}
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
{% endif %}
2021-12-23 18:21:29 +00:00
{{ caller() }}
{% else %}
{{ render_fields(form) }}
{% endif %}
2020-08-17 13:49:48 +00:00
{% if action_text %}
2023-03-17 15:03:36 +00:00
<div class="ui right aligned container">
<div class="ui stackable buttons">
<button type="submit" class="{{ btn_class }}">{{ action_text }}</button>
</div>
</div>
2020-08-17 13:49:48 +00:00
{% endif %}
</form>
{%- endmacro %}