36 lines
924 B
Python
36 lines
924 B
Python
"""Small HTMX helpers shared by admin pages."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from fastapi import Request
|
|
|
|
from app.admin_ui.components import esc
|
|
|
|
|
|
def is_htmx_request(request: Request | None) -> bool:
|
|
if request is None:
|
|
return False
|
|
return str(request.headers.get("HX-Request") or "").lower() == "true"
|
|
|
|
|
|
def htmx_attrs(
|
|
url: str,
|
|
*,
|
|
target: str,
|
|
swap: str = "innerHTML",
|
|
push_url: str | bool | None = None,
|
|
indicator: str | None = None,
|
|
) -> str:
|
|
attrs = [
|
|
f'hx-get="{esc(url)}"',
|
|
f'hx-target="{esc(target)}"',
|
|
f'hx-swap="{esc(swap)}"',
|
|
]
|
|
if push_url is not None:
|
|
value = "true" if push_url is True else "false" if push_url is False else str(push_url)
|
|
attrs.append(f'hx-push-url="{esc(value)}"')
|
|
if indicator:
|
|
attrs.append(f'hx-indicator="{esc(indicator)}"')
|
|
return " ".join(attrs)
|