🚧 Working on actions

This commit is contained in:
Manuel Cillero 2024-03-27 07:12:42 +01:00
parent e732244a2e
commit d99a5aa586
23 changed files with 480 additions and 323 deletions

View file

@ -15,10 +15,10 @@ impl PackageTrait for Admin {
L10n::t("package_description", &LOCALES_ADMIN)
}
fn actions(&self) -> Vec<Action> {
fn actions(&self) -> Vec<ActionBox> {
actions![
action::page::BeforePrepareBody::new(before_prepare_body),
action::component::BeforePrepareComponent::<Menu>::new(before_prepare_menu)
action::component::BeforePrepare::<Menu>::new(before_prepare_menu)
.filter_by_referer_id("admin-menu-test"),
]
}

View file

@ -11,6 +11,17 @@ impl PackageTrait for Bootsier {
Some(&Bootsier)
}
fn actions(&self) -> Vec<ActionBox> {
actions![
action::theme::BeforePrepare::<Icon>::new(&Self, before_prepare_icon),
action::theme::BeforePrepare::<Button>::new(&Self, before_prepare_button),
action::theme::BeforePrepare::<Heading>::new(&Self, before_prepare_heading),
action::theme::BeforePrepare::<Paragraph>::new(&Self, before_prepare_paragraph),
action::theme::RenderComponent::<Layout>::new(&Self, render_layout),
action::theme::RenderComponent::<Error404>::new(&Self, render_error404),
]
}
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
service_for_static_files!(scfg, bootsier => "/bootsier");
}
@ -49,114 +60,117 @@ impl ThemeTrait for Bootsier {
StyleSheet::at("/bootsier/css/styles.css").with_version("0.0.1"),
));
}
}
#[rustfmt::skip]
fn before_prepare_component(&self, component: &mut dyn ComponentTrait, _cx: &mut Context) {
if let Some(i) = component.downcast_mut::<Icon>() {
i.alter_classes(
ClassesOp::Replace(i.font_size().to_string()),
with_font(i.font_size()),
);
} else if let Some(b) = component.downcast_mut::<Button>() {
b.alter_classes(ClassesOp::Replace("button__tap".to_owned()), "btn");
b.alter_classes(
ClassesOp::Replace(b.style().to_string()),
match b.style() {
StyleBase::Default => "btn-primary",
StyleBase::Info => "btn-info",
StyleBase::Success => "btn-success",
StyleBase::Warning => "btn-warning",
StyleBase::Danger => "btn-danger",
StyleBase::Light => "btn-light",
StyleBase::Dark => "btn-dark",
StyleBase::Link => "btn-link",
},
);
b.alter_classes(
ClassesOp::Replace(b.font_size().to_string()),
with_font(b.font_size()),
);
} else if let Some(h) = component.downcast_mut::<Heading>() {
h.alter_classes(
ClassesOp::Replace(h.size().to_string()),
match h.size() {
HeadingSize::ExtraLarge => "display-1",
HeadingSize::XxLarge => "display-2",
HeadingSize::XLarge => "display-3",
HeadingSize::Large => "display-4",
HeadingSize::Medium => "display-5",
_ => "",
},
);
} else if let Some(p) = component.downcast_mut::<Paragraph>() {
p.alter_classes(
ClassesOp::Replace(p.font_size().to_string()),
with_font(p.font_size()),
);
fn before_prepare_icon(i: &mut Icon, _cx: &mut Context) {
i.alter_classes(
ClassesOp::Replace(i.font_size().to_string()),
with_font(i.font_size()),
);
}
#[rustfmt::skip]
fn before_prepare_button(b: &mut Button, _cx: &mut Context) {
b.alter_classes(ClassesOp::Replace("button__tap".to_owned()), "btn");
b.alter_classes(
ClassesOp::Replace(b.style().to_string()),
match b.style() {
StyleBase::Default => "btn-primary",
StyleBase::Info => "btn-info",
StyleBase::Success => "btn-success",
StyleBase::Warning => "btn-warning",
StyleBase::Danger => "btn-danger",
StyleBase::Light => "btn-light",
StyleBase::Dark => "btn-dark",
StyleBase::Link => "btn-link",
},
);
b.alter_classes(
ClassesOp::Replace(b.font_size().to_string()),
with_font(b.font_size()),
);
}
#[rustfmt::skip]
fn before_prepare_heading(h: &mut Heading, _cx: &mut Context) {
h.alter_classes(
ClassesOp::Replace(h.size().to_string()),
match h.size() {
HeadingSize::ExtraLarge => "display-1",
HeadingSize::XxLarge => "display-2",
HeadingSize::XLarge => "display-3",
HeadingSize::Large => "display-4",
HeadingSize::Medium => "display-5",
_ => "",
},
);
}
fn before_prepare_paragraph(p: &mut Paragraph, _cx: &mut Context) {
p.alter_classes(
ClassesOp::Replace(p.font_size().to_string()),
with_font(p.font_size()),
);
}
fn render_layout(_: &Layout, cx: &mut Context) -> Option<Markup> {
Some(
match cx.layout() {
"admin" => Container::new().add_item(
Flex::new()
.add_component(Region::named("top-menu"))
.add_component(Region::named("side-menu"))
.add_component(Region::named("content")),
),
_ => Container::new().add_item(
Flex::new()
.add_component(Region::named("header"))
.add_component(Region::named("nav_branding"))
.add_component(Region::named("nav_main"))
.add_component(Region::named("nav_additional"))
.add_component(Region::named("breadcrumb"))
.add_component(Region::named("content"))
.add_component(Region::named("sidebar_first"))
.add_component(Region::named("sidebar_second"))
.add_component(Region::named("footer")),
),
}
}
.render(cx),
)
}
fn render_component(&self, component: &dyn ComponentTrait, cx: &mut Context) -> Option<Markup> {
if component.downcast_ref::<Layout>().is_some() {
Some(
match cx.layout() {
"admin" => Container::new().add_item(
Flex::new()
.add_component(Region::named("top-menu"))
.add_component(Region::named("side-menu"))
.add_component(Region::named("content")),
),
_ => Container::new().add_item(
Flex::new()
.add_component(Region::named("header"))
.add_component(Region::named("nav_branding"))
.add_component(Region::named("nav_main"))
.add_component(Region::named("nav_additional"))
.add_component(Region::named("breadcrumb"))
.add_component(Region::named("content"))
.add_component(Region::named("sidebar_first"))
.add_component(Region::named("sidebar_second"))
.add_component(Region::named("footer")),
),
}
.render(cx),
)
} else if component.downcast_ref::<Error404>().is_some() {
Some(html! {
div class="jumbotron" {
div class="media" {
img
src="/bootsier/images/caution.png"
class="mr-4"
style="width: 20%; max-width: 188px"
alt="Caution!";
div class="media-body" {
h1 class="display-4" { ("RESOURCE NOT FOUND") }
p class="lead" {
(L10n::t("e404-description", &LOCALES_BOOTSIER)
.escaped(cx.langid()))
}
hr class="my-4";
p {
(L10n::t("e404-description", &LOCALES_BOOTSIER)
.escaped(cx.langid()))
}
a
class="btn btn-primary btn-lg"
href="/"
role="button"
{
(L10n::t("back-homepage", &LOCALES_BOOTSIER)
.escaped(cx.langid()))
}
}
fn render_error404(_: &Error404, cx: &mut Context) -> Option<Markup> {
Some(html! {
div class="jumbotron" {
div class="media" {
img
src="/bootsier/images/caution.png"
class="mr-4"
style="width: 20%; max-width: 188px"
alt="Caution!";
div class="media-body" {
h1 class="display-4" { ("RESOURCE NOT FOUND") }
p class="lead" {
(L10n::t("e404-description", &LOCALES_BOOTSIER)
.escaped(cx.langid()))
}
hr class="my-4";
p {
(L10n::t("e404-description", &LOCALES_BOOTSIER)
.escaped(cx.langid()))
}
a
class="btn btn-primary btn-lg"
href="/"
role="button"
{
(L10n::t("back-homepage", &LOCALES_BOOTSIER)
.escaped(cx.langid()))
}
}
})
} else {
None
}
}
}
})
}
#[rustfmt::skip]

View file

@ -9,6 +9,16 @@ impl PackageTrait for Bulmix {
Some(&Bulmix)
}
fn actions(&self) -> Vec<ActionBox> {
actions![
action::theme::BeforePrepare::<Icon>::new(&Self, before_prepare_icon),
action::theme::BeforePrepare::<Button>::new(&Self, before_prepare_button),
action::theme::BeforePrepare::<Heading>::new(&Self, before_prepare_heading),
action::theme::BeforePrepare::<Paragraph>::new(&Self, before_prepare_paragraph),
action::theme::RenderComponent::<Icon>::new(&Self, render_icon),
]
}
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
service_for_static_files!(scfg, bulmix => "/bulmix");
}
@ -38,63 +48,60 @@ impl ThemeTrait for Bulmix {
StyleSheet::at("/bulmix/css/styles.css").with_version("0.0.1"),
));
}
}
#[rustfmt::skip]
fn before_prepare_component(&self, component: &mut dyn ComponentTrait, _cx: &mut Context) {
if let Some(i) = component.downcast_mut::<Icon>() {
i.alter_classes(
ClassesOp::Replace(i.font_size().to_string()),
with_font(i.font_size()),
);
} else if let Some(b) = component.downcast_mut::<Button>() {
b.alter_classes(ClassesOp::Replace("button__tap".to_owned()), "button");
b.alter_classes(
ClassesOp::Replace(b.style().to_string()),
match b.style() {
StyleBase::Default => "is-primary",
StyleBase::Info => "is-info",
StyleBase::Success => "is-success",
StyleBase::Warning => "is-warning",
StyleBase::Danger => "is-danger",
StyleBase::Light => "is-light",
StyleBase::Dark => "is-dark",
StyleBase::Link => "is-text",
},
);
b.alter_classes(
ClassesOp::Replace(b.font_size().to_string()),
with_font(b.font_size()),
);
} else if let Some(h) = component.downcast_mut::<Heading>() {
match h.size() {
HeadingSize::Subtitle => {
h.alter_classes(ClassesOp::Replace(h.size().to_string()), "subtitle")
}
_ => h.alter_classes(ClassesOp::Add, "title"),
};
} else if let Some(p) = component.downcast_mut::<Paragraph>() {
p.alter_classes(ClassesOp::Add, "block");
p.alter_classes(
ClassesOp::Replace(p.font_size().to_string()),
with_font(p.font_size()),
);
}
}
fn before_prepare_icon(i: &mut Icon, _cx: &mut Context) {
i.alter_classes(
ClassesOp::Replace(i.font_size().to_string()),
with_font(i.font_size()),
);
}
fn render_component(
&self,
component: &dyn ComponentTrait,
_cx: &mut Context,
) -> Option<Markup> {
if let Some(i) = component.downcast_ref::<Icon>() {
return match i.icon_name().get() {
None => None,
_ => Some(html! { span class="icon" { i class=[i.classes().get()] {} } }),
};
} else {
None
#[rustfmt::skip]
fn before_prepare_button(b: &mut Button, _cx: &mut Context) {
b.alter_classes(ClassesOp::Replace("button__tap".to_owned()), "button");
b.alter_classes(
ClassesOp::Replace(b.style().to_string()),
match b.style() {
StyleBase::Default => "is-primary",
StyleBase::Info => "is-info",
StyleBase::Success => "is-success",
StyleBase::Warning => "is-warning",
StyleBase::Danger => "is-danger",
StyleBase::Light => "is-light",
StyleBase::Dark => "is-dark",
StyleBase::Link => "is-text",
},
);
b.alter_classes(
ClassesOp::Replace(b.font_size().to_string()),
with_font(b.font_size()),
);
}
#[rustfmt::skip]
fn before_prepare_heading(h: &mut Heading, _cx: &mut Context) {
match h.size() {
HeadingSize::Subtitle => {
h.alter_classes(ClassesOp::Replace(h.size().to_string()), "subtitle")
}
}
_ => h.alter_classes(ClassesOp::Add, "title"),
};
}
fn before_prepare_paragraph(p: &mut Paragraph, _cx: &mut Context) {
p.alter_classes(ClassesOp::Add, "block");
p.alter_classes(
ClassesOp::Replace(p.font_size().to_string()),
with_font(p.font_size()),
);
}
fn render_icon(i: &Icon, _cx: &mut Context) -> Option<Markup> {
return match i.icon_name().get() {
None => None,
_ => Some(html! { span class="icon" { i class=[i.classes().get()] {} } }),
};
}
#[rustfmt::skip]

View file

@ -20,7 +20,7 @@ impl PackageTrait for Node {
scfg.route("/node", service::web::get().to(node));
}
fn actions(&self) -> Vec<Action> {
fn actions(&self) -> Vec<ActionBox> {
actions![action::page::BeforePrepareBody::new(before_prepare_body).with_weight(-1)]
}

View file

@ -1,3 +1,9 @@
pub mod component;
use crate::prelude::*;
pub type FnActionWithComponent<C> = fn(component: &mut C, cx: &mut Context);
pub mod page;
pub mod theme;
pub mod component;

View file

@ -1,7 +1,3 @@
use crate::prelude::*;
pub type FnActionComponent<C> = fn(component: &mut C, cx: &mut Context);
mod before_prepare_component;
pub use before_prepare_component::*;

View file

@ -1,15 +1,15 @@
use crate::prelude::*;
use super::FnActionComponent;
use crate::base::action::FnActionWithComponent;
pub struct AfterPrepareComponent<C: ComponentTrait> {
f: FnActionComponent<C>,
pub struct AfterPrepare<C: ComponentTrait> {
f: FnActionWithComponent<C>,
referer_type_id: Option<TypeId>,
referer_id: OptionId,
weight: Weight,
}
impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
impl<C: ComponentTrait> ActionTrait for AfterPrepare<C> {
fn referer_type_id(&self) -> Option<TypeId> {
self.referer_type_id
}
@ -23,9 +23,9 @@ impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
}
}
impl<C: ComponentTrait> AfterPrepareComponent<C> {
pub fn new(f: FnActionComponent<C>) -> Self {
AfterPrepareComponent {
impl<C: ComponentTrait> AfterPrepare<C> {
pub fn new(f: FnActionWithComponent<C>) -> Self {
AfterPrepare {
f,
referer_type_id: Some(TypeId::of::<C>()),
referer_id: OptionId::default(),
@ -44,10 +44,25 @@ impl<C: ComponentTrait> AfterPrepareComponent<C> {
}
#[inline(always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option<String>) {
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions(
(TypeId::of::<Self>(), Some(TypeId::of::<C>()), referer_id),
ActionKey::new(TypeId::of::<Self>(), None, Some(TypeId::of::<C>()), None),
|action: &Self| (action.f)(component, cx),
);
}
#[inline(always)]
pub(crate) fn dispatch_by_id(component: &mut C, cx: &mut Context) {
if component.id().is_some() {
dispatch_actions(
ActionKey::new(
TypeId::of::<Self>(),
None,
Some(TypeId::of::<C>()),
component.id(),
),
|action: &Self| (action.f)(component, cx),
);
}
}
}

View file

@ -1,15 +1,15 @@
use crate::prelude::*;
use super::FnActionComponent;
use crate::base::action::FnActionWithComponent;
pub struct BeforePrepareComponent<C: ComponentTrait> {
f: FnActionComponent<C>,
pub struct BeforePrepare<C: ComponentTrait> {
f: FnActionWithComponent<C>,
referer_type_id: Option<TypeId>,
referer_id: OptionId,
weight: Weight,
}
impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
impl<C: ComponentTrait> ActionTrait for BeforePrepare<C> {
fn referer_type_id(&self) -> Option<TypeId> {
self.referer_type_id
}
@ -23,9 +23,9 @@ impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
}
}
impl<C: ComponentTrait> BeforePrepareComponent<C> {
pub fn new(f: FnActionComponent<C>) -> Self {
BeforePrepareComponent {
impl<C: ComponentTrait> BeforePrepare<C> {
pub fn new(f: FnActionWithComponent<C>) -> Self {
BeforePrepare {
f,
referer_type_id: Some(TypeId::of::<C>()),
referer_id: OptionId::default(),
@ -44,10 +44,25 @@ impl<C: ComponentTrait> BeforePrepareComponent<C> {
}
#[inline(always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option<String>) {
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions(
(TypeId::of::<Self>(), Some(TypeId::of::<C>()), referer_id),
ActionKey::new(TypeId::of::<Self>(), None, Some(TypeId::of::<C>()), None),
|action: &Self| (action.f)(component, cx),
);
}
#[inline(always)]
pub(crate) fn dispatch_by_id(component: &mut C, cx: &mut Context) {
if component.id().is_some() {
dispatch_actions(
ActionKey::new(
TypeId::of::<Self>(),
None,
Some(TypeId::of::<C>()),
component.id(),
),
|action: &Self| (action.f)(component, cx),
);
}
}
}

View file

@ -1,7 +1,3 @@
use crate::prelude::*;
pub type FnActionPage = fn(page: &mut Page);
mod before_prepare_body;
pub use before_prepare_body::*;

View file

@ -1,9 +1,9 @@
use crate::prelude::*;
use super::FnActionPage;
pub type FnAfterPrepareBody = fn(page: &mut Page);
pub struct AfterPrepareBody {
f: FnActionPage,
f: FnAfterPrepareBody,
weight: Weight,
}
@ -14,7 +14,7 @@ impl ActionTrait for AfterPrepareBody {
}
impl AfterPrepareBody {
pub fn new(f: FnActionPage) -> Self {
pub fn new(f: FnAfterPrepareBody) -> Self {
AfterPrepareBody { f, weight: 0 }
}
@ -25,8 +25,9 @@ impl AfterPrepareBody {
#[inline(always)]
pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions((TypeId::of::<Self>(), None, None), |action: &Self| {
(action.f)(page)
});
dispatch_actions(
ActionKey::new(TypeId::of::<Self>(), None, None, None),
|action: &Self| (action.f)(page),
);
}
}

View file

@ -1,9 +1,9 @@
use crate::prelude::*;
use super::FnActionPage;
pub type FnBeforePrepareBody = fn(page: &mut Page);
pub struct BeforePrepareBody {
f: FnActionPage,
f: FnBeforePrepareBody,
weight: Weight,
}
@ -14,7 +14,7 @@ impl ActionTrait for BeforePrepareBody {
}
impl BeforePrepareBody {
pub fn new(f: FnActionPage) -> Self {
pub fn new(f: FnBeforePrepareBody) -> Self {
BeforePrepareBody { f, weight: 0 }
}
@ -25,8 +25,9 @@ impl BeforePrepareBody {
#[inline(always)]
pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions((TypeId::of::<Self>(), None, None), |action: &Self| {
(action.f)(page)
});
dispatch_actions(
ActionKey::new(TypeId::of::<Self>(), None, None, None),
|action: &Self| (action.f)(page),
);
}
}

8
src/base/action/theme.rs Normal file
View file

@ -0,0 +1,8 @@
mod before_prepare_component;
pub use before_prepare_component::*;
mod after_prepare_component;
pub use after_prepare_component::*;
mod render_component;
pub use render_component::*;

View file

@ -0,0 +1,42 @@
use crate::prelude::*;
use crate::base::action::FnActionWithComponent;
pub struct AfterPrepare<C: ComponentTrait> {
f: FnActionWithComponent<C>,
theme_type_id: Option<TypeId>,
referer_type_id: Option<TypeId>,
}
impl<C: ComponentTrait> ActionTrait for AfterPrepare<C> {
fn theme_type_id(&self) -> Option<TypeId> {
self.theme_type_id
}
fn referer_type_id(&self) -> Option<TypeId> {
self.referer_type_id
}
}
impl<C: ComponentTrait> AfterPrepare<C> {
pub fn new(theme: ThemeRef, f: FnActionWithComponent<C>) -> Self {
AfterPrepare {
f,
theme_type_id: Some(theme.type_id()),
referer_type_id: Some(TypeId::of::<C>()),
}
}
#[inline(always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions(
ActionKey::new(
TypeId::of::<Self>(),
Some(cx.theme().type_id()),
Some(TypeId::of::<C>()),
None,
),
|action: &Self| (action.f)(component, cx),
);
}
}

View file

@ -0,0 +1,42 @@
use crate::prelude::*;
use crate::base::action::FnActionWithComponent;
pub struct BeforePrepare<C: ComponentTrait> {
f: FnActionWithComponent<C>,
theme_type_id: Option<TypeId>,
referer_type_id: Option<TypeId>,
}
impl<C: ComponentTrait> ActionTrait for BeforePrepare<C> {
fn theme_type_id(&self) -> Option<TypeId> {
self.theme_type_id
}
fn referer_type_id(&self) -> Option<TypeId> {
self.referer_type_id
}
}
impl<C: ComponentTrait> BeforePrepare<C> {
pub fn new(theme: ThemeRef, f: FnActionWithComponent<C>) -> Self {
BeforePrepare {
f,
theme_type_id: Some(theme.type_id()),
referer_type_id: Some(TypeId::of::<C>()),
}
}
#[inline(always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions(
ActionKey::new(
TypeId::of::<Self>(),
Some(cx.theme().type_id()),
Some(TypeId::of::<C>()),
None,
),
|action: &Self| (action.f)(component, cx),
);
}
}

View file

@ -0,0 +1,48 @@
use crate::prelude::*;
pub type FnRenderComponent<C> = fn(component: &C, cx: &mut Context) -> Option<Markup>;
pub struct RenderComponent<C: ComponentTrait> {
f: FnRenderComponent<C>,
theme_type_id: Option<TypeId>,
referer_type_id: Option<TypeId>,
}
impl<C: ComponentTrait> ActionTrait for RenderComponent<C> {
fn theme_type_id(&self) -> Option<TypeId> {
self.theme_type_id
}
fn referer_type_id(&self) -> Option<TypeId> {
self.referer_type_id
}
}
impl<C: ComponentTrait> RenderComponent<C> {
pub fn new(theme: ThemeRef, f: FnRenderComponent<C>) -> Self {
RenderComponent {
f,
theme_type_id: Some(theme.type_id()),
referer_type_id: Some(TypeId::of::<C>()),
}
}
#[inline(always)]
pub(crate) fn dispatch(component: &C, cx: &mut Context) -> Option<Markup> {
let mut render_component: Option<Markup> = None;
dispatch_actions(
ActionKey::new(
TypeId::of::<Self>(),
Some(cx.theme().type_id()),
Some(TypeId::of::<C>()),
None,
),
|action: &Self| {
if render_component.is_none() {
render_component = (action.f)(component, cx);
}
},
);
render_component
}
}

View file

@ -1,10 +1,19 @@
mod definition;
pub use definition::ActionTrait;
pub use definition::{ActionBase, ActionBox, ActionKey, ActionTrait};
mod list;
pub use list::Action;
use list::ActionsList;
mod all;
pub(crate) use all::add_action;
pub use all::{dispatch_actions, KeyAction};
pub use all::dispatch_actions;
#[macro_export]
macro_rules! actions {
() => {
Vec::<ActionBox>::new()
};
( $($action:expr),+ $(,)? ) => {{
vec![$(Box::new($action),)+]
}};
}

View file

@ -1,35 +1,31 @@
use crate::core::action::{Action, ActionTrait, ActionsList};
use crate::{LazyStatic, TypeId};
use crate::core::action::{ActionBox, ActionKey, ActionTrait, ActionsList};
use crate::LazyStatic;
use std::collections::HashMap;
use std::sync::RwLock;
pub type KeyAction = (TypeId, Option<TypeId>, Option<String>);
// Registered actions.
static ACTIONS: LazyStatic<RwLock<HashMap<KeyAction, ActionsList>>> =
static ACTIONS: LazyStatic<RwLock<HashMap<ActionKey, ActionsList>>> =
LazyStatic::new(|| RwLock::new(HashMap::new()));
pub fn add_action(action: Action) {
pub fn add_action(action: ActionBox) {
let key = action.key();
let mut actions = ACTIONS.write().unwrap();
let key_action = (
action.type_id(),
action.referer_type_id(),
action.referer_id(),
);
if let Some(list) = actions.get_mut(&key_action) {
if let Some(list) = actions.get_mut(&key) {
list.add(action);
} else {
actions.insert(key_action, ActionsList::new(action));
let mut list = ActionsList::new();
list.add(action);
actions.insert(key, list);
}
}
pub fn dispatch_actions<A, B, F>(key_action: KeyAction, f: F)
pub fn dispatch_actions<A, B, F>(key: ActionKey, f: F)
where
A: ActionTrait,
F: FnMut(&A) -> B,
{
if let Some(list) = ACTIONS.read().unwrap().get(&key_action) {
if let Some(list) = ACTIONS.read().unwrap().get(&key) {
list.iter_map(f)
}
}

View file

@ -1,7 +1,41 @@
use crate::core::AnyBase;
use crate::{TypeId, Weight};
pub trait ActionTrait: AnyBase + Send + Sync {
pub type ActionBox = Box<dyn ActionTrait>;
#[derive(Eq, PartialEq, Hash)]
pub struct ActionKey {
action_type_id: TypeId,
theme_type_id: Option<TypeId>,
referer_type_id: Option<TypeId>,
referer_id: Option<String>,
}
impl ActionKey {
pub fn new(
action_type_id: TypeId,
theme_type_id: Option<TypeId>,
referer_type_id: Option<TypeId>,
referer_id: Option<String>,
) -> Self {
ActionKey {
action_type_id,
theme_type_id,
referer_type_id,
referer_id,
}
}
}
pub trait ActionBase {
fn key(&self) -> ActionKey;
}
pub trait ActionTrait: ActionBase + AnyBase + Send + Sync {
fn theme_type_id(&self) -> Option<TypeId> {
None
}
fn referer_type_id(&self) -> Option<TypeId> {
None
}
@ -14,3 +48,14 @@ pub trait ActionTrait: AnyBase + Send + Sync {
0
}
}
impl<A: ActionTrait> ActionBase for A {
fn key(&self) -> ActionKey {
ActionKey {
action_type_id: self.type_id(),
theme_type_id: self.theme_type_id(),
referer_type_id: self.referer_type_id(),
referer_id: self.referer_id(),
}
}
}

View file

@ -1,23 +1,19 @@
use crate::core::action::ActionTrait;
use crate::core::action::{ActionBox, ActionTrait};
use crate::core::AnyTo;
use crate::trace;
use crate::AutoDefault;
use std::sync::{Arc, RwLock};
pub type Action = Box<dyn ActionTrait>;
use std::sync::RwLock;
#[derive(AutoDefault)]
pub struct ActionsList(Arc<RwLock<Vec<Action>>>);
pub struct ActionsList(RwLock<Vec<ActionBox>>);
impl ActionsList {
pub fn new(action: Action) -> Self {
let mut list = ActionsList::default();
list.add(action);
list
pub fn new() -> Self {
ActionsList::default()
}
pub fn add(&mut self, action: Action) {
pub fn add(&mut self, action: ActionBox) {
let mut list = self.0.write().unwrap();
list.push(action);
list.sort_by_key(|a| a.weight());
@ -35,26 +31,12 @@ impl ActionsList {
.unwrap()
.iter()
.map(|a| {
if let Some(action) = (&**a).downcast_ref::<A>() {
if let Some(action) = (**a).downcast_ref::<A>() {
f(action);
} else {
trace::error!("Failed to downcast action of type {}", (&**a).type_name());
trace::error!("Failed to downcast action of type {}", (**a).type_name());
}
})
.collect();
}
}
#[macro_export]
macro_rules! actions {
() => {
Vec::<Action>::new()
};
( $($action:expr),+ $(,)? ) => {{
let mut v = Vec::<Action>::new();
$(
v.push(Box::new($action));
)*
v
}};
}

View file

@ -50,16 +50,14 @@ impl<C: ComponentTrait> ComponentBase for C {
self.setup_before_prepare(cx);
// Acciones del tema antes de preparar el componente.
cx.theme().before_prepare_component(self, cx);
action::theme::BeforePrepare::dispatch(self, cx);
// Acciones de los módulos antes de preparar el componente.
action::component::BeforePrepareComponent::dispatch(self, cx, None);
if let Some(id) = self.id() {
action::component::BeforePrepareComponent::dispatch(self, cx, Some(id));
}
action::component::BeforePrepare::dispatch(self, cx);
action::component::BeforePrepare::dispatch_by_id(self, cx);
// Renderiza el componente.
let markup = match cx.theme().render_component(self, cx) {
let markup = match action::theme::RenderComponent::dispatch(self, cx) {
Some(html) => html,
None => match self.prepare_component(cx) {
PrepareMarkup::None => html! {},
@ -69,13 +67,11 @@ impl<C: ComponentTrait> ComponentBase for C {
};
// Acciones del tema después de preparar el componente.
cx.theme().after_prepare_component(self, cx);
action::theme::AfterPrepare::dispatch(self, cx);
// Acciones de los módulos después de preparar el componente.
action::component::AfterPrepareComponent::dispatch(self, cx, None);
if let Some(id) = self.id() {
action::component::AfterPrepareComponent::dispatch(self, cx, Some(id));
}
action::component::AfterPrepare::dispatch(self, cx);
action::component::AfterPrepare::dispatch_by_id(self, cx);
markup
} else {

View file

@ -1,4 +1,4 @@
use crate::core::action::Action;
use crate::core::action::ActionBox;
use crate::core::theme::ThemeRef;
use crate::core::AnyBase;
use crate::locale::L10n;
@ -31,7 +31,7 @@ pub trait PackageTrait: AnyBase + Send + Sync {
vec![]
}
fn actions(&self) -> Vec<Action> {
fn actions(&self) -> Vec<ActionBox> {
actions![]
}

View file

@ -1,5 +1,5 @@
use crate::base::component::Layout;
use crate::core::component::{ComponentBase, ComponentTrait, Context};
use crate::core::component::{ComponentBase, ComponentTrait};
use crate::core::package::PackageTrait;
use crate::html::{html, Favicon, Markup};
use crate::locale::L10n;
@ -26,13 +26,13 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
fn before_prepare_body(&self, page: &mut Page) {}
fn prepare_body(&self, page: &mut Page) -> Markup {
let skip_to = concat_string!("#", page.skip_to().get().unwrap_or("content".to_owned()));
let skip_to_id = concat_string!("#", page.skip_to().get().unwrap_or("content".to_owned()));
html! {
body id=[page.body_id().get()] class=[page.body_classes().get()] {
@if let Some(skip) = L10n::l("skip_to_content").using(page.context().langid()) {
div class="skip__to_content" {
a href=(skip_to) { (skip) }
a href=(skip_to_id) { (skip) }
}
}
(Layout::new().render(page.context()))
@ -80,66 +80,4 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
}
}
}
#[rustfmt::skip]
#[allow(unused_variables)]
fn before_prepare_component(
&self,
component: &mut dyn ComponentTrait,
cx: &mut Context,
) {
/*
Cómo usarlo:
match component.type_id() {
t if t == TypeId::of::<Block>() => {
if let Some(b) = component.downcast_mut::<Block>() {
b.alter_title("New title");
}
},
_ => {},
}
*/
}
#[rustfmt::skip]
#[allow(unused_variables)]
fn after_prepare_component(
&self,
component: &mut dyn ComponentTrait,
cx: &mut Context,
) {
/*
Cómo usarlo:
match component.type_id() {
t if t == TypeId::of::<Block>() => {
if let Some(b) = component.downcast_mut::<Block>() {
b.alter_title("New title");
}
},
_ => {},
}
*/
}
#[rustfmt::skip]
#[allow(unused_variables)]
fn render_component(
&self,
component: &dyn ComponentTrait,
cx: &mut Context,
) -> Option<Markup> {
None
/*
Cómo usarlo:
match component.type_id() {
t if t == TypeId::of::<Block>() => {
Some(block_default(block))
},
_ => None,
}
*/
}
}

View file

@ -168,7 +168,7 @@ impl Page {
// Page RENDER.
pub fn render(&mut self) -> ResultPage<Markup, ErrorPage> {
// Theme actions before preparing the page body.
// Theme operations before preparing the page body.
self.context.theme().before_prepare_body(self);
// Packages actions before preparing the page body.
@ -177,7 +177,7 @@ impl Page {
// Prepare page body.
let body = self.context.theme().prepare_body(self);
// Theme actions after preparing the page body.
// Theme operations after preparing the page body.
self.context.theme().after_prepare_body(self);
// Packages actions after preparing the page body.