♻️ (admin): Construye piezas del panel con new()

AdminSection, AdminPage, AdminTask y AdminAction pasan a campos
privados con constructores y pesos Weight.
This commit is contained in:
Manuel Cillero 2026-11-07 07:58:01 +01:00
parent 8e0c887ba6
commit 71ea609088
11 changed files with 616 additions and 445 deletions

View file

@ -4,20 +4,14 @@ use super::FnActionDeclareAdminActions;
/// Acción para declarar acciones locales (botones de acción) en páginas del panel.
///
/// Cada [`AdminAction`] indica en `for_path` la página en la que aparece el botón.
/// Cada [`AdminAction`] indica la página en la que aparece el botón.
///
/// # Ejemplo
///
/// ```rust,no_run
/// # use pagetop::prelude::*;
/// fn declare_actions(bag: &mut AdminActionBag) {
/// bag.add(AdminAction {
/// url: "config/export/new".to_owned(),
/// for_path: "config/export".to_owned(),
/// title: Lc::n("Add export"),
/// weight: 0,
/// permission: None,
/// });
/// bag.add(AdminAction::new("config/export/new", "config/export", Lc::n("Add export")));
/// }
/// // En Extension::actions():
/// // action::admin::DeclareAdminActions::new(declare_actions)

View file

@ -12,12 +12,7 @@ use super::FnActionDeclareAdminSections;
/// ```rust,no_run
/// # use pagetop::prelude::*;
/// fn declare_sections(bag: &mut AdminSectionBag) {
/// bag.add(AdminSection {
/// key: "tools".to_owned(),
/// path: "tools".to_owned(),
/// title: Lc::n("Tools"),
/// weight: 60,
/// });
/// bag.add(AdminSection::new("tools", Lc::n("Tools")).with_weight(60));
/// }
/// // En Extension::actions():
/// // action::admin::DeclareAdminSections::new(declare_sections)

View file

@ -4,21 +4,16 @@ use super::FnActionDeclareAdminTasks;
/// Acción para declarar tareas (pestañas) locales en páginas del panel.
///
/// Cada [`AdminTask`] indica en `parent_path` la página a la que pertenece.
/// Cada [`AdminTask`] indica la página a la que pertenece; la pestaña con la ruta de la propia
/// página es la activa al entrar en ella.
///
/// # Ejemplo
///
/// ```rust,no_run
/// # use pagetop::prelude::*;
/// fn declare_tasks(bag: &mut AdminTaskBag) {
/// bag.add(AdminTask {
/// path: "config/export/csv".to_owned(),
/// parent_path: "config/export".to_owned(),
/// title: Lc::n("CSV"),
/// weight: 0,
/// is_default: true,
/// permission: None,
/// });
/// bag.add(AdminTask::new("config/export", "config/export", Lc::n("All")));
/// bag.add(AdminTask::new("config/export/csv", "config/export", Lc::n("CSV")).with_weight(1));
/// }
/// // En Extension::actions():
/// // action::admin::DeclareAdminTasks::new(declare_tasks)

View file

