♻️ Rename types for Mixed and Typed components

This commit is contained in:
Manuel Cillero 2024-03-07 21:27:36 +01:00
parent 57e71a9399
commit c65ac74a6f
20 changed files with 151 additions and 154 deletions

View file

@ -156,13 +156,9 @@ pub async fn summary(request: HttpRequest) -> ResultPage<Markup, ErrorPage> {
.with_component_in("top-menu", side_menu)
.with_component(
flex::Container::new()
.add_item(flex::Item::new().add_component(Html::with(html! {
p { "Columna 1"}
})))
.add_item(flex::Item::new().add_component(top_menu))
.add_item(flex::Item::new().add_component(Html::with(html! {
p { "Columna 3"}
}))),
.add_item(flex::Item::with(Html::with(html! { p { "Columna 1"} })))
.add_item(flex::Item::with(top_menu))
.add_item(flex::Item::with(Html::with(html! { p { "Columna 3"} }))),
)
.with_template("admin")
.render()

View file

@ -8,7 +8,7 @@ pub struct Block {
renderable: Renderable,
classes : OptionClasses,
title : OptionTranslated,
stuff : MixedComponents,
mixed : MixedComponents,
}
impl ComponentTrait for Block {
@ -78,13 +78,13 @@ impl Block {
#[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.stuff.alter_value(OneOp::Add(OneComponent::with(component)));
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component)));
self
}
#[fn_builder]
pub fn alter_components(&mut self, op: OneOp) -> &mut Self {
self.stuff.alter_value(op);
pub fn alter_components(&mut self, op: MixedOp) -> &mut Self {
self.mixed.alter_value(op);
self
}
@ -95,6 +95,6 @@ impl Block {
}
pub fn components(&self) -> &MixedComponents {
&self.stuff
&self.mixed
}
}

View file

