💥 Integra componentes base en core

This commit is contained in:
Manuel Cillero 2023-07-10 18:38:59 +02:00
parent 7ea54060f8
commit abb35ef07f
14 changed files with 158 additions and 163 deletions

View file

@ -13,6 +13,8 @@ mod anchor;
pub use anchor::{Anchor, AnchorTarget, AnchorType, COMPONENT_ANCHOR};
mod image;
pub use image::{Image, COMPONENT_IMAGE};
mod block;
pub use block::{Block, COMPONENT_BLOCK};
pub mod form_element;
pub use form_element::{Form, FormMethod, COMPONENT_FORM};

View file

@ -1,7 +1,9 @@
use crate::prelude::*;
use pagetop::prelude::*;
use_handle!(COMPONENT_BLOCK);
actions_for_component!(Block);
#[rustfmt::skip]
#[derive(Default)]
pub struct Block {
@ -36,7 +38,7 @@ impl ComponentTrait for Block {
}
fn before_prepare_component(&mut self, cx: &mut Context) {
action::block::run_actions_before_prepare_block(self, cx);
run_actions_before_prepare_block(self, cx);
}
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
@ -54,7 +56,7 @@ impl ComponentTrait for Block {
}
fn after_prepare_component(&mut self, cx: &mut Context) {
action::block::run_actions_after_prepare_block(self, cx);
run_actions_after_prepare_block(self, cx);
}
fn as_ref_any(&self) -> &dyn AnyComponent {

View file

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

View file

@ -1,140 +0,0 @@
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,8 +0,0 @@
mod html;
pub use html::{Html, COMPONENT_HTML};
mod l10n;
pub use l10n::{L10n, COMPONENT_L10N};
mod block;
pub use block::{Block, COMPONENT_BLOCK};

View file

@ -12,3 +12,143 @@ pub use pack::{PackComponents, PackOp};
mod renderable;
pub use renderable::{IsRenderable, Renderable};
pub mod html;
pub mod l10n;
#[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,5 +1,5 @@
use crate::base::component::L10n;
use crate::core::action::Action;
use crate::core::component::l10n::L10n;
use crate::core::theme::ThemeStaticRef;
use crate::{service, util, Handle};

View file

@ -1,4 +1,4 @@
use crate::base::component::L10n;
use crate::core::component::l10n::L10n;
use crate::core::component::{ComponentTrait, Context};
use crate::core::module::ModuleTrait;
use crate::html::{html, Favicon, Markup};

View file

@ -87,7 +87,7 @@
//! use_locale!(LOCALE_SAMPLE["path/to/locale"]);
//! ```
//!
//! Usa el componente [L10n](crate::base::component::L10n) para incluir textos y contenidos
//! Usa el componente [L10n](crate::core::component::l10n::L10n) para incluir textos y contenidos
//! opcionalmente traducibles según el contexto de renderizado.
use crate::{config, kv, trace, LazyStatic};

View file

@ -23,10 +23,13 @@ pub use crate::html::*;
#[cfg(feature = "database")]
pub use crate::{db, db::*, migration_item, pub_migration};
pub use crate::core::{action::*, component::*, module::*, theme::*};
pub use crate::core::action::*;
pub use crate::core::component::html::*;
pub use crate::core::component::l10n::*;
pub use crate::core::component::*;
pub use crate::core::module::*;
pub use crate::core::theme::*;
pub use crate::base::action;
pub use crate::base::component::*;
pub use crate::base::theme;
pub use crate::service;

View file

@ -3,7 +3,7 @@ pub use error403::ERROR_403;
mod error404;
pub use error404::ERROR_404;
use crate::base::component::L10n;
use crate::core::component::l10n::L10n;
use crate::response::{page::Page, ResponseError};
use crate::service::http::{header::ContentType, StatusCode};
use crate::service::{HttpRequest, HttpResponse};

View file

@ -1,7 +1,7 @@
mod action;
pub use action::*;
use crate::base::component::L10n;
use crate::core::component::l10n::L10n;
use crate::core::component::{ComponentTrait, Context, ContextOp, OneComponent};
use crate::core::theme::ComponentsRegions;
use crate::html::{html, Classes, ClassesOp, Favicon, Markup, DOCTYPE};