From 3d264598439902007a42c7c2d2b5992bfd7a1e90 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Mon, 11 Mar 2024 23:29:37 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=9A=20Rename=20operations=20with=20ass?= =?UTF-8?q?ets=20in=20context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/pagetop-admin/src/summary.rs | 1 - packages/pagetop-bootsier/src/lib.rs | 8 ++--- packages/pagetop-bulmix/src/lib.rs | 6 ++-- src/base/component.rs | 17 ++++++----- src/base/package/welcome.rs | 4 +-- src/base/theme/basic.rs | 6 ++-- src/base/theme/chassis.rs | 6 ++-- src/base/theme/inception.rs | 6 ++-- src/core/component.rs | 2 +- src/core/component/context.rs | 42 +++++++++++++++++---------- src/core/theme/definition.rs | 2 +- src/response/page.rs | 23 +++++++-------- 12 files changed, 67 insertions(+), 56 deletions(-) diff --git a/packages/pagetop-admin/src/summary.rs b/packages/pagetop-admin/src/summary.rs index fd8a5e1c..ecdd1968 100644 --- a/packages/pagetop-admin/src/summary.rs +++ b/packages/pagetop-admin/src/summary.rs @@ -151,7 +151,6 @@ pub async fn summary(request: HttpRequest) -> ResultPage { .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) diff --git a/packages/pagetop-bootsier/src/lib.rs b/packages/pagetop-bootsier/src/lib.rs index 548a0a8e..01653553 100644 --- a/packages/pagetop-bootsier/src/lib.rs +++ b/packages/pagetop-bootsier/src/lib.rs @@ -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"), )); } diff --git a/packages/pagetop-bulmix/src/lib.rs b/packages/pagetop-bulmix/src/lib.rs index fc38e70c..73c8029f 100644 --- a/packages/pagetop-bulmix/src/lib.rs +++ b/packages/pagetop-bulmix/src/lib.rs @@ -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"), )); } diff --git a/src/base/component.rs b/src/base/component.rs index 113f2f8f..a9912870 100644 --- a/src/base/component.rs +++ b/src/base/component.rs @@ -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::(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::(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::(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::(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), diff --git a/src/base/package/welcome.rs b/src/base/package/welcome.rs index 7bb7f470..2437689a 100644 --- a/src/base/package/welcome.rs +++ b/src/base/package/welcome.rs @@ -34,8 +34,8 @@ async fn home_lang( fn home(request: HttpRequest, lang: &'static LanguageIdentifier) -> ResultPage { 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") diff --git a/src/base/theme/basic.rs b/src/base/theme/basic.rs index ad63f0f1..9c2cf3c7 100644 --- a/src/base/theme/basic.rs +++ b/src/base/theme/basic.rs @@ -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), diff --git a/src/base/theme/chassis.rs b/src/base/theme/chassis.rs index d61bc609..fb6c34f4 100644 --- a/src/base/theme/chassis.rs +++ b/src/base/theme/chassis.rs @@ -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), diff --git a/src/base/theme/inception.rs b/src/base/theme/inception.rs index 95d35332..3492d5d4 100644 --- a/src/base/theme/inception.rs +++ b/src/base/theme/inception.rs @@ -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), diff --git a/src/core/component.rs b/src/core/component.rs index cf88dfbc..e36c4ec2 100644 --- a/src/core/component.rs +++ b/src/core/component.rs @@ -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; diff --git a/src/core/component/context.rs b/src/core/component/context.rs index 011a985e..1ca84e1b 100644 --- a/src/core/component/context.rs +++ b/src/core/component/context.rs @@ -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, // Styles in head. javascript: Assets, // JavaScripts. headscript: Assets, // Scripts in head. + regions : ComponentsInRegions, params : HashMap<&'static str, String>, id_counter: usize, } @@ -52,40 +54,46 @@ impl Context { headstyles: Assets::::new(), // Styles in head. javascript: Assets::::new(), // JavaScripts. headscript: Assets::::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(&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(&mut self, key: &'static str) -> Option { 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. diff --git a/src/core/theme/definition.rs b/src/core/theme/definition.rs index ca5a9c47..ca111e3a 100644 --- a/src/core/theme/definition.rs +++ b/src/core/theme/definition.rs @@ -148,7 +148,7 @@ pub trait ThemeTrait: PackageTrait + Send + Sync { (favicon.prepare()) } - (page.context().prepare()) + (page.context().prepare_assets()) } } } diff --git a/src/response/page.rs b/src/response/page.rs index f3b1b0c3..4ce4ddff 100644 --- a/src/response/page.rs +++ b/src/response/page.rs @@ -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.