🚧 Working on regions layout

This commit is contained in:
Manuel Cillero 2024-03-28 23:17:15 +01:00
parent 625d16c0f2
commit ee5742e0b2
11 changed files with 264 additions and 220 deletions

View file

@ -2,7 +2,9 @@ use crate::base::component::add_base_assets;
use crate::core::component::AnyOp;
use crate::core::theme::all::{theme_by_single_name, THEME_DEFAULT};
use crate::core::theme::{ComponentsInRegions, ThemeRef};
use crate::html::{html, Assets, HeadScript, HeadStyles, JavaScript, Markup, StyleSheet};
use crate::html::{html, Markup};
use crate::html::{Assets, HeadScript, HeadStyles, JavaScript, StyleSheet};
use crate::html::{ClassesOp, OptionClasses, OptionId};
use crate::locale::{LanguageIdentifier, LANGID_DEFAULT};
use crate::service::HttpRequest;
use crate::{concat_string, util};
@ -32,17 +34,20 @@ pub enum AssetsOp {
#[rustfmt::skip]
pub struct Context {
request : HttpRequest,
langid : &'static LanguageIdentifier,
theme : ThemeRef,
layout : &'static str,
stylesheet: Assets<StyleSheet>, // Stylesheets.
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,
request : HttpRequest,
langid : &'static LanguageIdentifier,
theme : ThemeRef,
layout : &'static str,
stylesheet : Assets<StyleSheet>, // Stylesheets.
headstyles : Assets<HeadStyles>, // Styles in head.
javascript : Assets<JavaScript>, // JavaScripts.
headscript : Assets<HeadScript>, // Scripts in head.
body_id : OptionId,
body_classes: OptionClasses,
body_skip_to: OptionId,
regions : ComponentsInRegions,
params : HashMap<&'static str, String>,
id_counter : usize,
}
impl Context {
@ -50,16 +55,19 @@ impl Context {
pub(crate) fn new(request: HttpRequest) -> Self {
Context {
request,
langid : &LANGID_DEFAULT,
theme : *THEME_DEFAULT,
layout : "default",
stylesheet: Assets::<StyleSheet>::new(), // Stylesheets.
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,
langid : &LANGID_DEFAULT,
theme : *THEME_DEFAULT,
layout : "default",
stylesheet : Assets::<StyleSheet>::new(), // Stylesheets.
headstyles : Assets::<HeadStyles>::new(), // Styles in head.
javascript : Assets::<JavaScript>::new(), // JavaScripts.
headscript : Assets::<HeadScript>::new(), // Scripts in head.
body_id : OptionId::default(),
body_classes: OptionClasses::default(),
body_skip_to: OptionId::default(),
regions : ComponentsInRegions::default(),
params : HashMap::<&str, String>::new(),
id_counter : 0,
}
}
@ -95,6 +103,21 @@ impl Context {
self
}
pub fn alter_body_id(&mut self, id: impl Into<String>) -> &mut Self {
self.body_id.alter_value(id);
self
}
pub fn alter_body_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.body_classes.alter_value(op, classes);
self
}
pub fn alter_body_skip_to(&mut self, id: impl Into<String>) -> &mut Self {
self.body_skip_to.alter_value(id);
self
}
pub fn alter_regions(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
self.regions.alter_components(region, op);
self
@ -128,6 +151,18 @@ impl Context {
self.layout
}
pub fn body_id(&self) -> &OptionId {
&self.body_id
}
pub fn body_classes(&self) -> &OptionClasses {
&self.body_classes
}
pub fn body_skip_to(&self) -> &OptionId {
&self.body_skip_to
}
pub fn regions(&self) -> &ComponentsInRegions {
&self.regions
}
@ -143,7 +178,7 @@ impl Context {
/// Context PREPARE.
pub fn prepare_assets(&mut self) -> Markup {
pub(crate) fn prepare_assets(&mut self) -> Markup {
html! {
(self.stylesheet.prepare()) // Stylesheets.
(self.headstyles.prepare()) // Styles in head.
@ -152,8 +187,10 @@ impl Context {
}
}
pub fn prepare_region(&mut self, region: &str) -> Markup {
self.regions.all_components(self.theme, region).render(self)
pub(crate) fn prepare_region(&mut self, region: impl Into<String>) -> Markup {
self.regions
.all_components(self.theme, region.into().as_str())
.render(self)
}
// Context EXTRAS.

View file

@ -1,10 +1,10 @@
use crate::base::component::*;
use crate::core::component::{ComponentBase, ComponentClassesOp, ComponentTrait};
use crate::config;
use crate::core::component::{ComponentBase, ComponentTrait};
use crate::core::package::PackageTrait;
use crate::html::{html, ClassesOp, Favicon, Markup};
use crate::html::{html, Favicon, Markup};
use crate::locale::L10n;
use crate::response::page::Page;
use crate::{concat_string, config};
pub type ThemeRef = &'static dyn ThemeTrait;
@ -26,51 +26,38 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
fn before_prepare_body(&self, page: &mut Page) {}
fn prepare_body(&self, page: &mut Page) -> Markup {
let skip_to_id = concat_string!("#", page.skip_to().get().unwrap_or("content".to_owned()));
flex::Container::body()
.with_id(page.body_id().get().unwrap_or_default())
.with_classes(ClassesOp::Add, page.body_classes().get().unwrap_or_default())
.add_item(flex::Item::bundle()
.add_component(Html::with(html! {
@if let Some(skip) = L10n::l("skip_to_content").using(page.context().langid()) {
div class="skip__to_content" {
a href=(skip_to_id) { (skip) }
}
}
}))
.add_component(flex::Container::new()
.with_id("body__wrapper")
.with_direction(flex::Direction::Column(BreakPoint::None))
.with_align(flex::Align::Center)
.add_item(flex::Item::with(flex::Region::named("header")).with_id("header"))
.add_item(flex::Item::with(flex::Region::named("pagetop")).with_id("pagetop"))
.add_item(
flex::Item::with(
flex::Container::new()
.with_direction(flex::Direction::Row(BreakPoint::None))
.add_item(
flex::Item::with(flex::Region::named("sidebar_left"))
.with_id("sidebar_left")
.with_grow(flex::Grow::Is1),
)
.add_item(
flex::Item::with(flex::Region::named("content"))
.with_id("content")
.with_grow(flex::Grow::Is3),
)
.add_item(
flex::Item::with(flex::Region::named("sidebar_right"))
.with_id("sidebar_right")
.with_grow(flex::Grow::Is1),
),
)
.with_id("flex__wrapper"),
Body::with(
flex::Container::new()
.with_id("body__wrapper")
.with_direction(flex::Direction::Column(BreakPoint::None))
.with_align(flex::Align::Center)
.add_item(flex::Item::region().with_id("header"))
.add_item(flex::Item::region().with_id("pagetop"))
.add_item(
flex::Item::with(
flex::Container::new()
.with_direction(flex::Direction::Row(BreakPoint::None))
.add_item(
flex::Item::region()
.with_id("sidebar_left")
.with_grow(flex::Grow::Is1),
)
.add_item(
flex::Item::region()
.with_id("content")
.with_grow(flex::Grow::Is3),
)
.add_item(
flex::Item::region()
.with_id("sidebar_right")
.with_grow(flex::Grow::Is1),
),
)
.add_item(flex::Item::with(flex::Region::named("footer")).with_id("footer")),
.with_id("flex__wrapper"),
)
)
.render(page.context())
.add_item(flex::Item::region().with_id("footer")),
)
.render(page.context())
}
fn after_prepare_body(&self, page: &mut Page) {