🚚 Rename Any ops to ChildComponent for Children

This commit is contained in:
Manuel Cillero 2024-12-01 08:53:37 +01:00
parent 556c9159d3
commit 0a61270f22
10 changed files with 75 additions and 140 deletions

View file

@ -10,5 +10,5 @@ pub use classes::{ComponentClasses, ComponentClassesOp};
mod children;
pub use children::Children;
pub use children::{AnyComponent, AnyOp};
pub use children::{ChildComponent, ChildOp};
pub use children::{TypedComponent, TypedOp};

View file

@ -5,20 +5,20 @@ use crate::{fn_builder, TypeId};
use std::sync::{Arc, RwLock};
#[derive(Clone)]
pub struct AnyComponent(Arc<RwLock<dyn ComponentTrait>>);
pub struct ChildComponent(Arc<RwLock<dyn ComponentTrait>>);
impl AnyComponent {
impl ChildComponent {
pub fn with(component: impl ComponentTrait) -> Self {
AnyComponent(Arc::new(RwLock::new(component)))
ChildComponent(Arc::new(RwLock::new(component)))
}
// AnyComponent RENDER.
// ChildComponent RENDER.
pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx)
}
// AnyComponent HELPERS.
// ChildComponent HELPERS.
fn type_id(&self) -> TypeId {
self.0.read().unwrap().type_id()
@ -52,20 +52,20 @@ impl<C: ComponentTrait> TypedComponent<C> {
// TypedComponent HELPERS.
fn to_any(&self) -> AnyComponent {
AnyComponent(self.0.clone())
fn to_child(&self) -> ChildComponent {
ChildComponent(self.0.clone())
}
}
// *************************************************************************************************
pub enum AnyOp {
Add(AnyComponent),
InsertAfterId(&'static str, AnyComponent),
InsertBeforeId(&'static str, AnyComponent),
Prepend(AnyComponent),
pub enum ChildOp {
Add(ChildComponent),
InsertAfterId(&'static str, ChildComponent),
InsertBeforeId(&'static str, ChildComponent),
Prepend(ChildComponent),
RemoveById(&'static str),
ReplaceById(&'static str, AnyComponent),
ReplaceById(&'static str, ChildComponent),
Reset,
}
@ -80,15 +80,15 @@ pub enum TypedOp<C: ComponentTrait> {
}
#[derive(Clone, Default)]
pub struct Children(Vec<AnyComponent>);
pub struct Children(Vec<ChildComponent>);
impl Children {
pub fn new() -> Self {
Children::default()
}
pub fn with(any: AnyComponent) -> Self {
Children::default().with_value(AnyOp::Add(any))
pub fn with(child: ChildComponent) -> Self {
Children::default().with_value(ChildOp::Add(child))
}
pub(crate) fn merge(mixes: &[Option<&Children>]) -> Self {
@ -102,15 +102,15 @@ impl Children {
// Children BUILDER.
#[fn_builder]
pub fn set_value(&mut self, op: AnyOp) -> &mut Self {
pub fn set_value(&mut self, op: ChildOp) -> &mut Self {
match op {
AnyOp::Add(any) => self.add(any),
AnyOp::InsertAfterId(id, any) => self.insert_after_id(id, any),
AnyOp::InsertBeforeId(id, any) => self.insert_before_id(id, any),
AnyOp::Prepend(any) => self.prepend(any),
AnyOp::RemoveById(id) => self.remove_by_id(id),
AnyOp::ReplaceById(id, any) => self.replace_by_id(id, any),
AnyOp::Reset => self.reset(),
ChildOp::Add(any) => self.add(any),
ChildOp::InsertAfterId(id, any) => self.insert_after_id(id, any),
ChildOp::InsertBeforeId(id, any) => self.insert_before_id(id, any),
ChildOp::Prepend(any) => self.prepend(any),
ChildOp::RemoveById(id) => self.remove_by_id(id),
ChildOp::ReplaceById(id, any) => self.replace_by_id(id, any),
ChildOp::Reset => self.reset(),
};
self
}
@ -118,41 +118,41 @@ impl Children {
#[fn_builder]
pub fn set_typed<C: ComponentTrait + Default>(&mut self, op: TypedOp<C>) -> &mut Self {
match op {
TypedOp::Add(typed) => self.add(typed.to_any()),
TypedOp::InsertAfterId(id, typed) => self.insert_after_id(id, typed.to_any()),
TypedOp::InsertBeforeId(id, typed) => self.insert_before_id(id, typed.to_any()),
TypedOp::Prepend(typed) => self.prepend(typed.to_any()),
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::RemoveById(id) => self.remove_by_id(id),
TypedOp::ReplaceById(id, typed) => self.replace_by_id(id, typed.to_any()),
TypedOp::ReplaceById(id, typed) => self.replace_by_id(id, typed.to_child()),
TypedOp::Reset => self.reset(),
};
self
}
#[inline]
fn add(&mut self, any: AnyComponent) {
self.0.push(any);
fn add(&mut self, child: ChildComponent) {
self.0.push(child);
}
#[inline]
fn insert_after_id(&mut self, id: &str, any: AnyComponent) {
fn insert_after_id(&mut self, id: &str, child: ChildComponent) {
match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index + 1, any),
_ => self.0.push(any),
Some(index) => self.0.insert(index + 1, child),
_ => self.0.push(child),
};
}
#[inline]
fn insert_before_id(&mut self, id: &str, any: AnyComponent) {
fn insert_before_id(&mut self, id: &str, child: ChildComponent) {
match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index, any),
_ => self.0.insert(0, any),
Some(index) => self.0.insert(index, child),
_ => self.0.insert(0, child),
};
}
#[inline]
fn prepend(&mut self, any: AnyComponent) {
self.0.insert(0, any);
fn prepend(&mut self, child: ChildComponent) {
self.0.insert(0, child);
}
#[inline]
@ -163,10 +163,10 @@ impl Children {
}
#[inline]
fn replace_by_id(&mut self, id: &str, any: AnyComponent) {
fn replace_by_id(&mut self, id: &str, child: ChildComponent) {
for c in &mut self.0 {
if c.id() == id {
*c = any;
*c = child;
break;
}
}
@ -187,17 +187,17 @@ impl Children {
self.0.is_empty()
}
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&AnyComponent> {
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&ChildComponent> {
let id = id.into();
self.0.iter().find(|c| c.id() == id)
}
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &AnyComponent> {
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &ChildComponent> {
let id = id.into();
self.0.iter().filter(move |&c| c.id() == id)
}
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &AnyComponent> {
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &ChildComponent> {
self.0.iter().filter(move |&c| c.type_id() == type_id)
}

View file

@ -1,5 +1,5 @@
use crate::concat_string;
use crate::core::component::AnyOp;
use crate::core::component::ChildOp;
use crate::core::theme::all::{theme_by_short_name, DEFAULT_THEME};
use crate::core::theme::{ChildrenInRegions, ThemeRef};
use crate::html::{html, Markup};
@ -115,7 +115,7 @@ impl Context {
self
}
pub fn set_in_region(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
pub fn set_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
self.regions.set_in_region(region, op);
self
}
@ -168,7 +168,7 @@ impl Context {
pub fn prepare_region(&mut self, region: impl Into<String>) -> Markup {
self.regions
.all_components(self.theme, region.into().as_str())
.all_in_region(self.theme, region.into().as_str())
.render(self)
}

View file

@ -9,23 +9,9 @@ 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![]
vec![("content", L10n::l("content"))]
}
#[allow(unused_variables)]
fn before_prepare_body(&self, page: &mut Page) {}
fn prepare_body(&self, page: &mut Page) -> PrepareMarkup {
PrepareMarkup::With(html! {
body id=[page.body_id().get()] class=[page.body_classes().get()] {
(page.context().prepare_region("content"))
}
})
}
#[allow(unused_variables)]
fn after_prepare_body(&self, page: &mut Page) {}
fn prepare_head(&self, page: &mut Page) -> PrepareMarkup {
let viewport = "width=device-width, initial-scale=1, shrink-to-fit=no";
PrepareMarkup::With(html! {
@ -56,33 +42,18 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
}
})
}
/*
fn prepare_page_body(&self, page: &mut Page) -> PrepareMarkup {
#[allow(unused_variables)]
fn before_prepare_body(&self, page: &mut Page) {}
fn prepare_body(&self, page: &mut Page) -> PrepareMarkup {
PrepareMarkup::With(html! {
body id=[page.body_id().get()] class=[page.body_classes().get()] {
(page.body_content().render())
(page.context().prepare_region("content"))
}
})
}
fn error_403(&self, request: service::HttpRequest) -> Page {
Page::new(request)
.with_title(L10n::n("Error FORBIDDEN"))
.with_body(PrepareMarkup::With(html! {
div {
h1 { ("FORBIDDEN ACCESS") }
}
}))
}
fn error_404(&self, request: service::HttpRequest) -> Page {
Page::new(request)
.with_title(L10n::n("Error RESOURCE NOT FOUND"))
.with_body(PrepareMarkup::With(html! {
div {
h1 { ("RESOURCE NOT FOUND") }
}
}))
}
*/
#[allow(unused_variables)]
fn after_prepare_body(&self, page: &mut Page) {}
}

View file

@ -1,4 +1,4 @@
use crate::core::component::{AnyComponent, AnyOp, Children};
use crate::core::component::{ChildComponent, ChildOp, Children};
use crate::core::theme::ThemeRef;
use crate::{fn_builder, AutoDefault, TypeId};
@ -15,12 +15,16 @@ static COMMON_REGIONS: LazyLock<RwLock<ChildrenInRegions>> =
pub struct ChildrenInRegions(HashMap<&'static str, Children>);
impl ChildrenInRegions {
pub fn new(region: &'static str, any: AnyComponent) -> Self {
ChildrenInRegions::default().with_in_region(region, AnyOp::Add(any))
pub fn new() -> Self {
ChildrenInRegions::default()
}
pub fn with(region: &'static str, child: ChildComponent) -> Self {
ChildrenInRegions::default().with_in_region(region, ChildOp::Add(child))
}
#[fn_builder]
pub fn set_in_region(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
pub fn set_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
if let Some(region) = self.0.get_mut(region) {
region.set_value(op);
} else {
@ -29,7 +33,7 @@ impl ChildrenInRegions {
self
}
pub fn all_components(&self, theme: ThemeRef, region: &str) -> Children {
pub fn all_in_region(&self, theme: ThemeRef, region: &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)])
@ -46,26 +50,26 @@ pub enum InRegion {
}
impl InRegion {
pub fn add(&self, any: AnyComponent) -> &Self {
pub fn add(&self, child: ChildComponent) -> &Self {
match self {
InRegion::Content => {
COMMON_REGIONS
.write()
.unwrap()
.set_in_region("content", AnyOp::Add(any));
.set_in_region("content", ChildOp::Add(child));
}
InRegion::Named(name) => {
COMMON_REGIONS
.write()
.unwrap()
.set_in_region(name, AnyOp::Add(any));
.set_in_region(name, ChildOp::Add(child));
}
InRegion::OfTheme(region, theme) => {
let mut regions = THEME_REGIONS.write().unwrap();
if let Some(r) = regions.get_mut(&theme.type_id()) {
r.set_in_region(region, AnyOp::Add(any));
r.set_in_region(region, ChildOp::Add(child));
} else {
regions.insert(theme.type_id(), ChildrenInRegions::new(region, any));
regions.insert(theme.type_id(), ChildrenInRegions::with(region, child));
}
}
}

View file

@ -1,13 +0,0 @@
# Branding component.
site_home = Home
# PoweredBy component.
poweredby_pagetop = Powered by {$pagetop_link}
pagetop_logo = PageTop logo
# Menu component.
menu_toggle = Toggle menu visibility
# Form components.
button_submit = Submit
button_reset = Reset

View file

@ -1,8 +1 @@
header = Header
pagetop = Page Top
content = Content
sidebar_left = Sidebar Left
sidebar_right = Sidebar Right
footer = Footer
skip_to_content = Skip to main content (Press Enter)

View file

@ -1,13 +0,0 @@
# Branding component.
site_home = Inicio
# PoweredBy component.
poweredby_pagetop = Funciona con {$pagetop_link}
pagetop_logo = Logotipo de PageTop
# Menu component.
menu_toggle = Alternar visibilidad del menú
# Form components.
button_submit = Enviar
button_reset = Reiniciar

View file

@ -1,8 +1 @@
header = Cabecera
pagetop = Superior
content = Contenido
sidebar_left = Barra lateral izquierda
sidebar_right = Barra lateral derecha
footer = Pie
skip_to_content = Ir al contenido principal (Pulsar Intro)

View file

@ -4,8 +4,8 @@ pub use error::ErrorPage;
pub use actix_web::Result as ResultPage;
use crate::base::action;
use crate::core::component::{AnyComponent, AnyOp, ComponentTrait};
use crate::core::component::{AssetsOp, Context};
use crate::core::component::{ChildComponent, ChildOp, ComponentTrait};
use crate::fn_builder;
use crate::html::{html, Markup, DOCTYPE};
use crate::html::{ClassesOp, OptionClasses, OptionId, OptionTranslated};
@ -98,14 +98,14 @@ impl Page {
}
#[fn_builder]
pub fn set_in_region(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
pub fn set_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
self.context.set_in_region(region, op);
self
}
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.context
.set_in_region("content", AnyOp::Add(AnyComponent::with(component)));
.set_in_region("content", ChildOp::Add(ChildComponent::with(component)));
self
}
@ -115,7 +115,7 @@ impl Page {
component: impl ComponentTrait,
) -> Self {
self.context
.set_in_region(region, AnyOp::Add(AnyComponent::with(component)));
.set_in_region(region, ChildOp::Add(ChildComponent::with(component)));
self
}