🚚 Define acción base "before_render_component"

This commit is contained in:
Manuel Cillero 2023-06-07 23:21:42 +02:00
parent d8812433f1
commit cb04a29388
12 changed files with 81 additions and 81 deletions

View file

@ -153,7 +153,7 @@ impl MegaMenuItem {
define_handle!(COMPONENT_MEGAMENU);
action_before_render_component!(ACTION_BEFORE_RENDER_MENU, MegaMenu);
action_before_render_component!(ACTION_BEFORE_RENDER_MENU for MegaMenu);
#[rustfmt::skip]
#[derive(Default)]

View file

@ -2,7 +2,7 @@ use pagetop::prelude::*;
define_handle!(COMPONENT_CONTAINER);
action_before_render_component!(ACTION_BEFORE_RENDER_CONTAINER, Container);
action_before_render_component!(ACTION_BEFORE_RENDER_CONTAINER for Container);
#[derive(Default)]
pub enum ContainerType {

View file

@ -2,7 +2,7 @@ use pagetop::prelude::*;
define_handle!(COMPONENT_FORM);
action_before_render_component!(ACTION_BEFORE_RENDER_FORM, Form);
action_before_render_component!(ACTION_BEFORE_RENDER_FORM for Form);
#[derive(Default)]
pub enum FormMethod {

View file

@ -2,7 +2,7 @@ use pagetop::prelude::*;
define_handle!(COMPONENT_COLUMN);
action_before_render_component!(ACTION_BEFORE_RENDER_COLUMN, Column);
action_before_render_component!(ACTION_BEFORE_RENDER_COLUMN for Column);
const SIZE__DEFAULT: &str = "col-md";
const SIZE__1_OF_12: &str = "col-md-1";

View file

@ -4,7 +4,7 @@ use crate::component::grid;
define_handle!(COMPONENT_ROW);
action_before_render_component!(ACTION_BEFORE_RENDER_ROW, Row);
action_before_render_component!(ACTION_BEFORE_RENDER_ROW for Row);
#[rustfmt::skip]
#[derive(Default)]

View file

@ -1,3 +1,5 @@
pub mod action;
pub mod component;
pub mod theme;

View file

@ -0,0 +1 @@
mod before_render_component;

View file

@ -0,0 +1,64 @@
#[macro_export]
macro_rules! action_before_render_component {
( $ACTION_HANDLE:ident for $Component:ty ) => {
$crate::paste! {
$crate::define_handle!($ACTION_HANDLE);
type Action = fn(&$Component, &mut RenderContext);
pub struct [< BeforeRender $Component >] {
action: Option<Action>,
weight: isize,
}
impl ActionTrait for [< BeforeRender $Component >] {
fn new() -> Self {
[< BeforeRender $Component >] {
action: None,
weight: 0,
}
}
fn handle(&self) -> Handle {
$ACTION_HANDLE
}
fn weight(&self) -> isize {
self.weight
}
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
}
impl [< BeforeRender $Component >] {
#[allow(dead_code)]
pub fn with_action(mut self, action: Action) -> Self {
self.action = Some(action);
self
}
#[allow(dead_code)]
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
self
}
pub fn run(&self, component: &mut $Component, rcx: &mut RenderContext) {
if let Some(action) = self.action {
action(component, rcx)
}
}
}
#[inline(always)]
pub fn before_render_inline(component: &mut $Component, rcx: &mut RenderContext) {
run_actions($ACTION_HANDLE, |action|
action_ref::<[< BeforeRender $Component >]>(&**action)
.run(component, rcx)
);
}
}
};
}

View file

@ -2,7 +2,7 @@ use crate::prelude::*;
define_handle!(COMPONENT_BLOCK);
action_before_render_component!(ACTION_BEFORE_RENDER_BLOCK, Block);
action_before_render_component!(ACTION_BEFORE_RENDER_BLOCK for Block);
#[rustfmt::skip]
#[derive(Default)]

View file

@ -11,20 +11,18 @@ pub enum L10nOp {
Escaped(&'static str, &'static Locales),
}
impl Default for L10nOp {
fn default() -> Self {
L10nOp::None
}
}
#[derive(Default)]
pub struct L10n {
op: L10nOp,
args: HashMap<&'static str, String>,
}
impl Default for L10n {
fn default() -> Self {
L10n {
op: L10nOp::None,
args: HashMap::new(),
}
}
}
impl ComponentTrait for L10n {
fn new() -> Self {
L10n::default()

View file

@ -74,68 +74,3 @@ pub fn component_ref<C: 'static>(component: &dyn ComponentTrait) -> &C {
pub fn component_mut<C: 'static>(component: &mut dyn ComponentTrait) -> &mut C {
component.as_mut_any().downcast_mut::<C>().unwrap()
}
#[macro_export]
macro_rules! action_before_render_component {
( $ACTION_HANDLE:ident, $Component:ty ) => {
$crate::paste! {
$crate::define_handle!($ACTION_HANDLE);
type Action = fn(&$Component, &mut RenderContext);
pub struct [< BeforeRender $Component >] {
action: Option<Action>,
weight: isize,
}
impl ActionTrait for [< BeforeRender $Component >] {
fn new() -> Self {
[< BeforeRender $Component >] {
action: None,
weight: 0,
}
}
fn handle(&self) -> Handle {
$ACTION_HANDLE
}
fn weight(&self) -> isize {
self.weight
}
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
}
impl [< BeforeRender $Component >] {
#[allow(dead_code)]
pub fn with_action(mut self, action: Action) -> Self {
self.action = Some(action);
self
}
#[allow(dead_code)]
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
self
}
pub fn run(&self, component: &mut $Component, rcx: &mut RenderContext) {
if let Some(action) = self.action {
action(component, rcx)
}
}
}
#[inline(always)]
pub fn before_render_inline(component: &mut $Component, rcx: &mut RenderContext) {
run_actions($ACTION_HANDLE, |action|
action_ref::<[< BeforeRender $Component >]>(&**action)
.run(component, rcx)
);
}
}
};
}

View file

@ -78,7 +78,7 @@
//!
//! define_locale!(LOCALE_SAMPLE, "static/locales");
//! ```
//! Y utiliza el componente [L10n](crate::core::component::L10n) para incluir, en respuestas a las
//! Y utiliza el componente [L10n](crate::base::component::L10n) para incluir, en respuestas a las
//! peticiones web, textos y contenidos opcionalmente traducibles según el contexto de renderizado.
use crate::{args, config, trace, LazyStatic};