{# One @edit_action / @list_action control. A GET action is a link; a POST action is a small form carrying the CSRF token, so it can change state safely and ask for confirmation first (up-confirm, only allowed on POST actions). Both look the same. variant: "button" (labelled .fcu-btn), "icon" (bare header icon), "row" (list row icon) or "menu" (an entry in a row's "…" menu, icon and label). target: Unpoly target. A GET link is only followed by Unpoly when one is given; a POST form falls back to #main-content. history: carry up-history="true" (RCRUD tab content). next: optional return URL, posted as "next" for the handler. #} {% macro action_control(construct, action, href, variant="button", target=none, history=false, next=none) %} {%- set label = construct.t(action.name) -%} {%- set cls = {"button": "fcu-btn", "icon": "fcu-icon-action", "row": "fcu-row-action", "menu": "fcu-row-menu__item"}[variant] -%} {%- if action.method == "post" -%} {# nosemgrep: django-no-csrf-token — Flask app; CSRF is handled by {{ csrf_field() }} below, not Django's {% csrf_token %}. #}
{{ csrf_field() }} {%- if next %} {%- endif %}
{%- else -%} {{- _action_content(action, label, variant) -}} {%- endif -%} {% endmacro %} {% macro _action_content(action, label, variant) -%} {%- if variant == "button" -%} {{ icon(action.icon, size="sm") }} {{ label }} {%- elif variant == "menu" -%} {{ icon(action.icon, size="sm") }}{{ label }} {%- else -%} {{ icon(action.icon, label=label) }} {%- endif -%} {%- endmacro %} {#- A list row's actions (list_row_actions, list_rows.py). The caller renders the row's entries for the variant it is handed — "row" (the icons, side by side, as always) or "menu" (icon and label, behind one "…" toggle) — so each list keeps its own URLs: {% call(variant) row_actions(construct, rows.menu, record) %} {{ row_entry_content("edit", label, variant) }} {% endcall %} The menu is a native
: it opens and its entries work without JavaScript; core.js places it above the table, closes it on Escape, a click outside or a chosen entry, and keeps one open at a time. A row with no entries gets no toggle. record: the row's name, for the toggle's accessible name ("Actions for Lamp"), so a screen reader's list of buttons tells the rows apart. panel_return: the row is the one open in the side panel. Its Edit entry is hidden in the closed menu and cannot take the focus back when the panel closes, so the toggle does. #} {% macro row_actions(construct, menu=false, record=none, panel_return=false) -%} {%- if menu -%} {%- set entries = caller("menu") -%} {%- if entries | trim -%} {%- set label = construct.t("crud-row-actions-for", {"record": record | string}) if record not in (none, "") else construct.t("crud-row-actions") -%} {#- data-fcu-keep-open: a click on the menu is not a click outside the side panel, which would close it. #}
{{- icon("more", label=label) -}}
{{ entries }}
{%- endif -%} {%- else -%}
{{ caller("row") }}
{%- endif -%} {%- endmacro %} {#- The class of one of the row's own entries (edit, view, delete) for the variant row_actions handed its caller. #} {% macro row_entry_class(variant, danger=false) -%} {%- if variant == "menu" -%} fcu-row-menu__item{% if danger %} fcu-row-menu__item--danger{% endif %} {%- else -%} fcu-row-action{% if danger %} fcu-row-action--danger{% endif %} {%- endif -%} {%- endmacro %} {#- Its content: the bare icon with its label for screen readers on a row, icon and visible label in the menu. #} {% macro row_entry_content(icon_name, label, variant) -%} {%- if variant == "menu" -%} {{ icon(icon_name, size="sm") }}{{ label }} {%- else -%} {{ icon(icon_name, label=label) }} {%- endif -%} {%- endmacro %} {#- Its title: the tooltip that names a bare icon; the menu's entries carry their label as text. #} {% macro row_entry_title(label, variant) -%} {%- if variant != "menu" %} title="{{ label }}"{% endif -%} {%- endmacro %} {#- A list cell that opens its record (list_link_fields): the cell's rendered value inside one link. An empty value would leave nothing to click, so it shows a dash, named by the record for a screen reader. href is none when the row may be neither edited nor viewed: the value then stays plain. attrs: the link's Unpoly attributes, as markup. #} {% macro cell_link(content, href, attrs, record) -%} {%- if href -%} {%- if content | striptags | trim -%} {{ content }} {%- else -%} {{ record }} {%- endif -%} {%- else -%} {{ content }} {%- endif -%} {%- endmacro %} {#- Bulk actions on a CRUD list (@bulk_action, bulk.py). Without JavaScript: tick rows, press an action; the form posts the ticked ids, the CSRF token and "next". The select-all box in the header only works with JavaScript (core.js), so it is rendered hidden and shown by the script; the running count likewise. #} {% macro bulk_select_all(construct, stack=false) -%} {{ construct.t("crud-bulk-select") }} {%- endmacro %} {% macro bulk_select_row(construct, row, key, fields, stack=false) -%} {%- set first = fields[0] if fields else none -%} {%- set record = (first[1].format_value(first[0], row) if first else none) -%} {%- set record = record if record not in (none, "") else "#" ~ key -%} {%- endmacro %} {% macro bulk_bar(construct, view, actions, next) -%} {# nosemgrep: django-no-csrf-token — Flask app; CSRF is handled by {{ csrf_field() }} below, not Django's {% csrf_token %}. #}
{{ csrf_field() }} {{ construct.t("crud-bulk-with-selected") }} {%- for action in actions %} {%- endfor %}
{%- endmacro %}