@ -49,9 +49,9 @@ impl Component for AdminFrame {
// Sin título propio, la página declarada aporta el suyo, también al `<head>` (que se
// renderiza después del `<body>`).
if cx.title().is_none()
&& let Some(page) = AdminRegistry::global().pages().get(&path)
&& let Some(page) = AdminRegistry::global().page(&path)
{
cx.alter_title(page.title.clone());
cx.alter_title(page.title().clone());
}
let breadcrumbs = breadcrumbs(cx, &path).render(cx).await;
@ -89,26 +89,26 @@ fn breadcrumbs(cx: &Context, path: &str) -> Breadcrumb {
admin_path(""),
));
if let Some(page) = registry.nearest_page(path) {
if let Some(section) = registry.sections().get(&page.section) {
if let Some(section) = registry.section(page.section()) {
bc = bc.with_crumb(breadcrumb::Crumb::new(
section.title.clone(),
admin_path(&section.path),
section.title().clone(),
admin_path(section.path()),
));
}
if page.path == path {
bc = bc.with_crumb(breadcrumb::Crumb::current(page.title.clone()));
if page.path() == path {
bc = bc.with_crumb(breadcrumb::Crumb::current(page.title().clone()));
} else {
bc = bc.with_crumb(breadcrumb::Crumb::new(
page.title.clone(),
admin_path(&page.path),
page.title().clone(),
admin_path(page.path()),
));
if !cx.title().is_none() {
bc = bc.with_crumb(breadcrumb::Crumb::current(cx.title().clone()));
}
}
} else if let Some(section) = registry.sections().values().find(|s| s.path == path) {
} else if let Some(section) = registry.section_by_path(path) {
// La ruta es la propia sección (su página de aterrizaje), no una página declarada en ella.
bc = bc.with_crumb(breadcrumb::Crumb::current(section.title.clone()));
bc = bc.with_crumb(breadcrumb::Crumb::current(section.title().clone()));
}
bc
}
@ -117,16 +117,11 @@ fn breadcrumbs(cx: &Context, path: &str) -> Breadcrumb {
// permiso que el usuario no tiene no se muestran.
fn local_tasks(cx: &Context, path: &str) -> Markup {
let registry = AdminRegistry::global();
let parent = registry
.tasks()
.values()
.flatten()
.find(|task| task.path == path)
.map_or(path, |task| task.parent_path.as_str());
let parent = registry.task(path).map_or(path, |task| task.parent_path());
let tasks: Vec<_> = registry
.tasks_for(parent)
.into_iter()
.filter(|task| is_granted(cx, task.permission))
.iter()
.filter(|task| is_granted(cx, task.permission()))
.collect();
if tasks.is_empty() {
return html! {};
@ -134,8 +129,8 @@ fn local_tasks(cx: &Context, path: &str) -> Markup {
html! {
ul class="admin-tasks-list" {
@for task in tasks {
li class="admin-task" .admin-task-active[task.path == path] {
a href=(cx.route(admin_path(&task.path))) { (task.title.using(cx)) }
li class="admin-task" .admin-task-active[task.path() == path] {
a href=(cx.route(admin_path(task.path()))) { (task.title().using(cx)) }
}
}
}
@ -147,8 +142,8 @@ fn local_tasks(cx: &Context, path: &str) -> Markup {
fn local_actions(cx: &Context, path: &str) -> Markup {
let actions: Vec<_> = AdminRegistry::global()
.actions_for(path)
.into_iter()
.filter(|action| is_granted(cx, action.permission))
.iter()
.filter(|action| is_granted(cx, action.permission()))
.collect();
if actions.is_empty() {
return html! {};
@ -157,8 +152,8 @@ fn local_actions(cx: &Context, path: &str) -> Markup {
ul class="admin-actions-list" {
@for action in actions {
li class="admin-action" {
a class="admin-action-link" href=(cx.route(admin_path(&action.url))) {
(action.title.using(cx))
a class="admin-action-link" href=(cx.route(admin_path(action.url()))) {
(action.title().using(cx))
}
}
}

View file

@ -32,20 +32,20 @@ impl Component for AdminMenu {
let mut nav =
Nav::new().with_item(nav::Item::link(Lc::l("admin_dashboard"), admin_path("")));
for section in registry.ordered_sections() {
for section in registry.sections() {
let pages: Vec<_> = registry
.pages_for_section(&section.key)
.into_iter()
.pages_for_section(section.key())
.iter()
.filter(|p| p.is_accessible(cx))
.collect();
if pages.is_empty() {
continue;
}
let mut menu = Dropdown::new().with_title(section.title.clone());
let mut menu = Dropdown::new().with_title(section.title().clone());
for page in pages {
menu = menu.with_item(dropdown::Item::link(
page.title.clone(),
admin_path(&page.path),
page.title().clone(),
admin_path(page.path()),
));
}
nav = nav.with_item(nav::Item::dropdown(menu));