Añade elección de tema específico por página

Característica útil para facilitar que el módulo Admin pueda renderizar
sus páginas siempre con el mismo tema, indepedientemente del tema por
defecto. También podrá ser decisivo para permitir a los usuarios usar un
tema diferente.
This commit is contained in:
Manuel Cillero 2022-02-26 21:15:00 +01:00
parent edf5ddf81b
commit 3764f707da
11 changed files with 127 additions and 86 deletions

View file

@ -1,4 +1,5 @@
use crate::core::theme::{Markup, PreEscaped, html};
use crate::core::all::DEFAULT_THEME;
use crate::core::theme::{Markup, PreEscaped, Theme, html};
// -----------------------------------------------------------------------------
// Favicon.
@ -172,6 +173,7 @@ impl JavaScript {
// -----------------------------------------------------------------------------
pub struct Assets {
theme : &'static dyn Theme,
favicon : Option<Favicon>,
metadata : Vec<(String, String)>,
stylesheets: Vec<StyleSheet>,
@ -183,6 +185,7 @@ pub struct Assets {
impl Assets {
pub fn new() -> Self {
Assets {
theme : *DEFAULT_THEME,
favicon : None,
metadata : Vec::new(),
stylesheets: Vec::new(),
@ -192,6 +195,11 @@ impl Assets {
}
}
pub fn using_theme(&mut self, theme: &'static dyn Theme) -> &mut Self {
self.theme = theme;
self
}
pub fn with_favicon(&mut self, favicon: Favicon) -> &mut Self {
self.favicon = Some(favicon);
self
@ -238,6 +246,14 @@ impl Assets {
self
}
/// Assets GETTERS.
pub fn theme(&mut self) -> &'static dyn Theme {
self.theme
}
/// Assets RENDER.
pub fn render(&mut self) -> Markup {
let ordered_css = &mut self.stylesheets;
ordered_css.sort_by_key(|o| o.weight);

View file

@ -1,3 +1,5 @@
use crate::core::all::COMPONENTS;
pub mod assets;
pub use assets::Assets as PageAssets;
@ -10,3 +12,12 @@ pub use container::Container as PageContainer;
mod page;
pub use page::Page;
pub use page::render_component;
pub fn add_component_to(region: &'static str, component: impl PageComponent) {
let mut hmap = COMPONENTS.write().unwrap();
if let Some(regions) = hmap.get_mut(region) {
regions.add(component);
} else {
hmap.insert(region, PageContainer::new_with(component));
}
}

View file

@ -1,7 +1,7 @@
use crate::config::SETTINGS;
use crate::core::server;
use crate::core::state::{COMPONENTS, THEME};
use crate::core::theme::{DOCTYPE, Markup, html};
use crate::core::all::COMPONENTS;
use crate::core::theme::{DOCTYPE, Markup, Theme, html};
use crate::core::response::page::{PageAssets, PageComponent, PageContainer};
use std::borrow::Cow;
@ -132,13 +132,13 @@ impl<'a> Page<'a> {
pub fn render(&mut self) -> server::Result<Markup> {
// Acciones del tema antes de renderizar la página.
THEME.before_render_page(self);
self.assets.theme().before_render_page(self);
// Primero, renderizar el cuerpo.
let body = THEME.render_page_body(self);
let body = self.assets.theme().render_page_body(self);
// Luego, renderizar la cabecera.
let head = THEME.render_page_head(self);
let head = self.assets.theme().render_page_head(self);
// Finalmente, renderizar la página.
return Ok(html! {
@ -156,6 +156,13 @@ impl<'a> Page<'a> {
None => html! {}
}
}
// Page EXTRAS.
pub fn using_theme(&mut self, theme: &'static dyn Theme) -> &mut Self {
self.assets.using_theme(theme);
self
}
}
pub fn render_component(
@ -163,7 +170,7 @@ pub fn render_component(
assets: &mut PageAssets
) -> Markup {
match component.is_renderable() {
true => match THEME.render_component(component, assets) {
true => match assets.theme().render_component(component, assets) {
Some(markup) => markup,
None => component.default_render(assets)
},