💥 OneComponents are now TypedComponents
This commit is contained in:
parent
66117992c1
commit
f2031307d7
23 changed files with 301 additions and 313 deletions
|
|
@ -4,8 +4,8 @@ use crate::component::MegaMenu;
|
|||
|
||||
new_handle!(COMPONENT_MEGAITEM);
|
||||
|
||||
type Label = ComponentOne<L10n>;
|
||||
type Content = ComponentOne<Html>;
|
||||
type Label = TypedComponent<L10n>;
|
||||
type Content = TypedComponent<Html>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub enum MegaItemType {
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ pub struct MegaMenu {
|
|||
weight : Weight,
|
||||
renderable: Renderable,
|
||||
id : IdentifierValue,
|
||||
items : LisComponents<MegaItem>,
|
||||
items : TypedComponents<MegaItem>,
|
||||
theme : MegaMenuTheme,
|
||||
}
|
||||
|
||||
|
|
@ -153,12 +153,12 @@ impl MegaMenu {
|
|||
}
|
||||
|
||||
pub fn with_item(mut self, item: MegaItem) -> Self {
|
||||
self.items.alter(LisOp::Add(ComponentOne::with(item)));
|
||||
self.items.alter(TypedOp::Add(TypedComponent::with(item)));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_items(&mut self, op: LisOp<MegaItem>) -> &mut Self {
|
||||
pub fn alter_items(&mut self, op: TypedOp<MegaItem>) -> &mut Self {
|
||||
self.items.alter(op);
|
||||
self
|
||||
}
|
||||
|
|
@ -171,7 +171,7 @@ impl MegaMenu {
|
|||
|
||||
// MegaMenu GETTERS.
|
||||
|
||||
pub fn items(&self) -> &LisComponents<MegaItem> {
|
||||
pub fn items(&self) -> &TypedComponents<MegaItem> {
|
||||
&self.items
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ pub enum AnchorTarget {
|
|||
Context(String),
|
||||
}
|
||||
|
||||
type AnchorIcon = ComponentOne<Icon>;
|
||||
type AnchorHtml = ComponentOne<L10n>;
|
||||
type AnchorIcon = TypedComponent<Icon>;
|
||||
type AnchorHtml = TypedComponent<L10n>;
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[derive(Default)]
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ pub struct Block {
|
|||
id : IdentifierValue,
|
||||
classes : Classes,
|
||||
title : AttributeValue,
|
||||
stuff : MixComponents,
|
||||
stuff : ArcComponents,
|
||||
template : String,
|
||||
}
|
||||
|
||||
|
|
@ -94,12 +94,12 @@ impl Block {
|
|||
}
|
||||
|
||||
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
|
||||
self.stuff.alter(MixOp::Add(ComponentArc::with(component)));
|
||||
self.stuff.alter(ArcOp::Add(ArcComponent::with(component)));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_components(&mut self, op: MixOp) -> &mut Self {
|
||||
pub fn alter_components(&mut self, op: ArcOp) -> &mut Self {
|
||||
self.stuff.alter(op);
|
||||
self
|
||||
}
|
||||
|
|
@ -120,7 +120,7 @@ impl Block {
|
|||
&self.title
|
||||
}
|
||||
|
||||
pub fn components(&self) -> &MixComponents {
|
||||
pub fn components(&self) -> &ArcComponents {
|
||||
&self.stuff
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ pub struct Container {
|
|||
classes : Classes,
|
||||
inner_classes : Classes,
|
||||
container_type: ContainerType,
|
||||
stuff : MixComponents,
|
||||
stuff : ArcComponents,
|
||||
template : String,
|
||||
}
|
||||
|
||||
|
|
@ -155,12 +155,12 @@ impl Container {
|
|||
}
|
||||
|
||||
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
|
||||
self.stuff.alter(MixOp::Add(ComponentArc::with(component)));
|
||||
self.stuff.alter(ArcOp::Add(ArcComponent::with(component)));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_components(&mut self, op: MixOp) -> &mut Self {
|
||||
pub fn alter_components(&mut self, op: ArcOp) -> &mut Self {
|
||||
self.stuff.alter(op);
|
||||
self
|
||||
}
|
||||
|
|
@ -185,7 +185,7 @@ impl Container {
|
|||
&self.container_type
|
||||
}
|
||||
|
||||
pub fn components(&self) -> &MixComponents {
|
||||
pub fn components(&self) -> &ArcComponents {
|
||||
&self.stuff
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ pub struct Container {
|
|||
renderable: Renderable,
|
||||
id : IdentifierValue,
|
||||
classes : Classes,
|
||||
items : LisComponents<flex::Item>,
|
||||
items : TypedComponents<flex::Item>,
|
||||
template : String,
|
||||
}
|
||||
|
||||
|
|
@ -83,12 +83,12 @@ impl Container {
|
|||
}
|
||||
|
||||
pub fn with_item(mut self, item: flex::Item) -> Self {
|
||||
self.items.alter(LisOp::Add(ComponentOne::with(item)));
|
||||
self.items.alter(TypedOp::Add(TypedComponent::with(item)));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_items(&mut self, op: LisOp<flex::Item>) -> &mut Self {
|
||||
pub fn alter_items(&mut self, op: TypedOp<flex::Item>) -> &mut Self {
|
||||
self.items.alter(op);
|
||||
self
|
||||
}
|
||||
|
|
@ -105,7 +105,7 @@ impl Container {
|
|||
&self.classes
|
||||
}
|
||||
|
||||
pub fn items(&self) -> &LisComponents<flex::Item> {
|
||||
pub fn items(&self) -> &TypedComponents<flex::Item> {
|
||||
&self.items
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ pub struct Item {
|
|||
id : IdentifierValue,
|
||||
classes : Classes,
|
||||
size : ItemSize,
|
||||
stuff : MixComponents,
|
||||
stuff : ArcComponents,
|
||||
template : String,
|
||||
}
|
||||
|
||||
|
|
@ -136,12 +136,12 @@ impl Item {
|
|||
}
|
||||
|
||||
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
|
||||
self.stuff.alter(MixOp::Add(ComponentArc::with(component)));
|
||||
self.stuff.alter(ArcOp::Add(ArcComponent::with(component)));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_components(&mut self, op: MixOp) -> &mut Self {
|
||||
pub fn alter_components(&mut self, op: ArcOp) -> &mut Self {
|
||||
self.stuff.alter(op);
|
||||
self
|
||||
}
|
||||
|
|
@ -162,7 +162,7 @@ impl Item {
|
|||
&self.size
|
||||
}
|
||||
|
||||
pub fn components(&self) -> &MixComponents {
|
||||
pub fn components(&self) -> &ArcComponents {
|
||||
&self.stuff
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ pub enum ButtonType {
|
|||
Reset,
|
||||
}
|
||||
|
||||
type ButtonValue = ComponentOne<L10n>;
|
||||
type ButtonValue = TypedComponent<L10n>;
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[derive(Default)]
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ pub struct Form {
|
|||
action : AttributeValue,
|
||||
charset : AttributeValue,
|
||||
method : FormMethod,
|
||||
stuff : MixComponents,
|
||||
stuff : ArcComponents,
|
||||
template : String,
|
||||
}
|
||||
|
||||
|
|
@ -121,12 +121,12 @@ impl Form {
|
|||
}
|
||||
|
||||
pub fn with_element(mut self, element: impl ComponentTrait) -> Self {
|
||||
self.stuff.alter(MixOp::Add(ComponentArc::with(element)));
|
||||
self.stuff.alter(ArcOp::Add(ArcComponent::with(element)));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_elements(&mut self, op: MixOp) -> &mut Self {
|
||||
pub fn alter_elements(&mut self, op: ArcOp) -> &mut Self {
|
||||
self.stuff.alter(op);
|
||||
self
|
||||
}
|
||||
|
|
@ -155,7 +155,7 @@ impl Form {
|
|||
&self.method
|
||||
}
|
||||
|
||||
pub fn elements(&self) -> &MixComponents {
|
||||
pub fn elements(&self) -> &ArcComponents {
|
||||
&self.stuff
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ pub enum InputType {
|
|||
Url,
|
||||
}
|
||||
|
||||
type InputLabel = ComponentOne<L10n>;
|
||||
type InputHelpText = ComponentOne<L10n>;
|
||||
type InputLabel = TypedComponent<L10n>;
|
||||
type InputHelpText = TypedComponent<L10n>;
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[derive(Default)]
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ pub struct Column {
|
|||
id : IdentifierValue,
|
||||
classes : Classes,
|
||||
size : ColumnSize,
|
||||
stuff : MixComponents,
|
||||
stuff : ArcComponents,
|
||||
template : String,
|
||||
}
|
||||
|
||||
|
|
@ -136,12 +136,12 @@ impl Column {
|
|||
}
|
||||
|
||||
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
|
||||
self.stuff.alter(MixOp::Add(ComponentArc::with(component)));
|
||||
self.stuff.alter(ArcOp::Add(ArcComponent::with(component)));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_components(&mut self, op: MixOp) -> &mut Self {
|
||||
pub fn alter_components(&mut self, op: ArcOp) -> &mut Self {
|
||||
self.stuff.alter(op);
|
||||
self
|
||||
}
|
||||
|
|
@ -162,7 +162,7 @@ impl Column {
|
|||
&self.size
|
||||
}
|
||||
|
||||
pub fn components(&self) -> &MixComponents {
|
||||
pub fn components(&self) -> &ArcComponents {
|
||||
&self.stuff
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ pub struct Row {
|
|||
renderable: Renderable,
|
||||
id : IdentifierValue,
|
||||
classes : Classes,
|
||||
columns : LisComponents<grid::Column>,
|
||||
cols : TypedComponents<grid::Column>,
|
||||
template : String,
|
||||
}
|
||||
|
||||
|
|
@ -83,13 +83,13 @@ impl Row {
|
|||
}
|
||||
|
||||
pub fn with_column(mut self, column: grid::Column) -> Self {
|
||||
self.columns.alter(LisOp::Add(ComponentOne::with(column)));
|
||||
self.cols.alter(TypedOp::Add(TypedComponent::with(column)));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_columns(&mut self, op: LisOp<grid::Column>) -> &mut Self {
|
||||
self.columns.alter(op);
|
||||
pub fn alter_columns(&mut self, op: TypedOp<grid::Column>) -> &mut Self {
|
||||
self.cols.alter(op);
|
||||
self
|
||||
}
|
||||
|
||||
|
|
@ -105,8 +105,8 @@ impl Row {
|
|||
&self.classes
|
||||
}
|
||||
|
||||
pub fn columns(&self) -> &LisComponents<grid::Column> {
|
||||
&self.columns
|
||||
pub fn columns(&self) -> &TypedComponents<grid::Column> {
|
||||
&self.cols
|
||||
}
|
||||
|
||||
pub fn template(&self) -> &str {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ pub enum HeadingDisplay {
|
|||
Subtitle,
|
||||
}
|
||||
|
||||
type HeadingText = ComponentOne<L10n>;
|
||||
type HeadingText = TypedComponent<L10n>;
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[derive(Default)]
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ pub struct Paragraph {
|
|||
renderable: Renderable,
|
||||
id : IdentifierValue,
|
||||
classes : Classes,
|
||||
stuff : MixComponents,
|
||||
stuff : ArcComponents,
|
||||
display : ParagraphDisplay,
|
||||
template : String,
|
||||
}
|
||||
|
|
@ -90,12 +90,12 @@ impl Paragraph {
|
|||
}
|
||||
|
||||
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
|
||||
self.stuff.alter(MixOp::Add(ComponentArc::with(component)));
|
||||
self.stuff.alter(ArcOp::Add(ArcComponent::with(component)));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_components(&mut self, op: MixOp) -> &mut Self {
|
||||
pub fn alter_components(&mut self, op: ArcOp) -> &mut Self {
|
||||
self.stuff.alter(op);
|
||||
self
|
||||
}
|
||||
|
|
@ -130,7 +130,7 @@ impl Paragraph {
|
|||
&self.classes
|
||||
}
|
||||
|
||||
pub fn components(&self) -> &MixComponents {
|
||||
pub fn components(&self) -> &ArcComponents {
|
||||
&self.stuff
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ use crate::LOCALES_MINIMAL;
|
|||
|
||||
new_handle!(COMPONENT_BRANDING);
|
||||
|
||||
type SiteSlogan = ComponentOne<L10n>;
|
||||
type SiteLogo = ComponentOne<Image>;
|
||||
type SiteSlogan = TypedComponent<L10n>;
|
||||
type SiteLogo = TypedComponent<Image>;
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub struct SiteBranding {
|
||||
|
|
|
|||
|
|
@ -9,16 +9,10 @@ mod definition;
|
|||
pub use definition::{component_as_mut, component_as_ref, ComponentBase, ComponentTrait};
|
||||
|
||||
mod arc;
|
||||
pub use arc::ComponentArc;
|
||||
pub use arc::{ArcComponent, ArcComponents, ArcOp};
|
||||
|
||||
mod mix;
|
||||
pub use mix::{MixComponents, MixOp};
|
||||
|
||||
mod one;
|
||||
pub use one::ComponentOne;
|
||||
|
||||
mod lis;
|
||||
pub use lis::{LisComponents, LisOp};
|
||||
mod typed;
|
||||
pub use typed::{TypedComponent, TypedComponents, TypedOp};
|
||||
|
||||
pub mod html;
|
||||
pub mod l10n;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::core::component::{ComponentTrait, Context};
|
||||
use crate::html::Markup;
|
||||
use crate::html::{html, Markup};
|
||||
use crate::{new_handle, Handle, Weight};
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
|
@ -20,21 +20,21 @@ impl ComponentTrait for ComponentNull {
|
|||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ComponentArc(Arc<RwLock<dyn ComponentTrait>>);
|
||||
pub struct ArcComponent(Arc<RwLock<dyn ComponentTrait>>);
|
||||
|
||||
impl Default for ComponentArc {
|
||||
impl Default for ArcComponent {
|
||||
fn default() -> Self {
|
||||
ComponentArc(Arc::new(RwLock::new(ComponentNull)))
|
||||
ArcComponent(Arc::new(RwLock::new(ComponentNull)))
|
||||
}
|
||||
}
|
||||
|
||||
impl ComponentArc {
|
||||
impl ArcComponent {
|
||||
pub fn new() -> Self {
|
||||
ComponentArc::default()
|
||||
ArcComponent::default()
|
||||
}
|
||||
|
||||
pub fn with(component: impl ComponentTrait) -> Self {
|
||||
ComponentArc(Arc::new(RwLock::new(component)))
|
||||
ArcComponent(Arc::new(RwLock::new(component)))
|
||||
}
|
||||
|
||||
pub fn set(&mut self, component: impl ComponentTrait) {
|
||||
|
|
@ -53,9 +53,104 @@ impl ComponentArc {
|
|||
self.0.read().unwrap().weight()
|
||||
}
|
||||
|
||||
// ComponentArc PREPARE.
|
||||
// ArcComponent PREPARE.
|
||||
|
||||
pub fn prepare(&self, cx: &mut Context) -> Markup {
|
||||
self.0.write().unwrap().prepare(cx)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum ArcOp {
|
||||
Add(ArcComponent),
|
||||
AddAfterId(&'static str, ArcComponent),
|
||||
AddBeforeId(&'static str, ArcComponent),
|
||||
AddFirst(ArcComponent),
|
||||
RemoveById(&'static str),
|
||||
ReplaceById(&'static str, ArcComponent),
|
||||
Reset,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct ArcComponents(Vec<ArcComponent>);
|
||||
|
||||
impl ArcComponents {
|
||||
pub fn new() -> Self {
|
||||
ArcComponents::default()
|
||||
}
|
||||
|
||||
pub fn with(arc: ArcComponent) -> Self {
|
||||
let mut components = ArcComponents::new();
|
||||
components.alter(ArcOp::Add(arc));
|
||||
components
|
||||
}
|
||||
|
||||
pub(crate) fn merge(mixes: &[Option<&ArcComponents>]) -> Self {
|
||||
let mut components = ArcComponents::default();
|
||||
for m in mixes.iter().flatten() {
|
||||
components.0.append(&mut m.0.clone());
|
||||
}
|
||||
components
|
||||
}
|
||||
|
||||
// ArcComponents BUILDER.
|
||||
|
||||
pub fn alter(&mut self, op: ArcOp) -> &mut Self {
|
||||
match op {
|
||||
ArcOp::Add(arc) => self.0.push(arc),
|
||||
ArcOp::AddAfterId(id, arc) => {
|
||||
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
|
||||
Some(index) => self.0.insert(index + 1, arc),
|
||||
_ => self.0.push(arc),
|
||||
}
|
||||
}
|
||||
ArcOp::AddBeforeId(id, arc) => {
|
||||
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
|
||||
Some(index) => self.0.insert(index, arc),
|
||||
_ => self.0.insert(0, arc),
|
||||
}
|
||||
}
|
||||
ArcOp::AddFirst(arc) => self.0.insert(0, arc),
|
||||
ArcOp::RemoveById(id) => {
|
||||
if let Some(index) = self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
|
||||
self.0.remove(index);
|
||||
}
|
||||
}
|
||||
ArcOp::ReplaceById(id, arc) => {
|
||||
for c in self.0.iter_mut() {
|
||||
if c.id().as_deref() == Some(id) {
|
||||
*c = arc;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ArcOp::Reset => self.0.clear(),
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
// ArcComponents GETTERS.
|
||||
|
||||
pub fn get_by_id(&self, id: &'static str) -> Option<&ArcComponent> {
|
||||
self.0.iter().find(|&c| c.id().as_deref() == Some(id))
|
||||
}
|
||||
|
||||
pub fn iter_by_id(&self, id: &'static str) -> impl Iterator<Item = &ArcComponent> {
|
||||
self.0.iter().filter(|&c| c.id().as_deref() == Some(id))
|
||||
}
|
||||
|
||||
pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ArcComponent> {
|
||||
self.0.iter().filter(move |&c| c.handle() == handle)
|
||||
}
|
||||
|
||||
// ArcComponents PREPARE.
|
||||
|
||||
pub fn prepare(&self, cx: &mut Context) -> Markup {
|
||||
let mut components = self.0.clone();
|
||||
components.sort_by_key(|c| c.weight());
|
||||
html! {
|
||||
@for c in components.iter() {
|
||||
" " (c.prepare(cx)) " "
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,90 +0,0 @@
|
|||
use crate::core::component::{ComponentOne, ComponentTrait, Context};
|
||||
use crate::html::{html, Markup};
|
||||
use crate::Handle;
|
||||
|
||||
pub enum LisOp<T: ComponentTrait + Default> {
|
||||
Add(ComponentOne<T>),
|
||||
AddAfterId(&'static str, ComponentOne<T>),
|
||||
AddBeforeId(&'static str, ComponentOne<T>),
|
||||
AddFirst(ComponentOne<T>),
|
||||
RemoveById(&'static str),
|
||||
ReplaceById(&'static str, ComponentOne<T>),
|
||||
Reset,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct LisComponents<T: ComponentTrait + Default>(Vec<ComponentOne<T>>);
|
||||
|
||||
impl<T: ComponentTrait + Default> LisComponents<T> {
|
||||
pub fn new() -> Self {
|
||||
LisComponents::<T>::default()
|
||||
}
|
||||
|
||||
pub fn with(one: ComponentOne<T>) -> Self {
|
||||
let mut components = LisComponents::new();
|
||||
components.alter(LisOp::Add(one));
|
||||
components
|
||||
}
|
||||
|
||||
// LisComponents BUILDER.
|
||||
|
||||
pub fn alter(&mut self, op: LisOp<T>) -> &mut Self {
|
||||
match op {
|
||||
LisOp::Add(one) => self.0.push(one),
|
||||
LisOp::AddAfterId(id, one) => {
|
||||
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
|
||||
Some(index) => self.0.insert(index + 1, one),
|
||||
_ => self.0.push(one),
|
||||
}
|
||||
}
|
||||
LisOp::AddBeforeId(id, one) => {
|
||||
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
|
||||
Some(index) => self.0.insert(index, one),
|
||||
_ => self.0.insert(0, one),
|
||||
}
|
||||
}
|
||||
LisOp::AddFirst(one) => self.0.insert(0, one),
|
||||
LisOp::RemoveById(id) => {
|
||||
if let Some(index) = self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
|
||||
self.0.remove(index);
|
||||
}
|
||||
}
|
||||
LisOp::ReplaceById(id, one) => {
|
||||
for c in self.0.iter_mut() {
|
||||
if c.id().as_deref() == Some(id) {
|
||||
*c = one;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
LisOp::Reset => self.0.clear(),
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
// LisComponents GETTERS.
|
||||
|
||||
pub fn get_by_id(&self, id: &'static str) -> Option<&ComponentOne<T>> {
|
||||
self.0.iter().find(|&c| c.id().as_deref() == Some(id))
|
||||
}
|
||||
|
||||
pub fn iter_by_id(&self, id: &'static str) -> impl Iterator<Item = &ComponentOne<T>> {
|
||||
self.0.iter().filter(|&c| c.id().as_deref() == Some(id))
|
||||
}
|
||||
|
||||
pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ComponentOne<T>> {
|
||||
self.0.iter().filter(move |&c| c.handle() == handle)
|
||||
}
|
||||
|
||||
// LisComponents PREPARE.
|
||||
|
||||
pub fn prepare(&self, cx: &mut Context) -> Markup {
|
||||
let mut components = self.0.clone();
|
||||
components.sort_by_key(|c| c.weight());
|
||||
html! {
|
||||
@for c in components.iter() {
|
||||
" " (c.prepare(cx)) " "
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
use crate::core::component::{ComponentArc, Context};
|
||||
use crate::html::{html, Markup};
|
||||
use crate::Handle;
|
||||
|
||||
pub enum MixOp {
|
||||
Add(ComponentArc),
|
||||
AddAfterId(&'static str, ComponentArc),
|
||||
AddBeforeId(&'static str, ComponentArc),
|
||||
AddFirst(ComponentArc),
|
||||
RemoveById(&'static str),
|
||||
ReplaceById(&'static str, ComponentArc),
|
||||
Reset,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct MixComponents(Vec<ComponentArc>);
|
||||
|
||||
impl MixComponents {
|
||||
pub fn new() -> Self {
|
||||
MixComponents::default()
|
||||
}
|
||||
|
||||
pub fn with(arc: ComponentArc) -> Self {
|
||||
let mut components = MixComponents::new();
|
||||
components.alter(MixOp::Add(arc));
|
||||
components
|
||||
}
|
||||
|
||||
pub(crate) fn merge(mixes: &[Option<&MixComponents>]) -> Self {
|
||||
let mut components = MixComponents::default();
|
||||
for m in mixes.iter().flatten() {
|
||||
components.0.append(&mut m.0.clone());
|
||||
}
|
||||
components
|
||||
}
|
||||
|
||||
// MixComponents BUILDER.
|
||||
|
||||
pub fn alter(&mut self, op: MixOp) -> &mut Self {
|
||||
match op {
|
||||
MixOp::Add(arc) => self.0.push(arc),
|
||||
MixOp::AddAfterId(id, arc) => {
|
||||
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
|
||||
Some(index) => self.0.insert(index + 1, arc),
|
||||
_ => self.0.push(arc),
|
||||
}
|
||||
}
|
||||
MixOp::AddBeforeId(id, arc) => {
|
||||
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
|
||||
Some(index) => self.0.insert(index, arc),
|
||||
_ => self.0.insert(0, arc),
|
||||
}
|
||||
}
|
||||
MixOp::AddFirst(arc) => self.0.insert(0, arc),
|
||||
MixOp::RemoveById(id) => {
|
||||
if let Some(index) = self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
|
||||
self.0.remove(index);
|
||||
}
|
||||
}
|
||||
MixOp::ReplaceById(id, arc) => {
|
||||
for c in self.0.iter_mut() {
|
||||
if c.id().as_deref() == Some(id) {
|
||||
*c = arc;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
MixOp::Reset => self.0.clear(),
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
// MixComponents GETTERS.
|
||||
|
||||
pub fn get_by_id(&self, id: &'static str) -> Option<&ComponentArc> {
|
||||
self.0.iter().find(|&c| c.id().as_deref() == Some(id))
|
||||
}
|
||||
|
||||
pub fn iter_by_id(&self, id: &'static str) -> impl Iterator<Item = &ComponentArc> {
|
||||
self.0.iter().filter(|&c| c.id().as_deref() == Some(id))
|
||||
}
|
||||
|
||||
pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ComponentArc> {
|
||||
self.0.iter().filter(move |&c| c.handle() == handle)
|
||||
}
|
||||
|
||||
// MixComponents PREPARE.
|
||||
|
||||
pub fn prepare(&self, cx: &mut Context) -> Markup {
|
||||
let mut components = self.0.clone();
|
||||
components.sort_by_key(|c| c.weight());
|
||||
html! {
|
||||
@for c in components.iter() {
|
||||
" " (c.prepare(cx)) " "
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
use crate::core::component::{ComponentTrait, Context};
|
||||
use crate::html::Markup;
|
||||
use crate::{Handle, Weight};
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ComponentOne<T: ComponentTrait + Default>(Arc<RwLock<T>>);
|
||||
|
||||
impl<T: ComponentTrait + Default> Clone for ComponentOne<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ComponentTrait + Default> ComponentOne<T> {
|
||||
pub fn new() -> Self {
|
||||
ComponentOne::<T>::default()
|
||||
}
|
||||
|
||||
pub fn with(component: T) -> Self {
|
||||
ComponentOne(Arc::new(RwLock::new(component)))
|
||||
}
|
||||
|
||||
pub fn set(&mut self, component: T) {
|
||||
self.0 = Arc::new(RwLock::new(component));
|
||||
}
|
||||
|
||||
pub(crate) fn handle(&self) -> Handle {
|
||||
self.0.read().unwrap().handle()
|
||||
}
|
||||
|
||||
pub(crate) fn id(&self) -> Option<String> {
|
||||
self.0.read().unwrap().id()
|
||||
}
|
||||
|
||||
pub(crate) fn weight(&self) -> Weight {
|
||||
self.0.read().unwrap().weight()
|
||||
}
|
||||
|
||||
// ComponentOne PREPARE.
|
||||
|
||||
pub fn prepare(&self, cx: &mut Context) -> Markup {
|
||||
self.0.write().unwrap().prepare(cx)
|
||||
}
|
||||
}
|
||||
133
pagetop/src/core/component/typed.rs
Normal file
133
pagetop/src/core/component/typed.rs
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
use crate::core::component::{ComponentTrait, Context};
|
||||
use crate::html::{html, Markup};
|
||||
use crate::{Handle, Weight};
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TypedComponent<T: ComponentTrait + Default>(Arc<RwLock<T>>);
|
||||
|
||||
impl<T: ComponentTrait + Default> Clone for TypedComponent<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ComponentTrait + Default> TypedComponent<T> {
|
||||
pub fn new() -> Self {
|
||||
TypedComponent::<T>::default()
|
||||
}
|
||||
|
||||
pub fn with(component: T) -> Self {
|
||||
TypedComponent(Arc::new(RwLock::new(component)))
|
||||
}
|
||||
|
||||
pub fn set(&mut self, component: T) {
|
||||
self.0 = Arc::new(RwLock::new(component));
|
||||
}
|
||||
|
||||
pub(crate) fn handle(&self) -> Handle {
|
||||
self.0.read().unwrap().handle()
|
||||
}
|
||||
|
||||
pub(crate) fn id(&self) -> Option<String> {
|
||||
self.0.read().unwrap().id()
|
||||
}
|
||||
|
||||
pub(crate) fn weight(&self) -> Weight {
|
||||
self.0.read().unwrap().weight()
|
||||
}
|
||||
|
||||
// TypedComponent PREPARE.
|
||||
|
||||
pub fn prepare(&self, cx: &mut Context) -> Markup {
|
||||
self.0.write().unwrap().prepare(cx)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum TypedOp<T: ComponentTrait + Default> {
|
||||
Add(TypedComponent<T>),
|
||||
AddAfterId(&'static str, TypedComponent<T>),
|
||||
AddBeforeId(&'static str, TypedComponent<T>),
|
||||
AddFirst(TypedComponent<T>),
|
||||
RemoveById(&'static str),
|
||||
ReplaceById(&'static str, TypedComponent<T>),
|
||||
Reset,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct TypedComponents<T: ComponentTrait + Default>(Vec<TypedComponent<T>>);
|
||||
|
||||
impl<T: ComponentTrait + Default> TypedComponents<T> {
|
||||
pub fn new() -> Self {
|
||||
TypedComponents::<T>::default()
|
||||
}
|
||||
|
||||
pub fn with(one: TypedComponent<T>) -> Self {
|
||||
let mut components = TypedComponents::new();
|
||||
components.alter(TypedOp::Add(one));
|
||||
components
|
||||
}
|
||||
|
||||
// TypedComponents BUILDER.
|
||||
|
||||
pub fn alter(&mut self, op: TypedOp<T>) -> &mut Self {
|
||||
match op {
|
||||
TypedOp::Add(one) => self.0.push(one),
|
||||
TypedOp::AddAfterId(id, one) => {
|
||||
match self.0.iter().position(|c| c.id().as_deref() == Some(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().as_deref() == Some(id)) {
|
||||
Some(index) => self.0.insert(index, one),
|
||||
_ => self.0.insert(0, one),
|
||||
}
|
||||
}
|
||||
TypedOp::AddFirst(one) => self.0.insert(0, one),
|
||||
TypedOp::RemoveById(id) => {
|
||||
if let Some(index) = self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
|
||||
self.0.remove(index);
|
||||
}
|
||||
}
|
||||
TypedOp::ReplaceById(id, one) => {
|
||||
for c in self.0.iter_mut() {
|
||||
if c.id().as_deref() == Some(id) {
|
||||
*c = one;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
TypedOp::Reset => self.0.clear(),
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
// TypedComponents GETTERS.
|
||||
|
||||
pub fn get_by_id(&self, id: &'static str) -> Option<&TypedComponent<T>> {
|
||||
self.0.iter().find(|&c| c.id().as_deref() == Some(id))
|
||||
}
|
||||
|
||||
pub fn iter_by_id(&self, id: &'static str) -> impl Iterator<Item = &TypedComponent<T>> {
|
||||
self.0.iter().filter(|&c| c.id().as_deref() == Some(id))
|
||||
}
|
||||
|
||||
pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &TypedComponent<T>> {
|
||||
self.0.iter().filter(move |&c| c.handle() == handle)
|
||||
}
|
||||
|
||||
// TypedComponents PREPARE.
|
||||
|
||||
pub fn prepare(&self, cx: &mut Context) -> Markup {
|
||||
let mut components = self.0.clone();
|
||||
components.sort_by_key(|c| c.weight());
|
||||
html! {
|
||||
@for c in components.iter() {
|
||||
" " (c.prepare(cx)) " "
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::core::component::{ComponentArc, MixComponents, MixOp};
|
||||
use crate::core::component::{ArcComponent, ArcComponents, ArcOp};
|
||||
use crate::core::theme::ThemeRef;
|
||||
use crate::{Handle, LazyStatic};
|
||||
|
||||
|
|
@ -12,33 +12,33 @@ static COMMON_REGIONS: LazyStatic<RwLock<ComponentsRegions>> =
|
|||
LazyStatic::new(|| RwLock::new(ComponentsRegions::new()));
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ComponentsRegions(HashMap<&'static str, MixComponents>);
|
||||
pub struct ComponentsRegions(HashMap<&'static str, ArcComponents>);
|
||||
|
||||
impl ComponentsRegions {
|
||||
pub fn new() -> Self {
|
||||
ComponentsRegions::default()
|
||||
}
|
||||
|
||||
pub fn with(region: &'static str, arc: ComponentArc) -> Self {
|
||||
pub fn with(region: &'static str, arc: ArcComponent) -> Self {
|
||||
let mut regions = ComponentsRegions::new();
|
||||
regions.add_in(region, arc);
|
||||
regions
|
||||
}
|
||||
|
||||
pub fn add_in(&mut self, region: &'static str, arc: ComponentArc) {
|
||||
pub fn add_in(&mut self, region: &'static str, arc: ArcComponent) {
|
||||
if let Some(region) = self.0.get_mut(region) {
|
||||
region.alter(MixOp::Add(arc));
|
||||
region.alter(ArcOp::Add(arc));
|
||||
} else {
|
||||
self.0.insert(region, MixComponents::with(arc));
|
||||
self.0.insert(region, ArcComponents::with(arc));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_components(&self, theme: ThemeRef, region: &str) -> MixComponents {
|
||||
pub fn get_components(&self, theme: ThemeRef, region: &str) -> ArcComponents {
|
||||
let common = COMMON_REGIONS.read().unwrap();
|
||||
if let Some(hm) = THEME_REGIONS.read().unwrap().get(&theme.handle()) {
|
||||
MixComponents::merge(&[common.0.get(region), self.0.get(region), hm.0.get(region)])
|
||||
ArcComponents::merge(&[common.0.get(region), self.0.get(region), hm.0.get(region)])
|
||||
} else {
|
||||
MixComponents::merge(&[common.0.get(region), self.0.get(region)])
|
||||
ArcComponents::merge(&[common.0.get(region), self.0.get(region)])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -48,7 +48,7 @@ pub enum Region {
|
|||
OfTheme(ThemeRef, &'static str),
|
||||
}
|
||||
|
||||
pub fn add_component_in(region: Region, arc: ComponentArc) {
|
||||
pub fn add_component_in(region: Region, arc: ArcComponent) {
|
||||
match region {
|
||||
Region::Named(name) => {
|
||||
COMMON_REGIONS.write().unwrap().add_in(name, arc);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ mod action;
|
|||
pub use action::*;
|
||||
|
||||
use crate::core::component::l10n::L10n;
|
||||
use crate::core::component::{ComponentArc, ComponentOne, ComponentTrait, Context, ContextOp};
|
||||
use crate::core::component::{ArcComponent, ComponentTrait, Context, ContextOp, TypedComponent};
|
||||
use crate::core::theme::ComponentsRegions;
|
||||
use crate::html::{html, Classes, ClassesOp, Favicon, Markup, DOCTYPE};
|
||||
use crate::response::fatal_error::FatalError;
|
||||
|
|
@ -12,8 +12,8 @@ use unic_langid::CharacterDirection;
|
|||
|
||||
pub use actix_web::Result as ResultPage;
|
||||
|
||||
type PageTitle = ComponentOne<L10n>;
|
||||
type PageDescription = ComponentOne<L10n>;
|
||||
type PageTitle = TypedComponent<L10n>;
|
||||
type PageDescription = TypedComponent<L10n>;
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub struct Page {
|
||||
|
|
@ -90,7 +90,7 @@ impl Page {
|
|||
|
||||
#[fn_builder]
|
||||
pub fn alter_in(&mut self, region: &'static str, component: impl ComponentTrait) -> &mut Self {
|
||||
self.regions.add_in(region, ComponentArc::with(component));
|
||||
self.regions.add_in(region, ArcComponent::with(component));
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue