💥 Introduce ComponentRef to add comp's to packs

This commit is contained in:
Manuel Cillero 2023-07-19 19:18:20 +02:00
parent df2d15a839
commit 0a95f3d51c
12 changed files with 106 additions and 103 deletions

View file

@ -244,12 +244,12 @@ impl MegaMenu {
} }
pub fn with_item(mut self, item: MegaMenuItem) -> Self { pub fn with_item(mut self, item: MegaMenuItem) -> Self {
self.items.alter_pack(PackOp::Add, item); self.items.alter(PackOp::Add, ComponentRef::to(item));
self self
} }
pub fn alter_items(&mut self, op: PackOp, item: MegaMenuItem) -> &mut Self { pub fn alter_items(&mut self, op: PackOp, item: MegaMenuItem) -> &mut Self {
self.items.alter_pack(op, item); self.items.alter(op, ComponentRef::to(item));
self self
} }

View file

@ -12,7 +12,7 @@ pub struct Block {
id : IdentifierValue, id : IdentifierValue,
classes : Classes, classes : Classes,
title : AttributeValue, title : AttributeValue,
components: PackComponents, content : PackComponents,
template : String, template : String,
} }
@ -94,12 +94,12 @@ impl Block {
} }
pub fn with_component(mut self, component: impl ComponentTrait) -> Self { pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.components.alter_pack(PackOp::Add, component); self.content.alter(PackOp::Add, ComponentRef::to(component));
self self
} }
pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self { pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_pack(op, component); self.content.alter(op, ComponentRef::to(component));
self self
} }
@ -120,7 +120,7 @@ impl Block {
} }
pub fn components(&self) -> &PackComponents { pub fn components(&self) -> &PackComponents {
&self.components &self.content
} }
pub fn template(&self) -> &str { pub fn template(&self) -> &str {

View file

@ -23,7 +23,7 @@ pub struct Container {
classes : Classes, classes : Classes,
inner_classes : Classes, inner_classes : Classes,
container_type: ContainerType, container_type: ContainerType,
components : PackComponents, content : PackComponents,
template : String, template : String,
} }
@ -155,12 +155,12 @@ impl Container {
} }
pub fn with_component(mut self, component: impl ComponentTrait) -> Self { pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.components.alter_pack(PackOp::Add, component); self.content.alter(PackOp::Add, ComponentRef::to(component));
self self
} }
pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self { pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_pack(op, component); self.content.alter(op, ComponentRef::to(component));
self self
} }
@ -185,7 +185,7 @@ impl Container {
} }
pub fn components(&self) -> &PackComponents { pub fn components(&self) -> &PackComponents {
&self.components &self.content
} }
pub fn template(&self) -> &str { pub fn template(&self) -> &str {

View file

@ -121,12 +121,12 @@ impl Form {
} }
pub fn with_element(mut self, element: impl ComponentTrait) -> Self { pub fn with_element(mut self, element: impl ComponentTrait) -> Self {
self.elements.alter_pack(PackOp::Add, element); self.elements.alter(PackOp::Add, ComponentRef::to(element));
self self
} }
pub fn alter_elements(&mut self, op: PackOp, element: impl ComponentTrait) -> &mut Self { pub fn alter_elements(&mut self, op: PackOp, element: impl ComponentTrait) -> &mut Self {
self.elements.alter_pack(op, element); self.elements.alter(op, ComponentRef::to(element));
self self
} }

View file

@ -4,16 +4,16 @@ create_handle!(COMPONENT_COLUMN);
actions_for_component!(Column); actions_for_component!(Column);
const SIZE__DEFAULT: &str = "col-md"; const SIZE_DEFAULT: &str = "col-md";
const SIZE__1_OF_12: &str = "col-md-1"; const SIZE_1_OF_12: &str = "col-md-1";
const SIZE__2_OF_12: &str = "col-md-2"; const SIZE_2_OF_12: &str = "col-md-2";
const SIZE__3_OF_12: &str = "col-md-3"; const SIZE_3_OF_12: &str = "col-md-3";
const SIZE__4_OF_12: &str = "col-md-4"; const SIZE_4_OF_12: &str = "col-md-4";
const SIZE__5_OF_12: &str = "col-md-5"; const SIZE_5_OF_12: &str = "col-md-5";
const SIZE__6_OF_12: &str = "col-md-6"; const SIZE_6_OF_12: &str = "col-md-6";
const SIZE__7_OF_12: &str = "col-md-7"; const SIZE_7_OF_12: &str = "col-md-7";
const SIZE__8_OF_12: &str = "col-md-8"; const SIZE_8_OF_12: &str = "col-md-8";
const SIZE__9_OF_12: &str = "col-md-9"; const SIZE_9_OF_12: &str = "col-md-9";
const SIZE_10_OF_12: &str = "col-md-10"; const SIZE_10_OF_12: &str = "col-md-10";
const SIZE_11_OF_12: &str = "col-md-11"; const SIZE_11_OF_12: &str = "col-md-11";
const SIZE_12_OF_12: &str = "col-md-12"; const SIZE_12_OF_12: &str = "col-md-12";
@ -44,13 +44,13 @@ pub struct Column {
id : IdentifierValue, id : IdentifierValue,
classes : Classes, classes : Classes,
size : ColumnSize, size : ColumnSize,
components: PackComponents, content : PackComponents,
template : String, template : String,
} }
impl ComponentTrait for Column { impl ComponentTrait for Column {
fn new() -> Self { fn new() -> Self {
Column::default().with_classes(ClassesOp::SetDefault, SIZE__DEFAULT) Column::default().with_classes(ClassesOp::SetDefault, SIZE_DEFAULT)
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
@ -117,16 +117,16 @@ impl Column {
#[fn_builder] #[fn_builder]
pub fn alter_size(&mut self, size: ColumnSize) -> &mut Self { pub fn alter_size(&mut self, size: ColumnSize) -> &mut Self {
match size { match size {
ColumnSize::Default => self.alter_classes(ClassesOp::SetDefault, SIZE__DEFAULT), ColumnSize::Default => self.alter_classes(ClassesOp::SetDefault, SIZE_DEFAULT),
ColumnSize::Is1of12 => self.alter_classes(ClassesOp::SetDefault, SIZE__1_OF_12), ColumnSize::Is1of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_1_OF_12),
ColumnSize::Is2of12 => self.alter_classes(ClassesOp::SetDefault, SIZE__2_OF_12), ColumnSize::Is2of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_2_OF_12),
ColumnSize::Is3of12 => self.alter_classes(ClassesOp::SetDefault, SIZE__3_OF_12), ColumnSize::Is3of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_3_OF_12),
ColumnSize::Is4of12 => self.alter_classes(ClassesOp::SetDefault, SIZE__4_OF_12), ColumnSize::Is4of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_4_OF_12),
ColumnSize::Is5of12 => self.alter_classes(ClassesOp::SetDefault, SIZE__5_OF_12), ColumnSize::Is5of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_5_OF_12),
ColumnSize::Is6of12 => self.alter_classes(ClassesOp::SetDefault, SIZE__6_OF_12), ColumnSize::Is6of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_6_OF_12),
ColumnSize::Is7of12 => self.alter_classes(ClassesOp::SetDefault, SIZE__7_OF_12), ColumnSize::Is7of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_7_OF_12),
ColumnSize::Is8of12 => self.alter_classes(ClassesOp::SetDefault, SIZE__8_OF_12), ColumnSize::Is8of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_8_OF_12),
ColumnSize::Is9of12 => self.alter_classes(ClassesOp::SetDefault, SIZE__9_OF_12), ColumnSize::Is9of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_9_OF_12),
ColumnSize::Is10of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_10_OF_12), ColumnSize::Is10of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_10_OF_12),
ColumnSize::Is11of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_11_OF_12), ColumnSize::Is11of12 => self.alter_classes(ClassesOp::SetDefault, SIZE_11_OF_12),
ColumnSize::IsFull => self.alter_classes(ClassesOp::SetDefault, SIZE_12_OF_12), ColumnSize::IsFull => self.alter_classes(ClassesOp::SetDefault, SIZE_12_OF_12),
@ -136,12 +136,12 @@ impl Column {
} }
pub fn with_component(mut self, component: impl ComponentTrait) -> Self { pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.components.alter_pack(PackOp::Add, component); self.content.alter(PackOp::Add, ComponentRef::to(component));
self self
} }
pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self { pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_pack(op, component); self.content.alter(op, ComponentRef::to(component));
self self
} }
@ -162,7 +162,7 @@ impl Column {
} }
pub fn components(&self) -> &PackComponents { pub fn components(&self) -> &PackComponents {
&self.components &self.content
} }
pub fn template(&self) -> &str { pub fn template(&self) -> &str {

View file

@ -83,12 +83,12 @@ impl Row {
} }
pub fn with_column(mut self, column: grid::Column) -> Self { pub fn with_column(mut self, column: grid::Column) -> Self {
self.columns.alter_pack(PackOp::Add, column); self.columns.alter(PackOp::Add, ComponentRef::to(column));
self self
} }
pub fn alter_columns(&mut self, op: PackOp, column: grid::Column) -> &mut Self { pub fn alter_columns(&mut self, op: PackOp, column: grid::Column) -> &mut Self {
self.columns.alter_pack(op, column); self.columns.alter(op, ComponentRef::to(column));
self self
} }

View file

@ -20,7 +20,7 @@ pub struct Paragraph {
renderable: Renderable, renderable: Renderable,
id : IdentifierValue, id : IdentifierValue,
classes : Classes, classes : Classes,
components: PackComponents, content : PackComponents,
display : ParagraphDisplay, display : ParagraphDisplay,
template : String, template : String,
} }
@ -90,12 +90,12 @@ impl Paragraph {
} }
pub fn with_component(mut self, component: impl ComponentTrait) -> Self { pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.components.alter_pack(PackOp::Add, component); self.content.alter(PackOp::Add, ComponentRef::to(component));
self self
} }
pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self { pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_pack(op, component); self.content.alter(op, ComponentRef::to(component));
self self
} }
@ -130,7 +130,7 @@ impl Paragraph {
} }
pub fn components(&self) -> &PackComponents { pub fn components(&self) -> &PackComponents {
&self.components &self.content
} }
pub fn display(&self) -> &ParagraphDisplay { pub fn display(&self) -> &ParagraphDisplay {

View file

@ -2,13 +2,13 @@ mod context;
pub use context::{Context, ContextOp}; pub use context::{Context, ContextOp};
mod definition; mod definition;
pub use definition::{component_mut, component_ref, BaseComponent, ComponentTrait}; pub use definition::{component_mut, component_ref, ComponentBase, ComponentTrait};
mod one; mod one;
pub use one::OneComponent; pub use one::OneComponent;
mod pack; mod pack;
pub use pack::{PackComponents, PackOp}; pub use pack::{ComponentRef, PackComponents, PackOp};
mod renderable; mod renderable;
pub use renderable::{IsRenderable, Renderable}; pub use renderable::{IsRenderable, Renderable};

View file

@ -4,7 +4,7 @@ use crate::{util, Handle, Weight};
use std::any::Any; use std::any::Any;
pub trait BaseComponent: Any { pub trait ComponentBase: Any {
fn prepare(&mut self, cx: &mut Context) -> Markup; fn prepare(&mut self, cx: &mut Context) -> Markup;
fn as_ref_any(&self) -> &dyn Any; fn as_ref_any(&self) -> &dyn Any;
@ -12,7 +12,7 @@ pub trait BaseComponent: Any {
fn as_mut_any(&mut self) -> &mut dyn Any; fn as_mut_any(&mut self) -> &mut dyn Any;
} }
pub trait ComponentTrait: BaseComponent + Send + Sync { pub trait ComponentTrait: ComponentBase + Send + Sync {
fn new() -> Self fn new() -> Self
where where
Self: Sized; Self: Sized;
@ -52,7 +52,7 @@ pub trait ComponentTrait: BaseComponent + Send + Sync {
fn after_prepare_component(&mut self, cx: &mut Context) {} fn after_prepare_component(&mut self, cx: &mut Context) {}
} }
impl<C: ComponentTrait> BaseComponent for C { impl<C: ComponentTrait> ComponentBase for C {
fn prepare(&mut self, cx: &mut Context) -> Markup { fn prepare(&mut self, cx: &mut Context) -> Markup {
if self.is_renderable(cx) { if self.is_renderable(cx) {
// Acciones antes de preparar el componente. // Acciones antes de preparar el componente.

View file

@ -1,9 +1,34 @@
use crate::core::component::{ComponentTrait, Context}; use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, Markup}; use crate::html::{html, Markup};
use crate::{fn_builder, Handle}; use crate::{Handle, Weight};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
#[derive(Clone)]
pub struct ComponentRef(Arc<RwLock<dyn ComponentTrait>>);
impl ComponentRef {
pub fn to(component: impl ComponentTrait) -> Self {
ComponentRef(Arc::new(RwLock::new(component)))
}
fn handle(&self) -> Handle {
self.0.read().unwrap().handle()
}
fn id(&self) -> Option<String> {
self.0.read().unwrap().id()
}
fn weight(&self) -> Weight {
self.0.read().unwrap().weight()
}
fn prepare(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().prepare(cx)
}
}
pub enum PackOp { pub enum PackOp {
Add, Add,
AddAfterId(&'static str), AddAfterId(&'static str),
@ -14,19 +39,17 @@ pub enum PackOp {
Reset, Reset,
} }
pub type ArcLockComponent = Arc<RwLock<dyn ComponentTrait>>;
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct PackComponents(Vec<ArcLockComponent>); pub struct PackComponents(Vec<ComponentRef>);
impl PackComponents { impl PackComponents {
pub fn new() -> Self { pub fn new() -> Self {
PackComponents::default() PackComponents::default()
} }
pub fn new_with(component: impl ComponentTrait) -> Self { pub fn new_with(cref: ComponentRef) -> Self {
let mut pack = PackComponents::new(); let mut pack = PackComponents::new();
pack.alter_pack(PackOp::Add, component); pack.alter(PackOp::Add, cref);
pack pack
} }
@ -46,45 +69,31 @@ impl PackComponents {
// PackComponents BUILDER. // PackComponents BUILDER.
#[fn_builder] pub fn alter(&mut self, op: PackOp, cref: ComponentRef) -> &mut Self {
pub fn alter_pack(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self {
let arc = Arc::new(RwLock::new(component));
match op { match op {
PackOp::Add => self.0.push(arc), PackOp::Add => self.0.push(cref),
PackOp::AddAfterId(id) => { PackOp::AddAfterId(id) => {
match self match self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
.0 Some(index) => self.0.insert(index + 1, cref),
.iter() _ => self.0.push(cref),
.position(|c| c.read().unwrap().id().as_deref() == Some(id))
{
Some(index) => self.0.insert(index + 1, arc),
_ => self.0.push(arc),
} }
} }
PackOp::AddBeforeId(id) => { PackOp::AddBeforeId(id) => {
match self match self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
.0 Some(index) => self.0.insert(index, cref),
.iter() _ => self.0.insert(0, cref),
.position(|c| c.read().unwrap().id().as_deref() == Some(id))
{
Some(index) => self.0.insert(index, arc),
_ => self.0.insert(0, arc),
} }
} }
PackOp::AddFirst => self.0.insert(0, arc), PackOp::AddFirst => self.0.insert(0, cref),
PackOp::RemoveById(id) => { PackOp::RemoveById(id) => {
if let Some(index) = self if let Some(index) = self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
.0
.iter()
.position(|c| c.read().unwrap().id().as_deref() == Some(id))
{
self.0.remove(index); self.0.remove(index);
} }
} }
PackOp::ReplaceById(id) => { PackOp::ReplaceById(id) => {
for c in self.0.iter_mut() { for c in self.0.iter_mut() {
if c.read().unwrap().id().as_deref() == Some(id) { if c.id().as_deref() == Some(id) {
*c = arc; *c = cref;
break; break;
} }
} }
@ -96,32 +105,26 @@ impl PackComponents {
// PackComponents GETTERS. // PackComponents GETTERS.
pub fn get_by_id(&self, id: &'static str) -> Option<&ArcLockComponent> { pub fn get_by_id(&self, id: &'static str) -> Option<&ComponentRef> {
self.0 self.0.iter().find(|&c| c.id().as_deref() == Some(id))
.iter()
.find(|&c| c.read().unwrap().id().as_deref() == Some(id))
} }
pub fn iter_by_id(&self, id: &'static str) -> impl Iterator<Item = &ArcLockComponent> { pub fn iter_by_id(&self, id: &'static str) -> impl Iterator<Item = &ComponentRef> {
self.0 self.0.iter().filter(|&c| c.id().as_deref() == Some(id))
.iter()
.filter(|&c| c.read().unwrap().id().as_deref() == Some(id))
} }
pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ArcLockComponent> { pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ComponentRef> {
self.0 self.0.iter().filter(move |&c| c.handle() == handle)
.iter()
.filter(move |&c| c.read().unwrap().handle() == handle)
} }
// PackComponents PREPARE. // PackComponents PREPARE.
pub fn prepare(&self, cx: &mut Context) -> Markup { pub fn prepare(&self, cx: &mut Context) -> Markup {
let mut components = self.0.clone(); let mut components = self.0.clone();
components.sort_by_key(|c| c.read().unwrap().weight()); components.sort_by_key(|c| c.weight());
html! { html! {
@for c in components.iter() { @for c in components.iter() {
(" ")(c.write().unwrap().prepare(cx))(" ") (" ")(c.prepare(cx))(" ")
} }
} }
} }

View file

@ -1,4 +1,4 @@
use crate::core::component::{ComponentTrait, PackComponents, PackOp}; use crate::core::component::{ComponentRef, PackComponents, PackOp};
use crate::LazyStatic; use crate::LazyStatic;
use std::collections::HashMap; use std::collections::HashMap;
@ -15,11 +15,11 @@ impl ComponentsRegions {
ComponentsRegions::default() ComponentsRegions::default()
} }
pub fn add_to(&mut self, region: &'static str, component: impl ComponentTrait) { pub fn add_to(&mut self, region: &'static str, cref: ComponentRef) {
if let Some(region) = self.0.get_mut(region) { if let Some(region) = self.0.get_mut(region) {
region.alter_pack(PackOp::Add, component); region.alter(PackOp::Add, cref);
} else { } else {
self.0.insert(region, PackComponents::new_with(component)); self.0.insert(region, PackComponents::new_with(cref));
} }
} }
@ -32,14 +32,14 @@ impl ComponentsRegions {
} }
} }
pub fn add_component_to(theme: &'static str, region: &'static str, component: impl ComponentTrait) { pub fn add_component_to(theme: &'static str, region: &'static str, cref: ComponentRef) {
let mut hm = THEME_REGIONS.write().unwrap(); let mut hm = THEME_REGIONS.write().unwrap();
if let Some(hm_theme) = hm.get_mut(theme) { if let Some(hm_theme) = hm.get_mut(theme) {
hm_theme.add_to(region, component); hm_theme.add_to(region, cref);
} else { } else {
hm.insert(theme, { hm.insert(theme, {
let mut regions = ComponentsRegions::new(); let mut regions = ComponentsRegions::new();
regions.add_to(region, component); regions.add_to(region, cref);
regions regions
}); });
} }

View file

@ -2,7 +2,7 @@ mod action;
pub use action::*; pub use action::*;
use crate::core::component::l10n::L10n; use crate::core::component::l10n::L10n;
use crate::core::component::{ComponentTrait, Context, ContextOp, OneComponent}; use crate::core::component::{ComponentRef, ComponentTrait, Context, ContextOp, OneComponent};
use crate::core::theme::ComponentsRegions; use crate::core::theme::ComponentsRegions;
use crate::html::{html, Classes, ClassesOp, Favicon, Markup, DOCTYPE}; use crate::html::{html, Classes, ClassesOp, Favicon, Markup, DOCTYPE};
use crate::response::fatal_error::FatalError; use crate::response::fatal_error::FatalError;
@ -90,7 +90,7 @@ impl Page {
#[fn_builder] #[fn_builder]
pub fn alter_in(&mut self, region: &'static str, component: impl ComponentTrait) -> &mut Self { pub fn alter_in(&mut self, region: &'static str, component: impl ComponentTrait) -> &mut Self {
self.regions.add_to(region, component); self.regions.add_to(region, ComponentRef::to(component));
self self
} }