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
}
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize {
self.weight
}
@ -264,10 +268,6 @@ impl MegaMenu {
// MegaMenu GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes {
&self.classes
}

View file

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

View file

@ -38,6 +38,10 @@ impl ComponentTrait for Container {
COMPONENT_CONTAINER
}
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize {
self.weight
}
@ -53,35 +57,35 @@ impl ComponentTrait for Container {
fn default_render(&self, rcx: &mut RenderContext) -> Markup {
match self.container_type() {
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()] {
(self.components().render(rcx))
}
}
},
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()] {
(self.components().render(rcx))
}
}
},
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()] {
(self.components().render(rcx))
}
}
},
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()] {
(self.components().render(rcx))
}
}
},
_ => html! {
div id=[self.id().get()] class=[self.classes().get()] {
div id=[self.id()] class=[self.classes().get()] {
(self.components().render(rcx))
}
},
@ -168,10 +172,6 @@ impl Container {
// Container GETTERS.
pub fn id(&self) -> &IdentifierValue {
&self.id
}
pub fn classes(&self) -> &Classes {
&self.classes
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,5 +1,5 @@
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::server::HttpRequest;
use crate::{concat_string, config, util, LazyStatic};
@ -120,8 +120,8 @@ impl RenderContext {
// Context EXTRAS.
pub fn required_id<T>(&mut self, id: &IdentifierValue) -> String {
match id.get() {
pub fn required_id<T>(&mut self, id: Option<String>) -> String {
match id {
Some(id) => id,
None => {
let prefix = util::single_type_name::<T>()

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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