Retoca la llamada al renderizado de componentes
This commit is contained in:
parent
db053531d1
commit
3eb9cfbff8
4 changed files with 29 additions and 20 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -2,4 +2,8 @@
|
||||||
**/log/*.log*
|
**/log/*.log*
|
||||||
**/update.sh
|
**/update.sh
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
|
||||||
|
website/website
|
||||||
|
|
||||||
|
.sitecopy
|
||||||
workdir
|
workdir
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,7 @@ mod context;
|
||||||
pub use context::{InContext, InContextOp};
|
pub use context::{InContext, InContextOp};
|
||||||
|
|
||||||
mod definition;
|
mod definition;
|
||||||
use definition::render_component;
|
pub use definition::{component_mut, component_ref, AnyComponent, BaseComponent, ComponentTrait};
|
||||||
pub use definition::{component_mut, component_ref, AnyComponent, ComponentTrait};
|
|
||||||
|
|
||||||
mod bundle;
|
mod bundle;
|
||||||
pub use bundle::ComponentsBundle;
|
pub use bundle::ComponentsBundle;
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ impl ComponentsBundle {
|
||||||
components.sort_by_key(|c| c.read().unwrap().weight());
|
components.sort_by_key(|c| c.read().unwrap().weight());
|
||||||
html! {
|
html! {
|
||||||
@for c in components.iter() {
|
@for c in components.iter() {
|
||||||
(" ")(super::render_component(&mut *c.write().unwrap(), context))(" ")
|
(" ")(c.write().unwrap().render(context))(" ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,11 @@ use crate::util;
|
||||||
|
|
||||||
pub use std::any::Any as AnyComponent;
|
pub use std::any::Any as AnyComponent;
|
||||||
|
|
||||||
pub trait ComponentTrait: AnyComponent + Send + Sync {
|
pub trait BaseComponent {
|
||||||
|
fn render(&mut self, context: &mut InContext) -> Markup;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync {
|
||||||
fn new() -> Self
|
fn new() -> Self
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
|
@ -41,6 +45,24 @@ pub trait ComponentTrait: AnyComponent + Send + Sync {
|
||||||
fn as_mut_any(&mut self) -> &mut dyn AnyComponent;
|
fn as_mut_any(&mut self) -> &mut dyn AnyComponent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<C: ComponentTrait> BaseComponent for C {
|
||||||
|
fn render(&mut self, context: &mut InContext) -> Markup {
|
||||||
|
// Acciones del componente antes de renderizar.
|
||||||
|
self.before_render(context);
|
||||||
|
|
||||||
|
// Acciones del tema antes de renderizar el componente.
|
||||||
|
context.theme().before_render_component(self, context);
|
||||||
|
|
||||||
|
match self.is_renderable(context) {
|
||||||
|
true => match context.theme().render_component(self, context) {
|
||||||
|
Some(html) => html,
|
||||||
|
None => self.default_render(context),
|
||||||
|
},
|
||||||
|
false => html! {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn component_ref<C: 'static>(component: &dyn ComponentTrait) -> &C {
|
pub fn component_ref<C: 'static>(component: &dyn ComponentTrait) -> &C {
|
||||||
component.as_ref_any().downcast_ref::<C>().unwrap()
|
component.as_ref_any().downcast_ref::<C>().unwrap()
|
||||||
}
|
}
|
||||||
|
|
@ -49,22 +71,6 @@ pub fn component_mut<C: 'static>(component: &mut dyn ComponentTrait) -> &mut C {
|
||||||
component.as_mut_any().downcast_mut::<C>().unwrap()
|
component.as_mut_any().downcast_mut::<C>().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_component(component: &mut dyn ComponentTrait, context: &mut InContext) -> Markup {
|
|
||||||
// Acciones del componente antes de renderizar.
|
|
||||||
component.before_render(context);
|
|
||||||
|
|
||||||
// Acciones del tema antes de renderizar el componente.
|
|
||||||
context.theme().before_render_component(component, context);
|
|
||||||
|
|
||||||
match component.is_renderable(context) {
|
|
||||||
true => match context.theme().render_component(component, context) {
|
|
||||||
Some(html) => html,
|
|
||||||
None => component.default_render(context),
|
|
||||||
},
|
|
||||||
false => html! {},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! hook_before_render_component {
|
macro_rules! hook_before_render_component {
|
||||||
( $ACTION_HANDLER:ident = $handler:literal, $Component:ty ) => {
|
( $ACTION_HANDLER:ident = $handler:literal, $Component:ty ) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue