🚚 Rename operations with assets in context

This commit is contained in:
Manuel Cillero 2024-03-11 23:29:37 +01:00
parent db40ac3321
commit 3d26459843
12 changed files with 67 additions and 56 deletions

View file

@ -151,7 +151,6 @@ pub async fn summary(request: HttpRequest) -> ResultPage<Markup, ErrorPage> {
.add_item(menu::Item::label(L10n::n("Opción 4")));
Page::new(request)
//.with_context(ContextOp::Theme("Bootsier"))
.with_title(L10n::n("Admin"))
.with_template("admin")
.with_component_in("top-menu", side_menu)

View file

@ -70,18 +70,18 @@ impl ThemeTrait for Bootsier {
fn after_prepare_body(&self, page: &mut Page) {
page.alter_favicon(Some(Favicon::new().with_icon("/base/favicon.ico")))
.alter_context(ContextOp::AddStyleSheet(
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/bootsier/css/bootstrap.min.css")
.with_version("5.1.3")
.with_weight(-99),
))
.alter_context(ContextOp::AddJavaScript(
.alter_assets(AssetsOp::AddJavaScript(
JavaScript::at("/bootsier/js/bootstrap.bundle.min.js")
.with_version("5.1.3")
.with_weight(-99),
))
.alter_context(ContextOp::AddBaseAssets)
.alter_context(ContextOp::AddStyleSheet(
.alter_assets(AssetsOp::AddBaseAssets)
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/bootsier/css/styles.css").with_version("0.0.1"),
));
}

View file

@ -27,13 +27,13 @@ impl ThemeTrait for Bulmix {
fn after_prepare_body(&self, page: &mut Page) {
page.alter_favicon(Some(Favicon::new().with_icon("/base/favicon.ico")))
.alter_context(ContextOp::AddStyleSheet(
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/bulmix/css/bulma.min.css")
.with_version("0.9.4")
.with_weight(-99),
))
.alter_context(ContextOp::AddBaseAssets)
.alter_context(ContextOp::AddStyleSheet(
.alter_assets(AssetsOp::AddBaseAssets)
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/bulmix/css/styles.css").with_version("0.0.1"),
));
}

View file

@ -1,4 +1,4 @@
use crate::core::component::{Context, ContextOp};
use crate::core::component::{AssetsOp, Context};
use crate::html::{JavaScript, StyleSheet};
use crate::{AutoDefault, Weight};
@ -11,23 +11,24 @@ pub const PARAM_BASE_INCLUDE_MENU_ASSETS: &str = "base.include.menu";
pub(crate) fn add_base_assets(cx: &mut Context) {
let weight = cx.get_param::<Weight>(PARAM_BASE_WEIGHT).unwrap_or(-90);
cx.alter(ContextOp::AddStyleSheet(
cx.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/root.css")
.with_version("0.0.1")
.with_weight(weight),
)).alter(ContextOp::AddStyleSheet(
))
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/looks.css")
.with_version("0.0.1")
.with_weight(weight),
))
.alter(ContextOp::AddStyleSheet(
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/buttons.css")
.with_version("0.0.2")
.with_weight(weight),
));
if let Some(true) = cx.get_param::<bool>(PARAM_BASE_INCLUDE_ICONS) {
cx.alter(ContextOp::AddStyleSheet(
cx.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/icons.min.css")
.with_version("1.11.1")
.with_weight(weight),
@ -35,7 +36,7 @@ pub(crate) fn add_base_assets(cx: &mut Context) {
}
if let Some(true) = cx.get_param::<bool>(PARAM_BASE_INCLUDE_FLEX_ASSETS) {
cx.alter(ContextOp::AddStyleSheet(
cx.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/flex.css")
.with_version("0.0.1")
.with_weight(weight),
@ -43,12 +44,12 @@ pub(crate) fn add_base_assets(cx: &mut Context) {
}
if let Some(true) = cx.get_param::<bool>(PARAM_BASE_INCLUDE_MENU_ASSETS) {
cx.alter(ContextOp::AddStyleSheet(
cx.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/menu.css")
.with_version("0.0.1")
.with_weight(weight),
))
.alter(ContextOp::AddJavaScript(
.alter_assets(AssetsOp::AddJavaScript(
JavaScript::at("/base/js/menu.js")
.with_version("0.0.1")
.with_weight(weight),

View file

@ -34,8 +34,8 @@ async fn home_lang(
fn home(request: HttpRequest, lang: &'static LanguageIdentifier) -> ResultPage<Markup, ErrorPage> {
Page::new(request)
.with_title(L10n::l("welcome_title"))
.with_context(ContextOp::LangId(lang))
.with_context(ContextOp::AddStyleSheet(StyleSheet::at(
.with_assets(AssetsOp::LangId(lang))
.with_assets(AssetsOp::AddStyleSheet(StyleSheet::at(
"/base/css/welcome.css",
)))
.with_body_id("welcome")

View file

@ -15,13 +15,13 @@ impl PackageTrait for Basic {
impl ThemeTrait for Basic {
fn after_prepare_body(&self, page: &mut Page) {
page.alter_favicon(Some(Favicon::new().with_icon("/base/favicon.ico")))
.alter_context(ContextOp::AddStyleSheet(
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/normalize.min.css")
.with_version("8.0.1")
.with_weight(-90),
))
.alter_context(ContextOp::AddBaseAssets)
.alter_context(ContextOp::AddStyleSheet(
.alter_assets(AssetsOp::AddBaseAssets)
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/basic.css")
.with_version("0.0.1")
.with_weight(-90),

View file

@ -15,13 +15,13 @@ impl PackageTrait for Chassis {
impl ThemeTrait for Chassis {
fn after_prepare_body(&self, page: &mut Page) {
page.alter_favicon(Some(Favicon::new().with_icon("/base/favicon.ico")))
.alter_context(ContextOp::AddStyleSheet(
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/normalize.min.css")
.with_version("8.0.1")
.with_weight(-90),
))
.alter_context(ContextOp::AddBaseAssets)
.alter_context(ContextOp::AddStyleSheet(
.alter_assets(AssetsOp::AddBaseAssets)
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/chassis.css")
.with_version("0.0.1")
.with_weight(-90),

View file

@ -15,13 +15,13 @@ impl PackageTrait for Inception {
impl ThemeTrait for Inception {
fn after_prepare_body(&self, page: &mut Page) {
page.alter_favicon(Some(Favicon::new().with_icon("/base/favicon.ico")))
.alter_context(ContextOp::AddStyleSheet(
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/normalize.min.css")
.with_version("8.0.1")
.with_weight(-90),
))
.alter_context(ContextOp::AddBaseAssets)
.alter_context(ContextOp::AddStyleSheet(
.alter_assets(AssetsOp::AddBaseAssets)
.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/inception.css")
.with_version("0.0.1")
.with_weight(-90),

View file

@ -1,5 +1,5 @@
mod context;
pub use context::{Context, ContextOp};
pub use context::{AssetsOp, Context};
pub type FnContextualPath = fn(cx: &Context) -> &str;
mod renderable;

View file

@ -1,6 +1,7 @@
use crate::base::component::add_base_assets;
use crate::core::component::MixedOp;
use crate::core::theme::all::{theme_by_single_name, THEME_DEFAULT};
use crate::core::theme::ThemeRef;
use crate::core::theme::{ComponentsInRegions, ThemeRef};
use crate::html::{html, Assets, HeadScript, HeadStyles, JavaScript, Markup, StyleSheet};
use crate::locale::{LanguageIdentifier, LANGID_DEFAULT};
use crate::service::HttpRequest;
@ -9,7 +10,7 @@ use crate::{concat_string, util};
use std::collections::HashMap;
use std::str::FromStr;
pub enum ContextOp {
pub enum AssetsOp {
LangId(&'static LanguageIdentifier),
Theme(&'static str),
// Stylesheets.
@ -37,6 +38,7 @@ pub struct Context {
headstyles: Assets<HeadStyles>, // Styles in head.
javascript: Assets<JavaScript>, // JavaScripts.
headscript: Assets<HeadScript>, // Scripts in head.
regions : ComponentsInRegions,
params : HashMap<&'static str, String>,
id_counter: usize,
}
@ -52,40 +54,46 @@ impl Context {
headstyles: Assets::<HeadStyles>::new(), // Styles in head.
javascript: Assets::<JavaScript>::new(), // JavaScripts.
headscript: Assets::<HeadScript>::new(), // Scripts in head.
regions : ComponentsInRegions::default(),
params : HashMap::<&str, String>::new(),
id_counter: 0,
}
}
#[rustfmt::skip]
pub fn alter(&mut self, op: ContextOp) -> &mut Self {
pub fn alter_assets(&mut self, op: AssetsOp) -> &mut Self {
match op {
ContextOp::LangId(langid) => {
AssetsOp::LangId(langid) => {
self.langid = langid;
}
ContextOp::Theme(theme_name) => {
AssetsOp::Theme(theme_name) => {
self.theme = theme_by_single_name(theme_name).unwrap_or(*THEME_DEFAULT);
}
// Stylesheets.
ContextOp::AddStyleSheet(css) => { self.stylesheet.add(css); }
ContextOp::RemoveStyleSheet(path) => { self.stylesheet.remove(path); }
AssetsOp::AddStyleSheet(css) => { self.stylesheet.add(css); }
AssetsOp::RemoveStyleSheet(path) => { self.stylesheet.remove(path); }
// Styles in head.
ContextOp::AddHeadStyles(styles) => { self.headstyles.add(styles); }
ContextOp::RemoveHeadStyles(path) => { self.headstyles.remove(path); }
AssetsOp::AddHeadStyles(styles) => { self.headstyles.add(styles); }
AssetsOp::RemoveHeadStyles(path) => { self.headstyles.remove(path); }
// JavaScripts.
ContextOp::AddJavaScript(js) => { self.javascript.add(js); }
ContextOp::RemoveJavaScript(path) => { self.javascript.remove(path); }
AssetsOp::AddJavaScript(js) => { self.javascript.add(js); }
AssetsOp::RemoveJavaScript(path) => { self.javascript.remove(path); }
// Scripts in head.
ContextOp::AddHeadScript(script) => { self.headscript.add(script); }
ContextOp::RemoveHeadScript(path) => { self.headscript.remove(path); }
AssetsOp::AddHeadScript(script) => { self.headscript.add(script); }
AssetsOp::RemoveHeadScript(path) => { self.headscript.remove(path); }
// Add assets to properly use base components.
ContextOp::AddBaseAssets => { add_base_assets(self); }
AssetsOp::AddBaseAssets => { add_base_assets(self); }
}
self
}
pub fn alter_regions(&mut self, region: &'static str, op: MixedOp) -> &mut Self {
self.regions.alter_components(region, op);
self
}
pub fn set_param<T: FromStr + ToString>(&mut self, key: &'static str, value: T) -> &mut Self {
self.params.insert(key, value.to_string());
self
@ -110,6 +118,10 @@ impl Context {
self.theme
}
pub fn regions(&self) -> &ComponentsInRegions {
&self.regions
}
pub fn get_param<T: FromStr + ToString>(&mut self, key: &'static str) -> Option<T> {
if let Some(value) = self.params.get(key) {
if let Ok(value) = T::from_str(value) {
@ -121,7 +133,7 @@ impl Context {
/// Context PREPARE.
pub fn prepare(&mut self) -> Markup {
pub fn prepare_assets(&mut self) -> Markup {
html! {
(self.stylesheet.prepare()) // Stylesheets.
(self.headstyles.prepare()) // Styles in head.

View file

@ -148,7 +148,7 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
(favicon.prepare())
}
(page.context().prepare())
(page.context().prepare_assets())
}
}
}

View file

@ -5,8 +5,7 @@ pub use actix_web::Result as ResultPage;
use crate::base::action;
use crate::core::component::{AnyComponent, ComponentTrait, MixedComponents, MixedOp};
use crate::core::component::{Context, ContextOp};
use crate::core::theme::ComponentsInRegions;
use crate::core::component::{AssetsOp, Context};
use crate::fn_builder;
use crate::html::{html, Markup, DOCTYPE};
use crate::html::{ClassesOp, Favicon, OptionClasses, OptionId, OptionTranslated};
@ -27,7 +26,6 @@ pub struct Page {
body_classes: OptionClasses,
skip_to : OptionId,
template : String,
regions : ComponentsInRegions,
}
impl Page {
@ -43,7 +41,6 @@ impl Page {
body_id : OptionId::default(),
body_classes: OptionClasses::default(),
skip_to : OptionId::default(),
regions : ComponentsInRegions::default(),
template : "default".to_owned(),
}
}
@ -81,8 +78,8 @@ impl Page {
}
#[fn_builder]
pub fn alter_context(&mut self, op: ContextOp) -> &mut Self {
self.context.alter(op);
pub fn alter_assets(&mut self, op: AssetsOp) -> &mut Self {
self.context.alter_assets(op);
self
}
@ -112,13 +109,13 @@ impl Page {
#[fn_builder]
pub fn alter_regions(&mut self, region: &'static str, op: MixedOp) -> &mut Self {
self.regions.alter_components(region, op);
self.context.alter_regions(region, op);
self
}
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.regions
.alter_components("content", MixedOp::Add(AnyComponent::with(component)));
self.context
.alter_regions("content", MixedOp::Add(AnyComponent::with(component)));
self
}
@ -127,8 +124,8 @@ impl Page {
region: &'static str,
component: impl ComponentTrait,
) -> Self {
self.regions
.alter_components(region, MixedOp::Add(AnyComponent::with(component)));
self.context
.alter_regions(region, MixedOp::Add(AnyComponent::with(component)));
self
}
@ -175,7 +172,9 @@ impl Page {
}
pub fn components_in(&self, region: &str) -> MixedComponents {
self.regions.all_components(self.context.theme(), region)
self.context
.regions()
.all_components(self.context.theme(), region)
}
// Page RENDER.