Modifica PageContainer para ser ComponentsHolder

This commit is contained in:
Manuel Cillero 2022-05-06 00:27:21 +02:00
parent c2b69fa539
commit d53408c6b6
11 changed files with 36 additions and 32 deletions

View file

@ -9,7 +9,7 @@ mod holder;
pub use holder::{ pub use holder::{
ActionItem, ActionItem,
}; };
pub(crate) use holder::{ use holder::{
ActionsHolder, ActionsHolder,
}; };

View file

@ -1,10 +1,10 @@
use crate::Lazy; use crate::Lazy;
use super::{ComponentTrait, PageContainer}; use super::{ComponentsHolder, ComponentTrait};
use std::sync::RwLock; use std::sync::RwLock;
use std::collections::HashMap; use std::collections::HashMap;
static COMPONENTS: Lazy<RwLock<HashMap<&str, PageContainer>>> = Lazy::new(|| { static COMPONENTS: Lazy<RwLock<HashMap<&str, ComponentsHolder>>> = Lazy::new(|| {
RwLock::new(HashMap::new()) RwLock::new(HashMap::new())
}); });
@ -13,10 +13,10 @@ pub fn add_component_to(region: &'static str, component: impl ComponentTrait) {
if let Some(regions) = hmap.get_mut(region) { if let Some(regions) = hmap.get_mut(region) {
regions.add(component); regions.add(component);
} else { } else {
hmap.insert(region, PageContainer::new_with(component)); hmap.insert(region, ComponentsHolder::new_with(component));
} }
} }
pub fn common_components() -> HashMap<&'static str, PageContainer> { pub fn common_components() -> HashMap<&'static str, ComponentsHolder> {
COMPONENTS.read().unwrap().clone() COMPONENTS.read().unwrap().clone()
} }

View file

