♻️ Weight is a property of mixed components struct

This commit is contained in:
Manuel Cillero 2024-04-14 22:33:20 +02:00
parent 322cd9b6bf
commit 45cb063e52
23 changed files with 60 additions and 266 deletions

View file

@ -4,7 +4,6 @@ use crate::prelude::*;
#[derive(AutoDefault, ComponentClasses)]
pub struct Block {
id : OptionId,
weight : Weight,
renderable: Renderable,
classes : OptionClasses,
style : StyleBase,
@ -21,10 +20,6 @@ impl ComponentTrait for Block {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -65,12 +60,6 @@ impl Block {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -4,7 +4,6 @@ use crate::prelude::*;
#[derive(AutoDefault)]
pub struct Branding {
id : OptionId,
weight : Weight,
renderable: Renderable,
#[default(_code = "config::SETTINGS.app.name.to_owned()")]
app_name : String,
@ -23,10 +22,6 @@ impl ComponentTrait for Branding {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -68,12 +63,6 @@ impl Branding {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -14,7 +14,6 @@ pub enum ButtonTarget {
#[derive(AutoDefault, ComponentClasses)]
pub struct Button {
id : OptionId,
weight : Weight,
renderable : Renderable,
classes : OptionClasses,
style : StyleBase,
@ -35,10 +34,6 @@ impl ComponentTrait for Button {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -92,12 +87,6 @@ impl Button {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -15,7 +15,6 @@ pub enum ContainerType {
#[derive(AutoDefault, ComponentClasses)]
pub struct Container {
id : OptionId,
weight : Weight,
renderable : Renderable,
classes : OptionClasses,
container_type: ContainerType,
@ -36,10 +35,6 @@ impl ComponentTrait for Container {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -149,12 +144,6 @@ impl Container {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -13,7 +13,6 @@ pub enum ItemType {
#[derive(AutoDefault, ComponentClasses)]
pub struct Item {
id : OptionId,
weight : Weight,
renderable : Renderable,
classes : OptionClasses,
item_type : ItemType,
@ -34,10 +33,6 @@ impl ComponentTrait for Item {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -72,20 +67,16 @@ impl ComponentTrait for Item {
if output.is_empty() && region.is_empty() {
return PrepareMarkup::None;
}
let order = match self.weight() {
0 => None,
_ => Some(concat_string!("order: ", self.weight().to_string(), ";")),
};
match self.item_type() {
ItemType::Default => PrepareMarkup::With(html! {
div id=[self.id()] class=[self.classes().get()] style=[order] {
div id=[self.id()] class=[self.classes().get()] {
div class="flex__content" {
(output)
}
}
}),
ItemType::Region => PrepareMarkup::With(html! {
div id=[self.id()] class=[self.classes().get()] style=[order] {
div id=[self.id()] class=[self.classes().get()] {
div class="flex__content flex__region" {
(region)
(output)
@ -93,7 +84,7 @@ impl ComponentTrait for Item {
}
}),
ItemType::Wrapper => PrepareMarkup::With(html! {
div id=[self.id()] class=[self.classes().get()] style=[order] {
div id=[self.id()] class=[self.classes().get()] {
(output)
}
}),
@ -138,12 +129,6 @@ impl Item {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -20,7 +20,6 @@ impl ToString for ActionButtonType {
#[rustfmt::skip]
#[derive(AutoDefault, ComponentClasses)]
pub struct ActionButton {
weight : Weight,
renderable : Renderable,
classes : OptionClasses,
button_type: ActionButtonType,
@ -39,10 +38,6 @@ impl ComponentTrait for ActionButton {
ActionButton::submit()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -100,12 +95,6 @@ impl ActionButton {
// Button BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -3,7 +3,6 @@ use crate::prelude::*;
#[rustfmt::skip]
#[derive(AutoDefault, ComponentClasses)]
pub struct Date {
weight : Weight,
renderable : Renderable,
classes : OptionClasses,
name : OptionString,
@ -23,10 +22,6 @@ impl ComponentTrait for Date {
Date::default().with_classes(ClassesOp::Add, "form-item form-type-date")
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -68,12 +63,6 @@ impl ComponentTrait for Date {
impl Date {
// Date BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -11,7 +11,6 @@ pub enum FormMethod {
#[derive(AutoDefault, ComponentClasses)]
pub struct Form {
id : OptionId,
weight : Weight,
renderable: Renderable,
classes : OptionClasses,
action : OptionString,
@ -31,10 +30,6 @@ impl ComponentTrait for Form {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -67,12 +62,6 @@ impl Form {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -3,7 +3,6 @@ use crate::prelude::*;
#[rustfmt::skip]
#[derive(AutoDefault)]
pub struct Hidden {
weight: Weight,
name : OptionName,
value : OptionString,
}
@ -13,10 +12,6 @@ impl ComponentTrait for Hidden {
Hidden::default()
}
fn weight(&self) -> Weight {
self.weight
}
fn prepare_component(&self, _cx: &mut Context) -> PrepareMarkup {
let id = self.name().get().map(|name| concat_string!("value-", name));
PrepareMarkup::With(html! {
@ -32,12 +27,6 @@ impl Hidden {
// Hidden BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_name(&mut self, name: &str) -> &mut Self {
self.name.alter_value(name);

View file

@ -14,7 +14,6 @@ pub enum InputType {
#[rustfmt::skip]
#[derive(AutoDefault, ComponentClasses)]
pub struct Input {
weight : Weight,
renderable : Renderable,
classes : OptionClasses,
input_type : InputType,
@ -41,10 +40,6 @@ impl ComponentTrait for Input {
.with_maxlength(Some(128))
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -147,12 +142,6 @@ impl Input {
// Input BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -42,7 +42,6 @@ impl ToString for HeadingSize {
#[derive(AutoDefault, ComponentClasses)]
pub struct Heading {
id : OptionId,
weight : Weight,
renderable : Renderable,
classes : OptionClasses,
heading_type: HeadingType,
@ -59,10 +58,6 @@ impl ComponentTrait for Heading {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -131,12 +126,6 @@ impl Heading {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -3,7 +3,6 @@ use crate::prelude::*;
#[rustfmt::skip]
#[derive(AutoDefault, ComponentClasses)]
pub struct Icon {
weight : Weight,
renderable: Renderable,
classes : OptionClasses,
icon_name : OptionString,
@ -15,10 +14,6 @@ impl ComponentTrait for Icon {
Icon::default()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -48,12 +43,6 @@ impl Icon {
// Icon BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -17,7 +17,6 @@ pub enum ImageSize {
#[derive(AutoDefault, ComponentClasses)]
pub struct Image {
id : OptionId,
weight : Weight,
renderable: Renderable,
classes : OptionClasses,
source : OptionString,
@ -33,10 +32,6 @@ impl ComponentTrait for Image {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -88,12 +83,6 @@ impl Image {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -18,7 +18,6 @@ pub enum ElementType {
#[rustfmt::skip]
#[derive(AutoDefault)]
pub struct Element {
weight : Weight,
renderable : Renderable,
element_type: ElementType,
}
@ -28,10 +27,6 @@ impl ComponentTrait for Element {
Element::default()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -66,12 +61,6 @@ impl Element {
// Element BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -6,7 +6,6 @@ use super::Element;
#[derive(AutoDefault)]
pub struct Group {
id : OptionId,
weight : Weight,
renderable: Renderable,
elements : MixedComponents,
}
@ -20,10 +19,6 @@ impl ComponentTrait for Group {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -46,12 +41,6 @@ impl Group {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -24,7 +24,6 @@ pub enum ItemType {
#[rustfmt::skip]
#[derive(AutoDefault)]
pub struct Item {
weight : Weight,
renderable : Renderable,
item_type : ItemType,
description: OptionTranslated,
@ -37,10 +36,6 @@ impl ComponentTrait for Item {
Item::default()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -156,12 +151,6 @@ impl Item {
// Item BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -6,7 +6,6 @@ use super::Group;
#[derive(AutoDefault)]
pub struct Megamenu {
id : OptionId,
weight : Weight,
renderable: Renderable,
groups : MixedComponents,
}
@ -20,10 +19,6 @@ impl ComponentTrait for Megamenu {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -46,12 +41,6 @@ impl Megamenu {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -6,7 +6,6 @@ use super::Item;
#[derive(AutoDefault)]
pub struct Menu {
id : OptionId,
weight : Weight,
renderable: Renderable,
items : MixedComponents,
}
@ -20,10 +19,6 @@ impl ComponentTrait for Menu {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -74,12 +69,6 @@ impl Menu {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -6,7 +6,6 @@ use super::Item;
#[derive(AutoDefault)]
pub struct Submenu {
id : OptionId,
weight : Weight,
renderable: Renderable,
title : OptionTranslated,
items : MixedComponents,
@ -21,10 +20,6 @@ impl ComponentTrait for Submenu {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -52,12 +47,6 @@ impl Submenu {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -4,7 +4,6 @@ use crate::prelude::*;
#[derive(AutoDefault, ComponentClasses)]
pub struct Paragraph {
id : OptionId,
weight : Weight,
renderable: Renderable,
classes : OptionClasses,
font_size : FontSize,
@ -20,10 +19,6 @@ impl ComponentTrait for Paragraph {
self.id.get()
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -61,12 +56,6 @@ impl Paragraph {
self
}
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -13,7 +13,6 @@ pub enum PoweredByLogo {
#[rustfmt::skip]
#[derive(AutoDefault)]
pub struct PoweredBy {
weight : Weight,
renderable: Renderable,
copyright : Option<String>,
logo : PoweredByLogo,
@ -29,10 +28,6 @@ impl ComponentTrait for PoweredBy {
}
}
fn weight(&self) -> Weight {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
@ -67,12 +62,6 @@ impl ComponentTrait for PoweredBy {
impl PoweredBy {
// PoweredBy BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: FnIsRenderable) -> &mut Self {
self.renderable.check = check;

View file

@ -3,7 +3,6 @@ use crate::core::component::Context;
use crate::core::AnyBase;
use crate::html::{html, Markup, PrepareMarkup};
use crate::util::TypeInfo;
use crate::Weight;
pub trait ComponentBase {
fn render(&mut self, cx: &mut Context) -> Markup;
@ -26,10 +25,6 @@ pub trait ComponentTrait: AnyBase + ComponentBase + Send + Sync {
None
}
fn weight(&self) -> Weight {
0
}
#[allow(unused_variables)]
fn is_renderable(&self, cx: &Context) -> bool {
true

View file

@ -5,83 +5,117 @@ use crate::{fn_builder, TypeId, Weight};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
#[derive(Clone)]
pub struct AnyComponent(Arc<RwLock<dyn ComponentTrait>>);
pub struct AnyComponent {
weight: Weight,
component: Arc<RwLock<dyn ComponentTrait>>,
}
impl AnyComponent {
pub fn with(component: impl ComponentTrait) -> Self {
AnyComponent(Arc::new(RwLock::new(component)))
AnyComponent {
weight: Weight::default(),
component: Arc::new(RwLock::new(component)),
}
}
// AnyComponent BUILDER.
pub fn set(&mut self, component: impl ComponentTrait) {
self.0 = Arc::new(RwLock::new(component));
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
// AnyComponent GETTERS.
pub fn weight(&self) -> Weight {
self.weight
}
pub fn get(&self) -> RwLockReadGuard<'_, dyn ComponentTrait> {
self.0.read().unwrap()
self.component.read().unwrap()
}
pub fn get_mut(&self) -> RwLockWriteGuard<'_, dyn ComponentTrait> {
self.0.write().unwrap()
self.component.write().unwrap()
}
// AnyComponent RENDER.
pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx)
self.component.write().unwrap().render(cx)
}
// AnyComponent HELPERS.
fn type_id(&self) -> TypeId {
self.0.read().unwrap().type_id()
self.component.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()
self.component.read().unwrap().id().unwrap_or_default()
}
}
// *************************************************************************************************
pub struct TypedComponent<C: ComponentTrait>(Arc<RwLock<C>>);
pub struct TypedComponent<C: ComponentTrait> {
weight: Weight,
component: Arc<RwLock<C>>,
}
impl<C: ComponentTrait> Clone for TypedComponent<C> {
fn clone(&self) -> Self {
Self(self.0.clone())
Self {
weight: self.weight.clone(),
component: self.component.clone(),
}
}
}
impl<C: ComponentTrait> TypedComponent<C> {
pub fn with(component: C) -> Self {
TypedComponent(Arc::new(RwLock::new(component)))
TypedComponent {
weight: Weight::default(),
component: Arc::new(RwLock::new(component)),
}
}
// TypedComponent BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
// TypedComponent GETTERS.
pub fn weight(&self) -> Weight {
self.weight
}
pub fn get(&self) -> RwLockReadGuard<'_, C> {
self.0.read().unwrap()
self.component.read().unwrap()
}
pub fn get_mut(&self) -> RwLockWriteGuard<'_, C> {
self.0.write().unwrap()
}
fn to_any(&self) -> AnyComponent {
AnyComponent(self.0.clone())
self.component.write().unwrap()
}
// TypedComponent RENDER.
pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx)
self.component.write().unwrap().render(cx)
}
// TypedComponent HELPERS.
fn to_any(&self) -> AnyComponent {
AnyComponent {
weight: self.weight.clone(),
component: self.component.clone(),
}
}
}