✏️ [pagetop] Un tema es ahora un tipo de módulo

This commit is contained in:
Manuel Cillero 2023-02-03 21:46:21 +01:00
parent 84f1fc4a5c
commit 8ec2c698c8
14 changed files with 61 additions and 80 deletions

View file

@ -23,7 +23,7 @@ impl ModuleTrait for Drust {
] ]
} }
fn uninstall_modules(&self) -> Vec<ModuleStaticRef> { fn drop_modules(&self) -> Vec<ModuleStaticRef> {
vec![ vec![
// &pagetop_node::Node // &pagetop_node::Node
] ]

View file

@ -33,9 +33,6 @@ impl Application {
// Registra los módulos de la aplicación. // Registra los módulos de la aplicación.
module::all::register_modules(app); module::all::register_modules(app);
// Registra los temas de los módulos.
module::all::register_themes();
// Registra acciones de los módulos. // Registra acciones de los módulos.
module::all::register_actions(); module::all::register_actions();

View file

@ -1,3 +1,2 @@
pub mod component; pub mod component;
pub mod module; pub mod module;
pub mod theme;

View file

@ -1,3 +1,3 @@
pub mod saturn;
pub mod menu; pub mod menu;
pub mod homepage; pub mod homepage;

View file

@ -4,8 +4,5 @@ pub mod component;
// API to define functions that alter the behavior of PageTop core. // API to define functions that alter the behavior of PageTop core.
pub mod hook; pub mod hook;
// API to add new features with modules. // API to add new features with modules and themes.
pub mod module; pub mod module;
// API to create themes.
pub mod theme;

View file

@ -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::html::{html, Assets, IdentifierValue, JavaScript, Markup, StyleSheet};
use crate::server::HttpRequest; use crate::server::HttpRequest;
use crate::{base, concat_string, config, util, LazyStatic}; use crate::{base, concat_string, config, util, LazyStatic};
@ -9,7 +9,7 @@ use std::str::FromStr;
static DEFAULT_THEME: LazyStatic<ThemeStaticRef> = static DEFAULT_THEME: LazyStatic<ThemeStaticRef> =
LazyStatic::new(|| match theme_by_single_name(&config::SETTINGS.app.theme) { LazyStatic::new(|| match theme_by_single_name(&config::SETTINGS.app.theme) {
Some(theme) => theme, Some(theme) => theme,
None => &base::theme::Saturn, None => &base::module::saturn::Saturn,
}); });
pub enum ContextOp { pub enum ContextOp {

View file

@ -1,4 +1,7 @@
mod definition; mod module;
pub use definition::{BaseModule, ModuleStaticRef, ModuleTrait}; pub use module::{BaseModule, ModuleStaticRef, ModuleTrait};
mod theme;
pub use theme::{ThemeStaticRef, ThemeTrait};
pub(crate) mod all; pub(crate) mod all;

View file

@ -1,8 +1,7 @@
use super::ModuleStaticRef; use super::{ModuleStaticRef, ThemeStaticRef};
use crate::base; use crate::base;
use crate::core::hook::add_action; use crate::core::hook::add_action;
use crate::core::theme;
use crate::{server, trace, LazyStatic}; use crate::{server, trace, LazyStatic};
#[cfg(feature = "database")] #[cfg(feature = "database")]
@ -10,26 +9,46 @@ use crate::{db::*, run_now};
use std::sync::RwLock; use std::sync::RwLock;
// REGISTER MODULES ******************************************************************************** // MODULES *****************************************************************************************
static ENABLED_MODULES: LazyStatic<RwLock<Vec<ModuleStaticRef>>> = static ENABLED_MODULES: LazyStatic<RwLock<Vec<ModuleStaticRef>>> =
LazyStatic::new(|| RwLock::new(Vec::new())); LazyStatic::new(|| RwLock::new(Vec::new()));
static DISCARDED_MODULES: LazyStatic<RwLock<Vec<ModuleStaticRef>>> = static DROPPED_MODULES: LazyStatic<RwLock<Vec<ModuleStaticRef>>> =
LazyStatic::new(|| RwLock::new(Vec::new())); LazyStatic::new(|| RwLock::new(Vec::new()));
// THEMES ******************************************************************************************
static THEMES: LazyStatic<RwLock<Vec<ThemeStaticRef>>> =
LazyStatic::new(|| RwLock::new(Vec::new()));
pub fn theme_by_single_name(single_name: &str) -> Option<ThemeStaticRef> {
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) { pub fn register_modules(app: ModuleStaticRef) {
// List of modules to disable. // List of modules to drop.
let mut list: Vec<ModuleStaticRef> = Vec::new(); let mut list: Vec<ModuleStaticRef> = Vec::new();
add_to_discarded(&mut list, app); add_to_dropped(&mut list, app);
DISCARDED_MODULES.write().unwrap().append(&mut list); DROPPED_MODULES.write().unwrap().append(&mut list);
// List of modules to enable. // List of modules to enable.
let mut list: Vec<ModuleStaticRef> = Vec::new(); let mut list: Vec<ModuleStaticRef> = Vec::new();
// 1 of 3. Enable base modules. // 1 of 3. Enable base modules.
add_to_enabled(&mut list, &base::module::menu::Menu); 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. // 2 of 3. Enable application modules.
add_to_enabled(&mut list, app); add_to_enabled(&mut list, app);
@ -41,28 +60,28 @@ pub fn register_modules(app: ModuleStaticRef) {
ENABLED_MODULES.write().unwrap().append(&mut list); ENABLED_MODULES.write().unwrap().append(&mut list);
} }
fn add_to_discarded(list: &mut Vec<ModuleStaticRef>, module: ModuleStaticRef) { fn add_to_dropped(list: &mut Vec<ModuleStaticRef>, module: ModuleStaticRef) {
for u in module.uninstall_modules().iter() { for d in module.drop_modules().iter() {
if !list.iter().any(|m| m.handle() == u.handle()) { if !list.iter().any(|m| m.handle() == d.handle()) {
list.push(*u); list.push(*d);
trace::debug!("Module \"{}\" discarded", u.single_name()); trace::debug!("Module \"{}\" dropped", d.single_name());
} }
} }
for d in module.dependencies().iter() { for d in module.dependencies().iter() {
add_to_discarded(list, *d); add_to_dropped(list, *d);
} }
} }
fn add_to_enabled(list: &mut Vec<ModuleStaticRef>, module: ModuleStaticRef) { fn add_to_enabled(list: &mut Vec<ModuleStaticRef>, module: ModuleStaticRef) {
if !list.iter().any(|m| m.handle() == module.handle()) { if !list.iter().any(|m| m.handle() == module.handle()) {
if DISCARDED_MODULES if DROPPED_MODULES
.read() .read()
.unwrap() .unwrap()
.iter() .iter()
.any(|m| m.handle() == module.handle()) .any(|m| m.handle() == module.handle())
{ {
panic!( panic!(
"Trying to enable \"{}\" module which is disabled", "Trying to enable \"{}\" module which is dropped",
module.single_name() module.single_name()
); );
} else { } else {
@ -74,19 +93,19 @@ fn add_to_enabled(list: &mut Vec<ModuleStaticRef>, module: ModuleStaticRef) {
add_to_enabled(list, *d); 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 ******************************************************************************** // REGISTER ACTIONS ********************************************************************************
pub fn register_actions() { pub fn register_actions() {
@ -130,7 +149,7 @@ pub fn run_migrations() {
impl MigratorTrait for Migrator { impl MigratorTrait for Migrator {
fn migrations() -> Vec<MigrationItem> { fn migrations() -> Vec<MigrationItem> {
let mut 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.append(&mut m.migrations());
} }
migrations migrations

View file

@ -1,5 +1,6 @@
use super::ThemeStaticRef;
use crate::core::hook::HookAction; use crate::core::hook::HookAction;
use crate::core::theme::ThemeStaticRef;
use crate::server; use crate::server;
use crate::util::{single_type_name, Handle}; use crate::util::{single_type_name, Handle};
@ -32,7 +33,7 @@ pub trait ModuleTrait: BaseModule + Send + Sync {
vec![] vec![]
} }
fn uninstall_modules(&self) -> Vec<ModuleStaticRef> { fn drop_modules(&self) -> Vec<ModuleStaticRef> {
vec![] vec![]
} }

View file

@ -1,6 +1,7 @@
use super::ModuleTrait;
use crate::base::component::{Container, Html}; use crate::base::component::{Container, Html};
use crate::core::component::{ComponentTrait, RenderContext}; use crate::core::component::{ComponentTrait, RenderContext};
use crate::core::module::ModuleTrait;
use crate::html::{html, Favicon, Markup}; use crate::html::{html, Favicon, Markup};
use crate::response::page::Page; use crate::response::page::Page;
use crate::{concat_string, config}; use crate::{concat_string, config};

View file

@ -1,4 +0,0 @@
mod definition;
pub use definition::{ThemeStaticRef, ThemeTrait};
pub(crate) mod all;

View file

@ -1,32 +0,0 @@
use super::ThemeStaticRef;
use crate::{trace, LazyStatic};
use std::sync::RwLock;
// Temas registrados.
static THEMES: LazyStatic<RwLock<Vec<ThemeStaticRef>>> =
LazyStatic::new(|| RwLock::new(Vec::new()));
pub fn register_theme(theme: Option<ThemeStaticRef>) {
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<ThemeStaticRef> {
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,
}
}

View file

@ -19,7 +19,7 @@ pub use crate::html::*;
#[cfg(feature = "database")] #[cfg(feature = "database")]
pub use crate::{db, db::*, migration_item, pub_migration}; 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}; pub use crate::{hook_action, hook_before_render_component};