@ -4,15 +4,15 @@ use super::{ComponentTrait, PageAssets};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
#[derive(Clone)] #[derive(Clone)]
pub struct PageContainer(Vec<Arc<RwLock<dyn ComponentTrait>>>); pub struct ComponentsHolder(Vec<Arc<RwLock<dyn ComponentTrait>>>);
impl PageContainer { impl ComponentsHolder {
pub fn new() -> Self { pub fn new() -> Self {
PageContainer(Vec::new()) ComponentsHolder(Vec::new())
} }
pub fn new_with(component: impl ComponentTrait) -> Self { pub fn new_with(component: impl ComponentTrait) -> Self {
let mut container = PageContainer::new(); let mut container = ComponentsHolder::new();
container.add(component); container.add(component);
container container
} }

View file

@ -20,10 +20,14 @@ pub use definition::{
component_ref, component_ref,
component_mut, component_mut,
}; };
use definition::render_component; use definition::{
render_component,
};
mod container; mod holder;
pub use container::PageContainer; pub use holder::{
ComponentsHolder,
};
mod all; mod all;
pub use all::{ pub use all::{

View file

@ -5,7 +5,7 @@ pub const TYPENAME_BLOCK: &str = "pagetop::base::component::block::Block";
pub struct Block { pub struct Block {
renderable: fn() -> bool, renderable: fn() -> bool,
weight : isize, weight : isize,
components: PageContainer, components: ComponentsHolder,
title : OptAttr, title : OptAttr,
id : OptIden, id : OptIden,
classes : Classes, classes : Classes,
@ -17,7 +17,7 @@ impl ComponentTrait for Block {
Block { Block {
renderable: render_always, renderable: render_always,
weight : 0, weight : 0,
components: PageContainer::new(), components: ComponentsHolder::new(),
title : OptAttr::new(), title : OptAttr::new(),
id : OptIden::new(), id : OptIden::new(),
classes : Classes::new_with_default("block"), classes : Classes::new_with_default("block"),
@ -66,7 +66,7 @@ impl Block {
self self
} }
pub fn components(&self) -> &PageContainer { pub fn components(&self) -> &ComponentsHolder {
&self.components &self.components
} }

View file

@ -7,7 +7,7 @@ pub enum ContainerType { Header, Footer, Main, Section, Wrapper }
pub struct Container { pub struct Container {
renderable : fn() -> bool, renderable : fn() -> bool,
weight : isize, weight : isize,
components : PageContainer, components : ComponentsHolder,
container : ContainerType, container : ContainerType,
id : OptIden, id : OptIden,
classes : Classes, classes : Classes,
@ -20,7 +20,7 @@ impl ComponentTrait for Container {
Container { Container {
renderable : render_always, renderable : render_always,
weight : 0, weight : 0,
components : PageContainer::new(), components : ComponentsHolder::new(),
container : ContainerType::Wrapper, container : ContainerType::Wrapper,
id : OptIden::new(), id : OptIden::new(),
classes : Classes::new_with_default("container"), classes : Classes::new_with_default("container"),
@ -116,7 +116,7 @@ impl Container {
self self
} }
pub fn components(&self) -> &PageContainer { pub fn components(&self) -> &ComponentsHolder {
&self.components &self.components
} }

View file

@ -7,7 +7,7 @@ pub enum FormMethod {Get, Post}
pub struct Form { pub struct Form {
renderable: fn() -> bool, renderable: fn() -> bool,
weight : isize, weight : isize,
elements : PageContainer, elements : ComponentsHolder,
action : OptAttr, action : OptAttr,
charset : OptAttr, charset : OptAttr,
method : FormMethod, method : FormMethod,
@ -21,7 +21,7 @@ impl ComponentTrait for Form {
Form { Form {
renderable: render_always, renderable: render_always,
weight : 0, weight : 0,
elements : PageContainer::new(), elements : ComponentsHolder::new(),
action : OptAttr::new(), action : OptAttr::new(),
charset : OptAttr::new_with_value("UTF-8"), charset : OptAttr::new_with_value("UTF-8"),
method : FormMethod::Post, method : FormMethod::Post,
@ -75,7 +75,7 @@ impl Form {
self self
} }
pub fn elements(&self) -> &PageContainer { pub fn elements(&self) -> &ComponentsHolder {
&self.elements &self.elements
} }

View file

@ -5,7 +5,7 @@ pub const TYPENAME_COLUMN: &str = "pagetop::base::component::grid::column::Colum
pub struct Column { pub struct Column {
renderable: fn() -> bool, renderable: fn() -> bool,
weight : isize, weight : isize,
components: PageContainer, components: ComponentsHolder,
id : OptIden, id : OptIden,
classes : Classes, classes : Classes,
template : String, template : String,
@ -16,7 +16,7 @@ impl ComponentTrait for Column {
Column { Column {
renderable: render_always, renderable: render_always,
weight : 0, weight : 0,
components: PageContainer::new(), components: ComponentsHolder::new(),
id : OptIden::new(), id : OptIden::new(),
classes : Classes::new_with_default("col"), classes : Classes::new_with_default("col"),
template : "default".to_owned(), template : "default".to_owned(),
@ -57,7 +57,7 @@ impl Column {
self self
} }
pub fn components(&self) -> &PageContainer { pub fn components(&self) -> &ComponentsHolder {
&self.components &self.components
} }

View file

@ -5,7 +5,7 @@ pub const TYPENAME_ROW: &str = "pagetop::base::component::grid::row::Row";
pub struct Row { pub struct Row {
renderable: fn() -> bool, renderable: fn() -> bool,
weight : isize, weight : isize,
columns : PageContainer, columns : ComponentsHolder,
id : OptIden, id : OptIden,
classes : Classes, classes : Classes,
template : String, template : String,
@ -16,7 +16,7 @@ impl ComponentTrait for Row {
Row { Row {
renderable: render_always, renderable: render_always,
weight : 0, weight : 0,
columns : PageContainer::new(), columns : ComponentsHolder::new(),
id : OptIden::new(), id : OptIden::new(),
classes : Classes::new_with_default("row"), classes : Classes::new_with_default("row"),
template : "default".to_owned(), template : "default".to_owned(),
@ -57,7 +57,7 @@ impl Row {
self self
} }
pub fn columns(&self) -> &PageContainer { pub fn columns(&self) -> &ComponentsHolder {
&self.columns &self.columns
} }

View file

@ -172,7 +172,7 @@ impl MenuItem {
pub struct Menu { pub struct Menu {
renderable: fn() -> bool, renderable: fn() -> bool,
weight : isize, weight : isize,
items : PageContainer, items : ComponentsHolder,
id : OptIden, id : OptIden,
classes : Classes, classes : Classes,
template : String, template : String,
@ -183,7 +183,7 @@ impl ComponentTrait for Menu {
Menu { Menu {
renderable: render_always, renderable: render_always,
weight : 0, weight : 0,
items : PageContainer::new(), items : ComponentsHolder::new(),
id : OptIden::new(), id : OptIden::new(),
classes : Classes::new_with_default("sm sm-clean"), classes : Classes::new_with_default("sm sm-clean"),
template : "default".to_owned(), template : "default".to_owned(),
@ -243,7 +243,7 @@ impl Menu {
self self
} }
pub fn items(&self) -> &PageContainer { pub fn items(&self) -> &ComponentsHolder {
&self.items &self.items
} }

View file

@ -42,7 +42,7 @@ pub struct Page<'a> {
title : OptAttr, title : OptAttr,
description : OptAttr, description : OptAttr,
assets : PageAssets, assets : PageAssets,
regions : HashMap<&'a str, PageContainer>, regions : HashMap<&'a str, ComponentsHolder>,
body_classes: Classes, body_classes: Classes,
template : String, template : String,
} }
@ -102,7 +102,7 @@ impl<'a> Page<'a> {
if let Some(regions) = self.regions.get_mut(region) { if let Some(regions) = self.regions.get_mut(region) {
regions.add(component); regions.add(component);
} else { } else {
self.regions.insert(region, PageContainer::new_with(component)); self.regions.insert(region, ComponentsHolder::new_with(component));
} }
self self
} }