✨ Añade soporte para responder páginas HTML
- Amplia la estructura "Page" para trabajar el renderizado con regiones de componentes para componer la página. - Añade acciones "BeforeRenderBody" y "AfterRenderBody" para alterar el contenido de la página antes y después del renderizado. - Actualiza "Context" para admitir parámetros dinámicos y mejorar la gestión de temas. - Implementa el manejo de errores HTTP respondiendo páginas. - Mejora la documentación y reorganiza el código en varios módulos.
This commit is contained in:
parent
f23c8d5c4c
commit
a1bb6cd12d
17 changed files with 669 additions and 143 deletions
|
@ -77,8 +77,8 @@ impl<C: ComponentTrait> Typed<C> {
|
|||
|
||||
// Typed HELPERS *******************************************************************************
|
||||
|
||||
/// Convierte el componente tipado en un [`Child`].
|
||||
fn to_child(&self) -> Child {
|
||||
// Convierte el componente tipado en un [`Child`].
|
||||
fn into_child(self) -> Child {
|
||||
Child(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
@ -155,12 +155,12 @@ impl Children {
|
|||
#[builder_fn]
|
||||
pub fn with_typed<C: ComponentTrait + Default>(mut self, op: TypedOp<C>) -> Self {
|
||||
match op {
|
||||
TypedOp::Add(typed) => self.add(typed.to_child()),
|
||||
TypedOp::InsertAfterId(id, typed) => self.insert_after_id(id, typed.to_child()),
|
||||
TypedOp::InsertBeforeId(id, typed) => self.insert_before_id(id, typed.to_child()),
|
||||
TypedOp::Prepend(typed) => self.prepend(typed.to_child()),
|
||||
TypedOp::Add(typed) => self.add(typed.into_child()),
|
||||
TypedOp::InsertAfterId(id, typed) => self.insert_after_id(id, typed.into_child()),
|
||||
TypedOp::InsertBeforeId(id, typed) => self.insert_before_id(id, typed.into_child()),
|
||||
TypedOp::Prepend(typed) => self.prepend(typed.into_child()),
|
||||
TypedOp::RemoveById(id) => self.remove_by_id(id),
|
||||
TypedOp::ReplaceById(id, typed) => self.replace_by_id(id, typed.to_child()),
|
||||
TypedOp::ReplaceById(id, typed) => self.replace_by_id(id, typed.into_child()),
|
||||
TypedOp::Reset => self.reset(),
|
||||
}
|
||||
}
|
||||
|
@ -174,6 +174,48 @@ impl Children {
|
|||
self
|
||||
}
|
||||
|
||||
// Children GETTERS ****************************************************************************
|
||||
|
||||
/// Devuelve el número de componentes hijo de la lista.
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
/// Indica si la lista está vacía.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
/// Devuelve el primer componente hijo con el identificador indicado, si existe.
|
||||
pub fn get_by_id(&self, id: impl AsRef<str>) -> Option<&Child> {
|
||||
let id = Some(id.as_ref());
|
||||
self.0.iter().find(|c| c.id().as_deref() == id)
|
||||
}
|
||||
|
||||
/// Devuelve un iterador sobre los componentes hijo con el identificador indicado.
|
||||
pub fn iter_by_id<'a>(&'a self, id: &'a str) -> impl Iterator<Item = &'a Child> + 'a {
|
||||
self.0.iter().filter(move |c| c.id().as_deref() == Some(id))
|
||||
}
|
||||
|
||||
/// Devuelve un iterador sobre los componentes hijo con el identificador de tipo ([`UniqueId`])
|
||||
/// indicado.
|
||||
pub fn iter_by_type_id(&self, type_id: UniqueId) -> impl Iterator<Item = &Child> {
|
||||
self.0.iter().filter(move |&c| c.type_id() == type_id)
|
||||
}
|
||||
|
||||
// Children RENDER *****************************************************************************
|
||||
|
||||
/// Renderiza todos los componentes hijo, en orden.
|
||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||
html! {
|
||||
@for c in &self.0 {
|
||||
(c.render(cx))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Children HELPERS ****************************************************************************
|
||||
|
||||
// Inserta un hijo después del componente con el `id` dado, o al final si no se encuentra.
|
||||
#[inline]
|
||||
fn insert_after_id(&mut self, id: impl AsRef<str>, child: Child) -> &mut Self {
|
||||
|
@ -232,46 +274,6 @@ impl Children {
|
|||
self.0.clear();
|
||||
self
|
||||
}
|
||||
|
||||
// Children GETTERS ****************************************************************************
|
||||
|
||||
/// Devuelve el número de componentes hijo de la lista.
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
/// Indica si la lista está vacía.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
/// Devuelve el primer componente hijo con el identificador indicado, si existe.
|
||||
pub fn get_by_id(&self, id: impl AsRef<str>) -> Option<&Child> {
|
||||
let id = Some(id.as_ref());
|
||||
self.0.iter().find(|c| c.id().as_deref() == id)
|
||||
}
|
||||
|
||||
/// Devuelve un iterador sobre los componentes hijo con el identificador indicado.
|
||||
pub fn iter_by_id<'a>(&'a self, id: &'a str) -> impl Iterator<Item = &'a Child> + 'a {
|
||||
self.0.iter().filter(move |c| c.id().as_deref() == Some(id))
|
||||
}
|
||||
|
||||
/// Devuelve un iterador sobre los componentes hijo con el identificador tipo ([`UniqueId`])
|
||||
/// indicado.
|
||||
pub fn iter_by_type_id(&self, type_id: UniqueId) -> impl Iterator<Item = &Child> {
|
||||
self.0.iter().filter(move |&c| c.type_id() == type_id)
|
||||
}
|
||||
|
||||
// Children RENDER *****************************************************************************
|
||||
|
||||
/// Renderiza todos los componentes hijo, en orden.
|
||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||
html! {
|
||||
@for c in &self.0 {
|
||||
(c.render(cx))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for Children {
|
||||
|
|
|
@ -22,3 +22,6 @@ pub(crate) use regions::ChildrenInRegions;
|
|||
pub use regions::InRegion;
|
||||
|
||||
pub(crate) mod all;
|
||||
|
||||
/// Nombre de la región por defecto: `content`.
|
||||
pub const CONTENT_REGION_NAME: &str = "content";
|
||||
|
|
|
@ -20,8 +20,8 @@ pub static DEFAULT_THEME: LazyLock<ThemeRef> =
|
|||
// TEMA POR NOMBRE *********************************************************************************
|
||||
|
||||
/// Devuelve el tema identificado por su [`short_name`](AnyInfo::short_name).
|
||||
pub fn theme_by_short_name(short_name: impl AsRef<str>) -> Option<ThemeRef> {
|
||||
let short_name = short_name.as_ref().to_lowercase();
|
||||
pub fn theme_by_short_name(short_name: &'static str) -> Option<ThemeRef> {
|
||||
let short_name = short_name.to_lowercase();
|
||||
match THEMES
|
||||
.read()
|
||||
.iter()
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
use crate::core::extension::ExtensionTrait;
|
||||
use crate::core::theme::CONTENT_REGION_NAME;
|
||||
use crate::global;
|
||||
use crate::html::{html, Markup};
|
||||
use crate::locale::L10n;
|
||||
use crate::response::page::Page;
|
||||
|
||||
/// Representa una referencia a un tema.
|
||||
///
|
||||
|
@ -30,6 +34,66 @@ pub type ThemeRef = &'static dyn ThemeTrait;
|
|||
/// ```
|
||||
pub trait ThemeTrait: ExtensionTrait + Send + Sync {
|
||||
fn regions(&self) -> Vec<(&'static str, L10n)> {
|
||||
vec![("content", L10n::l("content"))]
|
||||
vec![(CONTENT_REGION_NAME, L10n::l("content"))]
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
fn before_render_page_body(&self, page: &mut Page) {}
|
||||
|
||||
fn render_page_body(&self, page: &mut Page) -> Markup {
|
||||
html! {
|
||||
body id=[page.body_id().get()] class=[page.body_classes().get()] {
|
||||
@for (region_name, _) in self.regions() {
|
||||
@let output = page.render_region(region_name);
|
||||
@if !output.is_empty() {
|
||||
div id=(region_name) class={ "region-container region-" (region_name) } {
|
||||
(output)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
fn after_render_page_body(&self, page: &mut Page) {}
|
||||
|
||||
fn render_page_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() {
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn error403(&self, _page: &mut Page) -> Markup {
|
||||
html! { div { h1 { ("FORBIDDEN ACCESS") } } }
|
||||
}
|
||||
|
||||
fn error404(&self, _page: &mut Page) -> Markup {
|
||||
html! { div { h1 { ("RESOURCE NOT FOUND") } } }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::core::component::{Child, ChildOp, Children};
|
||||
use crate::core::theme::ThemeRef;
|
||||
use crate::core::theme::{ThemeRef, CONTENT_REGION_NAME};
|
||||
use crate::{builder_fn, AutoDefault, UniqueId};
|
||||
|
||||
use parking_lot::RwLock;
|
||||
|
@ -7,45 +7,43 @@ use parking_lot::RwLock;
|
|||
use std::collections::HashMap;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
// Regiones globales con componentes para un tema dado.
|
||||
static THEME_REGIONS: LazyLock<RwLock<HashMap<UniqueId, ChildrenInRegions>>> =
|
||||
LazyLock::new(|| RwLock::new(HashMap::new()));
|
||||
|
||||
// Regiones globales con componentes para cualquier tema.
|
||||
static COMMON_REGIONS: LazyLock<RwLock<ChildrenInRegions>> =
|
||||
LazyLock::new(|| RwLock::new(ChildrenInRegions::default()));
|
||||
|
||||
// Estructura interna para mantener los componentes de cada región dada.
|
||||
// Estructura interna para mantener los componentes de una región.
|
||||
#[derive(AutoDefault)]
|
||||
pub struct ChildrenInRegions(HashMap<&'static str, Children>);
|
||||
|
||||
impl ChildrenInRegions {
|
||||
pub fn new() -> Self {
|
||||
ChildrenInRegions::default()
|
||||
}
|
||||
|
||||
pub fn with(region_id: &'static str, child: Child) -> Self {
|
||||
ChildrenInRegions::default().with_in_region(region_id, ChildOp::Add(child))
|
||||
pub fn with(region_name: &'static str, child: Child) -> Self {
|
||||
ChildrenInRegions::default().with_child_in_region(region_name, ChildOp::Add(child))
|
||||
}
|
||||
|
||||
#[builder_fn]
|
||||
pub fn with_in_region(mut self, region_id: &'static str, op: ChildOp) -> Self {
|
||||
if let Some(region) = self.0.get_mut(region_id) {
|
||||
pub fn with_child_in_region(mut self, region_name: &'static str, op: ChildOp) -> Self {
|
||||
if let Some(region) = self.0.get_mut(region_name) {
|
||||
region.alter_child(op);
|
||||
} else {
|
||||
self.0.insert(region_id, Children::new().with_child(op));
|
||||
self.0.insert(region_name, Children::new().with_child(op));
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn all_in_region(&self, theme: ThemeRef, region_id: &str) -> Children {
|
||||
pub fn merge_all_components(&self, theme_ref: ThemeRef, region_name: &'static str) -> Children {
|
||||
let common = COMMON_REGIONS.read();
|
||||
if let Some(r) = THEME_REGIONS.read().get(&theme.type_id()) {
|
||||
if let Some(r) = THEME_REGIONS.read().get(&theme_ref.type_id()) {
|
||||
Children::merge(&[
|
||||
common.0.get(region_id),
|
||||
self.0.get(region_id),
|
||||
r.0.get(region_id),
|
||||
common.0.get(region_name),
|
||||
self.0.get(region_name),
|
||||
r.0.get(region_name),
|
||||
])
|
||||
} else {
|
||||
Children::merge(&[common.0.get(region_id), self.0.get(region_id)])
|
||||
Children::merge(&[common.0.get(region_name), self.0.get(region_name)])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -90,19 +88,22 @@ impl InRegion {
|
|||
InRegion::Content => {
|
||||
COMMON_REGIONS
|
||||
.write()
|
||||
.alter_in_region("region-content", ChildOp::Add(child));
|
||||
.alter_child_in_region(CONTENT_REGION_NAME, ChildOp::Add(child));
|
||||
}
|
||||
InRegion::Named(name) => {
|
||||
COMMON_REGIONS
|
||||
.write()
|
||||
.alter_in_region(name, ChildOp::Add(child));
|
||||
.alter_child_in_region(name, ChildOp::Add(child));
|
||||
}
|
||||
InRegion::OfTheme(region, theme) => {
|
||||
InRegion::OfTheme(region_name, theme_ref) => {
|
||||
let mut regions = THEME_REGIONS.write();
|
||||
if let Some(r) = regions.get_mut(&theme.type_id()) {
|
||||
r.alter_in_region(region, ChildOp::Add(child));
|
||||
if let Some(r) = regions.get_mut(&theme_ref.type_id()) {
|
||||
r.alter_child_in_region(region_name, ChildOp::Add(child));
|
||||
} else {
|
||||
regions.insert(theme.type_id(), ChildrenInRegions::with(region, child));
|
||||
regions.insert(
|
||||
theme_ref.type_id(),
|
||||
ChildrenInRegions::with(region_name, child),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue