🎨 Mejora uso de las regiones en contexto y página
This commit is contained in:
parent
6d6c0f874b
commit
31310c1c13
5 changed files with 142 additions and 121 deletions
|
|
@ -1,5 +1,6 @@
|
|||
use crate::core::component::ChildOp;
|
||||
use crate::core::theme::all::{theme_by_short_name, DEFAULT_THEME};
|
||||
use crate::core::theme::ThemeRef;
|
||||
use crate::core::theme::{ChildrenInRegions, ThemeRef};
|
||||
use crate::core::TypeInfo;
|
||||
use crate::html::{html, Markup};
|
||||
use crate::html::{Assets, Favicon, JavaScript, StyleSheet};
|
||||
|
|
@ -104,6 +105,10 @@ pub trait Contextual: LangId {
|
|||
#[builder_fn]
|
||||
fn with_assets(self, op: ContextOp) -> Self;
|
||||
|
||||
/// Opera con [`ChildOp`] en una región (`region_name`) de la página.
|
||||
#[builder_fn]
|
||||
fn with_child_in(self, region_name: &'static str, op: ChildOp) -> Self;
|
||||
|
||||
// **< Contextual GETTERS >*********************************************************************
|
||||
|
||||
/// Devuelve una referencia a la solicitud HTTP asociada, si existe.
|
||||
|
|
@ -211,6 +216,7 @@ pub struct Context {
|
|||
favicon : Option<Favicon>, // Favicon, si se ha definido.
|
||||
stylesheets: Assets<StyleSheet>, // Hojas de estilo CSS.
|
||||
javascripts: Assets<JavaScript>, // Scripts JavaScript.
|
||||
regions : ChildrenInRegions, // Regiones de componentes para renderizar.
|
||||
params : HashMap<&'static str, (Box<dyn Any>, &'static str)>, // Parámetros en ejecución.
|
||||
id_counter : usize, // Contador para generar identificadores únicos.
|
||||
}
|
||||
|
|
@ -250,6 +256,7 @@ impl Context {
|
|||
favicon : None,
|
||||
stylesheets: Assets::<StyleSheet>::new(),
|
||||
javascripts: Assets::<JavaScript>::new(),
|
||||
regions : ChildrenInRegions::default(),
|
||||
params : HashMap::default(),
|
||||
id_counter : 0,
|
||||
}
|
||||
|
|
@ -283,6 +290,13 @@ impl Context {
|
|||
markup
|
||||
}
|
||||
|
||||
/// Renderiza los componentes de una región (`region_name`).
|
||||
pub fn render_region(&mut self, region_name: &'static str) -> Markup {
|
||||
self.regions
|
||||
.merge_all_components(self.theme, region_name)
|
||||
.render(self)
|
||||
}
|
||||
|
||||
// **< Context PARAMS >*************************************************************************
|
||||
|
||||
/// Recupera una *referencia tipada* al parámetro solicitado.
|
||||
|
|
@ -471,6 +485,12 @@ impl Contextual for Context {
|
|||
self
|
||||
}
|
||||
|
||||
#[builder_fn]
|
||||
fn with_child_in(mut self, region_name: &'static str, op: ChildOp) -> Self {
|
||||
self.regions.alter_child_in(region_name, op);
|
||||
self
|
||||
}
|
||||
|
||||
// **< Contextual GETTERS >*********************************************************************
|
||||
|
||||
fn request(&self) -> Option<&HttpRequest> {
|
||||
|
|
|
|||
|
|
@ -26,65 +26,65 @@ pub type ThemeRef = &'static dyn Theme;
|
|||
/// - `<Self as ThemePage>::render_body(self, page, self.page_regions())`
|
||||
/// - `<Self as ThemePage>::render_head(self, page)`
|
||||
pub trait ThemePage {
|
||||
/// Renderiza el contenido del `<body>` de la página.
|
||||
/// Renderiza el **contenido interior** del `<body>` de la página.
|
||||
///
|
||||
/// Recorre `regions` en el **orden declarado** y, para cada región con contenido, genera un
|
||||
/// contenedor con `role="region"` y un `aria-label` localizado. Se asume que cada identificador
|
||||
/// de región es **único** dentro de la página.
|
||||
/// contenedor con `role="region"` y un `aria-label` localizado.
|
||||
/// Se asume que cada identificador de región es **único** dentro de la página.
|
||||
///
|
||||
/// La etiqueta `<body>` no se incluye aquí; únicamente renderiza su contenido.
|
||||
fn render_body(&self, page: &mut Page, regions: &[(Region, L10n)]) -> Markup {
|
||||
html! {
|
||||
body id=[page.body_id().get()] class=[page.body_classes().get()] {
|
||||
@for (region, region_label) in regions {
|
||||
@let output = page.render_region(region.key());
|
||||
@if !output.is_empty() {
|
||||
@let region_name = region.name();
|
||||
div
|
||||
id=(region_name)
|
||||
class={ "region region--" (region_name) }
|
||||
role="region"
|
||||
aria-label=[region_label.lookup(page)]
|
||||
{
|
||||
(output)
|
||||
}
|
||||
@for (region, region_label) in regions {
|
||||
@let output = page.render_region(region.key());
|
||||
@if !output.is_empty() {
|
||||
@let region_name = region.name();
|
||||
div
|
||||
id=(region_name)
|
||||
class={ "region region--" (region_name) }
|
||||
role="region"
|
||||
aria-label=[region_label.lookup(page)]
|
||||
{
|
||||
(output)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Renderiza el contenido del `<head>` de la página.
|
||||
/// Renderiza el **contenido interior** del `<head>` de la página.
|
||||
///
|
||||
/// Por defecto incluye las etiquetas básicas (`charset`, `title`, `description`, `viewport`,
|
||||
/// `X-UA-Compatible`), los metadatos (`name/content`) y propiedades (`property/content`),
|
||||
/// además de los recursos CSS/JS de la página.
|
||||
/// Recorre y genera por defecto las etiquetas básicas (`charset`, `title`, `description`,
|
||||
/// `viewport`, `X-UA-Compatible`), los metadatos (`name/content`) y propiedades
|
||||
/// (`property/content`), además de los recursos CSS/JS de la página.
|
||||
///
|
||||
/// La etiqueta `<head>` no se incluye aquí; únicamente renderiza su contenido.
|
||||
fn render_head(&self, page: &mut Page) -> Markup {
|
||||
let viewport = "width=device-width, initial-scale=1, shrink-to-fit=no";
|
||||
html! {
|
||||
head {
|
||||
meta charset="utf-8";
|
||||
meta charset="utf-8";
|
||||
|
||||
@if let Some(title) = page.title() {
|
||||
title { (global::SETTINGS.app.name) (" | ") (title) }
|
||||
} @else {
|
||||
title { (global::SETTINGS.app.name) }
|
||||
}
|
||||
|
||||
@if let Some(description) = page.description() {
|
||||
meta name="description" content=(description);
|
||||
}
|
||||
|
||||
meta name="viewport" content=(viewport);
|
||||
@for (name, content) in page.metadata() {
|
||||
meta name=(name) content=(content) {}
|
||||
}
|
||||
|
||||
meta http-equiv="X-UA-Compatible" content="IE=edge";
|
||||
@for (property, content) in page.properties() {
|
||||
meta property=(property) content=(content) {}
|
||||
}
|
||||
|
||||
(page.render_assets())
|
||||
@if let Some(title) = page.title() {
|
||||
title { (global::SETTINGS.app.name) (" | ") (title) }
|
||||
} @else {
|
||||
title { (global::SETTINGS.app.name) }
|
||||
}
|
||||
|
||||
@if let Some(description) = page.description() {
|
||||
meta name="description" content=(description);
|
||||
}
|
||||
|
||||
meta name="viewport" content=(viewport);
|
||||
@for (name, content) in page.metadata() {
|
||||
meta name=(name) content=(content) {}
|
||||
}
|
||||
|
||||
meta http-equiv="X-UA-Compatible" content="IE=edge";
|
||||
@for (property, content) in page.properties() {
|
||||
meta property=(property) content=(content) {}
|
||||
}
|
||||
|
||||
(page.render_assets())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ impl Region {
|
|||
self.key
|
||||
}
|
||||
|
||||
/// Devuelve el nombre normalizado de la región (para atributos y búsquedas).
|
||||
/// Devuelve el nombre normalizado de la región (para identificadores y atributos HTML).
|
||||
#[inline]
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue