diff --git a/pagetop-megamenu/src/component.rs b/pagetop-megamenu/src/component.rs index 4e7c60ae..d27c672e 100644 --- a/pagetop-megamenu/src/component.rs +++ b/pagetop-megamenu/src/component.rs @@ -154,7 +154,7 @@ impl MegaMenuItem { use_handle!(COMPONENT_MEGAMENU); -action_before_prepare_component!(ACTION_BEFORE_PREPARE_MENU for MegaMenu); +actions_for_component!(MegaMenu); #[rustfmt::skip] #[derive(Default)] @@ -189,7 +189,7 @@ impl ComponentTrait for MegaMenu { } fn before_prepare_component(&mut self, cx: &mut Context) { - run_actions_before_prepare_component(self, cx); + run_actions_before_prepare_megamenu(self, cx); } fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { @@ -219,6 +219,10 @@ impl ComponentTrait for MegaMenu { }) } + fn after_prepare_component(&mut self, cx: &mut Context) { + run_actions_after_prepare_megamenu(self, cx); + } + fn as_ref_any(&self) -> &dyn AnyComponent { self } diff --git a/pagetop-minimal/src/component/container.rs b/pagetop-minimal/src/component/container.rs index ec87627b..729abada 100644 --- a/pagetop-minimal/src/component/container.rs +++ b/pagetop-minimal/src/component/container.rs @@ -2,7 +2,7 @@ use pagetop::prelude::*; use_handle!(COMPONENT_CONTAINER); -action_before_prepare_component!(ACTION_BEFORE_PREPARE_CONTAINER for Container); +actions_for_component!(Container); #[derive(Default)] pub enum ContainerType { @@ -51,7 +51,7 @@ impl ComponentTrait for Container { } fn before_prepare_component(&mut self, cx: &mut Context) { - run_actions_before_prepare_component(self, cx); + run_actions_before_prepare_container(self, cx); } fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { @@ -92,6 +92,10 @@ impl ComponentTrait for Container { } } + fn after_prepare_component(&mut self, cx: &mut Context) { + run_actions_after_prepare_container(self, cx); + } + fn as_ref_any(&self) -> &dyn AnyComponent { self } diff --git a/pagetop-minimal/src/component/form_element/form.rs b/pagetop-minimal/src/component/form_element/form.rs index 416ac1ea..fc952471 100644 --- a/pagetop-minimal/src/component/form_element/form.rs +++ b/pagetop-minimal/src/component/form_element/form.rs @@ -2,7 +2,7 @@ use pagetop::prelude::*; use_handle!(COMPONENT_FORM); -action_before_prepare_component!(ACTION_BEFORE_PREPARE_FORM for Form); +actions_for_component!(Form); #[derive(Default)] pub enum FormMethod { @@ -49,7 +49,7 @@ impl ComponentTrait for Form { } fn before_prepare_component(&mut self, cx: &mut Context) { - run_actions_before_prepare_component(self, cx); + run_actions_before_prepare_form(self, cx); } fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { @@ -70,6 +70,10 @@ impl ComponentTrait for Form { }) } + fn after_prepare_component(&mut self, cx: &mut Context) { + run_actions_after_prepare_form(self, cx); + } + fn as_ref_any(&self) -> &dyn AnyComponent { self } diff --git a/pagetop-minimal/src/component/grid/column.rs b/pagetop-minimal/src/component/grid/column.rs index e55480da..90213109 100644 --- a/pagetop-minimal/src/component/grid/column.rs +++ b/pagetop-minimal/src/component/grid/column.rs @@ -2,7 +2,7 @@ use pagetop::prelude::*; use_handle!(COMPONENT_COLUMN); -action_before_prepare_component!(ACTION_BEFORE_PREPARE_COLUMN for Column); +actions_for_component!(Column); const SIZE__DEFAULT: &str = "col-md"; const SIZE__1_OF_12: &str = "col-md-1"; @@ -70,7 +70,7 @@ impl ComponentTrait for Column { } fn before_prepare_component(&mut self, cx: &mut Context) { - run_actions_before_prepare_component(self, cx); + run_actions_before_prepare_column(self, cx); } fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { @@ -81,6 +81,10 @@ impl ComponentTrait for Column { }) } + fn after_prepare_component(&mut self, cx: &mut Context) { + run_actions_after_prepare_column(self, cx); + } + fn as_ref_any(&self) -> &dyn AnyComponent { self } diff --git a/pagetop-minimal/src/component/grid/row.rs b/pagetop-minimal/src/component/grid/row.rs index abda86df..d30f2d09 100644 --- a/pagetop-minimal/src/component/grid/row.rs +++ b/pagetop-minimal/src/component/grid/row.rs @@ -4,7 +4,7 @@ use crate::component::grid; use_handle!(COMPONENT_ROW); -action_before_prepare_component!(ACTION_BEFORE_PREPARE_ROW for Row); +actions_for_component!(Row); #[rustfmt::skip] #[derive(Default)] @@ -39,7 +39,7 @@ impl ComponentTrait for Row { } fn before_prepare_component(&mut self, cx: &mut Context) { - run_actions_before_prepare_component(self, cx); + run_actions_before_prepare_row(self, cx); } fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { @@ -50,6 +50,10 @@ impl ComponentTrait for Row { }) } + fn after_prepare_component(&mut self, cx: &mut Context) { + run_actions_after_prepare_row(self, cx); + } + fn as_ref_any(&self) -> &dyn AnyComponent { self } diff --git a/pagetop/src/base/actions.rs b/pagetop/src/base/actions.rs index 3764ff1e..250e093c 100644 --- a/pagetop/src/base/actions.rs +++ b/pagetop/src/base/actions.rs @@ -1,7 +1,142 @@ -mod before_prepare_component; - -mod after_prepare_component; - -pub mod block; - pub mod page; + +pub mod block { + crate::actions_for_component!(Block); +} + +#[macro_export] +macro_rules! actions_for_component { + ( $Component:ty ) => { + $crate::paste! { + use $crate::prelude::*; + + pub type [] = fn(component: &$Component, cx: &mut Context); + + // ************************************************************************************* + // ACTION BEFORE PREPARE COMPONENT + // ************************************************************************************* + + $crate::use_handle!([] for Action); + + pub struct [] { + action: Option<[]>, + weight: isize, + } + + impl ActionTrait for [] { + fn new() -> Self { + [] { + action: None, + weight: 0, + } + } + + fn handle(&self) -> Handle { + [] + } + + fn weight(&self) -> isize { + self.weight + } + + fn as_ref_any(&self) -> &dyn AnyAction { + self + } + } + + impl [] { + #[allow(dead_code)] + pub fn with_action(mut self, action: []) -> Self { + self.action = Some(action); + self + } + + #[allow(dead_code)] + pub fn with_weight(mut self, weight: isize) -> Self { + self.weight = weight; + self + } + + pub(crate) fn run(&self, component: &mut $Component, cx: &mut Context) { + if let Some(action) = self.action { + action(component, cx) + } + } + } + + #[inline(always)] + pub(crate) fn []( + component: &mut $Component, + cx: &mut Context + ) { + run_actions([], |action| + action_ref::<[]>(&**action) + .run(component, cx) + ); + } + + // ************************************************************************************* + // ACTION AFTER PREPARE COMPONENT + // ************************************************************************************* + + $crate::use_handle!([] for Action); + + pub struct [] { + action: Option<[]>, + weight: isize, + } + + impl ActionTrait for [] { + fn new() -> Self { + [] { + action: None, + weight: 0, + } + } + + fn handle(&self) -> Handle { + [] + } + + fn weight(&self) -> isize { + self.weight + } + + fn as_ref_any(&self) -> &dyn AnyAction { + self + } + } + + impl [] { + #[allow(dead_code)] + pub fn with_action(mut self, action: []) -> Self { + self.action = Some(action); + self + } + + #[allow(dead_code)] + pub fn with_weight(mut self, weight: isize) -> Self { + self.weight = weight; + self + } + + pub(crate) fn run(&self, component: &mut $Component, cx: &mut Context) { + if let Some(action) = self.action { + action(component, cx) + } + } + } + + #[inline(always)] + pub(crate) fn []( + component: &mut $Component, + cx: &mut Context + ) { + run_actions([], |action| + action_ref::<[]>(&**action) + .run(component, cx) + ); + } + } + }; +} diff --git a/pagetop/src/base/actions/after_prepare_component.rs b/pagetop/src/base/actions/after_prepare_component.rs deleted file mode 100644 index 34ca40b3..00000000 --- a/pagetop/src/base/actions/after_prepare_component.rs +++ /dev/null @@ -1,67 +0,0 @@ -#[macro_export] -macro_rules! action_after_prepare_component { - ( $ACTION_HANDLE:ident for $Component:ty ) => { - $crate::paste! { - $crate::use_handle!($ACTION_HANDLE); - - pub type ActionAfter = fn(component: &$Component, cx: &mut Context); - - pub struct [] { - action: Option, - weight: isize, - } - - impl ActionTrait for [] { - fn new() -> Self { - [] { - 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 [] { - #[allow(dead_code)] - pub fn with_action(mut self, action: ActionAfter) -> 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, cx: &mut Context) { - if let Some(action) = self.action { - action(component, cx) - } - } - } - - #[inline(always)] - pub fn run_actions_after_prepare_component( - component: &mut $Component, - cx: &mut Context - ) { - run_actions($ACTION_HANDLE, |action| - action_ref::<[]>(&**action) - .run(component, cx) - ); - } - } - }; -} diff --git a/pagetop/src/base/actions/before_prepare_component.rs b/pagetop/src/base/actions/before_prepare_component.rs deleted file mode 100644 index 6aac7090..00000000 --- a/pagetop/src/base/actions/before_prepare_component.rs +++ /dev/null @@ -1,67 +0,0 @@ -#[macro_export] -macro_rules! action_before_prepare_component { - ( $ACTION_HANDLE:ident for $Component:ty ) => { - $crate::paste! { - $crate::use_handle!($ACTION_HANDLE); - - pub type ActionBefore = fn(component: &$Component, cx: &mut Context); - - pub struct [] { - action: Option, - weight: isize, - } - - impl ActionTrait for [] { - fn new() -> Self { - [] { - 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 [] { - #[allow(dead_code)] - pub fn with_action(mut self, action: ActionBefore) -> 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, cx: &mut Context) { - if let Some(action) = self.action { - action(component, cx) - } - } - } - - #[inline(always)] - pub fn run_actions_before_prepare_component( - component: &mut $Component, - cx: &mut Context - ) { - run_actions($ACTION_HANDLE, |action| - action_ref::<[]>(&**action) - .run(component, cx) - ); - } - } - }; -} diff --git a/pagetop/src/base/actions/block.rs b/pagetop/src/base/actions/block.rs deleted file mode 100644 index 25b364ad..00000000 --- a/pagetop/src/base/actions/block.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::prelude::*; - -action_before_prepare_component!( - ACTION_BEFORE_PREPARE_BLOCK for Block -); -action_after_prepare_component!( - ACTION_AFTER_PREPARE_BLOCK for Block -); diff --git a/pagetop/src/base/actions/page.rs b/pagetop/src/base/actions/page.rs index 20e55a3a..eb8791e3 100644 --- a/pagetop/src/base/actions/page.rs +++ b/pagetop/src/base/actions/page.rs @@ -1,13 +1,9 @@ -use crate::prelude::*; +use crate::response::page::Page; pub type ActionPage = fn(page: &mut Page); mod before_prepare_page; -pub use before_prepare_page::{ - run_actions_before_prepare_page, ActionBeforePreparePage, ACTION_BEFORE_PREPARE_PAGE, -}; +pub use before_prepare_page::*; mod before_render_page; -pub use before_render_page::{ - run_actions_before_render_page, ActionBeforeRenderPage, ACTION_BEFORE_RENDER_PAGE, -}; +pub use before_render_page::*; diff --git a/pagetop/src/base/actions/page/before_prepare_page.rs b/pagetop/src/base/actions/page/before_prepare_page.rs index 13fdd909..aa9ecf61 100644 --- a/pagetop/src/base/actions/page/before_prepare_page.rs +++ b/pagetop/src/base/actions/page/before_prepare_page.rs @@ -2,7 +2,7 @@ use crate::prelude::*; use super::ActionPage; -use_handle!(ACTION_BEFORE_PREPARE_PAGE); +use_handle!(ACTION_BEFORE_PREPARE_PAGE for Action); pub struct ActionBeforePreparePage { action: Option, @@ -41,7 +41,7 @@ impl ActionBeforePreparePage { self } - pub fn run(&self, page: &mut Page) { + pub(crate) fn run(&self, page: &mut Page) { if let Some(action) = self.action { action(page) } @@ -49,7 +49,7 @@ impl ActionBeforePreparePage { } #[inline(always)] -pub fn run_actions_before_prepare_page(page: &mut Page) { +pub(crate) fn run_actions_before_prepare_page(page: &mut Page) { run_actions(ACTION_BEFORE_PREPARE_PAGE, |action| { action_ref::(&**action).run(page) }); diff --git a/pagetop/src/base/actions/page/before_render_page.rs b/pagetop/src/base/actions/page/before_render_page.rs index d035d2d8..733eb7e5 100644 --- a/pagetop/src/base/actions/page/before_render_page.rs +++ b/pagetop/src/base/actions/page/before_render_page.rs @@ -2,7 +2,7 @@ use crate::prelude::*; use super::ActionPage; -use_handle!(ACTION_BEFORE_RENDER_PAGE); +use_handle!(ACTION_BEFORE_RENDER_PAGE for Action); pub struct ActionBeforeRenderPage { action: Option, @@ -41,7 +41,7 @@ impl ActionBeforeRenderPage { self } - pub fn run(&self, page: &mut Page) { + pub(crate) fn run(&self, page: &mut Page) { if let Some(action) = self.action { action(page) } @@ -49,7 +49,7 @@ impl ActionBeforeRenderPage { } #[inline(always)] -pub fn run_actions_before_render_page(page: &mut Page) { +pub(crate) fn run_actions_before_render_page(page: &mut Page) { run_actions(ACTION_BEFORE_RENDER_PAGE, |action| { action_ref::(&**action).run(page) }); diff --git a/pagetop/src/base/components/block.rs b/pagetop/src/base/components/block.rs index 4a45b94d..74095a79 100644 --- a/pagetop/src/base/components/block.rs +++ b/pagetop/src/base/components/block.rs @@ -36,7 +36,7 @@ impl ComponentTrait for Block { } fn before_prepare_component(&mut self, cx: &mut Context) { - actions::block::run_actions_before_prepare_component(self, cx); + actions::block::run_actions_before_prepare_block(self, cx); } fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { @@ -54,7 +54,7 @@ impl ComponentTrait for Block { } fn after_prepare_component(&mut self, cx: &mut Context) { - actions::block::run_actions_after_prepare_component(self, cx); + actions::block::run_actions_after_prepare_block(self, cx); } fn as_ref_any(&self) -> &dyn AnyComponent { diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index dd1e1a65..3574fc55 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -7,7 +7,7 @@ pub use crate::{ // Funciones y macros Ăștiles. pub use crate::util; -pub use crate::{action, action_after_prepare_component, action_before_prepare_component}; +pub use crate::{action, actions_for_component}; pub use crate::{default_settings, kv, serve_static_files, use_handle, use_locale, use_static}; // ************************************************************************************************* diff --git a/pagetop/src/util.rs b/pagetop/src/util.rs index 5b695ee0..40d847cc 100644 --- a/pagetop/src/util.rs +++ b/pagetop/src/util.rs @@ -82,12 +82,15 @@ macro_rules! kv { #[macro_export] macro_rules! use_handle { - ( $($HANDLE:ident),* $(,)? ) => { - $( - /// Public constant handle to represent a unique PageTop building element. - pub const $HANDLE: $crate::Handle = - $crate::util::handle(module_path!(), file!(), line!(), column!()); - )* + ( $HANDLE:ident ) => { + /// Public constant handle to represent a unique PageTop building element. + pub const $HANDLE: $crate::Handle = + $crate::util::handle(module_path!(), file!(), line!(), column!()); + }; + ( $HANDLE:ident for Action ) => { + /// Constant handle to represent a unique PageTop action. + pub(crate) const $HANDLE: $crate::Handle = + $crate::util::handle(module_path!(), file!(), line!(), column!()); }; }