💥 Cambios antes/después de preparar cuerpo pág.

This commit is contained in:
Manuel Cillero 2023-07-11 19:17:05 +02:00
parent 44550edafa
commit 0880964fd1
12 changed files with 63 additions and 63 deletions

View file

@ -30,7 +30,7 @@ impl ModuleTrait for Admin {
} }
fn actions(&self) -> Vec<Action> { fn actions(&self) -> Vec<Action> {
vec![action!(ActionBeforePreparePage => before_prepare_page)] vec![action!(ActionBeforePrepareBody => before_prepare_body)]
} }
fn configure_service(&self, cfg: &mut service::web::ServiceConfig) { fn configure_service(&self, cfg: &mut service::web::ServiceConfig) {
@ -40,6 +40,6 @@ impl ModuleTrait for Admin {
} }
} }
fn before_prepare_page(page: &mut Page) { fn before_prepare_body(page: &mut Page) {
page.alter_body_classes(ClassesOp::Add, "test-admin"); page.alter_body_classes(ClassesOp::Add, "test-admin");
} }

View file

@ -21,7 +21,7 @@ impl ModuleTrait for Aliner {
} }
impl ThemeTrait for Aliner { impl ThemeTrait for Aliner {
fn before_prepare_page(&self, page: &mut Page) { fn before_prepare_body(&self, page: &mut Page) {
page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico"))) page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico")))
.alter_context(ContextOp::AddStyleSheet( .alter_context(ContextOp::AddStyleSheet(
StyleSheet::at("/aliner/css/styles.css").with_weight(-99), StyleSheet::at("/aliner/css/styles.css").with_weight(-99),

View file

@ -43,7 +43,7 @@ impl ThemeTrait for Bootsier {
] ]
} }
fn before_prepare_page(&self, page: &mut Page) { fn before_prepare_body(&self, page: &mut Page) {
page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico"))) page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico")))
.alter_context(ContextOp::AddStyleSheet( .alter_context(ContextOp::AddStyleSheet(
StyleSheet::at("/bootsier/css/bootstrap.min.css") StyleSheet::at("/bootsier/css/bootstrap.min.css")
@ -58,7 +58,7 @@ impl ThemeTrait for Bootsier {
JQuery.enable_jquery(page.context()); JQuery.enable_jquery(page.context());
} }
fn prepare_page_body(&self, page: &mut Page) -> Markup { fn prepare_body(&self, page: &mut Page) -> Markup {
match page.template() { match page.template() {
"admin" => html! { "admin" => html! {
body class=[page.body_classes().get()] { body class=[page.body_classes().get()] {

View file

@ -31,7 +31,7 @@ impl ModuleTrait for Bulmix {
} }
impl ThemeTrait for Bulmix { impl ThemeTrait for Bulmix {
fn before_prepare_page(&self, page: &mut Page) { fn before_prepare_body(&self, page: &mut Page) {
page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico"))) page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico")))
.alter_context(ContextOp::AddStyleSheet( .alter_context(ContextOp::AddStyleSheet(
StyleSheet::at("/bulmix/css/bulma.min.css") StyleSheet::at("/bulmix/css/bulma.min.css")

View file

@ -24,7 +24,7 @@ impl ModuleTrait for JQuery {
} }
fn actions(&self) -> Vec<Action> { fn actions(&self) -> Vec<Action> {
vec![action!(ActionBeforeRenderPage => before_render_page)] vec![action!(ActionAfterPrepareBody => after_prepare_body)]
} }
fn configure_service(&self, cfg: &mut service::web::ServiceConfig) { fn configure_service(&self, cfg: &mut service::web::ServiceConfig) {
@ -44,7 +44,7 @@ impl JQuery {
} }
} }
fn before_render_page(page: &mut Page) { fn after_prepare_body(page: &mut Page) {
if let Some(true) = page.context().get_param::<bool>(PARAM_JQUERY) { if let Some(true) = page.context().get_param::<bool>(PARAM_JQUERY) {
page.context().alter(ContextOp::AddJavaScript( page.context().alter(ContextOp::AddJavaScript(
JavaScript::at("/jquery/jquery.min.js") JavaScript::at("/jquery/jquery.min.js")

View file

@ -27,7 +27,7 @@ impl ModuleTrait for Node {
} }
fn actions(&self) -> Vec<Action> { fn actions(&self) -> Vec<Action> {
vec![action!(ActionBeforePreparePage => before_prepare_page, -1)] vec![action!(ActionBeforePrepareBody => before_prepare_body, -1)]
} }
fn migrations(&self) -> Vec<MigrationItem> { fn migrations(&self) -> Vec<MigrationItem> {
@ -44,6 +44,6 @@ async fn node(request: service::HttpRequest) -> ResultPage<Markup, FatalError> {
Page::new(request).with_title(L10n::n("Nodo")).render() Page::new(request).with_title(L10n::n("Nodo")).render()
} }
fn before_prepare_page(page: &mut Page) { fn before_prepare_body(page: &mut Page) {
page.alter_body_classes(ClassesOp::Add, "test-node"); page.alter_body_classes(ClassesOp::Add, "test-node");
} }

View file

@ -27,7 +27,7 @@ impl ModuleTrait for Basic {
} }
impl ThemeTrait for Basic { impl ThemeTrait for Basic {
fn before_prepare_page(&self, page: &mut Page) { fn before_prepare_body(&self, page: &mut Page) {
page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico"))) page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico")))
.alter_context(ContextOp::AddStyleSheet( .alter_context(ContextOp::AddStyleSheet(
StyleSheet::at("/theme/css/normalize.min.css").with_version("8.0.1"), StyleSheet::at("/theme/css/normalize.min.css").with_version("8.0.1"),

View file

@ -21,9 +21,27 @@ pub trait ThemeTrait: ModuleTrait + Send + Sync {
} }
#[allow(unused_variables)] #[allow(unused_variables)]
fn before_prepare_page(&self, page: &mut Page) {} fn before_prepare_body(&self, page: &mut Page) {}
fn prepare_page_head(&self, page: &mut Page) -> Markup { fn prepare_body(&self, page: &mut Page) -> Markup {
html! {
body class=[page.body_classes().get()] {
@for (region, _) in self.regions().iter() {
@if let Some(content) = page.prepare_region(region) {
#(region) { (content) }
}
}
}
}
}
fn after_prepare_body(&self, page: &mut Page) {
if page.favicon().is_none() {
page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico")));
}
}
fn prepare_head(&self, page: &mut Page) -> Markup {
let title = page.title(); let title = page.title();
let description = page.description(); let description = page.description();
let viewport = "width=device-width, initial-scale=1, shrink-to-fit=no"; let viewport = "width=device-width, initial-scale=1, shrink-to-fit=no";
@ -60,24 +78,6 @@ pub trait ThemeTrait: ModuleTrait + Send + Sync {
} }
} }
fn prepare_page_body(&self, page: &mut Page) -> Markup {
html! {
body class=[page.body_classes().get()] {
@for (region, _) in self.regions().iter() {
@if let Some(content) = page.prepare_region(region) {
#(region) { (content) }
}
}
}
}
}
fn before_render_page(&self, page: &mut Page) {
if page.favicon().is_none() {
page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico")));
}
}
#[rustfmt::skip] #[rustfmt::skip]
#[allow(unused_variables)] #[allow(unused_variables)]
fn before_prepare_component( fn before_prepare_component(

View file

@ -137,23 +137,23 @@ impl Page {
// Page RENDER. // Page RENDER.
pub fn render(&mut self) -> ResultPage<Markup, FatalError> { pub fn render(&mut self) -> ResultPage<Markup, FatalError> {
// Module actions before preparing the page. // Module actions before preparing the page body.
run_actions_before_prepare_page(self); run_actions_before_prepare_body(self);
// Theme actions before preparing the page. // Theme actions before preparing the page body.
self.context.theme().before_prepare_page(self); self.context.theme().before_prepare_body(self);
// Prepare page body. // Prepare page body.
let body = self.context.theme().prepare_page_body(self); let body = self.context.theme().prepare_body(self);
// Module actions before rendering the page. // Module actions after preparing the page body.
run_actions_before_render_page(self); run_actions_after_prepare_body(self);
// Theme actions before rendering the page. // Theme actions after preparing the page body.
self.context.theme().before_render_page(self); self.context.theme().after_prepare_body(self);
// Prepare page head. // Prepare page head.
let head = self.context.theme().prepare_page_head(self); let head = self.context.theme().prepare_head(self);
// Render the page. // Render the page.
let lang = self.context.langid().language.as_str(); let lang = self.context.langid().language.as_str();

View file

@ -2,8 +2,8 @@ use crate::response::page::Page;
pub type ActionPage = fn(page: &mut Page); pub type ActionPage = fn(page: &mut Page);
mod before_prepare_page; mod before_prepare_body;
pub use before_prepare_page::*; pub use before_prepare_body::*;
mod before_render_page; mod after_prepare_body;
pub use before_render_page::*; pub use after_prepare_body::*;

View file

@ -3,23 +3,23 @@ use crate::response::page::action::ActionPage;
use crate::response::page::Page; use crate::response::page::Page;
use crate::{use_handle, Handle}; use crate::{use_handle, Handle};
use_handle!(ACTION_BEFORE_RENDER_PAGE for Action); use_handle!(ACTION_AFTER_PREPARE_BODY for Action);
pub struct ActionBeforeRenderPage { pub struct ActionAfterPrepareBody {
action: Option<ActionPage>, action: Option<ActionPage>,
weight: isize, weight: isize,
} }
impl ActionTrait for ActionBeforeRenderPage { impl ActionTrait for ActionAfterPrepareBody {
fn new() -> Self { fn new() -> Self {
ActionBeforeRenderPage { ActionAfterPrepareBody {
action: None, action: None,
weight: 0, weight: 0,
} }
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
ACTION_BEFORE_RENDER_PAGE ACTION_AFTER_PREPARE_BODY
} }
fn weight(&self) -> isize { fn weight(&self) -> isize {
@ -31,7 +31,7 @@ impl ActionTrait for ActionBeforeRenderPage {
} }
} }
impl ActionBeforeRenderPage { impl ActionAfterPrepareBody {
pub fn with_action(mut self, action: ActionPage) -> Self { pub fn with_action(mut self, action: ActionPage) -> Self {
self.action = Some(action); self.action = Some(action);
self self
@ -50,8 +50,8 @@ impl ActionBeforeRenderPage {
} }
#[inline(always)] #[inline(always)]
pub(crate) fn run_actions_before_render_page(page: &mut Page) { pub(crate) fn run_actions_after_prepare_body(page: &mut Page) {
run_actions(ACTION_BEFORE_RENDER_PAGE, |action| { run_actions(ACTION_AFTER_PREPARE_BODY, |action| {
action_ref::<ActionBeforeRenderPage>(&**action).run(page) action_ref::<ActionAfterPrepareBody>(&**action).run(page)
}); });
} }

View file

@ -3,23 +3,23 @@ use crate::response::page::action::ActionPage;
use crate::response::page::Page; use crate::response::page::Page;
use crate::{use_handle, Handle}; use crate::{use_handle, Handle};
use_handle!(ACTION_BEFORE_PREPARE_PAGE for Action); use_handle!(ACTION_BEFORE_PREPARE_BODY for Action);
pub struct ActionBeforePreparePage { pub struct ActionBeforePrepareBody {
action: Option<ActionPage>, action: Option<ActionPage>,
weight: isize, weight: isize,
} }
impl ActionTrait for ActionBeforePreparePage { impl ActionTrait for ActionBeforePrepareBody {
fn new() -> Self { fn new() -> Self {
ActionBeforePreparePage { ActionBeforePrepareBody {
action: None, action: None,
weight: 0, weight: 0,
} }
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
ACTION_BEFORE_PREPARE_PAGE ACTION_BEFORE_PREPARE_BODY
} }
fn weight(&self) -> isize { fn weight(&self) -> isize {
@ -31,7 +31,7 @@ impl ActionTrait for ActionBeforePreparePage {
} }
} }
impl ActionBeforePreparePage { impl ActionBeforePrepareBody {
pub fn with_action(mut self, action: ActionPage) -> Self { pub fn with_action(mut self, action: ActionPage) -> Self {
self.action = Some(action); self.action = Some(action);
self self
@ -50,8 +50,8 @@ impl ActionBeforePreparePage {
} }
#[inline(always)] #[inline(always)]
pub(crate) fn run_actions_before_prepare_page(page: &mut Page) { pub(crate) fn run_actions_before_prepare_body(page: &mut Page) {
run_actions(ACTION_BEFORE_PREPARE_PAGE, |action| { run_actions(ACTION_BEFORE_PREPARE_BODY, |action| {
action_ref::<ActionBeforePreparePage>(&**action).run(page) action_ref::<ActionBeforePrepareBody>(&**action).run(page)
}); });
} }