🚧 Mejora la estructura de las acciones

This commit is contained in:
Manuel Cillero 2023-07-09 19:21:47 +02:00
parent 1a32db1974
commit a05355c4d1
15 changed files with 192 additions and 180 deletions

View file

@ -154,7 +154,7 @@ impl MegaMenuItem {
use_handle!(COMPONENT_MEGAMENU); use_handle!(COMPONENT_MEGAMENU);
action_before_prepare_component!(ACTION_BEFORE_PREPARE_MENU for MegaMenu); actions_for_component!(MegaMenu);
#[rustfmt::skip] #[rustfmt::skip]
#[derive(Default)] #[derive(Default)]
@ -189,7 +189,7 @@ impl ComponentTrait for MegaMenu {
} }
fn before_prepare_component(&mut self, cx: &mut Context) { 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 { 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 { fn as_ref_any(&self) -> &dyn AnyComponent {
self self
} }

View file

@ -2,7 +2,7 @@ use pagetop::prelude::*;
use_handle!(COMPONENT_CONTAINER); use_handle!(COMPONENT_CONTAINER);
action_before_prepare_component!(ACTION_BEFORE_PREPARE_CONTAINER for Container); actions_for_component!(Container);
#[derive(Default)] #[derive(Default)]
pub enum ContainerType { pub enum ContainerType {
@ -51,7 +51,7 @@ impl ComponentTrait for Container {
} }
fn before_prepare_component(&mut self, cx: &mut Context) { 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 { 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 { fn as_ref_any(&self) -> &dyn AnyComponent {
self self
} }

View file

@ -2,7 +2,7 @@ use pagetop::prelude::*;
use_handle!(COMPONENT_FORM); use_handle!(COMPONENT_FORM);
action_before_prepare_component!(ACTION_BEFORE_PREPARE_FORM for Form); actions_for_component!(Form);
#[derive(Default)] #[derive(Default)]
pub enum FormMethod { pub enum FormMethod {
@ -49,7 +49,7 @@ impl ComponentTrait for Form {
} }
fn before_prepare_component(&mut self, cx: &mut Context) { 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 { 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 { fn as_ref_any(&self) -> &dyn AnyComponent {
self self
} }

View file

@ -2,7 +2,7 @@ use pagetop::prelude::*;
use_handle!(COMPONENT_COLUMN); 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__DEFAULT: &str = "col-md";
const SIZE__1_OF_12: &str = "col-md-1"; 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) { 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 { 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 { fn as_ref_any(&self) -> &dyn AnyComponent {
self self
} }

View file

@ -4,7 +4,7 @@ use crate::component::grid;
use_handle!(COMPONENT_ROW); use_handle!(COMPONENT_ROW);
action_before_prepare_component!(ACTION_BEFORE_PREPARE_ROW for Row); actions_for_component!(Row);
#[rustfmt::skip] #[rustfmt::skip]
#[derive(Default)] #[derive(Default)]
@ -39,7 +39,7 @@ impl ComponentTrait for Row {
} }
fn before_prepare_component(&mut self, cx: &mut Context) { 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 { 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 { fn as_ref_any(&self) -> &dyn AnyComponent {
self self
} }

View file

@ -1,7 +1,142 @@
mod before_prepare_component;
mod after_prepare_component;
pub mod block;
pub mod page; 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 [<Action $Component>] = fn(component: &$Component, cx: &mut Context);
// *************************************************************************************
// ACTION BEFORE PREPARE COMPONENT
// *************************************************************************************
$crate::use_handle!([<ACTION_BEFORE_PREPARE_ $Component:upper>] for Action);
pub struct [<BeforePrepare $Component>] {
action: Option<[<Action $Component>]>,
weight: isize,
}
impl ActionTrait for [<BeforePrepare $Component>] {
fn new() -> Self {
[<BeforePrepare $Component>] {
action: None,
weight: 0,
}
}
fn handle(&self) -> Handle {
[<ACTION_BEFORE_PREPARE_ $Component:upper>]
}
fn weight(&self) -> isize {
self.weight
}
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
}
impl [<BeforePrepare $Component>] {
#[allow(dead_code)]
pub fn with_action(mut self, action: [<Action $Component>]) -> 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 [<run_actions_before_prepare_ $Component:lower>](
component: &mut $Component,
cx: &mut Context
) {
run_actions([<ACTION_BEFORE_PREPARE_ $Component:upper>], |action|
action_ref::<[<BeforePrepare $Component>]>(&**action)
.run(component, cx)
);
}
// *************************************************************************************
// ACTION AFTER PREPARE COMPONENT
// *************************************************************************************
$crate::use_handle!([<ACTION_AFTER_PREPARE_ $Component:upper>] for Action);
pub struct [<AfterPrepare $Component>] {
action: Option<[<Action $Component>]>,
weight: isize,
}
impl ActionTrait for [<AfterPrepare $Component>] {
fn new() -> Self {
[<AfterPrepare $Component>] {
action: None,
weight: 0,
}
}
fn handle(&self) -> Handle {
[<ACTION_AFTER_PREPARE_ $Component:upper>]
}
fn weight(&self) -> isize {
self.weight
}
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
}
impl [<AfterPrepare $Component>] {
#[allow(dead_code)]
pub fn with_action(mut self, action: [<Action $Component>]) -> 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 [<run_actions_after_prepare_ $Component:lower>](
component: &mut $Component,
cx: &mut Context
) {
run_actions([<ACTION_AFTER_PREPARE_ $Component:upper>], |action|
action_ref::<[<AfterPrepare $Component>]>(&**action)
.run(component, cx)
);
}
}
};
}

View file

@ -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 [<AfterPrepare $Component>] {
action: Option<ActionAfter>,
weight: isize,
}
impl ActionTrait for [<AfterPrepare $Component>] {
fn new() -> Self {
[<AfterPrepare $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 [<AfterPrepare $Component>] {
#[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::<[<AfterPrepare $Component>]>(&**action)
.run(component, cx)
);
}
}
};
}

View file

@ -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 [<BeforePrepare $Component>] {
action: Option<ActionBefore>,
weight: isize,
}
impl ActionTrait for [<BeforePrepare $Component>] {
fn new() -> Self {
[<BeforePrepare $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 [<BeforePrepare $Component>] {
#[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::<[<BeforePrepare $Component>]>(&**action)
.run(component, cx)
);
}
}
};
}

View file

@ -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
);

View file

@ -1,13 +1,9 @@
use crate::prelude::*; use crate::response::page::Page;
pub type ActionPage = fn(page: &mut Page); pub type ActionPage = fn(page: &mut Page);
mod before_prepare_page; mod before_prepare_page;
pub use before_prepare_page::{ pub use before_prepare_page::*;
run_actions_before_prepare_page, ActionBeforePreparePage, ACTION_BEFORE_PREPARE_PAGE,
};
mod before_render_page; mod before_render_page;
pub use before_render_page::{ pub use before_render_page::*;
run_actions_before_render_page, ActionBeforeRenderPage, ACTION_BEFORE_RENDER_PAGE,
};

View file

@ -2,7 +2,7 @@ use crate::prelude::*;
use super::ActionPage; use super::ActionPage;
use_handle!(ACTION_BEFORE_PREPARE_PAGE); use_handle!(ACTION_BEFORE_PREPARE_PAGE for Action);
pub struct ActionBeforePreparePage { pub struct ActionBeforePreparePage {
action: Option<ActionPage>, action: Option<ActionPage>,
@ -41,7 +41,7 @@ impl ActionBeforePreparePage {
self self
} }
pub fn run(&self, page: &mut Page) { pub(crate) fn run(&self, page: &mut Page) {
if let Some(action) = self.action { if let Some(action) = self.action {
action(page) action(page)
} }
@ -49,7 +49,7 @@ impl ActionBeforePreparePage {
} }
#[inline(always)] #[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| { run_actions(ACTION_BEFORE_PREPARE_PAGE, |action| {
action_ref::<ActionBeforePreparePage>(&**action).run(page) action_ref::<ActionBeforePreparePage>(&**action).run(page)
}); });

View file

@ -2,7 +2,7 @@ use crate::prelude::*;
use super::ActionPage; use super::ActionPage;
use_handle!(ACTION_BEFORE_RENDER_PAGE); use_handle!(ACTION_BEFORE_RENDER_PAGE for Action);
pub struct ActionBeforeRenderPage { pub struct ActionBeforeRenderPage {
action: Option<ActionPage>, action: Option<ActionPage>,
@ -41,7 +41,7 @@ impl ActionBeforeRenderPage {
self self
} }
pub fn run(&self, page: &mut Page) { pub(crate) fn run(&self, page: &mut Page) {
if let Some(action) = self.action { if let Some(action) = self.action {
action(page) action(page)
} }
@ -49,7 +49,7 @@ impl ActionBeforeRenderPage {
} }
#[inline(always)] #[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| { run_actions(ACTION_BEFORE_RENDER_PAGE, |action| {
action_ref::<ActionBeforeRenderPage>(&**action).run(page) action_ref::<ActionBeforeRenderPage>(&**action).run(page)
}); });

View file

@ -36,7 +36,7 @@ impl ComponentTrait for Block {
} }
fn before_prepare_component(&mut self, cx: &mut Context) { 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 { 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) { 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 { fn as_ref_any(&self) -> &dyn AnyComponent {

View file

@ -7,7 +7,7 @@ pub use crate::{
// Funciones y macros útiles. // Funciones y macros útiles.
pub use crate::util; 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}; pub use crate::{default_settings, kv, serve_static_files, use_handle, use_locale, use_static};
// ************************************************************************************************* // *************************************************************************************************

View file

@ -82,12 +82,15 @@ macro_rules! kv {
#[macro_export] #[macro_export]
macro_rules! use_handle { macro_rules! use_handle {
( $($HANDLE:ident),* $(,)? ) => { ( $HANDLE:ident ) => {
$( /// Public constant handle to represent a unique PageTop building element.
/// Public constant handle to represent a unique PageTop building element. pub const $HANDLE: $crate::Handle =
pub const $HANDLE: $crate::Handle = $crate::util::handle(module_path!(), file!(), line!(), column!());
$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!());
}; };
} }