@ -7,7 +7,7 @@ pub struct Container {
weight : Weight,
renderable : Renderable,
classes : OptionClasses,
items : VectorComponents<flex::Item>,
items : TypedComponents<flex::Item>,
direction : flex::Direction,
wrap_align : flex::WrapAlign,
content_justify: flex::ContentJustify,
@ -84,7 +84,7 @@ impl Container {
#[rustfmt::skip]
pub fn add_item(mut self, item: flex::Item) -> Self {
self.items.alter_value(TypedOp::Add(TypedComponent::with(item)));
self.items.alter_value(TypedOp::Add(OneComponent::with(item)));
self
}
@ -126,7 +126,7 @@ impl Container {
// Container GETTERS.
pub fn items(&self) -> &VectorComponents<flex::Item> {
pub fn items(&self) -> &TypedComponents<flex::Item> {
&self.items
}

View file

@ -12,7 +12,7 @@ pub struct Item {
item_size : flex::ItemSize,
item_offset : flex::ItemOffset,
item_align : flex::ItemAlign,
stuff : MixedComponents,
mixed : MixedComponents,
}
impl ComponentTrait for Item {
@ -63,6 +63,10 @@ impl ComponentTrait for Item {
}
impl Item {
pub fn with(component: impl ComponentTrait) -> Self {
Item::default().add_component(component)
}
// Item BUILDER.
#[fn_builder]
@ -115,13 +119,13 @@ impl Item {
#[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.stuff.alter_value(OneOp::Add(OneComponent::with(component)));
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component)));
self
}
#[fn_builder]
pub fn alter_components(&mut self, op: OneOp) -> &mut Self {
self.stuff.alter_value(op);
pub fn alter_components(&mut self, op: MixedOp) -> &mut Self {
self.mixed.alter_value(op);
self
}
@ -148,6 +152,6 @@ impl Item {
}
pub fn components(&self) -> &MixedComponents {
&self.stuff
&self.mixed
}
}

View file

@ -17,7 +17,7 @@ pub struct Form {
action : OptionString,
charset : OptionString,
method : FormMethod,
stuff : MixedComponents,
mixed : MixedComponents,
}
impl ComponentTrait for Form {
@ -99,13 +99,13 @@ impl Form {
#[rustfmt::skip]
pub fn add_element(mut self, element: impl ComponentTrait) -> Self {
self.stuff.alter_value(OneOp::Add(OneComponent::with(element)));
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(element)));
self
}
#[fn_builder]
pub fn alter_elements(&mut self, op: OneOp) -> &mut Self {
self.stuff.alter_value(op);
pub fn alter_elements(&mut self, op: MixedOp) -> &mut Self {
self.mixed.alter_value(op);
self
}
@ -124,6 +124,6 @@ impl Form {
}
pub fn elements(&self) -> &MixedComponents {
&self.stuff
&self.mixed
}
}

View file

@ -2,8 +2,8 @@ use crate::prelude::*;
use super::Submenu;
type Content = TypedComponent<Html>;
type SubmenuItems = TypedComponent<Submenu>;
type Content = OneComponent<Html>;
type SubmenuItems = OneComponent<Submenu>;
#[derive(AutoDefault)]
pub enum ElementType {

View file

@ -8,7 +8,7 @@ pub struct Group {
id : OptionId,
weight : Weight,
renderable: Renderable,
elements : VectorComponents<Element>,
elements : TypedComponents<Element>,
}
impl ComponentTrait for Group {
@ -60,7 +60,7 @@ impl Group {
#[rustfmt::skip]
pub fn add_element(mut self, element: Element) -> Self {
self.elements.alter_value(TypedOp::Add(TypedComponent::with(element)));
self.elements.alter_value(TypedOp::Add(OneComponent::with(element)));
self
}
@ -72,7 +72,7 @@ impl Group {
// Group GETTERS.
pub fn elements(&self) -> &VectorComponents<Element> {
pub fn elements(&self) -> &TypedComponents<Element> {
&self.elements
}
}

View file

@ -3,9 +3,9 @@ use crate::prelude::*;
use super::{Megamenu, Submenu};
type Label = L10n;
type Content = TypedComponent<Html>;
type SubmenuItems = TypedComponent<Submenu>;
type MegamenuGroups = TypedComponent<Megamenu>;
type Content = OneComponent<Html>;
type SubmenuItems = OneComponent<Submenu>;
type MegamenuGroups = OneComponent<Megamenu>;
#[derive(AutoDefault)]
pub enum ItemType {

View file

@ -8,7 +8,7 @@ pub struct Megamenu {
id : OptionId,
weight : Weight,
renderable: Renderable,
groups : VectorComponents<Group>,
groups : TypedComponents<Group>,
}
impl ComponentTrait for Megamenu {
@ -60,7 +60,7 @@ impl Megamenu {
#[rustfmt::skip]
pub fn add_group(mut self, group: Group) -> Self {
self.groups.alter_value(TypedOp::Add(TypedComponent::with(group)));
self.groups.alter_value(TypedOp::Add(OneComponent::with(group)));
self
}
@ -72,7 +72,7 @@ impl Megamenu {
// Megamenu GETTERS.
pub fn groups(&self) -> &VectorComponents<Group> {
pub fn groups(&self) -> &TypedComponents<Group> {
&self.groups
}
}

View file

@ -8,7 +8,7 @@ pub struct Menu {
id : OptionId,
weight : Weight,
renderable: Renderable,
items : VectorComponents<Item>,
items : TypedComponents<Item>,
}
impl ComponentTrait for Menu {
@ -88,7 +88,7 @@ impl Menu {
#[rustfmt::skip]
pub fn add_item(mut self, item: Item) -> Self {
self.items.alter_value(TypedOp::Add(TypedComponent::with(item)));
self.items.alter_value(TypedOp::Add(OneComponent::with(item)));
self
}
@ -100,7 +100,7 @@ impl Menu {
// Menu GETTERS.
pub fn items(&self) -> &VectorComponents<Item> {
pub fn items(&self) -> &TypedComponents<Item> {
&self.items
}
}

View file

@ -9,7 +9,7 @@ pub struct Submenu {
weight : Weight,
renderable: Renderable,
title : OptionTranslated,
items : VectorComponents<Item>,
items : TypedComponents<Item>,
}
impl ComponentTrait for Submenu {
@ -72,7 +72,7 @@ impl Submenu {
#[rustfmt::skip]
pub fn add_item(mut self, item: Item) -> Self {
self.items.alter_value(TypedOp::Add(TypedComponent::with(item)));
self.items.alter_value(TypedOp::Add(OneComponent::with(item)));
self
}
@ -88,7 +88,7 @@ impl Submenu {
&self.title
}
pub fn items(&self) -> &VectorComponents<Item> {
pub fn items(&self) -> &TypedComponents<Item> {
&self.items
}
}

View file

@ -8,7 +8,7 @@ pub struct Paragraph {
renderable: Renderable,
classes : OptionClasses,
font_size : FontSize,
stuff : MixedComponents,
mixed : MixedComponents,
}
impl ComponentTrait for Paragraph {
@ -49,8 +49,8 @@ impl Paragraph {
Paragraph::default().add_component(component)
}
pub fn translated(l10n: L10n) -> Self {
Paragraph::default().add_translated(l10n)
pub fn fluent(l10n: L10n) -> Self {
Paragraph::default().add_component(Fluent::with(l10n))
}
// Paragraph BUILDER.
@ -81,19 +81,13 @@ impl Paragraph {
#[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.stuff.alter_value(OneOp::Add(OneComponent::with(component)));
self
}
#[rustfmt::skip]
pub fn add_translated(mut self, l10n: L10n) -> Self {
self.stuff.alter_value(OneOp::Add(OneComponent::with(Translate::with(l10n))));
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component)));
self
}
#[fn_builder]
pub fn alter_components(&mut self, op: OneOp) -> &mut Self {
self.stuff.alter_value(op);
pub fn alter_components(&mut self, op: MixedOp) -> &mut Self {
self.mixed.alter_value(op);
self
}
@ -104,6 +98,6 @@ impl Paragraph {
}
pub fn components(&self) -> &MixedComponents {
&self.stuff
&self.mixed
}
}

View file

@ -13,12 +13,12 @@ pub enum WrapperType {
#[rustfmt::skip]
#[derive(AutoDefault, ComponentClasses)]
pub struct Wrapper {
id : OptionId,
weight : Weight,
renderable : Renderable,
classes : OptionClasses,
wrapper_type : WrapperType,
stuff : MixedComponents,
id : OptionId,
weight : Weight,
renderable : Renderable,
classes : OptionClasses,
wrapper_type: WrapperType,
mixed : MixedComponents,
}
impl ComponentTrait for Wrapper {
@ -126,13 +126,13 @@ impl Wrapper {
#[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.stuff.alter_value(OneOp::Add(OneComponent::with(component)));
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component)));
self
}
#[fn_builder]
pub fn alter_components(&mut self, op: OneOp) -> &mut Self {
self.stuff.alter_value(op);
pub fn alter_components(&mut self, op: MixedOp) -> &mut Self {
self.mixed.alter_value(op);
self
}
@ -143,6 +143,6 @@ impl Wrapper {
}
pub fn components(&self) -> &MixedComponents {
&self.stuff
&self.mixed
}
}

View file

@ -61,7 +61,7 @@ fn hello_world() -> Wrapper {
Heading::h1(L10n::l("welcome_title")).with_size(HeadingSize::Medium),
)
.add_component(
Paragraph::translated(L10n::l("welcome_intro").with_arg(
Paragraph::fluent(L10n::l("welcome_intro").with_arg(
"app",
format!(
"<span class=\"app-name\">{}</span>",
@ -70,7 +70,7 @@ fn hello_world() -> Wrapper {
))
.with_font_size(FontSize::Medium),
)
.add_component(Paragraph::translated(L10n::l("welcome_powered").with_arg(
.add_component(Paragraph::fluent(L10n::l("welcome_powered").with_arg(
"pagetop",
format!(
"<a href=\"{}\" target=\"_blank\">{}</a>",
@ -119,10 +119,8 @@ fn welcome() -> Wrapper {
))
.with_size(HeadingSize::Subtitle),
)
.add_component(
Paragraph::translated(L10n::l("welcome_text1")).with_font_size(FontSize::Medium),
)
.add_component(Paragraph::translated(L10n::l("welcome_text2")))
.add_component(Paragraph::fluent(L10n::l("welcome_text1")).with_font_size(FontSize::Medium))
.add_component(Paragraph::fluent(L10n::l("welcome_text2")))
}
fn about_pagetop() -> Wrapper {
@ -142,11 +140,11 @@ fn about_pagetop() -> Wrapper {
.with_classes(ClassesOp::Add, "pagetop-col-text")
.add_component(Heading::h2(L10n::l("welcome_pagetop_title")))
.add_component(
Paragraph::translated(L10n::l("welcome_pagetop_text1"))
Paragraph::fluent(L10n::l("welcome_pagetop_text1"))
.with_font_size(FontSize::Medium),
)
.add_component(Paragraph::translated(L10n::l("welcome_pagetop_text2")))
.add_component(Paragraph::translated(L10n::l("welcome_pagetop_text3"))),
.add_component(Paragraph::fluent(L10n::l("welcome_pagetop_text2")))
.add_component(Paragraph::fluent(L10n::l("welcome_pagetop_text3"))),
),
)
}
@ -163,7 +161,7 @@ fn promo_pagetop() -> Wrapper {
.with_size(flex::ItemSize::Percent50)
.add_component(Heading::h2(L10n::l("welcome_promo_title")))
.add_component(
Paragraph::translated(L10n::l("welcome_promo_text1").with_arg(
Paragraph::fluent(L10n::l("welcome_promo_text1").with_arg(
"pagetop",
format!(
"<a href=\"{}\" target=\"_blank\">{}</a>",
@ -199,10 +197,10 @@ fn reporting_issues() -> Wrapper {
.with_size(flex::ItemSize::Percent50)
.add_component(Heading::h2(L10n::l("welcome_issues_title")))
.add_component(
Paragraph::translated(L10n::l("welcome_issues_text1"))
Paragraph::fluent(L10n::l("welcome_issues_text1"))
.with_font_size(FontSize::Medium),
)
.add_component(Paragraph::translated(
.add_component(Paragraph::fluent(
L10n::l("welcome_issues_text2").with_arg(
"app",
format!(

View file

@ -11,10 +11,8 @@ pub use definition::{component_as_mut, component_as_ref, ComponentBase, Componen
mod classes;
pub use classes::{ComponentClasses, ComponentClassesOp};
mod arc_one;
pub use arc_one::MixedComponents;
pub use arc_one::{OneComponent, OneOp};
mod arc_mixed;
pub use arc_mixed::{AnyComponent, MixedComponents, MixedOp};
mod arc_typed;
pub use arc_typed::VectorComponents;
pub use arc_typed::{TypedComponent, TypedOp};
pub use arc_typed::{OneComponent, TypedComponents, TypedOp};

View file

@ -5,20 +5,20 @@ use crate::{fn_builder, TypeId, Weight};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
#[derive(Clone)]
pub struct OneComponent(Arc<RwLock<dyn ComponentTrait>>);
pub struct AnyComponent(Arc<RwLock<dyn ComponentTrait>>);
impl OneComponent {
impl AnyComponent {
pub fn with(component: impl ComponentTrait) -> Self {
OneComponent(Arc::new(RwLock::new(component)))
AnyComponent(Arc::new(RwLock::new(component)))
}
// OneComponent BUILDER.
// AnyComponent BUILDER.
pub fn set(&mut self, component: impl ComponentTrait) {
self.0 = Arc::new(RwLock::new(component));
}
// OneComponent GETTERS.
// AnyComponent GETTERS.
pub fn get(&self) -> RwLockReadGuard<'_, dyn ComponentTrait> {
self.0.read().unwrap()
@ -28,13 +28,13 @@ impl OneComponent {
self.0.write().unwrap()
}
// OneComponent RENDER.
// AnyComponent RENDER.
pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx)
}
// OneComponent HELPERS.
// AnyComponent HELPERS.
fn type_id(&self) -> TypeId {
self.0.read().unwrap().type_id()
@ -51,22 +51,22 @@ impl OneComponent {
// *************************************************************************************************
pub enum OneOp {
Add(OneComponent),
AddAfterId(&'static str, OneComponent),
AddBeforeId(&'static str, OneComponent),
Prepend(OneComponent),
pub enum MixedOp {
Add(AnyComponent),
InsertAfterId(&'static str, AnyComponent),
InsertBeforeId(&'static str, AnyComponent),
Prepend(AnyComponent),
RemoveById(&'static str),
ReplaceById(&'static str, OneComponent),
ReplaceById(&'static str, AnyComponent),
Reset,
}
#[derive(Clone, Default)]
pub struct MixedComponents(Vec<OneComponent>);
pub struct MixedComponents(Vec<AnyComponent>);
impl MixedComponents {
pub fn new(any: OneComponent) -> Self {
MixedComponents::default().with_value(OneOp::Add(any))
pub fn new(any: AnyComponent) -> Self {
MixedComponents::default().with_value(MixedOp::Add(any))
}
pub(crate) fn merge(mixes: &[Option<&MixedComponents>]) -> Self {
@ -80,24 +80,24 @@ impl MixedComponents {
// MixedComponents BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, op: OneOp) -> &mut Self {
pub fn alter_value(&mut self, op: MixedOp) -> &mut Self {
match op {
OneOp::Add(any) => self.0.push(any),
OneOp::AddAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) {
MixedOp::Add(any) => self.0.push(any),
MixedOp::InsertAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index + 1, any),
_ => self.0.push(any),
},
OneOp::AddBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) {
MixedOp::InsertBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index, any),
_ => self.0.insert(0, any),
},
OneOp::Prepend(any) => self.0.insert(0, any),
OneOp::RemoveById(id) => {
MixedOp::Prepend(any) => self.0.insert(0, any),
MixedOp::RemoveById(id) => {
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
self.0.remove(index);
}
}
OneOp::ReplaceById(id, any) => {
MixedOp::ReplaceById(id, any) => {
for c in self.0.iter_mut() {
if c.id() == id {
*c = any;
@ -105,7 +105,7 @@ impl MixedComponents {
}
}
}
OneOp::Reset => self.0.clear(),
MixedOp::Reset => self.0.clear(),
}
self
}
@ -116,17 +116,17 @@ impl MixedComponents {
self.0.is_empty()
}
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&OneComponent> {
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&AnyComponent> {
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 = &OneComponent> {
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &AnyComponent> {
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 = &OneComponent> {
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &AnyComponent> {
self.0.iter().filter(move |&c| c.type_id() == type_id)
}

View file

@ -4,26 +4,26 @@ use crate::{fn_builder, TypeId, Weight};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
pub struct TypedComponent<C: ComponentTrait>(Arc<RwLock<C>>);
pub struct OneComponent<C: ComponentTrait>(Arc<RwLock<C>>);
impl<C: ComponentTrait> Clone for TypedComponent<C> {
impl<C: ComponentTrait> Clone for OneComponent<C> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<C: ComponentTrait> TypedComponent<C> {
impl<C: ComponentTrait> OneComponent<C> {
pub fn with(component: C) -> Self {
TypedComponent(Arc::new(RwLock::new(component)))
OneComponent(Arc::new(RwLock::new(component)))
}
// TypedComponent BUILDER.
// OneComponent BUILDER.
pub fn set(&mut self, component: C) {
self.0 = Arc::new(RwLock::new(component));
}
// TypedComponent GETTERS.
// OneComponent GETTERS.
pub fn get(&self) -> RwLockReadGuard<'_, C> {
self.0.read().unwrap()
@ -33,13 +33,13 @@ impl<C: ComponentTrait> TypedComponent<C> {
self.0.write().unwrap()
}
// TypedComponent RENDER.
// OneComponent RENDER.
pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx)
}
// TypedComponent HELPERS.
// OneComponent HELPERS.
fn type_id(&self) -> TypeId {
self.0.read().unwrap().type_id()
@ -57,34 +57,34 @@ impl<C: ComponentTrait> TypedComponent<C> {
// *************************************************************************************************
pub enum TypedOp<C: ComponentTrait> {
Add(TypedComponent<C>),
AddAfterId(&'static str, TypedComponent<C>),
AddBeforeId(&'static str, TypedComponent<C>),
Prepend(TypedComponent<C>),
Add(OneComponent<C>),
InsertAfterId(&'static str, OneComponent<C>),
InsertBeforeId(&'static str, OneComponent<C>),
Prepend(OneComponent<C>),
RemoveById(&'static str),
ReplaceById(&'static str, TypedComponent<C>),
ReplaceById(&'static str, OneComponent<C>),
Reset,
}
#[derive(Clone, Default)]
pub struct VectorComponents<C: ComponentTrait>(Vec<TypedComponent<C>>);
pub struct TypedComponents<C: ComponentTrait>(Vec<OneComponent<C>>);
impl<C: ComponentTrait + Default> VectorComponents<C> {
pub fn new(one: TypedComponent<C>) -> Self {
VectorComponents::default().with_value(TypedOp::Add(one))
impl<C: ComponentTrait + Default> TypedComponents<C> {
pub fn new(one: OneComponent<C>) -> Self {
TypedComponents::default().with_value(TypedOp::Add(one))
}
// VectorComponents BUILDER.
// TypedComponents BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, op: TypedOp<C>) -> &mut Self {
match op {
TypedOp::Add(one) => self.0.push(one),
TypedOp::AddAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) {
TypedOp::InsertAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index + 1, one),
_ => self.0.push(one),
},
TypedOp::AddBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) {
TypedOp::InsertBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index, one),
_ => self.0.insert(0, one),
},
@ -107,27 +107,27 @@ impl<C: ComponentTrait + Default> VectorComponents<C> {
self
}
// VectorComponents GETTERS.
// TypedComponents GETTERS.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&TypedComponent<C>> {
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&OneComponent<C>> {
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 = &TypedComponent<C>> {
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &OneComponent<C>> {
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 = &TypedComponent<C>> {
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &OneComponent<C>> {
self.0.iter().filter(move |&c| c.type_id() == type_id)
}
// VectorComponents RENDER.
// TypedComponents RENDER.
pub fn render(&self, cx: &mut Context) -> Markup {
let mut components = self.0.clone();

View file

@ -1,4 +1,4 @@
use crate::core::component::{MixedComponents, OneComponent, OneOp};
use crate::core::component::{AnyComponent, MixedComponents, MixedOp};
use crate::core::theme::ThemeRef;
use crate::{AutoDefault, LazyStatic, TypeId};
@ -15,17 +15,25 @@ static COMMON_REGIONS: LazyStatic<RwLock<ComponentsInRegions>> =
pub struct ComponentsInRegions(HashMap<&'static str, MixedComponents>);
impl ComponentsInRegions {
pub fn new(region: &'static str, one: OneComponent) -> Self {
pub fn new(region: &'static str, any: AnyComponent) -> Self {
let mut regions = ComponentsInRegions::default();
regions.add_in(region, one);
regions.add_in(region, any);
regions
}
pub fn add_in(&mut self, region: &'static str, one: OneComponent) {
if let Some(region) = self.0.get_mut(region) {
region.alter_value(OneOp::Add(one));
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(region, MixedComponents::new(one));
self.0.insert("content", MixedComponents::new(any));
}
}
pub fn add_in(&mut self, region: &'static str, any: AnyComponent) {
if let Some(region) = self.0.get_mut(region) {
region.alter_value(MixedOp::Add(any));
} else {
self.0.insert(region, MixedComponents::new(any));
}
}
@ -46,20 +54,20 @@ pub enum InRegion {
}
impl InRegion {
pub fn add(&self, one: OneComponent) -> &Self {
pub fn add(&self, any: AnyComponent) -> &Self {
match self {
InRegion::Content => {
COMMON_REGIONS.write().unwrap().add_in("content", one);
COMMON_REGIONS.write().unwrap().add(any);
}
InRegion::Named(name) => {
COMMON_REGIONS.write().unwrap().add_in(name, one);
COMMON_REGIONS.write().unwrap().add_in(name, 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, one);
r.add_in(region, any);
} else {
regions.insert(theme.type_id(), ComponentsInRegions::new(region, one));
regions.insert(theme.type_id(), ComponentsInRegions::new(region, any));
}
}
}

View file

@ -1,8 +1,8 @@
use crate::core::component::{ComponentTrait, Context, TypedComponent};
use crate::core::component::{ComponentTrait, Context, OneComponent};
use crate::fn_builder;
use crate::html::{html, Markup};
pub struct OptionComponent<C: ComponentTrait>(Option<TypedComponent<C>>);
pub struct OptionComponent<C: ComponentTrait>(Option<OneComponent<C>>);
impl<C: ComponentTrait> Default for OptionComponent<C> {
fn default() -> Self {
@ -20,7 +20,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
#[fn_builder]
pub fn alter_value(&mut self, component: Option<C>) -> &mut Self {
if let Some(component) = component {
self.0 = Some(TypedComponent::with(component));
self.0 = Some(OneComponent::with(component));
} else {
self.0 = None;
}
@ -29,7 +29,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
// OptionComponent GETTERS.
pub fn get(&self) -> Option<TypedComponent<C>> {
pub fn get(&self) -> Option<OneComponent<C>> {
if let Some(value) = &self.0 {
return Some(value.clone());
}

View file

@ -4,7 +4,7 @@ pub use error::ErrorPage;
pub use actix_web::Result as ResultPage;
use crate::base::action;
use crate::core::component::{ComponentTrait, MixedComponents, OneComponent};
use crate::core::component::{AnyComponent, ComponentTrait, MixedComponents};
use crate::core::component::{Context, ContextOp};
use crate::core::theme::ComponentsInRegions;
use crate::fn_builder;
@ -106,8 +106,7 @@ impl Page {
#[fn_builder]
pub fn alter_component(&mut self, component: impl ComponentTrait) -> &mut Self {
self.regions
.add_in("content", OneComponent::with(component));
self.regions.add(AnyComponent::with(component));
self
}
@ -117,7 +116,7 @@ impl Page {
region: &'static str,
component: impl ComponentTrait,
) -> &mut Self {
self.regions.add_in(region, OneComponent::with(component));
self.regions.add_in(region, AnyComponent::with(component));
self
}