Añade nuevo componente Region

This commit is contained in:
Manuel Cillero 2025-01-04 08:37:32 +01:00
parent 190b685431
commit 970cb3a0b1
6 changed files with 95 additions and 20 deletions

View file

@ -9,3 +9,6 @@ pub use error403::Error403;
mod error404;
pub use error404::Error404;
mod region;
pub use region::Region;

View file

@ -0,0 +1,64 @@
use crate::prelude::*;
#[rustfmt::skip]
#[derive(AutoDefault)]
pub struct Region {
id : OptionId,
classes: OptionClasses,
}
impl ComponentTrait for Region {
fn new() -> Self {
Region::default()
}
fn id(&self) -> Option<String> {
self.id.get()
}
fn setup_before_prepare(&mut self, _cx: &mut Context) {
self.alter_classes(ClassesOp::Prepend, "region-container");
}
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
let output = if let Some(id) = self.id() {
cx.render_region(id)
} else {
html! {}
};
if output.is_empty() {
return PrepareMarkup::None;
}
PrepareMarkup::With(html! {
div id=[self.id()] class=[self.classes().get()] {
(output)
}
})
}
}
impl Region {
pub fn of(id: impl Into<String>) -> Self {
Region::default().with_id(id)
}
// Region BUILDER.
#[fn_builder]
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id.alter_value(id);
self
}
#[fn_builder]
pub fn with_classes(mut self, op: ClassesOp, classes: impl Into<String>) -> Self {
self.classes.alter_value(op, classes);
self
}
// Region GETTERS.
fn classes(&self) -> &OptionClasses {
&self.classes
}
}

View file

@ -115,8 +115,8 @@ impl Context {
self
}
pub fn alter_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
self.regions.alter_in_region(region, op);
pub fn alter_in_region(&mut self, region_id: &'static str, op: ChildOp) -> &mut Self {
self.regions.alter_in_region(region_id, op);
self
}
@ -170,9 +170,9 @@ impl Context {
}
}
pub fn render_region(&mut self, region: impl Into<String>) -> Markup {
pub fn render_region(&mut self, region_id: impl Into<String>) -> Markup {
self.regions
.all_in_region(self.theme, &region.into())
.all_in_region(self.theme, &region_id.into())
.render(self)
}

View file

@ -1,3 +1,5 @@
use crate::base::component::Region;
use crate::core::component::ComponentBase;
use crate::core::package::PackageTrait;
use crate::global;
use crate::html::{html, Markup};
@ -9,7 +11,7 @@ pub type ThemeRef = &'static dyn ThemeTrait;
/// Los temas deben implementar este "trait".
pub trait ThemeTrait: PackageTrait + Send + Sync {
fn regions(&self) -> Vec<(&'static str, L10n)> {
vec![("content", L10n::l("content"))]
vec![("region-content", L10n::l("content"))]
}
#[allow(unused_variables)]
@ -18,8 +20,8 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
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() {
(page.context().render_region(region_name))
@for (region_id, _) in self.regions() {
(Region::of(region_id).render(page.context()))
}
}
}

View file

@ -19,26 +19,30 @@ impl ChildrenInRegions {
ChildrenInRegions::default()
}
pub fn with(region: &'static str, child: ChildComponent) -> Self {
ChildrenInRegions::default().with_in_region(region, ChildOp::Add(child))
pub fn with(region_id: &'static str, child: ChildComponent) -> Self {
ChildrenInRegions::default().with_in_region(region_id, ChildOp::Add(child))
}
#[fn_builder]
pub fn with_in_region(mut self, region: &'static str, op: ChildOp) -> Self {
if let Some(region) = self.0.get_mut(region) {
pub fn with_in_region(mut self, region_id: &'static str, op: ChildOp) -> Self {
if let Some(region) = self.0.get_mut(region_id) {
region.alter_child(op);
} else {
self.0.insert(region, Children::new().with_child(op));
self.0.insert(region_id, Children::new().with_child(op));
}
self
}
pub fn all_in_region(&self, theme: ThemeRef, region: &str) -> Children {
pub fn all_in_region(&self, theme: ThemeRef, region_id: &str) -> Children {
let common = COMMON_REGIONS.read().unwrap();
if let Some(r) = THEME_REGIONS.read().unwrap().get(&theme.type_id()) {
Children::merge(&[common.0.get(region), self.0.get(region), r.0.get(region)])
Children::merge(&[
common.0.get(region_id),
self.0.get(region_id),
r.0.get(region_id),
])
} else {
Children::merge(&[common.0.get(region), self.0.get(region)])
Children::merge(&[common.0.get(region_id), self.0.get(region_id)])
}
}
}
@ -56,7 +60,7 @@ impl InRegion {
COMMON_REGIONS
.write()
.unwrap()
.alter_in_region("content", ChildOp::Add(child));
.alter_in_region("region-content", ChildOp::Add(child));
}
InRegion::Named(name) => {
COMMON_REGIONS

View file

@ -96,14 +96,16 @@ impl Page {
}
#[fn_builder]
pub fn with_in_region(mut self, region: &'static str, op: ChildOp) -> Self {
self.context.alter_in_region(region, op);
pub fn with_in_region(mut self, region_id: &'static str, op: ChildOp) -> Self {
self.context.alter_in_region(region_id, op);
self
}
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.context
.alter_in_region("content", ChildOp::Add(ChildComponent::with(component)));
self.context.alter_in_region(
"region-content",
ChildOp::Add(ChildComponent::with(component)),
);
self
}