✨ 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
|
@ -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