🧑💻 Simplify component vector handling
This commit is contained in:
parent
400a492311
commit
d7acf0c3d1
17 changed files with 154 additions and 221 deletions
|
|
@ -90,14 +90,14 @@ impl Block {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fn_builder]
|
#[fn_builder]
|
||||||
pub fn alter_components(&mut self, op: MixedOp) -> &mut Self {
|
pub fn alter_components(&mut self, op: AnyOp) -> &mut Self {
|
||||||
self.mixed.alter_value(op);
|
self.mixed.alter_value(op);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
|
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
|
||||||
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component)));
|
self.mixed.alter_value(AnyOp::Add(AnyComponent::with(component)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -139,14 +139,14 @@ impl Item {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fn_builder]
|
#[fn_builder]
|
||||||
pub fn alter_components(&mut self, op: MixedOp) -> &mut Self {
|
pub fn alter_components(&mut self, op: AnyOp) -> &mut Self {
|
||||||
self.mixed.alter_value(op);
|
self.mixed.alter_value(op);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
|
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
|
||||||
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component)));
|
self.mixed.alter_value(AnyOp::Add(AnyComponent::with(component)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -97,15 +97,15 @@ impl Form {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[fn_builder]
|
||||||
pub fn add_element(mut self, element: impl ComponentTrait) -> Self {
|
pub fn alter_elements(&mut self, op: AnyOp) -> &mut Self {
|
||||||
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(element)));
|
self.mixed.alter_value(op);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fn_builder]
|
#[rustfmt::skip]
|
||||||
pub fn alter_elements(&mut self, op: MixedOp) -> &mut Self {
|
pub fn add_element(mut self, element: impl ComponentTrait) -> Self {
|
||||||
self.mixed.alter_value(op);
|
self.mixed.alter_value(AnyOp::Add(AnyComponent::with(element)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ use crate::prelude::*;
|
||||||
|
|
||||||
use super::Submenu;
|
use super::Submenu;
|
||||||
|
|
||||||
type Content = OneComponent<Html>;
|
type Content = TypedComponent<Html>;
|
||||||
type SubmenuItems = OneComponent<Submenu>;
|
type SubmenuItems = TypedComponent<Submenu>;
|
||||||
|
|
||||||
#[derive(AutoDefault)]
|
#[derive(AutoDefault)]
|
||||||
pub enum ElementType {
|
pub enum ElementType {
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ pub struct Group {
|
||||||
id : OptionId,
|
id : OptionId,
|
||||||
weight : Weight,
|
weight : Weight,
|
||||||
renderable: Renderable,
|
renderable: Renderable,
|
||||||
elements : TypedComponents<Element>,
|
elements : MixedComponents,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ComponentTrait for Group {
|
impl ComponentTrait for Group {
|
||||||
|
|
@ -58,21 +58,21 @@ impl Group {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[fn_builder]
|
||||||
pub fn add_element(mut self, element: Element) -> Self {
|
pub fn alter_elements(&mut self, op: TypedOp<Element>) -> &mut Self {
|
||||||
self.elements.alter_value(TypedOp::Add(OneComponent::with(element)));
|
self.elements.alter_typed(op);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fn_builder]
|
#[rustfmt::skip]
|
||||||
pub fn alter_elements(&mut self, op: TypedOp<Element>) -> &mut Self {
|
pub fn add_element(mut self, element: Element) -> Self {
|
||||||
self.elements.alter_value(op);
|
self.elements.alter_value(AnyOp::Add(AnyComponent::with(element)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group GETTERS.
|
// Group GETTERS.
|
||||||
|
|
||||||
pub fn elements(&self) -> &TypedComponents<Element> {
|
pub fn elements(&self) -> &MixedComponents {
|
||||||
&self.elements
|
&self.elements
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,9 @@ use crate::prelude::*;
|
||||||
use super::{Megamenu, Submenu};
|
use super::{Megamenu, Submenu};
|
||||||
|
|
||||||
type Label = L10n;
|
type Label = L10n;
|
||||||
type Content = OneComponent<Html>;
|
type Content = TypedComponent<Html>;
|
||||||
type SubmenuItems = OneComponent<Submenu>;
|
type SubmenuItems = TypedComponent<Submenu>;
|
||||||
type MegamenuGroups = OneComponent<Megamenu>;
|
type MegamenuGroups = TypedComponent<Megamenu>;
|
||||||
|
|
||||||
#[derive(AutoDefault)]
|
#[derive(AutoDefault)]
|
||||||
pub enum ItemType {
|
pub enum ItemType {
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ pub struct Megamenu {
|
||||||
id : OptionId,
|
id : OptionId,
|
||||||
weight : Weight,
|
weight : Weight,
|
||||||
renderable: Renderable,
|
renderable: Renderable,
|
||||||
groups : TypedComponents<Group>,
|
groups : MixedComponents,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ComponentTrait for Megamenu {
|
impl ComponentTrait for Megamenu {
|
||||||
|
|
@ -58,21 +58,21 @@ impl Megamenu {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[fn_builder]
|
||||||
pub fn add_group(mut self, group: Group) -> Self {
|
pub fn alter_groups(&mut self, op: TypedOp<Group>) -> &mut Self {
|
||||||
self.groups.alter_value(TypedOp::Add(OneComponent::with(group)));
|
self.groups.alter_typed(op);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fn_builder]
|
#[rustfmt::skip]
|
||||||
pub fn alter_groups(&mut self, op: TypedOp<Group>) -> &mut Self {
|
pub fn add_group(mut self, group: Group) -> Self {
|
||||||
self.groups.alter_value(op);
|
self.groups.alter_value(AnyOp::Add(AnyComponent::with(group)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
// Megamenu GETTERS.
|
// Megamenu GETTERS.
|
||||||
|
|
||||||
pub fn groups(&self) -> &TypedComponents<Group> {
|
pub fn groups(&self) -> &MixedComponents {
|
||||||
&self.groups
|
&self.groups
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ pub struct Menu {
|
||||||
id : OptionId,
|
id : OptionId,
|
||||||
weight : Weight,
|
weight : Weight,
|
||||||
renderable: Renderable,
|
renderable: Renderable,
|
||||||
items : TypedComponents<Item>,
|
items : MixedComponents,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ComponentTrait for Menu {
|
impl ComponentTrait for Menu {
|
||||||
|
|
@ -86,21 +86,21 @@ impl Menu {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[fn_builder]
|
||||||
pub fn add_item(mut self, item: Item) -> Self {
|
pub fn alter_items(&mut self, op: TypedOp<Item>) -> &mut Self {
|
||||||
self.items.alter_value(TypedOp::Add(OneComponent::with(item)));
|
self.items.alter_typed(op);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fn_builder]
|
#[rustfmt::skip]
|
||||||
pub fn alter_items(&mut self, op: TypedOp<Item>) -> &mut Self {
|
pub fn add_item(mut self, item: Item) -> Self {
|
||||||
self.items.alter_value(op);
|
self.items.alter_value(AnyOp::Add(AnyComponent::with(item)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
// Menu GETTERS.
|
// Menu GETTERS.
|
||||||
|
|
||||||
pub fn items(&self) -> &TypedComponents<Item> {
|
pub fn items(&self) -> &MixedComponents {
|
||||||
&self.items
|
&self.items
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ pub struct Submenu {
|
||||||
weight : Weight,
|
weight : Weight,
|
||||||
renderable: Renderable,
|
renderable: Renderable,
|
||||||
title : OptionTranslated,
|
title : OptionTranslated,
|
||||||
items : TypedComponents<Item>,
|
items : MixedComponents,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ComponentTrait for Submenu {
|
impl ComponentTrait for Submenu {
|
||||||
|
|
@ -70,15 +70,15 @@ impl Submenu {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[fn_builder]
|
||||||
pub fn add_item(mut self, item: Item) -> Self {
|
pub fn alter_items(&mut self, op: TypedOp<Item>) -> &mut Self {
|
||||||
self.items.alter_value(TypedOp::Add(OneComponent::with(item)));
|
self.items.alter_typed(op);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fn_builder]
|
#[rustfmt::skip]
|
||||||
pub fn alter_items(&mut self, op: TypedOp<Item>) -> &mut Self {
|
pub fn add_item(mut self, item: Item) -> Self {
|
||||||
self.items.alter_value(op);
|
self.items.alter_value(AnyOp::Add(AnyComponent::with(item)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,7 +88,7 @@ impl Submenu {
|
||||||
&self.title
|
&self.title
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn items(&self) -> &TypedComponents<Item> {
|
pub fn items(&self) -> &MixedComponents {
|
||||||
&self.items
|
&self.items
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -80,14 +80,14 @@ impl Paragraph {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fn_builder]
|
#[fn_builder]
|
||||||
pub fn alter_components(&mut self, op: MixedOp) -> &mut Self {
|
pub fn alter_components(&mut self, op: AnyOp) -> &mut Self {
|
||||||
self.mixed.alter_value(op);
|
self.mixed.alter_value(op);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
|
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
|
||||||
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component)));
|
self.mixed.alter_value(AnyOp::Add(AnyComponent::with(component)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,7 @@ pub use definition::{component_as_mut, component_as_ref, ComponentBase, Componen
|
||||||
mod classes;
|
mod classes;
|
||||||
pub use classes::{ComponentClasses, ComponentClassesOp};
|
pub use classes::{ComponentClasses, ComponentClassesOp};
|
||||||
|
|
||||||
mod arc_mixed;
|
mod mixed;
|
||||||
pub use arc_mixed::{AnyComponent, MixedComponents, MixedOp};
|
pub use mixed::MixedComponents;
|
||||||
|
pub use mixed::{AnyComponent, AnyOp};
|
||||||
mod arc_typed;
|
pub use mixed::{TypedComponent, TypedOp};
|
||||||
pub use arc_typed::{OneComponent, TypedComponents, TypedOp};
|
|
||||||
|
|
|
||||||
|
|
@ -1,141 +0,0 @@
|
||||||
use crate::core::component::{ComponentTrait, Context};
|
|
||||||
use crate::html::{html, Markup};
|
|
||||||
use crate::{fn_builder, TypeId, Weight};
|
|
||||||
|
|
||||||
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
|
||||||
|
|
||||||
pub struct OneComponent<C: ComponentTrait>(Arc<RwLock<C>>);
|
|
||||||
|
|
||||||
impl<C: ComponentTrait> Clone for OneComponent<C> {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self(self.0.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<C: ComponentTrait> OneComponent<C> {
|
|
||||||
pub fn with(component: C) -> Self {
|
|
||||||
OneComponent(Arc::new(RwLock::new(component)))
|
|
||||||
}
|
|
||||||
|
|
||||||
// OneComponent BUILDER.
|
|
||||||
|
|
||||||
pub fn set(&mut self, component: C) {
|
|
||||||
self.0 = Arc::new(RwLock::new(component));
|
|
||||||
}
|
|
||||||
|
|
||||||
// OneComponent GETTERS.
|
|
||||||
|
|
||||||
pub fn get(&self) -> RwLockReadGuard<'_, C> {
|
|
||||||
self.0.read().unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_mut(&self) -> RwLockWriteGuard<'_, C> {
|
|
||||||
self.0.write().unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
// OneComponent RENDER.
|
|
||||||
|
|
||||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
|
||||||
self.0.write().unwrap().render(cx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// OneComponent HELPERS.
|
|
||||||
|
|
||||||
fn type_id(&self) -> TypeId {
|
|
||||||
self.0.read().unwrap().type_id()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn id(&self) -> String {
|
|
||||||
self.0.read().unwrap().id().unwrap_or_default()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn weight(&self) -> Weight {
|
|
||||||
self.0.read().unwrap().weight()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// *************************************************************************************************
|
|
||||||
|
|
||||||
pub enum TypedOp<C: ComponentTrait> {
|
|
||||||
Add(OneComponent<C>),
|
|
||||||
InsertAfterId(&'static str, OneComponent<C>),
|
|
||||||
InsertBeforeId(&'static str, OneComponent<C>),
|
|
||||||
Prepend(OneComponent<C>),
|
|
||||||
RemoveById(&'static str),
|
|
||||||
ReplaceById(&'static str, OneComponent<C>),
|
|
||||||
Reset,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
|
||||||
pub struct TypedComponents<C: ComponentTrait>(Vec<OneComponent<C>>);
|
|
||||||
|
|
||||||
impl<C: ComponentTrait + Default> TypedComponents<C> {
|
|
||||||
pub fn new(one: OneComponent<C>) -> Self {
|
|
||||||
TypedComponents::default().with_value(TypedOp::Add(one))
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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::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::InsertBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) {
|
|
||||||
Some(index) => self.0.insert(index, one),
|
|
||||||
_ => self.0.insert(0, one),
|
|
||||||
},
|
|
||||||
TypedOp::Prepend(one) => self.0.insert(0, one),
|
|
||||||
TypedOp::RemoveById(id) => {
|
|
||||||
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
|
|
||||||
self.0.remove(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TypedOp::ReplaceById(id, one) => {
|
|
||||||
for c in self.0.iter_mut() {
|
|
||||||
if c.id() == id {
|
|
||||||
*c = one;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TypedOp::Reset => self.0.clear(),
|
|
||||||
}
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
// TypedComponents GETTERS.
|
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
|
||||||
self.0.is_empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = &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 = &OneComponent<C>> {
|
|
||||||
self.0.iter().filter(move |&c| c.type_id() == type_id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TypedComponents RENDER.
|
|
||||||
|
|
||||||
pub fn render(&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.render(cx))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::base::component::add_base_assets;
|
use crate::base::component::add_base_assets;
|
||||||
use crate::core::component::MixedOp;
|
use crate::core::component::AnyOp;
|
||||||
use crate::core::theme::all::{theme_by_single_name, THEME_DEFAULT};
|
use crate::core::theme::all::{theme_by_single_name, THEME_DEFAULT};
|
||||||
use crate::core::theme::{ComponentsInRegions, ThemeRef};
|
use crate::core::theme::{ComponentsInRegions, ThemeRef};
|
||||||
use crate::html::{html, Assets, HeadScript, HeadStyles, JavaScript, Markup, StyleSheet};
|
use crate::html::{html, Assets, HeadScript, HeadStyles, JavaScript, Markup, StyleSheet};
|
||||||
|
|
@ -95,7 +95,7 @@ impl Context {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn alter_regions(&mut self, region: &'static str, op: MixedOp) -> &mut Self {
|
pub fn alter_regions(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
|
||||||
self.regions.alter_components(region, op);
|
self.regions.alter_components(region, op);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,43 @@ impl AnyComponent {
|
||||||
|
|
||||||
// *************************************************************************************************
|
// *************************************************************************************************
|
||||||
|
|
||||||
pub enum MixedOp {
|
pub struct TypedComponent<C: ComponentTrait>(Arc<RwLock<C>>);
|
||||||
|
|
||||||
|
impl<C: ComponentTrait> Clone for TypedComponent<C> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self(self.0.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C: ComponentTrait> TypedComponent<C> {
|
||||||
|
pub fn with(component: C) -> Self {
|
||||||
|
TypedComponent(Arc::new(RwLock::new(component)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TypedComponent GETTERS.
|
||||||
|
|
||||||
|
pub fn get(&self) -> RwLockReadGuard<'_, C> {
|
||||||
|
self.0.read().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_mut(&self) -> RwLockWriteGuard<'_, C> {
|
||||||
|
self.0.write().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_any(&self) -> AnyComponent {
|
||||||
|
AnyComponent(self.0.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TypedComponent RENDER.
|
||||||
|
|
||||||
|
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||||
|
self.0.write().unwrap().render(cx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// *************************************************************************************************
|
||||||
|
|
||||||
|
pub enum AnyOp {
|
||||||
Add(AnyComponent),
|
Add(AnyComponent),
|
||||||
InsertAfterId(&'static str, AnyComponent),
|
InsertAfterId(&'static str, AnyComponent),
|
||||||
InsertBeforeId(&'static str, AnyComponent),
|
InsertBeforeId(&'static str, AnyComponent),
|
||||||
|
|
@ -61,6 +97,16 @@ pub enum MixedOp {
|
||||||
Reset,
|
Reset,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum TypedOp<C: ComponentTrait> {
|
||||||
|
Add(TypedComponent<C>),
|
||||||
|
InsertAfterId(&'static str, TypedComponent<C>),
|
||||||
|
InsertBeforeId(&'static str, TypedComponent<C>),
|
||||||
|
Prepend(TypedComponent<C>),
|
||||||
|
RemoveById(&'static str),
|
||||||
|
ReplaceById(&'static str, TypedComponent<C>),
|
||||||
|
Reset,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
pub struct MixedComponents(Vec<AnyComponent>);
|
pub struct MixedComponents(Vec<AnyComponent>);
|
||||||
|
|
||||||
|
|
@ -70,7 +116,7 @@ impl MixedComponents {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with(any: AnyComponent) -> Self {
|
pub fn with(any: AnyComponent) -> Self {
|
||||||
MixedComponents::default().with_value(MixedOp::Add(any))
|
MixedComponents::default().with_value(AnyOp::Add(any))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn merge(mixes: &[Option<&MixedComponents>]) -> Self {
|
pub(crate) fn merge(mixes: &[Option<&MixedComponents>]) -> Self {
|
||||||
|
|
@ -84,24 +130,24 @@ impl MixedComponents {
|
||||||
// MixedComponents BUILDER.
|
// MixedComponents BUILDER.
|
||||||
|
|
||||||
#[fn_builder]
|
#[fn_builder]
|
||||||
pub fn alter_value(&mut self, op: MixedOp) -> &mut Self {
|
pub fn alter_value(&mut self, op: AnyOp) -> &mut Self {
|
||||||
match op {
|
match op {
|
||||||
MixedOp::Add(any) => self.0.push(any),
|
AnyOp::Add(any) => self.0.push(any),
|
||||||
MixedOp::InsertAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) {
|
AnyOp::InsertAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) {
|
||||||
Some(index) => self.0.insert(index + 1, any),
|
Some(index) => self.0.insert(index + 1, any),
|
||||||
_ => self.0.push(any),
|
_ => self.0.push(any),
|
||||||
},
|
},
|
||||||
MixedOp::InsertBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) {
|
AnyOp::InsertBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) {
|
||||||
Some(index) => self.0.insert(index, any),
|
Some(index) => self.0.insert(index, any),
|
||||||
_ => self.0.insert(0, any),
|
_ => self.0.insert(0, any),
|
||||||
},
|
},
|
||||||
MixedOp::Prepend(any) => self.0.insert(0, any),
|
AnyOp::Prepend(any) => self.0.insert(0, any),
|
||||||
MixedOp::RemoveById(id) => {
|
AnyOp::RemoveById(id) => {
|
||||||
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
|
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
|
||||||
self.0.remove(index);
|
self.0.remove(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MixedOp::ReplaceById(id, any) => {
|
AnyOp::ReplaceById(id, any) => {
|
||||||
for c in self.0.iter_mut() {
|
for c in self.0.iter_mut() {
|
||||||
if c.id() == id {
|
if c.id() == id {
|
||||||
*c = any;
|
*c = any;
|
||||||
|
|
@ -109,11 +155,40 @@ impl MixedComponents {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MixedOp::Reset => self.0.clear(),
|
AnyOp::Reset => self.0.clear(),
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[fn_builder]
|
||||||
|
#[rustfmt::skip]
|
||||||
|
pub fn alter_typed<C: ComponentTrait + Default>(&mut self, op: TypedOp<C>) -> &mut Self {
|
||||||
|
match op {
|
||||||
|
TypedOp::Add(typed) => {
|
||||||
|
self.alter_value(AnyOp::Add(typed.to_any()))
|
||||||
|
}
|
||||||
|
TypedOp::InsertAfterId(id, typed) => {
|
||||||
|
self.alter_value(AnyOp::InsertAfterId(id, typed.to_any()))
|
||||||
|
}
|
||||||
|
TypedOp::InsertBeforeId(id, typed) => {
|
||||||
|
self.alter_value(AnyOp::InsertBeforeId(id, typed.to_any()))
|
||||||
|
}
|
||||||
|
TypedOp::Prepend(typed) => {
|
||||||
|
self.alter_value(AnyOp::Prepend(typed.to_any()))
|
||||||
|
}
|
||||||
|
TypedOp::RemoveById(id) => {
|
||||||
|
self.alter_value(AnyOp::RemoveById(id))
|
||||||
|
}
|
||||||
|
TypedOp::ReplaceById(id, typed) => {
|
||||||
|
self.alter_value(AnyOp::ReplaceById(id, typed.to_any()))
|
||||||
|
}
|
||||||
|
TypedOp::Reset => {
|
||||||
|
self.alter_value(AnyOp::Reset)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
// MixedComponents GETTERS.
|
// MixedComponents GETTERS.
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::core::component::{AnyComponent, MixedComponents, MixedOp};
|
use crate::core::component::{AnyComponent, AnyOp, MixedComponents};
|
||||||
use crate::core::theme::ThemeRef;
|
use crate::core::theme::ThemeRef;
|
||||||
use crate::{fn_builder, AutoDefault, LazyStatic, TypeId};
|
use crate::{fn_builder, AutoDefault, LazyStatic, TypeId};
|
||||||
|
|
||||||
|
|
@ -16,11 +16,11 @@ pub struct ComponentsInRegions(HashMap<&'static str, MixedComponents>);
|
||||||
|
|
||||||
impl ComponentsInRegions {
|
impl ComponentsInRegions {
|
||||||
pub fn new(region: &'static str, any: AnyComponent) -> Self {
|
pub fn new(region: &'static str, any: AnyComponent) -> Self {
|
||||||
ComponentsInRegions::default().with_components(region, MixedOp::Add(any))
|
ComponentsInRegions::default().with_components(region, AnyOp::Add(any))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fn_builder]
|
#[fn_builder]
|
||||||
pub fn alter_components(&mut self, region: &'static str, op: MixedOp) -> &mut Self {
|
pub fn alter_components(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
|
||||||
if let Some(region) = self.0.get_mut(region) {
|
if let Some(region) = self.0.get_mut(region) {
|
||||||
region.alter_value(op);
|
region.alter_value(op);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -52,18 +52,18 @@ impl InRegion {
|
||||||
COMMON_REGIONS
|
COMMON_REGIONS
|
||||||
.write()
|
.write()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.alter_components("content", MixedOp::Add(any));
|
.alter_components("content", AnyOp::Add(any));
|
||||||
}
|
}
|
||||||
InRegion::Named(name) => {
|
InRegion::Named(name) => {
|
||||||
COMMON_REGIONS
|
COMMON_REGIONS
|
||||||
.write()
|
.write()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.alter_components(name, MixedOp::Add(any));
|
.alter_components(name, AnyOp::Add(any));
|
||||||
}
|
}
|
||||||
InRegion::OfTheme(region, theme) => {
|
InRegion::OfTheme(region, theme) => {
|
||||||
let mut regions = THEME_REGIONS.write().unwrap();
|
let mut regions = THEME_REGIONS.write().unwrap();
|
||||||
if let Some(r) = regions.get_mut(&theme.type_id()) {
|
if let Some(r) = regions.get_mut(&theme.type_id()) {
|
||||||
r.alter_components(region, MixedOp::Add(any));
|
r.alter_components(region, AnyOp::Add(any));
|
||||||
} else {
|
} else {
|
||||||
regions.insert(theme.type_id(), ComponentsInRegions::new(region, any));
|
regions.insert(theme.type_id(), ComponentsInRegions::new(region, any));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::core::component::{ComponentTrait, Context, OneComponent};
|
use crate::core::component::{ComponentTrait, Context, TypedComponent};
|
||||||
use crate::fn_builder;
|
use crate::fn_builder;
|
||||||
use crate::html::{html, Markup};
|
use crate::html::{html, Markup};
|
||||||
|
|
||||||
pub struct OptionComponent<C: ComponentTrait>(Option<OneComponent<C>>);
|
pub struct OptionComponent<C: ComponentTrait>(Option<TypedComponent<C>>);
|
||||||
|
|
||||||
impl<C: ComponentTrait> Default for OptionComponent<C> {
|
impl<C: ComponentTrait> Default for OptionComponent<C> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
|
@ -20,7 +20,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
|
||||||
#[fn_builder]
|
#[fn_builder]
|
||||||
pub fn alter_value(&mut self, component: Option<C>) -> &mut Self {
|
pub fn alter_value(&mut self, component: Option<C>) -> &mut Self {
|
||||||
if let Some(component) = component {
|
if let Some(component) = component {
|
||||||
self.0 = Some(OneComponent::with(component));
|
self.0 = Some(TypedComponent::with(component));
|
||||||
} else {
|
} else {
|
||||||
self.0 = None;
|
self.0 = None;
|
||||||
}
|
}
|
||||||
|
|
@ -29,7 +29,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
|
||||||
|
|
||||||
// OptionComponent GETTERS.
|
// OptionComponent GETTERS.
|
||||||
|
|
||||||
pub fn get(&self) -> Option<OneComponent<C>> {
|
pub fn get(&self) -> Option<TypedComponent<C>> {
|
||||||
if let Some(value) = &self.0 {
|
if let Some(value) = &self.0 {
|
||||||
return Some(value.clone());
|
return Some(value.clone());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ pub use error::ErrorPage;
|
||||||
pub use actix_web::Result as ResultPage;
|
pub use actix_web::Result as ResultPage;
|
||||||
|
|
||||||
use crate::base::action;
|
use crate::base::action;
|
||||||
use crate::core::component::{AnyComponent, ComponentTrait, MixedOp};
|
use crate::core::component::{AnyComponent, AnyOp, ComponentTrait};
|
||||||
use crate::core::component::{AssetsOp, Context};
|
use crate::core::component::{AssetsOp, Context};
|
||||||
use crate::fn_builder;
|
use crate::fn_builder;
|
||||||
use crate::html::{html, Markup, DOCTYPE};
|
use crate::html::{html, Markup, DOCTYPE};
|
||||||
|
|
@ -106,14 +106,14 @@ impl Page {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fn_builder]
|
#[fn_builder]
|
||||||
pub fn alter_regions(&mut self, region: &'static str, op: MixedOp) -> &mut Self {
|
pub fn alter_regions(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
|
||||||
self.context.alter_regions(region, op);
|
self.context.alter_regions(region, op);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
|
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
|
||||||
self.context
|
self.context
|
||||||
.alter_regions("content", MixedOp::Add(AnyComponent::with(component)));
|
.alter_regions("content", AnyOp::Add(AnyComponent::with(component)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -123,7 +123,7 @@ impl Page {
|
||||||
component: impl ComponentTrait,
|
component: impl ComponentTrait,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.context
|
self.context
|
||||||
.alter_regions(region, MixedOp::Add(AnyComponent::with(component)));
|
.alter_regions(region, AnyOp::Add(AnyComponent::with(component)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue