🎨 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");
|
let intro_button_lnk: Option<&String> = page.param("intro_button_lnk");
|
||||||
|
|
||||||
html! {
|
html! {
|
||||||
body id=[page.body_id().get()] class=[page.body_classes().get()] {
|
|
||||||
header class="intro-header" {
|
header class="intro-header" {
|
||||||
section class="intro-header__body" {
|
section class="intro-header__body" {
|
||||||
h1 class="intro-header__title" {
|
h1 class="intro-header__title" {
|
||||||
|
|
@ -166,7 +165,6 @@ fn render_intro(page: &mut Page) -> Markup {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn render_pagetop_intro(page: &mut Page) -> Markup {
|
fn render_pagetop_intro(page: &mut Page) -> Markup {
|
||||||
page.alter_assets(ContextOp::AddJavaScript(JavaScript::on_load_async("intro-js", |cx|
|
page.alter_assets(ContextOp::AddJavaScript(JavaScript::on_load_async("intro-js", |cx|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
|
use crate::core::component::ChildOp;
|
||||||
use crate::core::theme::all::{theme_by_short_name, DEFAULT_THEME};
|
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::core::TypeInfo;
|
||||||
use crate::html::{html, Markup};
|
use crate::html::{html, Markup};
|
||||||
use crate::html::{Assets, Favicon, JavaScript, StyleSheet};
|
use crate::html::{Assets, Favicon, JavaScript, StyleSheet};
|
||||||
|
|
@ -104,6 +105,10 @@ pub trait Contextual: LangId {
|
||||||
#[builder_fn]
|
#[builder_fn]
|
||||||
fn with_assets(self, op: ContextOp) -> Self;
|
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 >*********************************************************************
|
// **< Contextual GETTERS >*********************************************************************
|
||||||
|
|
||||||
/// Devuelve una referencia a la solicitud HTTP asociada, si existe.
|
/// 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.
|
favicon : Option<Favicon>, // Favicon, si se ha definido.
|
||||||
stylesheets: Assets<StyleSheet>, // Hojas de estilo CSS.
|
stylesheets: Assets<StyleSheet>, // Hojas de estilo CSS.
|
||||||
javascripts: Assets<JavaScript>, // Scripts JavaScript.
|
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.
|
params : HashMap<&'static str, (Box<dyn Any>, &'static str)>, // Parámetros en ejecución.
|
||||||
id_counter : usize, // Contador para generar identificadores únicos.
|
id_counter : usize, // Contador para generar identificadores únicos.
|
||||||
}
|
}
|
||||||
|
|
@ -250,6 +256,7 @@ impl Context {
|
||||||
favicon : None,
|
favicon : None,
|
||||||
stylesheets: Assets::<StyleSheet>::new(),
|
stylesheets: Assets::<StyleSheet>::new(),
|
||||||
javascripts: Assets::<JavaScript>::new(),
|
javascripts: Assets::<JavaScript>::new(),
|
||||||
|
regions : ChildrenInRegions::default(),
|
||||||
params : HashMap::default(),
|
params : HashMap::default(),
|
||||||
id_counter : 0,
|
id_counter : 0,
|
||||||
}
|
}
|
||||||
|
|
@ -283,6 +290,13 @@ impl Context {
|
||||||
markup
|
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 >*************************************************************************
|
// **< Context PARAMS >*************************************************************************
|
||||||
|
|
||||||
/// Recupera una *referencia tipada* al parámetro solicitado.
|
/// Recupera una *referencia tipada* al parámetro solicitado.
|
||||||
|
|
@ -471,6 +485,12 @@ impl Contextual for Context {
|
||||||
self
|
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 >*********************************************************************
|
// **< Contextual GETTERS >*********************************************************************
|
||||||
|
|
||||||
fn request(&self) -> Option<&HttpRequest> {
|
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_body(self, page, self.page_regions())`
|
||||||
/// - `<Self as ThemePage>::render_head(self, page)`
|
/// - `<Self as ThemePage>::render_head(self, page)`
|
||||||
pub trait ThemePage {
|
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
|
/// 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
|
/// contenedor con `role="region"` y un `aria-label` localizado.
|
||||||
/// de región es **único** dentro de la página.
|
/// 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 {
|
fn render_body(&self, page: &mut Page, regions: &[(Region, L10n)]) -> Markup {
|
||||||
html! {
|
html! {
|
||||||
body id=[page.body_id().get()] class=[page.body_classes().get()] {
|
|
||||||
@for (region, region_label) in regions {
|
@for (region, region_label) in regions {
|
||||||
@let output = page.render_region(region.key());
|
@let output = page.render_region(region.key());
|
||||||
@if !output.is_empty() {
|
@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`,
|
/// Recorre y genera por defecto las etiquetas básicas (`charset`, `title`, `description`,
|
||||||
/// `X-UA-Compatible`), los metadatos (`name/content`) y propiedades (`property/content`),
|
/// `viewport`, `X-UA-Compatible`), los metadatos (`name/content`) y propiedades
|
||||||
/// además de los recursos CSS/JS de la página.
|
/// (`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 {
|
fn render_head(&self, page: &mut Page) -> Markup {
|
||||||
let viewport = "width=device-width, initial-scale=1, shrink-to-fit=no";
|
let viewport = "width=device-width, initial-scale=1, shrink-to-fit=no";
|
||||||
html! {
|
html! {
|
||||||
head {
|
|
||||||
meta charset="utf-8";
|
meta charset="utf-8";
|
||||||
|
|
||||||
@if let Some(title) = page.title() {
|
@if let Some(title) = page.title() {
|
||||||
|
|
@ -87,7 +88,6 @@ pub trait ThemePage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Interfaz común que debe implementar cualquier tema de PageTop.
|
/// Interfaz común que debe implementar cualquier tema de PageTop.
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ impl Region {
|
||||||
self.key
|
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]
|
#[inline]
|
||||||
pub fn name(&self) -> &str {
|
pub fn name(&self) -> &str {
|
||||||
&self.name
|
&self.name
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ pub use actix_web::Result as ResultPage;
|
||||||
|
|
||||||
use crate::base::action;
|
use crate::base::action;
|
||||||
use crate::core::component::{Child, ChildOp, Component, Context, ContextOp, Contextual};
|
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::{html, Markup, DOCTYPE};
|
||||||
use crate::html::{Assets, Favicon, JavaScript, StyleSheet};
|
use crate::html::{Assets, Favicon, JavaScript, StyleSheet};
|
||||||
use crate::html::{AttrClasses, ClassesOp};
|
use crate::html::{AttrClasses, ClassesOp};
|
||||||
|
|
@ -29,7 +29,6 @@ pub struct Page {
|
||||||
body_id : AttrId,
|
body_id : AttrId,
|
||||||
body_classes: AttrClasses,
|
body_classes: AttrClasses,
|
||||||
context : Context,
|
context : Context,
|
||||||
regions : ChildrenInRegions,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Page {
|
impl Page {
|
||||||
|
|
@ -47,7 +46,6 @@ impl Page {
|
||||||
body_id : AttrId::default(),
|
body_id : AttrId::default(),
|
||||||
body_classes: AttrClasses::default(),
|
body_classes: AttrClasses::default(),
|
||||||
context : Context::new(Some(request)),
|
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.
|
/// Añade un componente a la región de contenido por defecto.
|
||||||
pub fn add_component(mut self, component: impl Component) -> Self {
|
pub fn add_component(mut self, component: impl Component) -> Self {
|
||||||
self.regions
|
self.context
|
||||||
.alter_child_in(REGION_CONTENT, ChildOp::Add(Child::with(component)));
|
.alter_child_in(REGION_CONTENT, ChildOp::Add(Child::with(component)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
@ -122,7 +120,7 @@ impl Page {
|
||||||
region_name: &'static str,
|
region_name: &'static str,
|
||||||
component: impl Component,
|
component: impl Component,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.regions
|
self.context
|
||||||
.alter_child_in(region_name, ChildOp::Add(Child::with(component)));
|
.alter_child_in(region_name, ChildOp::Add(Child::with(component)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
@ -143,13 +141,6 @@ impl Page {
|
||||||
self
|
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 >***************************************************************************
|
// **< Page GETTERS >***************************************************************************
|
||||||
|
|
||||||
/// Devuelve el título traducido para el idioma de la página, si existe.
|
/// Devuelve el título traducido para el idioma de la página, si existe.
|
||||||
|
|
@ -194,13 +185,13 @@ impl Page {
|
||||||
// **< Page RENDER >****************************************************************************
|
// **< Page RENDER >****************************************************************************
|
||||||
|
|
||||||
/// Renderiza los componentes de una región (`region_name`) de la página.
|
/// 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 {
|
pub fn render_region(&mut self, region_name: &'static str) -> Markup {
|
||||||
self.regions
|
self.context.render_region(region_name)
|
||||||
.merge_all_components(self.context.theme(), region_name)
|
|
||||||
.render(&mut self.context)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Renderiza los recursos de la página.
|
/// Renderiza los recursos de la página.
|
||||||
|
#[inline]
|
||||||
pub fn render_assets(&mut self) -> Markup {
|
pub fn render_assets(&mut self) -> Markup {
|
||||||
self.context.render_assets()
|
self.context.render_assets()
|
||||||
}
|
}
|
||||||
|
|
@ -238,8 +229,14 @@ impl Page {
|
||||||
Ok(html! {
|
Ok(html! {
|
||||||
(DOCTYPE)
|
(DOCTYPE)
|
||||||
html lang=(lang) dir=(dir) {
|
html lang=(lang) dir=(dir) {
|
||||||
|
head {
|
||||||
(head)
|
(head)
|
||||||
|
}
|
||||||
|
body id=[self.body_id().get()] class=[self.body_classes().get()] {
|
||||||
|
(self.render_region("page-top"))
|
||||||
(body)
|
(body)
|
||||||
|
(self.render_region("page-bottom"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -290,6 +287,12 @@ impl Contextual for Page {
|
||||||
self
|
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 >*********************************************************************
|
// **< Contextual GETTERS >*********************************************************************
|
||||||
|
|
||||||
fn request(&self) -> Option<&HttpRequest> {
|
fn request(&self) -> Option<&HttpRequest> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue