diff --git a/packages/pagetop-admin/src/summary.rs b/packages/pagetop-admin/src/summary.rs index 70dd5088..fd8a5e1c 100644 --- a/packages/pagetop-admin/src/summary.rs +++ b/packages/pagetop-admin/src/summary.rs @@ -153,6 +153,7 @@ pub async fn summary(request: HttpRequest) -> ResultPage { Page::new(request) //.with_context(ContextOp::Theme("Bootsier")) .with_title(L10n::n("Admin")) + .with_template("admin") .with_component_in("top-menu", side_menu) .with_component( flex::Container::new() @@ -160,6 +161,5 @@ pub async fn summary(request: HttpRequest) -> ResultPage { .add_item(flex::Item::with(top_menu)) .add_item(flex::Item::with(Html::with(html! { p { "Columna 3"} }))), ) - .with_template("admin") .render() } diff --git a/src/base/component/block.rs b/src/base/component/block.rs index 6067c18f..056a5cad 100644 --- a/src/base/component/block.rs +++ b/src/base/component/block.rs @@ -76,18 +76,18 @@ impl Block { self } - #[rustfmt::skip] - pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); - self - } - #[fn_builder] pub fn alter_components(&mut self, op: MixedOp) -> &mut Self { self.mixed.alter_value(op); self } + #[rustfmt::skip] + pub fn add_component(mut self, component: impl ComponentTrait) -> Self { + self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); + self + } + // Block GETTERS. pub fn title(&self) -> &OptionTranslated { diff --git a/src/base/component/flex/item.rs b/src/base/component/flex/item.rs index d4b4e165..ff877b91 100644 --- a/src/base/component/flex/item.rs +++ b/src/base/component/flex/item.rs @@ -117,18 +117,18 @@ impl Item { self } - #[rustfmt::skip] - pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); - self - } - #[fn_builder] pub fn alter_components(&mut self, op: MixedOp) -> &mut Self { self.mixed.alter_value(op); self } + #[rustfmt::skip] + pub fn add_component(mut self, component: impl ComponentTrait) -> Self { + self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); + self + } + // Item GETTERS. pub fn grow(&self) -> &flex::ItemGrow { diff --git a/src/base/component/paragraph.rs b/src/base/component/paragraph.rs index 322257f6..b31eb8fe 100644 --- a/src/base/component/paragraph.rs +++ b/src/base/component/paragraph.rs @@ -79,18 +79,18 @@ impl Paragraph { self } - #[rustfmt::skip] - pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); - self - } - #[fn_builder] pub fn alter_components(&mut self, op: MixedOp) -> &mut Self { self.mixed.alter_value(op); self } + #[rustfmt::skip] + pub fn add_component(mut self, component: impl ComponentTrait) -> Self { + self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); + self + } + // Paragraph GETTERS. pub fn font_size(&self) -> &FontSize { diff --git a/src/base/component/wrapper.rs b/src/base/component/wrapper.rs index 86c5d07a..c26df2b9 100644 --- a/src/base/component/wrapper.rs +++ b/src/base/component/wrapper.rs @@ -124,18 +124,18 @@ impl Wrapper { self } - #[rustfmt::skip] - pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); - self - } - #[fn_builder] pub fn alter_components(&mut self, op: MixedOp) -> &mut Self { self.mixed.alter_value(op); self } + #[rustfmt::skip] + pub fn add_component(mut self, component: impl ComponentTrait) -> Self { + self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); + self + } + // Wrapper GETTERS. pub fn wrapper_type(&self) -> &WrapperType { diff --git a/src/core/component/arc_mixed.rs b/src/core/component/arc_mixed.rs index 1cfd4ce6..6ed978c6 100644 --- a/src/core/component/arc_mixed.rs +++ b/src/core/component/arc_mixed.rs @@ -65,7 +65,11 @@ pub enum MixedOp { pub struct MixedComponents(Vec); impl MixedComponents { - pub fn new(any: AnyComponent) -> Self { + pub fn new() -> Self { + MixedComponents::default() + } + + pub fn with(any: AnyComponent) -> Self { MixedComponents::default().with_value(MixedOp::Add(any)) } diff --git a/src/core/theme/regions.rs b/src/core/theme/regions.rs index 618727a0..09921ef4 100644 --- a/src/core/theme/regions.rs +++ b/src/core/theme/regions.rs @@ -1,6 +1,6 @@ use crate::core::component::{AnyComponent, MixedComponents, MixedOp}; use crate::core::theme::ThemeRef; -use crate::{AutoDefault, LazyStatic, TypeId}; +use crate::{fn_builder, AutoDefault, LazyStatic, TypeId}; use std::collections::HashMap; use std::sync::RwLock; @@ -16,28 +16,20 @@ pub struct ComponentsInRegions(HashMap<&'static str, MixedComponents>); impl ComponentsInRegions { pub fn new(region: &'static str, any: AnyComponent) -> Self { - let mut regions = ComponentsInRegions::default(); - regions.add_in(region, any); - regions + ComponentsInRegions::default().with_components(region, MixedOp::Add(any)) } - pub fn add(&mut self, any: AnyComponent) { - if let Some(region) = self.0.get_mut("content") { - region.alter_value(MixedOp::Add(any)); - } else { - self.0.insert("content", MixedComponents::new(any)); - } - } - - pub fn add_in(&mut self, region: &'static str, any: AnyComponent) { + #[fn_builder] + pub fn alter_components(&mut self, region: &'static str, op: MixedOp) -> &mut Self { if let Some(region) = self.0.get_mut(region) { - region.alter_value(MixedOp::Add(any)); + region.alter_value(op); } else { - self.0.insert(region, MixedComponents::new(any)); + self.0.insert(region, MixedComponents::new().with_value(op)); } + self } - pub fn get_components(&self, theme: ThemeRef, region: &str) -> MixedComponents { + pub fn all_components(&self, theme: ThemeRef, region: &str) -> MixedComponents { let common = COMMON_REGIONS.read().unwrap(); if let Some(r) = THEME_REGIONS.read().unwrap().get(&theme.type_id()) { MixedComponents::merge(&[common.0.get(region), self.0.get(region), r.0.get(region)]) @@ -57,15 +49,21 @@ impl InRegion { pub fn add(&self, any: AnyComponent) -> &Self { match self { InRegion::Content => { - COMMON_REGIONS.write().unwrap().add(any); + COMMON_REGIONS + .write() + .unwrap() + .alter_components("content", MixedOp::Add(any)); } InRegion::Named(name) => { - COMMON_REGIONS.write().unwrap().add_in(name, any); + COMMON_REGIONS + .write() + .unwrap() + .alter_components(name, MixedOp::Add(any)); } InRegion::OfTheme(region, theme) => { let mut regions = THEME_REGIONS.write().unwrap(); if let Some(r) = regions.get_mut(&theme.type_id()) { - r.add_in(region, any); + r.alter_components(region, MixedOp::Add(any)); } else { regions.insert(theme.type_id(), ComponentsInRegions::new(region, any)); } diff --git a/src/response/page.rs b/src/response/page.rs index 1c1dc8f6..f3b1b0c3 100644 --- a/src/response/page.rs +++ b/src/response/page.rs @@ -4,7 +4,7 @@ pub use error::ErrorPage; pub use actix_web::Result as ResultPage; use crate::base::action; -use crate::core::component::{AnyComponent, ComponentTrait, MixedComponents}; +use crate::core::component::{AnyComponent, ComponentTrait, MixedComponents, MixedOp}; use crate::core::component::{Context, ContextOp}; use crate::core::theme::ComponentsInRegions; use crate::fn_builder; @@ -26,8 +26,8 @@ pub struct Page { body_id : OptionId, body_classes: OptionClasses, skip_to : OptionId, - regions : ComponentsInRegions, template : String, + regions : ComponentsInRegions, } impl Page { @@ -104,28 +104,34 @@ impl Page { self } - #[fn_builder] - pub fn alter_component(&mut self, component: impl ComponentTrait) -> &mut Self { - self.regions.add(AnyComponent::with(component)); - self - } - - #[fn_builder] - pub fn alter_component_in( - &mut self, - region: &'static str, - component: impl ComponentTrait, - ) -> &mut Self { - self.regions.add_in(region, AnyComponent::with(component)); - self - } - #[fn_builder] pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } + #[fn_builder] + pub fn alter_regions(&mut self, region: &'static str, op: MixedOp) -> &mut Self { + self.regions.alter_components(region, op); + self + } + + pub fn with_component(mut self, component: impl ComponentTrait) -> Self { + self.regions + .alter_components("content", MixedOp::Add(AnyComponent::with(component))); + self + } + + pub fn with_component_in( + mut self, + region: &'static str, + component: impl ComponentTrait, + ) -> Self { + self.regions + .alter_components(region, MixedOp::Add(AnyComponent::with(component))); + self + } + // Page GETTERS. pub fn title(&mut self) -> Option { @@ -164,14 +170,14 @@ impl Page { &self.skip_to } - pub fn components_in(&self, region: &str) -> MixedComponents { - self.regions.get_components(self.context.theme(), region) - } - pub fn template(&self) -> &str { self.template.as_str() } + pub fn components_in(&self, region: &str) -> MixedComponents { + self.regions.all_components(self.context.theme(), region) + } + // Page RENDER. pub fn render(&mut self) -> ResultPage { diff --git a/src/response/page/error.rs b/src/response/page/error.rs index 841a7c68..4fad9540 100644 --- a/src/response/page/error.rs +++ b/src/response/page/error.rs @@ -31,8 +31,8 @@ impl fmt::Display for ErrorPage { let error_page = Page::new(request.clone()); if let Ok(page) = error_page .with_title(L10n::n("Error FORBIDDEN")) - .with_component(Error403) .with_template("error") + .with_component(Error403) .render() { write!(f, "{}", page.into_string()) @@ -45,8 +45,8 @@ impl fmt::Display for ErrorPage { let error_page = Page::new(request.clone()); if let Ok(page) = error_page .with_title(L10n::n("Error RESOURCE NOT FOUND")) - .with_component(Error404) .with_template("error") + .with_component(Error404) .render() { write!(f, "{}", page.into_string())