✨ Add components to theme-less regions
This commit is contained in:
parent
025dcd9b17
commit
84bd5f631a
7 changed files with 56 additions and 41 deletions
|
|
@ -53,18 +53,14 @@ impl PackComponents {
|
|||
pack
|
||||
}
|
||||
|
||||
pub(crate) fn merge(one: Option<&PackComponents>, other: Option<&PackComponents>) -> Self {
|
||||
if let Some(one) = one {
|
||||
let mut components = one.0.clone();
|
||||
if let Some(other) = other {
|
||||
components.append(&mut other.0.clone());
|
||||
pub(crate) fn merge(packs: &[Option<&PackComponents>]) -> Self {
|
||||
let mut pack = PackComponents::default();
|
||||
for p in packs {
|
||||
if let Some(p) = p {
|
||||
pack.0.append(&mut p.0.clone());
|
||||
}
|
||||
PackComponents(components)
|
||||
} else if let Some(other) = other {
|
||||
PackComponents(other.0.clone())
|
||||
} else {
|
||||
PackComponents::default()
|
||||
}
|
||||
pack
|
||||
}
|
||||
|
||||
// PackComponents BUILDER.
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ pub fn register_modules(app: ModuleRef) {
|
|||
// List of modules to enable.
|
||||
let mut list: Vec<ModuleRef> = Vec::new();
|
||||
|
||||
// Enable basic theme.
|
||||
add_to_enabled(&mut list, &crate::core::theme::Basic);
|
||||
// Enable default theme.
|
||||
add_to_enabled(&mut list, &crate::core::theme::DefaultTheme);
|
||||
|
||||
// Enable application modules.
|
||||
add_to_enabled(&mut list, app);
|
||||
|
|
|
|||
|
|
@ -2,10 +2,10 @@ mod definition;
|
|||
pub use definition::{ThemeRef, ThemeTrait};
|
||||
|
||||
mod regions;
|
||||
pub use regions::add_component_to;
|
||||
pub(crate) use regions::ComponentsRegions;
|
||||
pub use regions::{add_component_in, Region};
|
||||
|
||||
mod basic;
|
||||
pub(crate) use basic::Basic;
|
||||
mod default;
|
||||
pub use default::DefaultTheme;
|
||||
|
||||
pub(crate) mod all;
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ pub static THEMES: LazyStatic<RwLock<Vec<ThemeRef>>> = LazyStatic::new(|| RwLock
|
|||
pub static THEME: LazyStatic<ThemeRef> =
|
||||
LazyStatic::new(|| match theme_by_single_name(&config::SETTINGS.app.theme) {
|
||||
Some(theme) => theme,
|
||||
None => &crate::core::theme::Basic,
|
||||
None => &crate::core::theme::DefaultTheme,
|
||||
});
|
||||
|
||||
// THEME BY NAME ***********************************************************************************
|
||||
|
|
|
|||
|
|
@ -6,19 +6,19 @@ use crate::response::page::Page;
|
|||
use crate::service;
|
||||
use crate::{create_handle, serve_static_files, static_files, Handle};
|
||||
|
||||
create_handle!(THEME_BASIC);
|
||||
create_handle!(THEME_DEFAULT);
|
||||
|
||||
static_files!(theme);
|
||||
|
||||
pub struct Basic;
|
||||
pub struct DefaultTheme;
|
||||
|
||||
impl ModuleTrait for Basic {
|
||||
impl ModuleTrait for DefaultTheme {
|
||||
fn handle(&self) -> Handle {
|
||||
THEME_BASIC
|
||||
THEME_DEFAULT
|
||||
}
|
||||
|
||||
fn theme(&self) -> Option<ThemeRef> {
|
||||
Some(&Basic)
|
||||
Some(&DefaultTheme)
|
||||
}
|
||||
|
||||
fn configure_service(&self, cfg: &mut service::web::ServiceConfig) {
|
||||
|
|
@ -26,7 +26,7 @@ impl ModuleTrait for Basic {
|
|||
}
|
||||
}
|
||||
|
||||
impl ThemeTrait for Basic {
|
||||
impl ThemeTrait for DefaultTheme {
|
||||
fn before_prepare_body(&self, page: &mut Page) {
|
||||
page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico")))
|
||||
.alter_context(ContextOp::AddStyleSheet(
|
||||
|
|
@ -1,12 +1,16 @@
|
|||
use crate::core::component::{ComponentRef, PackComponents, PackOp};
|
||||
use crate::LazyStatic;
|
||||
use crate::core::theme::ThemeRef;
|
||||
use crate::{Handle, LazyStatic};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::RwLock;
|
||||
|
||||
static THEME_REGIONS: LazyStatic<RwLock<HashMap<&'static str, ComponentsRegions>>> =
|
||||
static THEME_REGIONS: LazyStatic<RwLock<HashMap<Handle, ComponentsRegions>>> =
|
||||
LazyStatic::new(|| RwLock::new(HashMap::new()));
|
||||
|
||||
static COMMON_REGIONS: LazyStatic<RwLock<ComponentsRegions>> =
|
||||
LazyStatic::new(|| RwLock::new(ComponentsRegions::new()));
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ComponentsRegions(HashMap<&'static str, PackComponents>);
|
||||
|
||||
|
|
@ -15,7 +19,13 @@ impl ComponentsRegions {
|
|||
ComponentsRegions::default()
|
||||
}
|
||||
|
||||
pub fn add_to(&mut self, region: &'static str, cref: ComponentRef) {
|
||||
pub fn new_with(region: &'static str, cref: ComponentRef) -> Self {
|
||||
let mut regions = ComponentsRegions::new();
|
||||
regions.add_in(region, cref);
|
||||
regions
|
||||
}
|
||||
|
||||
pub fn add_in(&mut self, region: &'static str, cref: ComponentRef) {
|
||||
if let Some(region) = self.0.get_mut(region) {
|
||||
region.alter(PackOp::Add, cref);
|
||||
} else {
|
||||
|
|
@ -23,24 +33,33 @@ impl ComponentsRegions {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_extended_pack(&self, theme: &str, region: &str) -> PackComponents {
|
||||
if let Some(hm_theme) = THEME_REGIONS.read().unwrap().get(theme) {
|
||||
PackComponents::merge(self.0.get(region), hm_theme.0.get(region))
|
||||
pub fn get_pack(&self, theme: ThemeRef, region: &str) -> PackComponents {
|
||||
let common = COMMON_REGIONS.read().unwrap();
|
||||
if let Some(hm) = THEME_REGIONS.read().unwrap().get(&theme.handle()) {
|
||||
PackComponents::merge(&[common.0.get(region), self.0.get(region), hm.0.get(region)])
|
||||
} else {
|
||||
PackComponents::merge(self.0.get(region), None)
|
||||
PackComponents::merge(&[common.0.get(region), self.0.get(region)])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_component_to(theme: &'static str, region: &'static str, cref: ComponentRef) {
|
||||
let mut hm = THEME_REGIONS.write().unwrap();
|
||||
if let Some(hm_theme) = hm.get_mut(theme) {
|
||||
hm_theme.add_to(region, cref);
|
||||
} else {
|
||||
hm.insert(theme, {
|
||||
let mut regions = ComponentsRegions::new();
|
||||
regions.add_to(region, cref);
|
||||
regions
|
||||
});
|
||||
pub enum Region {
|
||||
Named(&'static str),
|
||||
OfTheme(ThemeRef, &'static str),
|
||||
}
|
||||
|
||||
pub fn add_component_in(region: Region, cref: ComponentRef) {
|
||||
match region {
|
||||
Region::Named(name) => {
|
||||
COMMON_REGIONS.write().unwrap().add_in(name, cref);
|
||||
}
|
||||
Region::OfTheme(theme, region) => {
|
||||
let mut regions = THEME_REGIONS.write().unwrap();
|
||||
if let Some(hm) = regions.get_mut(&theme.handle()) {
|
||||
hm.add_in(region, cref);
|
||||
} else {
|
||||
regions.insert(theme.handle(), ComponentsRegions::new_with(region, cref));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ impl Page {
|
|||
|
||||
#[fn_builder]
|
||||
pub fn alter_in(&mut self, region: &'static str, component: impl ComponentTrait) -> &mut Self {
|
||||
self.regions.add_to(region, ComponentRef::to(component));
|
||||
self.regions.add_in(region, ComponentRef::to(component));
|
||||
self
|
||||
}
|
||||
|
||||
|
|
@ -173,7 +173,7 @@ impl Page {
|
|||
pub fn prepare_region(&mut self, region: &str) -> Option<Markup> {
|
||||
let render = self
|
||||
.regions
|
||||
.get_extended_pack(self.context.theme().single_name(), region)
|
||||
.get_pack(self.context.theme(), region)
|
||||
.prepare(self.context());
|
||||
if render.is_empty() {
|
||||
None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue