🔥 Remove body and region, not context-specific

This commit is contained in:
Manuel Cillero 2024-04-23 21:40:29 +02:00
parent ece78b2b4a
commit 2d906621de
6 changed files with 76 additions and 170 deletions

View file

@ -3,9 +3,3 @@ pub use html::Html;
mod fluent;
pub use fluent::Fluent;
mod body;
pub use body::Body;
mod region;
pub use region::Region;

View file

@ -1,51 +0,0 @@
use crate::prelude::*;
#[derive(AutoDefault)]
pub struct Body(MixedComponents);
impl ComponentTrait for Body {
fn new() -> Self {
Body::default()
}
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
let skip_to_id = cx.body_skip_to().get().unwrap_or("content".to_owned());
PrepareMarkup::With(html! {
body id=[cx.body_id().get()] class=[cx.body_classes().get()] {
@if let Some(skip) = L10n::l("skip_to_content").using(cx.langid()) {
div class="skip__to_content" {
a href=(concat_string!("#", skip_to_id)) { (skip) }
}
}
(self.components().render(cx))
}
})
}
}
impl Body {
pub fn with(component: impl ComponentTrait) -> Self {
Body::default().add_component(component)
}
// Body BUILDER.
#[fn_builder]
pub fn alter_components(&mut self, op: AnyOp) -> &mut Self {
self.0.alter_value(op);
self
}
#[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.0.alter_value(AnyOp::Add(AnyComponent::with(component)));
self
}
// Body GETTERS.
pub fn components(&self) -> &MixedComponents {
&self.0
}
}

View file

@ -1,31 +0,0 @@
use crate::prelude::*;
#[derive(AutoDefault)]
pub struct Region(OptionId);
impl ComponentTrait for Region {
fn new() -> Self {
Region::default()
}
fn id(&self) -> Option<String> {
self.0.get()
}
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
match self.id() {
Some(id) => PrepareMarkup::With(cx.prepare_region(id)),
_ => PrepareMarkup::None,
}
}
}
impl Region {
// Region BUILDER.
#[fn_builder]
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.0.alter_value(id);
self
}
}

View file

@ -5,7 +5,6 @@ use crate::core::theme::all::{theme_by_short_name, THEME_DEFAULT};
use crate::core::theme::{ComponentsInRegions, ThemeRef};
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::util::TypeInfo;
@ -43,9 +42,6 @@ pub struct Context {
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,
@ -63,9 +59,6 @@ impl Context {
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,
@ -104,21 +97,6 @@ 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
@ -152,18 +130,6 @@ 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
}

View file

@ -1,10 +1,10 @@
use crate::base::component::*;
use crate::config;
use crate::core::component::{ComponentBase, ComponentTrait};
use crate::core::package::PackageTrait;
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,38 +26,47 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
fn before_prepare_body(&self, page: &mut Page) {}
fn prepare_body(&self, page: &mut Page) -> Markup {
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),
),
let skip_to_id = page.body_skip_to().get().unwrap_or("content".to_owned());
html! {
body id=[page.body_id().get()] class=[page.body_classes().get()] {
@if let Some(skip) = L10n::l("skip_to_content").using(page.context().langid()) {
div class="skip__to_content" {
a href=(concat_string!("#", skip_to_id)) { (skip) }
}
}
(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),
),
)
.with_id("flex__wrapper"),
)
.with_id("flex__wrapper"),
)
.add_item(flex::Item::region().with_id("footer")),
)
.render(page.context())
.add_item(flex::Item::region().with_id("footer"))
.render(page.context()))
}
}
}
fn after_prepare_body(&self, page: &mut Page) {

View file

@ -7,7 +7,8 @@ use crate::base::action;
use crate::core::component::{AnyComponent, AnyOp, ComponentTrait};
use crate::core::component::{AssetsOp, Context};
use crate::fn_builder;
use crate::html::{html, ClassesOp, Favicon, Markup, OptionTranslated, DOCTYPE};
use crate::html::{html, Favicon, Markup, DOCTYPE};
use crate::html::{ClassesOp, OptionClasses, OptionId, OptionTranslated};
use crate::locale::L10n;
use crate::service::HttpRequest;
@ -15,24 +16,30 @@ use unic_langid::CharacterDirection;
#[rustfmt::skip]
pub struct Page {
title : OptionTranslated,
description: OptionTranslated,
metadata : Vec<(&'static str, &'static str)>,
properties : Vec<(&'static str, &'static str)>,
favicon : Option<Favicon>,
context : Context,
title : OptionTranslated,
description : OptionTranslated,
metadata : Vec<(&'static str, &'static str)>,
properties : Vec<(&'static str, &'static str)>,
favicon : Option<Favicon>,
context : Context,
body_id : OptionId,
body_classes: OptionClasses,
body_skip_to: OptionId,
}
impl Page {
#[rustfmt::skip]
pub fn new(request: HttpRequest) -> Self {
Page {
title : OptionTranslated::default(),
description: OptionTranslated::default(),
metadata : Vec::default(),
properties : Vec::default(),
favicon : None,
context : Context::new(request),
title : OptionTranslated::default(),
description : OptionTranslated::default(),
metadata : Vec::default(),
properties : Vec::default(),
favicon : None,
context : Context::new(request),
body_id : OptionId::default(),
body_classes: OptionClasses::default(),
body_skip_to: OptionId::default(),
}
}
@ -76,19 +83,19 @@ impl Page {
#[fn_builder]
pub fn alter_body_id(&mut self, id: impl Into<String>) -> &mut Self {
self.context.alter_body_id(id);
self.body_id.alter_value(id);
self
}
#[fn_builder]
pub fn alter_body_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.context.alter_body_classes(op, classes);
self.body_classes.alter_value(op, classes);
self
}
#[fn_builder]
pub fn alter_body_skip_to(&mut self, id: impl Into<String>) -> &mut Self {
self.context.alter_body_skip_to(id);
self.body_skip_to.alter_value(id);
self
}
@ -146,6 +153,18 @@ impl Page {
&mut self.context
}
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
}
// Page RENDER.
pub fn render(&mut self) -> ResultPage<Markup, ErrorPage> {