Añade identificador en componentes

This commit is contained in:
Manuel Cillero 2023-06-08 00:12:07 +02:00
parent cb04a29388
commit f0e4aea672
18 changed files with 77 additions and 90 deletions

View file

@ -175,6 +175,10 @@ impl ComponentTrait for MegaMenu {
COMPONENT_MEGAMENU COMPONENT_MEGAMENU
} }
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
@ -264,10 +268,6 @@ impl MegaMenu {
// MegaMenu GETTERS. // MegaMenu GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes { pub fn classes(&self) -> &Classes {
&self.classes &self.classes
} }

View file

@ -50,6 +50,10 @@ impl ComponentTrait for Anchor {
COMPONENT_ANCHOR COMPONENT_ANCHOR
} }
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
@ -69,7 +73,7 @@ impl ComponentTrait for Anchor {
}; };
html! { html! {
a a
id=[self.id().get()] id=[self.id()]
class=[self.classes().get()] class=[self.classes().get()]
href=[self.href().get()] href=[self.href().get()]
target=[target] target=[target]
@ -183,10 +187,6 @@ impl Anchor {
// Anchor GETTERS. // Anchor GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes { pub fn classes(&self) -> &Classes {
&self.classes &self.classes
} }

View file

@ -38,6 +38,10 @@ impl ComponentTrait for Container {
COMPONENT_CONTAINER COMPONENT_CONTAINER
} }
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
@ -53,35 +57,35 @@ impl ComponentTrait for Container {
fn default_render(&self, rcx: &mut RenderContext) -> Markup { fn default_render(&self, rcx: &mut RenderContext) -> Markup {
match self.container_type() { match self.container_type() {
ContainerType::Header => html! { ContainerType::Header => html! {
header id=[self.id().get()] class=[self.classes().get()] { header id=[self.id()] class=[self.classes().get()] {
div class=[self.inner_classes().get()] { div class=[self.inner_classes().get()] {
(self.components().render(rcx)) (self.components().render(rcx))
} }
} }
}, },
ContainerType::Footer => html! { ContainerType::Footer => html! {
footer id=[self.id().get()] class=[self.classes().get()] { footer id=[self.id()] class=[self.classes().get()] {
div class=[self.inner_classes().get()] { div class=[self.inner_classes().get()] {
(self.components().render(rcx)) (self.components().render(rcx))
} }
} }
}, },
ContainerType::Main => html! { ContainerType::Main => html! {
main id=[self.id().get()] class=[self.classes().get()] { main id=[self.id()] class=[self.classes().get()] {
div class=[self.inner_classes().get()] { div class=[self.inner_classes().get()] {
(self.components().render(rcx)) (self.components().render(rcx))
} }
} }
}, },
ContainerType::Section => html! { ContainerType::Section => html! {
section id=[self.id().get()] class=[self.classes().get()] { section id=[self.id()] class=[self.classes().get()] {
div class=[self.inner_classes().get()] { div class=[self.inner_classes().get()] {
(self.components().render(rcx)) (self.components().render(rcx))
} }
} }
}, },
_ => html! { _ => html! {
div id=[self.id().get()] class=[self.classes().get()] { div id=[self.id()] class=[self.classes().get()] {
(self.components().render(rcx)) (self.components().render(rcx))
} }
}, },
@ -168,10 +172,6 @@ impl Container {
// Container GETTERS. // Container GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes { pub fn classes(&self) -> &Classes {
&self.classes &self.classes
} }

View file

@ -36,6 +36,10 @@ impl ComponentTrait for Form {
COMPONENT_FORM COMPONENT_FORM
} }
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
@ -55,7 +59,7 @@ impl ComponentTrait for Form {
}; };
html! { html! {
form form
id=[self.id().get()] id=[self.id()]
class=[self.classes().get()] class=[self.classes().get()]
action=[self.action().get()] action=[self.action().get()]
method=[method] method=[method]
@ -134,10 +138,6 @@ impl Form {
// Form GETTERS. // Form GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes { pub fn classes(&self) -> &Classes {
&self.classes &self.classes
} }

View file

@ -57,6 +57,10 @@ impl ComponentTrait for Column {
COMPONENT_COLUMN COMPONENT_COLUMN
} }
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
@ -71,7 +75,7 @@ impl ComponentTrait for Column {
fn default_render(&self, rcx: &mut RenderContext) -> Markup { fn default_render(&self, rcx: &mut RenderContext) -> Markup {
html! { html! {
div id=[self.id().get()] class=[self.classes().get()] { div id=[self.id()] class=[self.classes().get()] {
(self.components().render(rcx)) (self.components().render(rcx))
} }
} }
@ -149,10 +153,6 @@ impl Column {
// Column GETTERS. // Column GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes { pub fn classes(&self) -> &Classes {
&self.classes &self.classes
} }

View file

@ -26,6 +26,10 @@ impl ComponentTrait for Row {
COMPONENT_ROW COMPONENT_ROW
} }
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
@ -40,7 +44,7 @@ impl ComponentTrait for Row {
fn default_render(&self, rcx: &mut RenderContext) -> Markup { fn default_render(&self, rcx: &mut RenderContext) -> Markup {
html! { html! {
div id=[self.id().get()] class=[self.classes().get()] { div id=[self.id()] class=[self.classes().get()] {
(self.columns().render(rcx)) (self.columns().render(rcx))
} }
} }
@ -96,10 +100,6 @@ impl Row {
// Row GETTERS. // Row GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes { pub fn classes(&self) -> &Classes {
&self.classes &self.classes
} }

View file

@ -49,6 +49,10 @@ impl ComponentTrait for Heading {
COMPONENT_HEADING COMPONENT_HEADING
} }
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
@ -58,7 +62,7 @@ impl ComponentTrait for Heading {
} }
fn default_render(&self, rcx: &mut RenderContext) -> Markup { fn default_render(&self, rcx: &mut RenderContext) -> Markup {
let id = self.id().get(); let id = self.id();
let classes = self.classes().get(); let classes = self.classes().get();
html! { @match &self.heading_type() { html! { @match &self.heading_type() {
HeadingType::H1 => h1 id=[id] class=[classes] { (self.text().render(rcx)) }, HeadingType::H1 => h1 id=[id] class=[classes] { (self.text().render(rcx)) },
@ -181,10 +185,6 @@ impl Heading {
// Paragraph GETTERS. // Paragraph GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes { pub fn classes(&self) -> &Classes {
&self.classes &self.classes
} }

View file

@ -22,6 +22,10 @@ impl ComponentTrait for Image {
COMPONENT_IMAGE COMPONENT_IMAGE
} }
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
@ -34,7 +38,7 @@ impl ComponentTrait for Image {
html! { html! {
img img
src=[self.source().get()] src=[self.source().get()]
id=[self.id().get()] id=[self.id()]
class=[self.classes().get()]; class=[self.classes().get()];
} }
} }
@ -93,10 +97,6 @@ impl Image {
// Image GETTERS. // Image GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes { pub fn classes(&self) -> &Classes {
&self.classes &self.classes
} }

View file

@ -34,6 +34,10 @@ impl ComponentTrait for Paragraph {
COMPONENT_PARAGRAPH COMPONENT_PARAGRAPH
} }
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
@ -45,7 +49,7 @@ impl ComponentTrait for Paragraph {
fn default_render(&self, rcx: &mut RenderContext) -> Markup { fn default_render(&self, rcx: &mut RenderContext) -> Markup {
html! { html! {
p p
id=[self.id().get()] id=[self.id()]
class=[self.classes().get()] class=[self.classes().get()]
{ {
(self.components().render(rcx)) (self.components().render(rcx))
@ -125,10 +129,6 @@ impl Paragraph {
// Paragraph GETTERS. // Paragraph GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes { pub fn classes(&self) -> &Classes {
&self.classes &self.classes
} }

View file

@ -6,14 +6,14 @@ macro_rules! action_before_render_component {
type Action = fn(&$Component, &mut RenderContext); type Action = fn(&$Component, &mut RenderContext);
pub struct [< BeforeRender $Component >] { pub struct [<BeforeRender $Component>] {
action: Option<Action>, action: Option<Action>,
weight: isize, weight: isize,
} }
impl ActionTrait for [< BeforeRender $Component >] { impl ActionTrait for [<BeforeRender $Component>] {
fn new() -> Self { fn new() -> Self {
[< BeforeRender $Component >] { [<BeforeRender $Component>] {
action: None, action: None,
weight: 0, weight: 0,
} }
@ -32,7 +32,7 @@ macro_rules! action_before_render_component {
} }
} }
impl [< BeforeRender $Component >] { impl [<BeforeRender $Component>] {
#[allow(dead_code)] #[allow(dead_code)]
pub fn with_action(mut self, action: Action) -> Self { pub fn with_action(mut self, action: Action) -> Self {
self.action = Some(action); self.action = Some(action);
@ -55,7 +55,7 @@ macro_rules! action_before_render_component {
#[inline(always)] #[inline(always)]
pub fn before_render_inline(component: &mut $Component, rcx: &mut RenderContext) { pub fn before_render_inline(component: &mut $Component, rcx: &mut RenderContext) {
run_actions($ACTION_HANDLE, |action| run_actions($ACTION_HANDLE, |action|
action_ref::<[< BeforeRender $Component >]>(&**action) action_ref::<[<BeforeRender $Component>]>(&**action)
.run(component, rcx) .run(component, rcx)
); );
} }

View file

@ -25,6 +25,10 @@ impl ComponentTrait for Block {
COMPONENT_BLOCK COMPONENT_BLOCK
} }
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
@ -107,10 +111,6 @@ impl Block {
// Block GETTERS. // Block GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes { pub fn classes(&self) -> &Classes {
&self.classes &self.classes
} }

View file

@ -1,5 +1,5 @@
use crate::core::theme::{all::theme_by_single_name, ThemeStaticRef}; use crate::core::theme::{all::theme_by_single_name, ThemeStaticRef};
use crate::html::{html, Assets, IdentifierValue, JavaScript, Markup, StyleSheet}; use crate::html::{html, Assets, JavaScript, Markup, StyleSheet};
use crate::locale::{LanguageIdentifier, LANGID}; use crate::locale::{LanguageIdentifier, LANGID};
use crate::server::HttpRequest; use crate::server::HttpRequest;
use crate::{concat_string, config, util, LazyStatic}; use crate::{concat_string, config, util, LazyStatic};
@ -120,8 +120,8 @@ impl RenderContext {
// Context EXTRAS. // Context EXTRAS.
pub fn required_id<T>(&mut self, id: &IdentifierValue) -> String { pub fn required_id<T>(&mut self, id: Option<String>) -> String {
match id.get() { match id {
Some(id) => id, Some(id) => id,
None => { None => {
let prefix = util::single_type_name::<T>() let prefix = util::single_type_name::<T>()

View file

@ -27,6 +27,10 @@ pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync {
None None
} }
fn id(&self) -> Option<String> {
None
}
fn weight(&self) -> isize { fn weight(&self) -> isize {
0 0
} }

View file

@ -1,3 +1,5 @@
use crate::fn_builder;
#[derive(Default)] #[derive(Default)]
pub struct AttributeValue(String); pub struct AttributeValue(String);
@ -8,13 +10,7 @@ impl AttributeValue {
// AttributeValue BUILDER. // AttributeValue BUILDER.
pub fn with_value(mut self, value: &str) -> Self { #[fn_builder]
self.alter_value(value);
self
}
// AttributeValue ALTER.
pub fn alter_value(&mut self, value: &str) -> &mut Self { pub fn alter_value(&mut self, value: &str) -> &mut Self {
self.0 = value.trim().to_owned(); self.0 = value.trim().to_owned();
self self

View file

@ -1,4 +1,4 @@
use crate::concat_string; use crate::{concat_string, fn_builder};
pub enum ClassesOp { pub enum ClassesOp {
Add, Add,
@ -26,13 +26,7 @@ impl Classes {
// Classes BUILDER. // Classes BUILDER.
pub fn with_value(mut self, op: ClassesOp, classes: &str) -> Self { #[fn_builder]
self.alter_value(op, classes);
self
}
// Classes ALTER.
pub fn alter_value(&mut self, op: ClassesOp, classes: &str) -> &mut Self { pub fn alter_value(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
let classes = classes.trim(); let classes = classes.trim();
match op { match op {

View file

@ -1,3 +1,5 @@
use crate::fn_builder;
#[derive(Default)] #[derive(Default)]
pub struct IdentifierValue(String); pub struct IdentifierValue(String);
@ -8,13 +10,7 @@ impl IdentifierValue {
// IdentifierValue BUILDER. // IdentifierValue BUILDER.
pub fn with_value(mut self, value: &str) -> Self { #[fn_builder]
self.alter_value(value);
self
}
// IdentifierValue ALTER.
pub fn alter_value(&mut self, value: &str) -> &mut Self { pub fn alter_value(&mut self, value: &str) -> &mut Self {
self.0 = value.trim().replace(' ', "_"); self.0 = value.trim().replace(' ', "_");
self self

View file

@ -1,3 +1,5 @@
use crate::fn_builder;
#[derive(Default)] #[derive(Default)]
pub struct NameValue(String); pub struct NameValue(String);
@ -8,13 +10,7 @@ impl NameValue {
// NameValue BUILDER. // NameValue BUILDER.
pub fn with_value(mut self, value: &str) -> Self { #[fn_builder]
self.alter_value(value);
self
}
// NameValue ALTER.
pub fn alter_value(&mut self, value: &str) -> &mut Self { pub fn alter_value(&mut self, value: &str) -> &mut Self {
self.0 = value.trim().replace(' ', "_"); self.0 = value.trim().replace(' ', "_");
self self

View file

@ -1,4 +1,4 @@
//! Funciones útiles y macros declarativas. //! Funciones útiles.
use crate::Handle; use crate::Handle;
@ -7,6 +7,7 @@ use crate::Handle;
// ************************************************************************************************* // *************************************************************************************************
// https://stackoverflow.com/a/71464396 // https://stackoverflow.com/a/71464396
#[doc(hidden)]
pub const fn handle( pub const fn handle(
module_path: &'static str, module_path: &'static str,
file: &'static str, file: &'static str,