From 0a61270f229b4285196941c11e66c1b3e12c615c Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sun, 1 Dec 2024 08:53:37 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=9A=20Rename=20Any=20ops=20to=20ChildC?= =?UTF-8?q?omponent=20for=20Children?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/component.rs | 2 +- src/core/component/children.rs | 88 +++++++++++++++++----------------- src/core/component/context.rs | 6 +-- src/core/theme/definition.rs | 47 ++++-------------- src/core/theme/regions.rs | 24 ++++++---- src/locale/en-US/base.ftl | 13 ----- src/locale/en-US/theme.ftl | 7 --- src/locale/es-ES/base.ftl | 13 ----- src/locale/es-ES/theme.ftl | 7 --- src/response/page.rs | 8 ++-- 10 files changed, 75 insertions(+), 140 deletions(-) delete mode 100644 src/locale/en-US/base.ftl delete mode 100644 src/locale/es-ES/base.ftl diff --git a/src/core/component.rs b/src/core/component.rs index 7fa670dd..d45e3f1f 100644 --- a/src/core/component.rs +++ b/src/core/component.rs @@ -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}; diff --git a/src/core/component/children.rs b/src/core/component/children.rs index 04215b2f..9a5db05b 100644 --- a/src/core/component/children.rs +++ b/src/core/component/children.rs @@ -5,20 +5,20 @@ use crate::{fn_builder, TypeId}; use std::sync::{Arc, RwLock}; #[derive(Clone)] -pub struct AnyComponent(Arc>); +pub struct ChildComponent(Arc>); -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 TypedComponent { // 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 { } #[derive(Clone, Default)] -pub struct Children(Vec); +pub struct Children(Vec); 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(&mut self, op: TypedOp) -> &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) -> Option<&AnyComponent> { + pub fn get_by_id(&self, id: impl Into) -> Option<&ChildComponent> { let id = id.into(); self.0.iter().find(|c| c.id() == id) } - pub fn iter_by_id(&self, id: impl Into) -> impl Iterator { + pub fn iter_by_id(&self, id: impl Into) -> impl Iterator { 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 { + pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator { self.0.iter().filter(move |&c| c.type_id() == type_id) } diff --git a/src/core/component/context.rs b/src/core/component/context.rs index 9fdfc632..70bf2859 100644 --- a/src/core/component/context.rs +++ b/src/core/component/context.rs @@ -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) -> Markup { self.regions - .all_components(self.theme, region.into().as_str()) + .all_in_region(self.theme, region.into().as_str()) .render(self) } diff --git a/src/core/theme/definition.rs b/src/core/theme/definition.rs index acd33093..4d567455 100644 --- a/src/core/theme/definition.rs +++ b/src/core/theme/definition.rs @@ -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) {} } diff --git a/src/core/theme/regions.rs b/src/core/theme/regions.rs index 17795ec1..7e8128c5 100644 --- a/src/core/theme/regions.rs +++ b/src/core/theme/regions.rs @@ -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> = 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)); } } } diff --git a/src/locale/en-US/base.ftl b/src/locale/en-US/base.ftl deleted file mode 100644 index 9ec4803b..00000000 --- a/src/locale/en-US/base.ftl +++ /dev/null @@ -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 diff --git a/src/locale/en-US/theme.ftl b/src/locale/en-US/theme.ftl index 6b3cb0e8..fd7f228d 100644 --- a/src/locale/en-US/theme.ftl +++ b/src/locale/en-US/theme.ftl @@ -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) diff --git a/src/locale/es-ES/base.ftl b/src/locale/es-ES/base.ftl deleted file mode 100644 index 953d891c..00000000 --- a/src/locale/es-ES/base.ftl +++ /dev/null @@ -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 diff --git a/src/locale/es-ES/theme.ftl b/src/locale/es-ES/theme.ftl index fb5caacd..c2026c6f 100644 --- a/src/locale/es-ES/theme.ftl +++ b/src/locale/es-ES/theme.ftl @@ -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) diff --git a/src/response/page.rs b/src/response/page.rs index 64856f37..c7e1309b 100644 --- a/src/response/page.rs +++ b/src/response/page.rs @@ -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 }