🐛 Corrige orden ejecución de acciones de página

This commit is contained in:
Manuel Cillero 2023-06-26 20:42:38 +02:00
parent 8eba48283e
commit 7138b9c58b
4 changed files with 24 additions and 35 deletions

View file

@ -72,7 +72,7 @@ pub trait ThemeTrait: ModuleTrait + Send + Sync {
} }
} }
fn after_prepare_page(&self, page: &mut Page) { fn before_render_page(&self, page: &mut Page) {
if page.favicon().is_none() { if page.favicon().is_none() {
page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico"))); page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico")));
} }

View file

@ -3,8 +3,8 @@ pub use actix_web::Result as ResultPage;
mod before_prepare_page; mod before_prepare_page;
pub use before_prepare_page::{ActionBeforePreparePage, ACTION_BEFORE_PREPARE_PAGE}; pub use before_prepare_page::{ActionBeforePreparePage, ACTION_BEFORE_PREPARE_PAGE};
mod after_prepare_page; mod before_render_page;
pub use after_prepare_page::{ActionAfterPreparePage, ACTION_AFTER_PREPARE_PAGE}; pub use before_render_page::{ActionBeforeRenderPage, ACTION_BEFORE_RENDER_PAGE};
mod definition; mod definition;
pub use definition::Page; pub use definition::Page;

View file

@ -2,25 +2,25 @@ use crate::core::action::{ActionTrait, AnyAction};
use crate::response::page::Page; use crate::response::page::Page;
use crate::{use_handle, Handle}; use crate::{use_handle, Handle};
use_handle!(ACTION_AFTER_PREPARE_PAGE); use_handle!(ACTION_BEFORE_RENDER_PAGE);
type Action = fn(&mut Page); type Action = fn(&mut Page);
pub struct ActionAfterPreparePage { pub struct ActionBeforeRenderPage {
action: Option<Action>, action: Option<Action>,
weight: isize, weight: isize,
} }
impl ActionTrait for ActionAfterPreparePage { impl ActionTrait for ActionBeforeRenderPage {
fn new() -> Self { fn new() -> Self {
ActionAfterPreparePage { ActionBeforeRenderPage {
action: None, action: None,
weight: 0, weight: 0,
} }
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
ACTION_AFTER_PREPARE_PAGE ACTION_BEFORE_RENDER_PAGE
} }
fn weight(&self) -> isize { fn weight(&self) -> isize {
@ -32,7 +32,7 @@ impl ActionTrait for ActionAfterPreparePage {
} }
} }
impl ActionAfterPreparePage { impl ActionBeforeRenderPage {
pub fn with_action(mut self, action: Action) -> Self { pub fn with_action(mut self, action: Action) -> Self {
self.action = Some(action); self.action = Some(action);
self self

View file

@ -3,11 +3,10 @@ use crate::core::action::{action_ref, run_actions};
use crate::core::component::{ComponentTrait, ContextOp, OneComponent, RenderContext}; use crate::core::component::{ComponentTrait, ContextOp, OneComponent, RenderContext};
use crate::core::theme::ComponentsRegions; use crate::core::theme::ComponentsRegions;
use crate::html::{html, Classes, ClassesOp, Favicon, Markup, DOCTYPE}; use crate::html::{html, Classes, ClassesOp, Favicon, Markup, DOCTYPE};
use crate::locale::{langid_for, LanguageIdentifier};
use crate::response::fatal_error::FatalError; use crate::response::fatal_error::FatalError;
use crate::response::page::ResultPage; use crate::response::page::ResultPage;
use crate::response::page::{ActionAfterPreparePage, ACTION_AFTER_PREPARE_PAGE};
use crate::response::page::{ActionBeforePreparePage, ACTION_BEFORE_PREPARE_PAGE}; use crate::response::page::{ActionBeforePreparePage, ACTION_BEFORE_PREPARE_PAGE};
use crate::response::page::{ActionBeforeRenderPage, ACTION_BEFORE_RENDER_PAGE};
use crate::{fn_builder, service}; use crate::{fn_builder, service};
use unic_langid::CharacterDirection; use unic_langid::CharacterDirection;
@ -54,12 +53,6 @@ impl Page {
// Page BUILDER. // Page BUILDER.
#[fn_builder]
pub fn alter_language(&mut self, language: &'static str) -> &mut Self {
self.context.alter(ContextOp::LangId(langid_for(language)));
self
}
#[fn_builder] #[fn_builder]
pub fn alter_title(&mut self, title: L10n) -> &mut Self { pub fn alter_title(&mut self, title: L10n) -> &mut Self {
self.title.set(title); self.title.set(title);
@ -116,10 +109,6 @@ impl Page {
// Page GETTERS. // Page GETTERS.
pub fn langid(&self) -> &LanguageIdentifier {
self.context.langid()
}
pub fn title(&mut self) -> String { pub fn title(&mut self) -> String {
self.title.prepare(&mut self.context).into_string() self.title.prepare(&mut self.context).into_string()
} }
@ -155,31 +144,31 @@ impl Page {
// Page RENDER. // Page RENDER.
pub fn render(&mut self) -> ResultPage<Markup, FatalError> { pub fn render(&mut self) -> ResultPage<Markup, FatalError> {
// Acciones de los módulos antes de preparar la página. // Module actions before preparing the page.
run_actions(ACTION_BEFORE_PREPARE_PAGE, |action| { run_actions(ACTION_BEFORE_PREPARE_PAGE, |action| {
action_ref::<ActionBeforePreparePage>(&**action).run(self) action_ref::<ActionBeforePreparePage>(&**action).run(self)
}); });
// Acciones del tema antes de preparar la página. // Theme actions before preparing the page.
self.context.theme().before_prepare_page(self); self.context.theme().before_prepare_page(self);
// Primero, preparar el cuerpo. // Prepare page body.
let body = self.context.theme().prepare_page_body(self); let body = self.context.theme().prepare_page_body(self);
// Luego, preparar la cabecera. // Module actions before rendering the page.
let head = self.context.theme().prepare_page_head(self); run_actions(ACTION_BEFORE_RENDER_PAGE, |action| {
action_ref::<ActionBeforeRenderPage>(&**action).run(self)
// Acciones de los módulos después de preparar la página.
run_actions(ACTION_AFTER_PREPARE_PAGE, |action| {
action_ref::<ActionAfterPreparePage>(&**action).run(self)
}); });
// Acciones del tema después de preparar la página. // Theme actions before rendering the page.
self.context.theme().after_prepare_page(self); self.context.theme().before_render_page(self);
// Finalmente, renderiza la página. // Prepare page head.
let lang = self.langid().language.as_str(); let head = self.context.theme().prepare_page_head(self);
let dir = match self.langid().character_direction() {
// Render the page.
let lang = self.context.langid().language.as_str();
let dir = match self.context.langid().character_direction() {
CharacterDirection::LTR => "ltr", CharacterDirection::LTR => "ltr",
CharacterDirection::RTL => "rtl", CharacterDirection::RTL => "rtl",
}; };