💥 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

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

@ -1,135 +0,0 @@
use crate::prelude::*;
use_handle!(COMPONENT_BLOCK);
#[rustfmt::skip]
#[derive(Default)]
pub struct Block {
weight : isize,
renderable: Renderable,
id : IdentifierValue,
classes : Classes,
title : AttributeValue,
components: PackComponents,
template : String,
}
impl ComponentTrait for Block {
fn new() -> Self {
Block::default().with_classes(ClassesOp::SetDefault, "block")
}
fn handle(&self) -> Handle {
COMPONENT_BLOCK
}
fn id(&self) -> Option<String> {
self.id.get()
}
fn weight(&self) -> isize {
self.weight
}
fn is_renderable(&self, cx: &Context) -> bool {
(self.renderable.check)(cx)
}
fn before_prepare_component(&mut self, cx: &mut Context) {
action::block::run_actions_before_prepare_block(self, cx);
}
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
let id = cx.required_id::<Block>(self.id());
PrepareMarkup::With(html! {
div id=(id) class=[self.classes().get()] {
@if let Some(title) = self.title().get() {
h2 class="block__title" { (title) }
}
div class="block__body" {
(self.components().prepare(cx))
}
}
})
}
fn after_prepare_component(&mut self, cx: &mut Context) {
action::block::run_actions_after_prepare_block(self, cx);
}
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Block {
// Block BUILDER.
#[fn_builder]
pub fn alter_id(&mut self, id: &str) -> &mut Self {
self.id.alter_value(id);
self
}
#[fn_builder]
pub fn alter_weight(&mut self, weight: isize) -> &mut Self {
self.weight = weight;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: IsRenderable) -> &mut Self {
self.renderable.check = check;
self
}
#[fn_builder]
pub fn alter_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
#[fn_builder]
pub fn alter_title(&mut self, title: &str) -> &mut Self {
self.title.alter_value(title);
self
}
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.components.alter_pack(PackOp::Add, component);
self
}
pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_pack(op, component);
self
}
#[fn_builder]
pub fn alter_template(&mut self, template: &str) -> &mut Self {
self.template = template.to_owned();
self
}
// Block GETTERS.
pub fn classes(&self) -> &Classes {
&self.classes
}
pub fn title(&self) -> &AttributeValue {
&self.title
}
pub fn components(&self) -> &PackComponents {
&self.components
}
pub fn template(&self) -> &str {
self.template.as_str()
}
}

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};