🎨 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
|
|
@ -93,7 +93,6 @@ fn render_intro(page: &mut Page) -> Markup {
|
|||
let intro_button_lnk: Option<&String> = page.param("intro_button_lnk");
|
||||
|
||||
html! {
|
||||
body id=[page.body_id().get()] class=[page.body_classes().get()] {
|
||||
header class="intro-header" {
|
||||
section class="intro-header__body" {
|
||||
h1 class="intro-header__title" {
|
||||
|
|
@ -165,7 +164,6 @@ fn render_intro(page: &mut Page) -> Markup {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn render_pagetop_intro(page: &mut Page) -> Markup {
|
||||
|
|
|
|||
|
|
@ -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,14 +26,15 @@ 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() {
|
||||
|
|
@ -50,17 +51,17 @@ pub trait ThemePage {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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";
|
||||
|
||||
@if let Some(title) = page.title() {
|
||||
|
|
@ -86,7 +87,6 @@ pub trait ThemePage {
|
|||
(page.render_assets())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Interfaz común que debe implementar cualquier tema de PageTop.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ pub use actix_web::Result as ResultPage;
|
|||
|
||||
use crate::base::action;
|
||||
use crate::core::component::{Child, ChildOp, Component, Context, ContextOp, Contextual};
|
||||
use crate::core::theme::{ChildrenInRegions, ThemeRef, REGION_CONTENT};
|
||||
use crate::core::theme::{ThemeRef, REGION_CONTENT};
|
||||
use crate::html::{html, Markup, DOCTYPE};
|
||||
use crate::html::{Assets, Favicon, JavaScript, StyleSheet};
|
||||
use crate::html::{AttrClasses, ClassesOp};
|
||||
|
|
@ -29,7 +29,6 @@ pub struct Page {
|
|||
body_id : AttrId,
|
||||
body_classes: AttrClasses,
|
||||
context : Context,
|
||||
regions : ChildrenInRegions,
|
||||
}
|
||||
|
||||
impl Page {
|
||||
|
|
@ -47,7 +46,6 @@ impl Page {
|
|||
body_id : AttrId::default(),
|
||||
body_classes: AttrClasses::default(),
|
||||
context : Context::new(Some(request)),
|
||||
regions : ChildrenInRegions::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -111,7 +109,7 @@ impl Page {
|
|||
|
||||
/// Añade un componente a la región de contenido por defecto.
|
||||
pub fn add_component(mut self, component: impl Component) -> Self {
|
||||
self.regions
|
||||
self.context
|
||||
.alter_child_in(REGION_CONTENT, ChildOp::Add(Child::with(component)));
|
||||
self
|
||||
}
|
||||
|
|
@ -122,7 +120,7 @@ impl Page {
|
|||
region_name: &'static str,
|
||||
component: impl Component,
|
||||
) -> Self {
|
||||
self.regions
|
||||
self.context
|
||||
.alter_child_in(region_name, ChildOp::Add(Child::with(component)));
|
||||
self
|
||||
}
|
||||
|
|
@ -143,13 +141,6 @@ impl Page {
|
|||
self
|
||||
}
|
||||
|
||||
/// Opera con [`ChildOp`] en una región (`region_name`) de la página.
|
||||
#[builder_fn]
|
||||
pub fn with_child_in(mut self, region_name: &'static str, op: ChildOp) -> Self {
|
||||
self.regions.alter_child_in(region_name, op);
|
||||
self
|
||||
}
|
||||
|
||||
// **< Page GETTERS >***************************************************************************
|
||||
|
||||
/// Devuelve el título traducido para el idioma de la página, si existe.
|
||||
|
|
@ -194,13 +185,13 @@ impl Page {
|
|||
// **< Page RENDER >****************************************************************************
|
||||
|
||||
/// Renderiza los componentes de una región (`region_name`) de la página.
|
||||
#[inline]
|
||||
pub fn render_region(&mut self, region_name: &'static str) -> Markup {
|
||||
self.regions
|
||||
.merge_all_components(self.context.theme(), region_name)
|
||||
.render(&mut self.context)
|
||||
self.context.render_region(region_name)
|
||||
}
|
||||
|
||||
/// Renderiza los recursos de la página.
|
||||
#[inline]
|
||||
pub fn render_assets(&mut self) -> Markup {
|
||||
self.context.render_assets()
|
||||
}
|
||||
|
|
@ -238,8 +229,14 @@ impl Page {
|
|||
Ok(html! {
|
||||
(DOCTYPE)
|
||||
html lang=(lang) dir=(dir) {
|
||||
head {
|
||||
(head)
|
||||
}
|
||||
body id=[self.body_id().get()] class=[self.body_classes().get()] {
|
||||
(self.render_region("page-top"))
|
||||
(body)
|
||||
(self.render_region("page-bottom"))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
@ -290,6 +287,12 @@ impl Contextual for Page {
|
|||
self
|
||||
}
|
||||
|
||||
#[builder_fn]
|
||||
fn with_child_in(mut self, region_name: &'static str, op: ChildOp) -> Self {
|
||||
self.context.alter_child_in(region_name, op);
|
||||
self
|
||||
}
|
||||
|
||||
// **< Contextual GETTERS >*********************************************************************
|
||||
|
||||
fn request(&self) -> Option<&HttpRequest> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue