🧑💻 Improve regions and components API
This commit is contained in:
parent
496c9d375b
commit
3bbad7f6a4
9 changed files with 77 additions and 69 deletions
|
|
@ -153,6 +153,7 @@ pub async fn summary(request: HttpRequest) -> ResultPage<Markup, ErrorPage> {
|
|||
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<Markup, ErrorPage> {
|
|||
.add_item(flex::Item::with(top_menu))
|
||||
.add_item(flex::Item::with(Html::with(html! { p { "Columna 3"} }))),
|
||||
)
|
||||
.with_template("admin")
|
||||
.render()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -65,7 +65,11 @@ pub enum MixedOp {
|
|||
pub struct MixedComponents(Vec<AnyComponent>);
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<String> {
|
||||
|
|
@ -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<Markup, ErrorPage> {
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue