✨ (base) Añade ListView y ListFilter para listados
- ListView: botones, mensajes, tabla y paginador en orden fijo; anuncia el número de resultados a los lectores de pantalla. - ListFilter: filtros en fila con "Filtrar" y "Limpiar", envío automático opcional y marcas de cambios sin aplicar. - pagetop-htmx: hx_pager::boost() y hx_filter::boost(); cancelan las peticiones solapadas y atenúan el listado mientras carga, con estilos sólo en las páginas con listado. - Usuarios y roles migrados al nuevo patrón.
This commit is contained in:
parent
0f71eba7ab
commit
d8493291e4
28 changed files with 1264 additions and 144 deletions
114
assets/js/list-filter.js
Normal file
114
assets/js/list-filter.js
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
// Señala en los filtros de listado (`ListFilter`) que se aplican con botón los campos cuyo valor ya
|
||||
// no coincide con el del filtro aplicado, para que el usuario sepa que el listado todavía no
|
||||
// corresponde a lo que muestra el formulario. Sólo pone y quita clases (`list-filter-changed` en el
|
||||
// contenedor de cada campo, `list-filter-pending` en el formulario) y el texto del aviso para
|
||||
// lectores de pantalla; el aspecto lo da cada tema. Sin dependencias.
|
||||
//
|
||||
// Actúa sólo en los formularios con el aviso `.list-filter-status`, que `ListFilter` no pinta
|
||||
// cuando los filtros se envían solos.
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
var FIELDS =
|
||||
'input:not([type="hidden"]):not([type="submit"])' +
|
||||
':not([type="button"]):not([type="reset"]), select, textarea';
|
||||
|
||||
// Opción que estaba elegida al aplicar el filtro. Un desplegable simple sin ninguna opción
|
||||
// marcada en el HTML muestra la primera.
|
||||
function wasSelected(select, option, index) {
|
||||
if (option.defaultSelected) {
|
||||
return true;
|
||||
}
|
||||
if (select.multiple) {
|
||||
return false;
|
||||
}
|
||||
for (var i = 0; i < select.options.length; i++) {
|
||||
if (select.options[i].defaultSelected) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return index === 0;
|
||||
}
|
||||
|
||||
// Indica si el campo tiene un valor distinto del que tenía al aplicar el filtro.
|
||||
function isChanged(field) {
|
||||
if (field.type === "checkbox" || field.type === "radio") {
|
||||
return field.checked !== field.defaultChecked;
|
||||
}
|
||||
if (field.tagName === "SELECT") {
|
||||
for (var i = 0; i < field.options.length; i++) {
|
||||
if (field.options[i].selected !== wasSelected(field, field.options[i], i)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return field.value !== field.defaultValue;
|
||||
}
|
||||
|
||||
// Toma los valores actuales como los del filtro aplicado. Hace falta cuando sólo se actualiza
|
||||
// el listado y el formulario no se vuelve a pintar.
|
||||
function commit(field) {
|
||||
if (field.type === "checkbox" || field.type === "radio") {
|
||||
field.defaultChecked = field.checked;
|
||||
} else if (field.tagName === "SELECT") {
|
||||
for (var i = 0; i < field.options.length; i++) {
|
||||
field.options[i].defaultSelected = field.options[i].selected;
|
||||
}
|
||||
} else {
|
||||
field.defaultValue = field.value;
|
||||
}
|
||||
}
|
||||
|
||||
function refresh(form) {
|
||||
var changed = [];
|
||||
form.querySelectorAll(FIELDS).forEach(function (field) {
|
||||
var container = field.closest(".form-field") || field;
|
||||
if (changed.indexOf(container) === -1 && isChanged(field)) {
|
||||
changed.push(container);
|
||||
}
|
||||
});
|
||||
form.querySelectorAll(".list-filter-changed").forEach(function (container) {
|
||||
if (changed.indexOf(container) === -1) {
|
||||
container.classList.remove("list-filter-changed");
|
||||
}
|
||||
});
|
||||
changed.forEach(function (container) {
|
||||
container.classList.add("list-filter-changed");
|
||||
});
|
||||
|
||||
var pending = changed.length > 0;
|
||||
form.classList.toggle("list-filter-pending", pending);
|
||||
// Sólo se cambia el texto al empezar o terminar los cambios pendientes, para que el lector
|
||||
// de pantalla lo anuncie una vez y no con cada pulsación.
|
||||
var status = form.querySelector(".list-filter-status");
|
||||
var text = pending ? status.getAttribute("data-pending") || "" : "";
|
||||
if (status.textContent !== text) {
|
||||
status.textContent = text;
|
||||
}
|
||||
}
|
||||
|
||||
function filterOf(element) {
|
||||
var form = element instanceof Element ? element.closest("form.list-filter") : null;
|
||||
return form && form.querySelector(".list-filter-status") ? form : null;
|
||||
}
|
||||
|
||||
function onChange(event) {
|
||||
var form = filterOf(event.target);
|
||||
if (form) {
|
||||
refresh(form);
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("input", onChange);
|
||||
document.addEventListener("change", onChange);
|
||||
|
||||
document.addEventListener("submit", function (event) {
|
||||
var form = filterOf(event.target);
|
||||
if (form) {
|
||||
form.querySelectorAll(FIELDS).forEach(commit);
|
||||
refresh(form);
|
||||
}
|
||||
});
|
||||
})();
|
||||
52
assets/js/list-view.js
Normal file
52
assets/js/list-view.js
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
// Anuncia a los lectores de pantalla el resumen de un listado (`ListView`, atributo
|
||||
// `data-summary`, p. ej. "23 resultados") cuando se actualiza sin recargar la página, al filtrar,
|
||||
// ordenar o cambiar de página. Un lector de pantalla no suele anunciar una zona `aria-live` que se
|
||||
// inserta de golpe, así que el aviso usa una única zona creada al cargar la página, y el listado
|
||||
// nuevo sólo le pasa su texto. Al cargar la página no anuncia nada. Sin dependencias: vale para
|
||||
// HTMX o para cualquier otra forma de actualizar sólo una parte de la página.
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
var SELECTOR = ".list-view[data-summary]";
|
||||
var REGION_ID = "list-view-status";
|
||||
|
||||
// La zona se busca por su identificador cada vez: si se restaura una página del historial
|
||||
// sustituyendo el contenido del `<body>` (como hace HTMX al volver atrás), la zona creada al
|
||||
// cargar desaparece y queda la copia restaurada.
|
||||
function region() {
|
||||
var element = document.getElementById(REGION_ID);
|
||||
if (!element) {
|
||||
element = document.createElement("div");
|
||||
element.id = REGION_ID;
|
||||
element.className = "visually-hidden";
|
||||
element.setAttribute("role", "status");
|
||||
document.body.appendChild(element);
|
||||
}
|
||||
return element;
|
||||
}
|
||||
region();
|
||||
|
||||
function announce(text) {
|
||||
var element = region();
|
||||
// Se vacía primero para que se anuncie también un texto igual al anterior.
|
||||
element.textContent = "";
|
||||
setTimeout(function () {
|
||||
element.textContent = text;
|
||||
}, 50);
|
||||
}
|
||||
|
||||
new MutationObserver(function (mutations) {
|
||||
mutations.forEach(function (mutation) {
|
||||
mutation.addedNodes.forEach(function (node) {
|
||||
if (!(node instanceof Element)) {
|
||||
return;
|
||||
}
|
||||
var list = node.matches(SELECTOR) ? node : node.querySelector(SELECTOR);
|
||||
if (list) {
|
||||
announce(list.getAttribute("data-summary"));
|
||||
}
|
||||
});
|
||||
});
|
||||
}).observe(document.body, { childList: true, subtree: true });
|
||||
})();
|
||||
|
|
@ -25,6 +25,8 @@
|
|||
@use "basic/dropdown";
|
||||
@use "basic/form";
|
||||
@use "basic/image";
|
||||
@use "basic/list-filter";
|
||||
@use "basic/list-view";
|
||||
@use "basic/messages";
|
||||
@use "basic/nav";
|
||||
@use "basic/navbar";
|
||||
|
|
|
|||
59
assets/scss/basic/_list-filter.scss
Normal file
59
assets/scss/basic/_list-filter.scss
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
// List filters (ListFilter): fields and button on one row, wrapping when there is no room.
|
||||
.list-filter {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: flex-end;
|
||||
gap: 0.5rem 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.list-filter > .form-field {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
// Marks of changed filters not applied yet (filters with a button), set by `list-filter.js`. Only
|
||||
// colors and opacity change, never sizes: the dot always takes its room, hidden, so nothing moves
|
||||
// when it shows up. A vivid orange, set apart from the primary color of the button, darkened just
|
||||
// enough to keep a 3:1 contrast against the background. Every changed field gets the dot by its
|
||||
// label, once per field (on the title of a group); text fields and dropdowns also show a gray
|
||||
// background.
|
||||
.list-filter {
|
||||
--list-filter-mark: color-mix(in srgb, #fd7e14 85%, black);
|
||||
}
|
||||
|
||||
.list-filter .form-field > .form-label::after,
|
||||
.list-filter .form-field.form-check > .form-check-label::after {
|
||||
content: "\25CF" / "";
|
||||
margin-left: 0.25em;
|
||||
color: var(--list-filter-mark);
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.list-filter .list-filter-changed > .form-label::after,
|
||||
.list-filter .list-filter-changed.form-check > .form-check-label::after {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.list-filter .list-filter-changed .form-control,
|
||||
.list-filter .list-filter-changed .form-select {
|
||||
background-color: color-mix(in srgb, var(--val-color--bg--muted) 60%, #fff);
|
||||
}
|
||||
|
||||
// Native checkboxes only take a color when checked; a switch is drawn by the theme.
|
||||
.list-filter .list-filter-changed .form-check-input {
|
||||
accent-color: var(--list-filter-mark);
|
||||
}
|
||||
|
||||
.list-filter .list-filter-changed.form-switch .form-check-input:checked {
|
||||
background-color: var(--list-filter-mark);
|
||||
}
|
||||
|
||||
.list-filter-pending > button[type="submit"] {
|
||||
background-color: var(--val-color--primary);
|
||||
border-color: var(--val-color--primary);
|
||||
}
|
||||
|
||||
.list-filter-pending > button[type="submit"]:hover {
|
||||
background-color: color-mix(in srgb, var(--val-color--primary) 85%, black);
|
||||
border-color: color-mix(in srgb, var(--val-color--primary) 85%, black);
|
||||
}
|
||||
8
assets/scss/basic/_list-view.scss
Normal file
8
assets/scss/basic/_list-view.scss
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// List view (ListView): action buttons above the table, then messages, table and pager.
|
||||
.list-view {
|
||||
margin: 1.5rem 0;
|
||||
}
|
||||
|
||||
.list-view-actions {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
|
@ -30,10 +30,6 @@
|
|||
margin-top: 1.5rem;
|
||||
}
|
||||
|
||||
.user-admin-table-wrapper {
|
||||
margin: 1.5rem 0;
|
||||
}
|
||||
|
||||
.user-form-error {
|
||||
padding: 0.625rem 0.875rem;
|
||||
margin-bottom: 1rem;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue