From 8ec2c698c83d3a0f091e8b51be77b60a4d74e24a Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Fri, 3 Feb 2023 21:46:21 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20[pagetop]=20Un=20tema=20es?= =?UTF-8?q?=20ahora=20un=20tipo=20de=20m=C3=B3dulo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- drust/src/main.rs | 2 +- pagetop/src/app.rs | 3 - pagetop/src/base.rs | 1 - pagetop/src/base/module.rs | 2 +- .../src/base/{theme.rs => module/saturn.rs} | 0 pagetop/src/core.rs | 5 +- pagetop/src/core/component/context.rs | 4 +- pagetop/src/core/module.rs | 7 +- pagetop/src/core/module/all.rs | 71 ++++++++++++------- .../core/module/{definition.rs => module.rs} | 5 +- .../{theme/definition.rs => module/theme.rs} | 3 +- pagetop/src/core/theme.rs | 4 -- pagetop/src/core/theme/all.rs | 32 --------- pagetop/src/prelude.rs | 2 +- 14 files changed, 61 insertions(+), 80 deletions(-) rename pagetop/src/base/{theme.rs => module/saturn.rs} (100%) rename pagetop/src/core/module/{definition.rs => module.rs} (92%) rename pagetop/src/core/{theme/definition.rs => module/theme.rs} (99%) delete mode 100644 pagetop/src/core/theme.rs delete mode 100644 pagetop/src/core/theme/all.rs diff --git a/drust/src/main.rs b/drust/src/main.rs index 91f4cc8e..4d12012c 100644 --- a/drust/src/main.rs +++ b/drust/src/main.rs @@ -23,7 +23,7 @@ impl ModuleTrait for Drust { ] } - fn uninstall_modules(&self) -> Vec { + fn drop_modules(&self) -> Vec { vec![ // &pagetop_node::Node ] diff --git a/pagetop/src/app.rs b/pagetop/src/app.rs index eebc7041..f4f08b3a 100644 --- a/pagetop/src/app.rs +++ b/pagetop/src/app.rs @@ -33,9 +33,6 @@ impl Application { // Registra los módulos de la aplicación. module::all::register_modules(app); - // Registra los temas de los módulos. - module::all::register_themes(); - // Registra acciones de los módulos. module::all::register_actions(); diff --git a/pagetop/src/base.rs b/pagetop/src/base.rs index d58aa565..74a98984 100644 --- a/pagetop/src/base.rs +++ b/pagetop/src/base.rs @@ -1,3 +1,2 @@ pub mod component; pub mod module; -pub mod theme; diff --git a/pagetop/src/base/module.rs b/pagetop/src/base/module.rs index ca296b77..b3ec2650 100644 --- a/pagetop/src/base/module.rs +++ b/pagetop/src/base/module.rs @@ -1,3 +1,3 @@ +pub mod saturn; pub mod menu; - pub mod homepage; diff --git a/pagetop/src/base/theme.rs b/pagetop/src/base/module/saturn.rs similarity index 100% rename from pagetop/src/base/theme.rs rename to pagetop/src/base/module/saturn.rs diff --git a/pagetop/src/core.rs b/pagetop/src/core.rs index 3d09c260..616ebedb 100644 --- a/pagetop/src/core.rs +++ b/pagetop/src/core.rs @@ -4,8 +4,5 @@ pub mod component; // API to define functions that alter the behavior of PageTop core. pub mod hook; -// API to add new features with modules. +// API to add new features with modules and themes. pub mod module; - -// API to create themes. -pub mod theme; diff --git a/pagetop/src/core/component/context.rs b/pagetop/src/core/component/context.rs index 8e25858a..c426424b 100644 --- a/pagetop/src/core/component/context.rs +++ b/pagetop/src/core/component/context.rs @@ -1,4 +1,4 @@ -use crate::core::theme::{all::theme_by_single_name, ThemeStaticRef}; +use crate::core::module::{all::theme_by_single_name, ThemeStaticRef}; use crate::html::{html, Assets, IdentifierValue, JavaScript, Markup, StyleSheet}; use crate::server::HttpRequest; use crate::{base, concat_string, config, util, LazyStatic}; @@ -9,7 +9,7 @@ use std::str::FromStr; static DEFAULT_THEME: LazyStatic = LazyStatic::new(|| match theme_by_single_name(&config::SETTINGS.app.theme) { Some(theme) => theme, - None => &base::theme::Saturn, + None => &base::module::saturn::Saturn, }); pub enum ContextOp { diff --git a/pagetop/src/core/module.rs b/pagetop/src/core/module.rs index 0ecfbc65..53b54c84 100644 --- a/pagetop/src/core/module.rs +++ b/pagetop/src/core/module.rs @@ -1,4 +1,7 @@ -mod definition; -pub use definition::{BaseModule, ModuleStaticRef, ModuleTrait}; +mod module; +pub use module::{BaseModule, ModuleStaticRef, ModuleTrait}; + +mod theme; +pub use theme::{ThemeStaticRef, ThemeTrait}; pub(crate) mod all; diff --git a/pagetop/src/core/module/all.rs b/pagetop/src/core/module/all.rs index 0a4efb55..453d07ff 100644 --- a/pagetop/src/core/module/all.rs +++ b/pagetop/src/core/module/all.rs @@ -1,8 +1,7 @@ -use super::ModuleStaticRef; +use super::{ModuleStaticRef, ThemeStaticRef}; use crate::base; use crate::core::hook::add_action; -use crate::core::theme; use crate::{server, trace, LazyStatic}; #[cfg(feature = "database")] @@ -10,26 +9,46 @@ use crate::{db::*, run_now}; use std::sync::RwLock; -// REGISTER MODULES ******************************************************************************** +// MODULES ***************************************************************************************** static ENABLED_MODULES: LazyStatic>> = LazyStatic::new(|| RwLock::new(Vec::new())); -static DISCARDED_MODULES: LazyStatic>> = +static DROPPED_MODULES: LazyStatic>> = LazyStatic::new(|| RwLock::new(Vec::new())); +// THEMES ****************************************************************************************** + +static THEMES: LazyStatic>> = + LazyStatic::new(|| RwLock::new(Vec::new())); + +pub fn theme_by_single_name(single_name: &str) -> Option { + let single_name = single_name.to_lowercase(); + match THEMES + .read() + .unwrap() + .iter() + .find(|t| t.single_name().to_lowercase() == single_name) + { + Some(theme) => Some(*theme), + _ => None, + } +} + +// REGISTER MODULES ******************************************************************************** + pub fn register_modules(app: ModuleStaticRef) { - // List of modules to disable. + // List of modules to drop. let mut list: Vec = Vec::new(); - add_to_discarded(&mut list, app); - DISCARDED_MODULES.write().unwrap().append(&mut list); + add_to_dropped(&mut list, app); + DROPPED_MODULES.write().unwrap().append(&mut list); // List of modules to enable. let mut list: Vec = Vec::new(); // 1 of 3. Enable base modules. add_to_enabled(&mut list, &base::module::menu::Menu); - add_to_enabled(&mut list, &base::theme::Saturn); + add_to_enabled(&mut list, &base::module::saturn::Saturn); // 2 of 3. Enable application modules. add_to_enabled(&mut list, app); @@ -41,28 +60,28 @@ pub fn register_modules(app: ModuleStaticRef) { ENABLED_MODULES.write().unwrap().append(&mut list); } -fn add_to_discarded(list: &mut Vec, module: ModuleStaticRef) { - for u in module.uninstall_modules().iter() { - if !list.iter().any(|m| m.handle() == u.handle()) { - list.push(*u); - trace::debug!("Module \"{}\" discarded", u.single_name()); +fn add_to_dropped(list: &mut Vec, module: ModuleStaticRef) { + for d in module.drop_modules().iter() { + if !list.iter().any(|m| m.handle() == d.handle()) { + list.push(*d); + trace::debug!("Module \"{}\" dropped", d.single_name()); } } for d in module.dependencies().iter() { - add_to_discarded(list, *d); + add_to_dropped(list, *d); } } fn add_to_enabled(list: &mut Vec, module: ModuleStaticRef) { if !list.iter().any(|m| m.handle() == module.handle()) { - if DISCARDED_MODULES + if DROPPED_MODULES .read() .unwrap() .iter() .any(|m| m.handle() == module.handle()) { panic!( - "Trying to enable \"{}\" module which is disabled", + "Trying to enable \"{}\" module which is dropped", module.single_name() ); } else { @@ -74,19 +93,19 @@ fn add_to_enabled(list: &mut Vec, module: ModuleStaticRef) { add_to_enabled(list, *d); } - trace::debug!("Enabling \"{}\" module", module.single_name()); + if let Some(theme) = module.theme() { + let mut registered_themes = THEMES.write().unwrap(); + if !registered_themes.iter().any(|t| t.handle() == theme.handle()) { + registered_themes.push(theme); + trace::debug!("Enabling \"{}\" theme", theme.single_name()); + } + } else { + trace::debug!("Enabling \"{}\" module", module.single_name()); + } } } } -// REGISTER THEMES ********************************************************************************* - -pub fn register_themes() { - for m in ENABLED_MODULES.read().unwrap().iter() { - theme::all::register_theme(m.theme()); - } -} - // REGISTER ACTIONS ******************************************************************************** pub fn register_actions() { @@ -130,7 +149,7 @@ pub fn run_migrations() { impl MigratorTrait for Migrator { fn migrations() -> Vec { let mut migrations = vec![]; - for m in DISCARDED_MODULES.read().unwrap().iter() { + for m in DROPPED_MODULES.read().unwrap().iter() { migrations.append(&mut m.migrations()); } migrations diff --git a/pagetop/src/core/module/definition.rs b/pagetop/src/core/module/module.rs similarity index 92% rename from pagetop/src/core/module/definition.rs rename to pagetop/src/core/module/module.rs index 31d7a566..db155221 100644 --- a/pagetop/src/core/module/definition.rs +++ b/pagetop/src/core/module/module.rs @@ -1,5 +1,6 @@ +use super::ThemeStaticRef; + use crate::core::hook::HookAction; -use crate::core::theme::ThemeStaticRef; use crate::server; use crate::util::{single_type_name, Handle}; @@ -32,7 +33,7 @@ pub trait ModuleTrait: BaseModule + Send + Sync { vec![] } - fn uninstall_modules(&self) -> Vec { + fn drop_modules(&self) -> Vec { vec![] } diff --git a/pagetop/src/core/theme/definition.rs b/pagetop/src/core/module/theme.rs similarity index 99% rename from pagetop/src/core/theme/definition.rs rename to pagetop/src/core/module/theme.rs index 6c130878..77918dab 100644 --- a/pagetop/src/core/theme/definition.rs +++ b/pagetop/src/core/module/theme.rs @@ -1,6 +1,7 @@ +use super::ModuleTrait; + use crate::base::component::{Container, Html}; use crate::core::component::{ComponentTrait, RenderContext}; -use crate::core::module::ModuleTrait; use crate::html::{html, Favicon, Markup}; use crate::response::page::Page; use crate::{concat_string, config}; diff --git a/pagetop/src/core/theme.rs b/pagetop/src/core/theme.rs deleted file mode 100644 index 67902dcb..00000000 --- a/pagetop/src/core/theme.rs +++ /dev/null @@ -1,4 +0,0 @@ -mod definition; -pub use definition::{ThemeStaticRef, ThemeTrait}; - -pub(crate) mod all; diff --git a/pagetop/src/core/theme/all.rs b/pagetop/src/core/theme/all.rs deleted file mode 100644 index 9b876356..00000000 --- a/pagetop/src/core/theme/all.rs +++ /dev/null @@ -1,32 +0,0 @@ -use super::ThemeStaticRef; -use crate::{trace, LazyStatic}; - -use std::sync::RwLock; - -// Temas registrados. -static THEMES: LazyStatic>> = - LazyStatic::new(|| RwLock::new(Vec::new())); - -pub fn register_theme(theme: Option) { - if let Some(theme) = theme { - let handle = theme.handle(); - let mut registered_themes = THEMES.write().unwrap(); - if !registered_themes.iter().any(|t| t.handle() == handle) { - trace::debug!("Registering theme \"{}\"", theme.single_name()); - registered_themes.push(theme); - } - } -} - -pub fn theme_by_single_name(single_name: &str) -> Option { - let single_name = single_name.to_lowercase(); - match THEMES - .read() - .unwrap() - .iter() - .find(|t| t.single_name().to_lowercase() == single_name) - { - Some(theme) => Some(*theme), - _ => None, - } -} diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index 0a4a4869..1e6a7cce 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -19,7 +19,7 @@ pub use crate::html::*; #[cfg(feature = "database")] pub use crate::{db, db::*, migration_item, pub_migration}; -pub use crate::core::{component::*, hook::*, module::*, theme::*}; +pub use crate::core::{component::*, hook::*, module::*}; pub use crate::{hook_action, hook_before_render_component};