✨ Define OneComponent con tipado restringido
This commit is contained in:
parent
dd443ca375
commit
23a6f36f62
12 changed files with 68 additions and 90 deletions
|
|
@ -2,15 +2,17 @@ use pagetop::prelude::*;
|
||||||
|
|
||||||
define_handle!(COMPONENT_MEGAMENUITEM);
|
define_handle!(COMPONENT_MEGAMENUITEM);
|
||||||
|
|
||||||
|
type Label = OneComponent<L10n>;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub enum MegaMenuItemType {
|
pub enum MegaMenuItemType {
|
||||||
#[default]
|
#[default]
|
||||||
Void,
|
Void,
|
||||||
Label(ComponentArc),
|
Label(Label),
|
||||||
Link(ComponentArc, String),
|
Link(Label, String),
|
||||||
LinkBlank(ComponentArc, String),
|
LinkBlank(Label, String),
|
||||||
Html(Markup),
|
Html(Markup),
|
||||||
Submenu(ComponentArc, MegaMenu),
|
Submenu(Label, MegaMenu),
|
||||||
Separator,
|
Separator,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -85,21 +87,21 @@ impl ComponentTrait for MegaMenuItem {
|
||||||
impl MegaMenuItem {
|
impl MegaMenuItem {
|
||||||
pub fn label(label: L10n) -> Self {
|
pub fn label(label: L10n) -> Self {
|
||||||
MegaMenuItem {
|
MegaMenuItem {
|
||||||
item_type: MegaMenuItemType::Label(ComponentArc::new_with(label)),
|
item_type: MegaMenuItemType::Label(OneComponent::new_with(label)),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn link(label: L10n, path: &str) -> Self {
|
pub fn link(label: L10n, path: &str) -> Self {
|
||||||
MegaMenuItem {
|
MegaMenuItem {
|
||||||
item_type: MegaMenuItemType::Link(ComponentArc::new_with(label), path.to_owned()),
|
item_type: MegaMenuItemType::Link(OneComponent::new_with(label), path.to_owned()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn link_blank(label: L10n, path: &str) -> Self {
|
pub fn link_blank(label: L10n, path: &str) -> Self {
|
||||||
MegaMenuItem {
|
MegaMenuItem {
|
||||||
item_type: MegaMenuItemType::LinkBlank(ComponentArc::new_with(label), path.to_owned()),
|
item_type: MegaMenuItemType::LinkBlank(OneComponent::new_with(label), path.to_owned()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -113,7 +115,7 @@ impl MegaMenuItem {
|
||||||
|
|
||||||
pub fn submenu(label: L10n, menu: MegaMenu) -> Self {
|
pub fn submenu(label: L10n, menu: MegaMenu) -> Self {
|
||||||
MegaMenuItem {
|
MegaMenuItem {
|
||||||
item_type: MegaMenuItemType::Submenu(ComponentArc::new_with(label), menu),
|
item_type: MegaMenuItemType::Submenu(OneComponent::new_with(label), menu),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ pub use heading::{Heading, HeadingDisplay, HeadingType, COMPONENT_HEADING};
|
||||||
mod paragraph;
|
mod paragraph;
|
||||||
pub use paragraph::{Paragraph, ParagraphDisplay, COMPONENT_PARAGRAPH};
|
pub use paragraph::{Paragraph, ParagraphDisplay, COMPONENT_PARAGRAPH};
|
||||||
mod anchor;
|
mod anchor;
|
||||||
pub use anchor::{Anchor, AnchorIcon, AnchorTarget, AnchorType, COMPONENT_ANCHOR};
|
pub use anchor::{Anchor, AnchorTarget, AnchorType, COMPONENT_ANCHOR};
|
||||||
mod block;
|
mod block;
|
||||||
pub use block::{Block, COMPONENT_BLOCK};
|
pub use block::{Block, COMPONENT_BLOCK};
|
||||||
mod image;
|
mod image;
|
||||||
|
|
|
||||||
|
|
@ -22,8 +22,8 @@ pub enum AnchorTarget {
|
||||||
Context(String),
|
Context(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type AnchorIcon = ComponentArc;
|
type AnchorIcon = OneComponent<Icon>;
|
||||||
pub type AnchorHtml = ComponentArc;
|
type AnchorHtml = OneComponent<L10n>;
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ pub enum ButtonType {
|
||||||
Reset,
|
Reset,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ButtonValue = ComponentArc;
|
type ButtonValue = OneComponent<L10n>;
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,8 @@ pub enum InputType {
|
||||||
Url,
|
Url,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type InputLabel = ComponentArc;
|
type InputLabel = OneComponent<L10n>;
|
||||||
pub type InputHelpText = ComponentArc;
|
type InputHelpText = OneComponent<L10n>;
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ pub enum HeadingDisplay {
|
||||||
Subtitle,
|
Subtitle,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type HeadingText = ComponentArc;
|
type HeadingText = OneComponent<L10n>;
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,8 @@ pub use context::{ContextOp, RenderContext};
|
||||||
mod definition;
|
mod definition;
|
||||||
pub use definition::{component_mut, component_ref, AnyComponent, BaseComponent, ComponentTrait};
|
pub use definition::{component_mut, component_ref, AnyComponent, BaseComponent, ComponentTrait};
|
||||||
|
|
||||||
mod default;
|
mod one;
|
||||||
pub(crate) use default::DefaultComponent;
|
pub use one::OneComponent;
|
||||||
|
|
||||||
mod arc;
|
|
||||||
pub use arc::ComponentArc;
|
|
||||||
|
|
||||||
mod bundle;
|
mod bundle;
|
||||||
pub use bundle::ComponentsBundle;
|
pub use bundle::ComponentsBundle;
|
||||||
|
|
|
||||||
|
|
@ -1,45 +0,0 @@
|
||||||
use crate::core::component::{ComponentTrait, DefaultComponent, RenderContext};
|
|
||||||
use crate::html::{html, Markup};
|
|
||||||
|
|
||||||
use std::sync::{Arc, RwLock};
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ComponentArc(Arc<RwLock<dyn ComponentTrait>>);
|
|
||||||
|
|
||||||
impl Default for ComponentArc {
|
|
||||||
fn default() -> Self {
|
|
||||||
ComponentArc(Arc::new(RwLock::new(DefaultComponent)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ComponentArc {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
ComponentArc::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_with(component: impl ComponentTrait) -> Self {
|
|
||||||
ComponentArc(Arc::new(RwLock::new(component)))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set(&mut self, component: impl ComponentTrait) {
|
|
||||||
self.0 = Arc::new(RwLock::new(component));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn weight(&self) -> isize {
|
|
||||||
self.0.read().unwrap().weight()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ComponentArc RENDER.
|
|
||||||
|
|
||||||
pub fn render(&self, rcx: &mut RenderContext) -> Markup {
|
|
||||||
self.0.write().unwrap().render(rcx)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn optional_render(&self, rcx: &mut RenderContext) -> Option<Markup> {
|
|
||||||
let render = self.0.write().unwrap().render(rcx).into_string();
|
|
||||||
if !render.trim().is_empty() {
|
|
||||||
return Some(html! { (render) });
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
use crate::core::component::{ComponentArc, ComponentTrait, RenderContext};
|
use crate::core::component::{ComponentTrait, RenderContext};
|
||||||
use crate::html::{html, Markup};
|
use crate::html::{html, Markup};
|
||||||
|
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
pub struct ComponentsBundle(Vec<ComponentArc>);
|
pub struct ComponentsBundle(Vec<Arc<RwLock<dyn ComponentTrait>>>);
|
||||||
|
|
||||||
impl ComponentsBundle {
|
impl ComponentsBundle {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
|
@ -16,7 +18,7 @@ impl ComponentsBundle {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, component: impl ComponentTrait) {
|
pub fn add(&mut self, component: impl ComponentTrait) {
|
||||||
self.0.push(ComponentArc::new_with(component));
|
self.0.push(Arc::new(RwLock::new(component)));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
|
|
@ -25,10 +27,10 @@ impl ComponentsBundle {
|
||||||
|
|
||||||
pub fn render(&self, rcx: &mut RenderContext) -> Markup {
|
pub fn render(&self, rcx: &mut RenderContext) -> Markup {
|
||||||
let mut components = self.0.clone();
|
let mut components = self.0.clone();
|
||||||
components.sort_by_key(|c| c.weight());
|
components.sort_by_key(|c| c.read().unwrap().weight());
|
||||||
html! {
|
html! {
|
||||||
@for c in components.iter() {
|
@for c in components.iter() {
|
||||||
(" ")(c.render(rcx))(" ")
|
(" ")(c.write().unwrap().render(rcx))(" ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
use crate::core::component::{AnyComponent, ComponentTrait};
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct DefaultComponent;
|
|
||||||
|
|
||||||
impl ComponentTrait for DefaultComponent {
|
|
||||||
fn new() -> Self {
|
|
||||||
DefaultComponent::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_ref_any(&self) -> &dyn AnyComponent {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
40
pagetop/src/core/component/one.rs
Normal file
40
pagetop/src/core/component/one.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
use crate::core::component::{ComponentTrait, RenderContext};
|
||||||
|
use crate::html::{html, Markup};
|
||||||
|
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
#[derive(Clone, Default)]
|
||||||
|
pub struct OneComponent<T: ComponentTrait + Default>(Option<Arc<RwLock<T>>>);
|
||||||
|
|
||||||
|
impl<T: ComponentTrait + Default> OneComponent<T> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
OneComponent::<T>::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_with(component: T) -> Self {
|
||||||
|
OneComponent(Some(Arc::new(RwLock::new(component))))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&mut self, component: T) {
|
||||||
|
self.0 = Some(Arc::new(RwLock::new(component)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// OneComponent RENDER.
|
||||||
|
|
||||||
|
pub fn render(&self, rcx: &mut RenderContext) -> Markup {
|
||||||
|
if let Some(component) = &self.0 {
|
||||||
|
return component.write().unwrap().render(rcx);
|
||||||
|
}
|
||||||
|
html! {}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn optional_render(&self, rcx: &mut RenderContext) -> Option<Markup> {
|
||||||
|
if let Some(component) = &self.0 {
|
||||||
|
let render = component.write().unwrap().render(rcx).into_string();
|
||||||
|
if !render.trim().is_empty() {
|
||||||
|
return Some(html! { (render) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -32,8 +32,8 @@ pub enum TextDirection {
|
||||||
RightToLeft,
|
RightToLeft,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type PageTitle = ComponentArc;
|
type PageTitle = OneComponent<L10n>;
|
||||||
pub type PageDescription = ComponentArc;
|
type PageDescription = OneComponent<L10n>;
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
pub struct Page {
|
pub struct Page {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue