From e011c9269a9b0b61234d778306548a226785b0d0 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Thu, 14 Jul 2022 22:44:51 +0200 Subject: [PATCH] =?UTF-8?q?Elimina=20la=20asignaci=C3=B3n=20de=20m=C3=A1rg?= =?UTF-8?q?enes/espaciado?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop-admin/src/summary.rs | 2 +- pagetop/src/base/component/anchor.rs | 17 --- pagetop/src/base/component/grid/column.rs | 18 +-- pagetop/src/base/component/grid/row.rs | 18 +-- pagetop/src/base/component/icon.rs | 22 +-- pagetop/src/base/component/menu.rs | 14 +- pagetop/src/base/module/demopage.rs | 45 ------ pagetop/src/base/theme/aliner.rs | 10 +- pagetop/src/base/theme/bootsier.rs | 16 +-- pagetop/src/base/theme/bulmix.rs | 14 +- pagetop/src/core/component.rs | 2 +- pagetop/src/core/component/context.rs | 69 ++++----- pagetop/src/core/theme/definition.rs | 8 +- pagetop/src/html.rs | 6 - pagetop/src/html/classes.rs | 2 +- pagetop/src/html/layout.rs | 165 ---------------------- pagetop/src/html/unit.rs | 52 ------- pagetop/src/response/page/definition.rs | 98 +++++++++---- 18 files changed, 142 insertions(+), 436 deletions(-) delete mode 100644 pagetop/src/html/layout.rs delete mode 100644 pagetop/src/html/unit.rs diff --git a/pagetop-admin/src/summary.rs b/pagetop-admin/src/summary.rs index 77a192b3..ffb1d014 100644 --- a/pagetop-admin/src/summary.rs +++ b/pagetop-admin/src/summary.rs @@ -34,7 +34,7 @@ pub async fn summary() -> app::Result { Page::new() - .using_theme("Bootsier") + .with_context(InContextOp::SetTheme("Bootsier")) .with_title("Admin") diff --git a/pagetop/src/base/component/anchor.rs b/pagetop/src/base/component/anchor.rs index b62676d7..25c9a33e 100644 --- a/pagetop/src/base/component/anchor.rs +++ b/pagetop/src/base/component/anchor.rs @@ -23,7 +23,6 @@ pub struct Anchor { weight : isize, id : IdentifierValue, classes : Classes, - layout : InlineLayout, anchor_type: AnchorType, href : AttributeValue, html : Markup, @@ -40,7 +39,6 @@ impl ComponentTrait for Anchor { weight : 0, id : IdentifierValue::new(), classes : Classes::new(), - layout : InlineLayout::new(), anchor_type: AnchorType::Link, href : AttributeValue::new(), html : html! {}, @@ -75,7 +73,6 @@ impl ComponentTrait for Anchor { a id=[self.id().get()] class=[self.classes().get()] - style=[self.layout().get()] href=[self.href().get()] target=[target] { @@ -130,11 +127,6 @@ impl Anchor { self } - pub fn with_layout(mut self, layout: &[LayoutSet]) -> Self { - self.alter_layout(layout); - self - } - pub fn with_type(mut self, anchor_type: AnchorType) -> Self { self.alter_type(anchor_type); self @@ -192,11 +184,6 @@ impl Anchor { self } - pub fn alter_layout(&mut self, layout: &[LayoutSet]) -> &mut Self { - self.layout.set(layout); - self - } - pub fn alter_type(&mut self, anchor_type: AnchorType) -> &mut Self { self.anchor_type = anchor_type; self.classes.alter(ClassesOp::SetDefault, match self.anchor_type { @@ -248,10 +235,6 @@ impl Anchor { &self.classes } - pub fn layout(&self) -> &InlineLayout { - &self.layout - } - pub fn anchor_type(&self) -> &AnchorType { &self.anchor_type } diff --git a/pagetop/src/base/component/grid/column.rs b/pagetop/src/base/component/grid/column.rs index 127a6d33..6fbc1989 100644 --- a/pagetop/src/base/component/grid/column.rs +++ b/pagetop/src/base/component/grid/column.rs @@ -22,7 +22,6 @@ pub struct Column { weight : isize, id : IdentifierValue, classes : Classes, - layout : InlineLayout, size : ColumnSize, components: ComponentsBundle, template : String, @@ -35,7 +34,6 @@ impl ComponentTrait for Column { weight : 0, id : IdentifierValue::new(), classes : Classes::new(), - layout : InlineLayout::new(), size : ColumnSize::Default, components: ComponentsBundle::new(), template : "default".to_owned(), @@ -74,7 +72,7 @@ impl ComponentTrait for Column { fn default_render(&self, context: &mut InContext) -> Markup { html! { - div id=[self.id().get()] class=[self.classes().get()] style=[self.layout().get()] { + div id=[self.id().get()] class=[self.classes().get()] { (self.components().render(context)) } } @@ -113,11 +111,6 @@ impl Column { self } - pub fn with_layout(mut self, layout: &[LayoutSet]) -> Self { - self.alter_layout(layout); - self - } - pub fn with_size(mut self, size: ColumnSize) -> Self { self.alter_size(size); self @@ -155,11 +148,6 @@ impl Column { self } - pub fn alter_layout(&mut self, layout: &[LayoutSet]) -> &mut Self { - self.layout.set(layout); - self - } - pub fn alter_size(&mut self, size: ColumnSize) -> &mut Self { self.size = size; self @@ -184,10 +172,6 @@ impl Column { &self.classes } - pub fn layout(&self) -> &InlineLayout { - &self.layout - } - pub fn size(&self) -> &ColumnSize { &self.size } diff --git a/pagetop/src/base/component/grid/row.rs b/pagetop/src/base/component/grid/row.rs index ce91033d..2681ae9b 100644 --- a/pagetop/src/base/component/grid/row.rs +++ b/pagetop/src/base/component/grid/row.rs @@ -7,7 +7,6 @@ pub struct Row { weight : isize, id : IdentifierValue, classes : Classes, - layout : InlineLayout, columns : ComponentsBundle, template : String, } @@ -19,7 +18,6 @@ impl ComponentTrait for Row { weight : 0, id : IdentifierValue::new(), classes : Classes::new_with_default("row"), - layout : InlineLayout::new(), columns : ComponentsBundle::new(), template : "default".to_owned(), } @@ -39,7 +37,7 @@ impl ComponentTrait for Row { fn default_render(&self, context: &mut InContext) -> Markup { html! { - div id=[self.id().get()] class=[self.classes().get()] style=[self.layout().get()] { + div id=[self.id().get()] class=[self.classes().get()] { (self.columns().render(context)) } } @@ -78,11 +76,6 @@ impl Row { self } - pub fn with_layout(mut self, layout: &[LayoutSet]) -> Self { - self.alter_layout(layout); - self - } - pub fn with_column(mut self, column: grid::Column) -> Self { self.alter_column(column); self @@ -115,11 +108,6 @@ impl Row { self } - pub fn alter_layout(&mut self, layout: &[LayoutSet]) -> &mut Self { - self.layout.set(layout); - self - } - pub fn alter_column(&mut self, column: grid::Column) -> &mut Self { self.columns.add(column); self @@ -140,10 +128,6 @@ impl Row { &self.classes } - pub fn layout(&self) -> &InlineLayout { - &self.layout - } - pub fn columns(&self) -> &ComponentsBundle { &self.columns } diff --git a/pagetop/src/base/component/icon.rs b/pagetop/src/base/component/icon.rs index 00b919b1..e3f2ede0 100644 --- a/pagetop/src/base/component/icon.rs +++ b/pagetop/src/base/component/icon.rs @@ -7,7 +7,6 @@ pub struct Icon { weight : isize, icon_name : String, classes : Classes, - layout : InlineLayout, } impl ComponentTrait for Icon { @@ -17,7 +16,6 @@ impl ComponentTrait for Icon { weight : 0, icon_name : "question-circle-fill".to_owned(), classes : Classes::new(), - layout : InlineLayout::new(), } } @@ -35,16 +33,16 @@ impl ComponentTrait for Icon { fn before_render(&mut self, context: &mut InContext) { context - .with_stylesheet(AssetsOp::Add( + .alter(InContextOp::StyleSheet(AssetsOp::Add( StyleSheet::located("/theme/icons/bootstrap-icons.css") .with_version("1.8.2") - )); + ))); self.alter_classes(ClassesOp::SetDefault, concat_string!("bi-", self.icon_name()).as_str()); } fn default_render(&self, _context: &mut InContext) -> Markup { - html! { i class=[self.classes().get()] style=[self.layout().get()] {}; } + html! { i class=[self.classes().get()] {}; } } fn as_ref_any(&self) -> &dyn AnyComponent { @@ -83,11 +81,6 @@ impl Icon { self } - pub fn with_layout(mut self, layout: &[LayoutSet]) -> Self { - self.alter_layout(layout); - self - } - // Icon ALTER. pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { @@ -110,11 +103,6 @@ impl Icon { self } - pub fn alter_layout(&mut self, layout: &[LayoutSet]) -> &mut Self { - self.layout.set(layout); - self - } - // Icon GETTERS. pub fn icon_name(&self) -> &str { @@ -124,8 +112,4 @@ impl Icon { pub fn classes(&self) -> &Classes { &self.classes } - - pub fn layout(&self) -> &InlineLayout { - &self.layout - } } diff --git a/pagetop/src/base/component/menu.rs b/pagetop/src/base/component/menu.rs index fe0689eb..c0c8a0b3 100644 --- a/pagetop/src/base/component/menu.rs +++ b/pagetop/src/base/component/menu.rs @@ -208,19 +208,19 @@ impl ComponentTrait for Menu { fn before_render(&mut self, context: &mut InContext) { context - .with_stylesheet(AssetsOp::Add( + .alter(InContextOp::StyleSheet(AssetsOp::Add( StyleSheet::located("/theme/menu/css/menu.css") .with_version("1.1.1") - )) - .with_stylesheet(AssetsOp::Add( + ))) + .alter(InContextOp::StyleSheet(AssetsOp::Add( StyleSheet::located("/theme/menu/css/menu-clean.css") .with_version("1.1.1") - )) - .with_javascript(AssetsOp::Add( + ))) + .alter(InContextOp::JavaScript(AssetsOp::Add( JavaScript::located("/theme/menu/js/menu.min.js") .with_version("1.1.1") - )) - .add_jquery(); + ))) + .alter(InContextOp::AddJQuery); } fn default_render(&self, context: &mut InContext) -> Markup { diff --git a/pagetop/src/base/module/demopage.rs b/pagetop/src/base/module/demopage.rs index 7c1eb22a..ac9e120e 100644 --- a/pagetop/src/base/module/demopage.rs +++ b/pagetop/src/base/module/demopage.rs @@ -39,16 +39,7 @@ fn hello_world() -> Container { Container::header() .with_id("hello-world") .with_component(grid::Row::new() - .with_layout(&[ - LayoutSet::PaddingTop(UnitValue::RelEm(2.0)), - LayoutSet::PaddingBottom(UnitValue::RelEm(2.0)), - LayoutSet::PaddingLeft(UnitValue::RelPct(2.0)), - ]) .with_column(grid::Column::new() - .with_layout(&[ - LayoutSet::PaddingTop(UnitValue::RelEm(2.0)), - LayoutSet::PaddingBottom(UnitValue::RelEm(2.0)), - ]) .with_size(grid::ColumnSize::Is4of12) .with_component(Heading::h1(html! { (l("page_title")) @@ -72,10 +63,6 @@ fn hello_world() -> Container { ("Offered services") }) .with_left_icon(Icon::with("card-checklist")) - .with_layout(&[ - LayoutSet::PaddingSide(UnitValue::UnSet, UnitValue::RelEm(1.5)), - LayoutSet::RadiusAll(UnitValue::RelEm(1.5)), - ]) ) .with_component(Anchor::button("#", html! { ("Get quote") @@ -93,19 +80,11 @@ fn welcome() -> Container { Container::new() .with_id("visiting") .with_component(grid::Row::new() - .with_layout(&[ - LayoutSet::PaddingSide(UnitValue::RelEm(1.0), UnitValue::RelPct(5.0)) - ]) .with_column(grid::Column::new() - .with_layout(&[LayoutSet::PaddingAll(UnitValue::RelPct(2.0))]) .with_size(grid::ColumnSize::Is5of12) .with_component(Image::image("/theme/images/demo-visiting.svg")) ) .with_column(grid::Column::new() - .with_layout(&[ - LayoutSet::PaddingTop(UnitValue::RelPct(2.5)), - LayoutSet::PaddingLeft(UnitValue::RelPct(5.0)), - ]) .with_component(Heading::h2(html! { (t("welcome_to", &args!["app" => SETTINGS.app.name.as_str()])) })) @@ -128,14 +107,7 @@ fn about_pagetop() -> Container { Container::new() .with_id("pagetop") .with_component(grid::Row::new() - .with_layout(&[ - LayoutSet::PaddingSide(UnitValue::RelEm(1.0), UnitValue::RelPct(5.0)) - ]) .with_column(grid::Column::new() - .with_layout(&[ - LayoutSet::PaddingTop(UnitValue::RelPct(2.5)), - LayoutSet::PaddingLeft(UnitValue::RelPct(5.0)), - ]) .with_size(grid::ColumnSize::Is7of12) .with_component(Heading::h2(html! { (l("pagetop_title")) @@ -156,7 +128,6 @@ fn about_pagetop() -> Container { ) ) .with_column(grid::Column::new() - .with_layout(&[LayoutSet::PaddingAll(UnitValue::RelPct(2.0))]) .with_component(Image::image("/theme/images/demo-pagetop.svg")) ) ) @@ -166,19 +137,11 @@ fn promo_pagetop() -> Container { Container::new() .with_id("promo") .with_component(grid::Row::new() - .with_layout(&[ - LayoutSet::PaddingSide(UnitValue::RelEm(1.0), UnitValue::RelPct(5.0)) - ]) .with_column(grid::Column::new() - .with_layout(&[LayoutSet::PaddingAll(UnitValue::RelPct(2.0))]) .with_size(grid::ColumnSize::Is5of12) .with_component(Image::image("/theme/images/demo-pagetop.svg")) ) .with_column(grid::Column::new() - .with_layout(&[ - LayoutSet::PaddingTop(UnitValue::RelPct(2.5)), - LayoutSet::PaddingLeft(UnitValue::RelPct(5.0)), - ]) .with_component(Heading::h2(html! { (l("pagetop_promo_title")) }) @@ -198,14 +161,7 @@ fn reporting_problems() -> Container { Container::new() .with_id("reporting") .with_component(grid::Row::new() - .with_layout(&[ - LayoutSet::PaddingSide(UnitValue::RelEm(1.0), UnitValue::RelPct(5.0)) - ]) .with_column(grid::Column::new() - .with_layout(&[ - LayoutSet::PaddingTop(UnitValue::RelPct(2.5)), - LayoutSet::PaddingLeft(UnitValue::RelPct(5.0)), - ]) .with_size(grid::ColumnSize::Is7of12) .with_component(Heading::h2(html! { (l("report_problems_title")) @@ -222,7 +178,6 @@ fn reporting_problems() -> Container { ) ) .with_column(grid::Column::new() - .with_layout(&[LayoutSet::PaddingAll(UnitValue::RelPct(2.0))]) .with_component(Image::image("/theme/images/demo-pagetop.svg")) ) ) diff --git a/pagetop/src/base/theme/aliner.rs b/pagetop/src/base/theme/aliner.rs index 10ff3379..e7a76922 100644 --- a/pagetop/src/base/theme/aliner.rs +++ b/pagetop/src/base/theme/aliner.rs @@ -16,15 +16,15 @@ impl ThemeTrait for Aliner { } fn before_render_page(&self, page: &mut Page) { - page.context() - .with_favicon(Some(Favicon::new() + page + .alter_context(InContextOp::Favicon(Some(Favicon::new() .with_icon("/theme/favicon.png") - )) - .with_stylesheet(AssetsOp::Add( + ))) + .alter_context(InContextOp::StyleSheet(AssetsOp::Add( StyleSheet::located( "/aliner/css/styles.css" ) .with_weight(-99) - )); + ))); } } diff --git a/pagetop/src/base/theme/bootsier.rs b/pagetop/src/base/theme/bootsier.rs index ed9d431e..582a5499 100644 --- a/pagetop/src/base/theme/bootsier.rs +++ b/pagetop/src/base/theme/bootsier.rs @@ -18,21 +18,21 @@ impl ThemeTrait for Bootsier { } fn before_render_page(&self, page: &mut Page) { - page.context() - .with_favicon(Some(Favicon::new() + page + .alter_context(InContextOp::Favicon(Some(Favicon::new() .with_icon("/theme/favicon.png") - )) - .with_stylesheet(AssetsOp::Add( + ))) + .alter_context(InContextOp::StyleSheet(AssetsOp::Add( StyleSheet::located("/bootsier/css/bootstrap.min.css") .with_version("5.1.3") .with_weight(-99) - )) - .with_javascript(AssetsOp::Add( + ))) + .alter_context(InContextOp::JavaScript(AssetsOp::Add( JavaScript::located("/bootsier/js/bootstrap.bundle.min.js") .with_version("5.1.3") .with_weight(-99) - )) - .add_jquery(); + ))) + .alter_context(InContextOp::AddJQuery); } fn render_error_page(&self, mut s: app::http::StatusCode) -> app::Result { diff --git a/pagetop/src/base/theme/bulmix.rs b/pagetop/src/base/theme/bulmix.rs index 23546c61..e46d601a 100644 --- a/pagetop/src/base/theme/bulmix.rs +++ b/pagetop/src/base/theme/bulmix.rs @@ -16,16 +16,16 @@ impl ThemeTrait for Bulmix { } fn before_render_page(&self, page: &mut Page) { - page.context() - .with_favicon(Some(Favicon::new() + page + .alter_context(InContextOp::Favicon(Some(Favicon::new() .with_icon("/theme/favicon.png") - )) - .with_stylesheet(AssetsOp::Add( + ))) + .alter_context(InContextOp::StyleSheet(AssetsOp::Add( StyleSheet::located("/bulmix/css/bulma.min.css") .with_version("0.9.4") .with_weight(-99) - )) - .add_jquery(); + ))) + .alter_context(InContextOp::AddJQuery); } fn before_render_component( @@ -100,7 +100,7 @@ impl ThemeTrait for Bulmix { let icon = component_ref::(component); Some(html! { span class="icon" { - i class=[icon.classes().get()] style=[icon.layout().get()] {}; + i class=[icon.classes().get()] {}; } }) }, diff --git a/pagetop/src/core/component.rs b/pagetop/src/core/component.rs index ef3e33d5..3e3c9dec 100644 --- a/pagetop/src/core/component.rs +++ b/pagetop/src/core/component.rs @@ -5,7 +5,7 @@ pub use hook::{ }; mod context; -pub use context::InContext; +pub use context::{InContext, InContextOp}; mod definition; pub use definition::{ diff --git a/pagetop/src/core/component/context.rs b/pagetop/src/core/component/context.rs index ef309f01..3f9de74a 100644 --- a/pagetop/src/core/component/context.rs +++ b/pagetop/src/core/component/context.rs @@ -11,6 +11,15 @@ static DEFAULT_THEME: Lazy<&dyn ThemeTrait> = Lazy::new(|| { } }); +pub enum InContextOp { + AddMetadata(&'static str, &'static str), + Favicon(Option), + SetTheme(&'static str), + StyleSheet(AssetsOp), + JavaScript(AssetsOp), + AddJQuery, +} + pub struct InContext { theme : &'static dyn ThemeTrait, favicon : Option, @@ -34,40 +43,32 @@ impl InContext { } } - pub fn using_theme(&mut self, theme_name: &str) -> &mut Self { - self.theme = theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME); - self - } - - pub fn with_favicon(&mut self, favicon: Option) -> &mut Self { - self.favicon = favicon; - self - } - - pub fn add_metadata(&mut self, name: String, content: String) -> &mut Self { - self.metadata.push((name, content)); - self - } - - pub fn with_stylesheet(&mut self, css: AssetsOp) -> &mut Self { - self.stylesheets.alter(css); - self - } - - pub fn with_javascript(&mut self, js: AssetsOp) -> &mut Self { - self.javascripts.alter(js); - self - } - - pub fn add_jquery(&mut self) -> &mut Self { - if !self.with_jquery { - self.with_javascript(AssetsOp::Add( - JavaScript::located("/theme/js/jquery.min.js") - .with_version("3.6.0") - .with_weight(isize::MIN) - .with_mode(JSMode::Normal) - )); - self.with_jquery = true; + pub fn alter(&mut self, op: InContextOp) -> &mut Self { + match op { + InContextOp::AddMetadata(name, content) => { + self.metadata.push((name.to_owned(), content.to_owned())); + }, + InContextOp::Favicon(favicon) => { + self.favicon = favicon; + }, + InContextOp::SetTheme(theme_name) => { + self.theme = theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME); + }, + InContextOp::StyleSheet(css) => { + self.stylesheets.alter(css); + }, + InContextOp::JavaScript(js) => { + self.javascripts.alter(js); + }, + InContextOp::AddJQuery => if !self.with_jquery { + self.javascripts.alter(AssetsOp::Add( + JavaScript::located("/theme/js/jquery.min.js") + .with_version("3.6.0") + .with_weight(isize::MIN) + .with_mode(JSMode::Normal) + )); + self.with_jquery = true; + }, } self } diff --git a/pagetop/src/core/theme/definition.rs b/pagetop/src/core/theme/definition.rs index 5d784875..8b63d833 100644 --- a/pagetop/src/core/theme/definition.rs +++ b/pagetop/src/core/theme/definition.rs @@ -1,7 +1,7 @@ use crate::{app, concat_string, util}; use crate::config::SETTINGS; use crate::html::{Favicon, Markup, html}; -use crate::core::component::{ComponentTrait, InContext}; +use crate::core::component::{ComponentTrait, InContext, InContextOp}; use crate::response::page::Page; use crate::base::component::Chunck; @@ -27,10 +27,10 @@ pub trait ThemeTrait: BaseTheme + Send + Sync { #[allow(unused_variables)] fn before_render_page(&self, page: &mut Page) { - page.context() - .with_favicon(Some(Favicon::new() + page + .alter_context(InContextOp::Favicon(Some(Favicon::new() .with_icon("/theme/favicon.png") - )); + ))); } fn render_page_head(&self, page: &mut Page) -> Markup { diff --git a/pagetop/src/html.rs b/pagetop/src/html.rs index 4e9d3355..4fa94ebb 100644 --- a/pagetop/src/html.rs +++ b/pagetop/src/html.rs @@ -16,9 +16,3 @@ pub use identifier::IdentifierValue; mod classes; pub use classes::{Classes, ClassesOp, ClassValue}; - -mod unit; -pub use unit::UnitValue; - -mod layout; -pub use layout::{InlineLayout, LayoutSet}; diff --git a/pagetop/src/html/classes.rs b/pagetop/src/html/classes.rs index 4268220c..197f1502 100644 --- a/pagetop/src/html/classes.rs +++ b/pagetop/src/html/classes.rs @@ -33,7 +33,7 @@ impl Classes { classes } - pub fn alter(&mut self, op: ClassesOp, classes: &str) -> &Self { + pub fn alter(&mut self, op: ClassesOp, classes: &str) -> &mut Self { let classes = classes.trim(); match op { ClassesOp::Add => { diff --git a/pagetop/src/html/layout.rs b/pagetop/src/html/layout.rs deleted file mode 100644 index b94a10fa..00000000 --- a/pagetop/src/html/layout.rs +++ /dev/null @@ -1,165 +0,0 @@ -use crate::concat_string; - -use super::unit::UnitValue; - -const RADIUS_BOTTOM_LEFT: &str = "border-bottom-left-radius"; -const RADIUS_BOTTOM_RIGHT: &str = "border-bottom-right-radius"; -const RADIUS_TOP_LEFT: &str = "border-top-left-radius"; -const RADIUS_TOP_RIGHT: &str = "border-top-right-radius"; - -const MARGIN_BOTTOM: &str = "margin-bottom"; -const MARGIN_LEFT: &str = "margin-left"; -const MARGIN_RIGHT: &str = "margin-right"; -const MARGIN_TOP: &str = "margin-top"; - -const PADDING_BOTTOM: &str = "padding-bottom"; -const PADDING_LEFT: &str = "padding-left"; -const PADDING_RIGHT: &str = "padding-right"; -const PADDING_TOP: &str = "padding-top"; - -pub enum LayoutSet { - Margin(UnitValue, UnitValue, UnitValue, UnitValue), - MarginAll(UnitValue), - MarginSide(UnitValue, UnitValue), - MarginBottom(UnitValue), - MarginLeft(UnitValue), - MarginRight(UnitValue), - MarginTop(UnitValue), - - Padding(UnitValue, UnitValue, UnitValue, UnitValue), - PaddingAll(UnitValue), - PaddingSide(UnitValue, UnitValue), - PaddingBottom(UnitValue), - PaddingLeft(UnitValue), - PaddingRight(UnitValue), - PaddingTop(UnitValue), - - Radius(UnitValue, UnitValue, UnitValue, UnitValue), - RadiusAll(UnitValue), - RadiusBottomLeft(UnitValue), - RadiusBottomRight(UnitValue), - RadiusTopLeft(UnitValue), - RadiusTopRight(UnitValue), -} - -impl LayoutSet { - fn set(&self, into_spaces: &mut InlineLayout) { - match self { - - // MARGIN LAYOUT. - LayoutSet::Margin(top, right, bottom, left) => { - self.add(MARGIN_TOP, top, into_spaces); - self.add(MARGIN_RIGHT, right, into_spaces); - self.add(MARGIN_BOTTOM, bottom, into_spaces); - self.add(MARGIN_LEFT, left, into_spaces); - }, - LayoutSet::MarginAll(val) => { - self.add(MARGIN_TOP, val, into_spaces); - self.add(MARGIN_RIGHT, val, into_spaces); - self.add(MARGIN_BOTTOM, val, into_spaces); - self.add(MARGIN_LEFT, val, into_spaces); - }, - LayoutSet::MarginSide(top_bottom, right_left) => { - self.add(MARGIN_TOP, top_bottom, into_spaces); - self.add(MARGIN_RIGHT, right_left, into_spaces); - self.add(MARGIN_BOTTOM, top_bottom, into_spaces); - self.add(MARGIN_LEFT, right_left, into_spaces); - }, - LayoutSet::MarginTop(val) => self.add(MARGIN_TOP, val, into_spaces), - LayoutSet::MarginRight(val) => self.add(MARGIN_RIGHT, val, into_spaces), - LayoutSet::MarginBottom(val) => self.add(MARGIN_BOTTOM, val, into_spaces), - LayoutSet::MarginLeft(val) => self.add(MARGIN_LEFT, val, into_spaces), - - // PADDING LAYOUT. - LayoutSet::Padding(top, right, bottom, left) => { - self.add(PADDING_TOP, top, into_spaces); - self.add(PADDING_RIGHT, right, into_spaces); - self.add(PADDING_BOTTOM, bottom, into_spaces); - self.add(PADDING_LEFT, left, into_spaces); - }, - LayoutSet::PaddingAll(val) => { - self.add(PADDING_TOP, val, into_spaces); - self.add(PADDING_RIGHT, val, into_spaces); - self.add(PADDING_BOTTOM, val, into_spaces); - self.add(PADDING_LEFT, val, into_spaces); - }, - LayoutSet::PaddingSide(top_bottom, right_left) => { - self.add(PADDING_TOP, top_bottom, into_spaces); - self.add(PADDING_RIGHT, right_left, into_spaces); - self.add(PADDING_BOTTOM, top_bottom, into_spaces); - self.add(PADDING_LEFT, right_left, into_spaces); - }, - LayoutSet::PaddingTop(val) => self.add(PADDING_TOP, val, into_spaces), - LayoutSet::PaddingRight(val) => self.add(PADDING_RIGHT, val, into_spaces), - LayoutSet::PaddingBottom(val) => self.add(PADDING_BOTTOM, val, into_spaces), - LayoutSet::PaddingLeft(val) => self.add(PADDING_LEFT, val, into_spaces), - - // BORDER RADIUS LAYOUT. - LayoutSet::Radius(top_left, top_right, bottom_right, bottom_left) => { - self.add(RADIUS_TOP_LEFT, top_left, into_spaces); - self.add(RADIUS_TOP_RIGHT, top_right, into_spaces); - self.add(RADIUS_BOTTOM_RIGHT, bottom_right, into_spaces); - self.add(RADIUS_BOTTOM_LEFT, bottom_left, into_spaces); - }, - LayoutSet::RadiusAll(val) => { - self.add(RADIUS_TOP_LEFT, val, into_spaces); - self.add(RADIUS_TOP_RIGHT, val, into_spaces); - self.add(RADIUS_BOTTOM_RIGHT, val, into_spaces); - self.add(RADIUS_BOTTOM_LEFT, val, into_spaces); - }, - LayoutSet::RadiusTopLeft(val) => self.add(RADIUS_TOP_LEFT, val, into_spaces), - LayoutSet::RadiusTopRight(val) => self.add(RADIUS_TOP_RIGHT, val, into_spaces), - LayoutSet::RadiusBottomRight(val) => self.add(RADIUS_BOTTOM_RIGHT, val, into_spaces), - LayoutSet::RadiusBottomLeft(val) => self.add(RADIUS_BOTTOM_LEFT, val, into_spaces), - } - } - - fn add(&self, property: &str, value: &UnitValue, into_spaces: &mut InlineLayout) { - let val = value.to_string(); - let style = InlineProperty { - property: property.to_owned(), - inline : concat_string!(property, ":", val, ";"), - }; - match into_spaces.0.iter().position(|s| s.property.eq(&style.property)) { - Some(pos) => { - into_spaces.0.remove(pos); - if !val.is_empty() { - into_spaces.0.insert(pos, style); - } - }, - _ => if !val.is_empty() { - into_spaces.0.push(style) - } - } - } -} - -struct InlineProperty { - property: String, - inline : String, -} - -pub struct InlineLayout(Vec); - -impl InlineLayout { - pub fn new() -> Self { - InlineLayout(Vec::new()) - } - - pub fn set(&mut self, layout: &[LayoutSet]) -> &Self { - for i in 0..layout.len() { - layout[i].set(self); - } - self - } - - pub fn get(&self) -> Option { - if self.0.len() == 0 { - None - } else { - let mut inline = "".to_owned(); - self.0.iter().for_each(|s| inline.push_str(s.inline.as_str())); - Some(inline) - } - } -} diff --git a/pagetop/src/html/unit.rs b/pagetop/src/html/unit.rs deleted file mode 100644 index 61641184..00000000 --- a/pagetop/src/html/unit.rs +++ /dev/null @@ -1,52 +0,0 @@ -use crate::concat_string; - -// About pixels: Pixels (px) are relative to the viewing device. For low-dpi -// devices, 1px is one device pixel (dot) of the display. For printers and high -// resolution screens 1px implies multiple device pixels. - -// About em: 2em means 2 times the size of the current font. The em and rem -// units are practical in creating perfectly scalable layout! - -// About viewport: If the browser window size is 50cm wide, 1vw = 0.5cm. - -pub enum UnitValue { - Auto, - - Cm(isize), // Centimeters. - In(isize), // Inches (1in = 96px = 2.54cm). - Mm(isize), // Millimeters. - Pc(isize), // Picas (1pc = 12pt). - Pt(isize), // Points (1pt = 1/72 of 1in). - Px(isize), // Pixels (1px = 1/96th of 1in). - - RelEm(f32), // Relative to the font-size of the element. - RelPct(f32), // Percentage relative to the parent element. - RelRem(f32), // Relative to font-size of the root element. - RelVh(f32), // Relative to 1% of the height of the viewport. - RelVw(f32), // Relative to 1% of the value of the viewport. - - UnSet, -} - -impl ToString for UnitValue { - fn to_string(&self) -> String { - match self { - UnitValue::Auto => "auto".to_owned(), - // Absolute value. - UnitValue::Cm(aw) => concat_string!(aw.to_string(), "cm"), - UnitValue::In(aw) => concat_string!(aw.to_string(), "in"), - UnitValue::Mm(aw) => concat_string!(aw.to_string(), "mm"), - UnitValue::Pc(aw) => concat_string!(aw.to_string(), "pc"), - UnitValue::Pt(aw) => concat_string!(aw.to_string(), "pt"), - UnitValue::Px(aw) => concat_string!(aw.to_string(), "px"), - // Relative value. - UnitValue::RelEm(rw) => concat_string!(rw.to_string(), "em"), - UnitValue::RelPct(rw) => concat_string!(rw.to_string(), "%"), - UnitValue::RelRem(rw) => concat_string!(rw.to_string(), "rem"), - UnitValue::RelVh(rw) => concat_string!(rw.to_string(), "vh"), - UnitValue::RelVw(rw) => concat_string!(rw.to_string(), "vw"), - - _ => "".to_owned(), - } - } -} diff --git a/pagetop/src/response/page/definition.rs b/pagetop/src/response/page/definition.rs index 3c3d9a9f..95362453 100644 --- a/pagetop/src/response/page/definition.rs +++ b/pagetop/src/response/page/definition.rs @@ -36,21 +36,22 @@ static DEFAULT_DIRECTION: Lazy> = Lazy::new(|| { pub enum TextDirection { Auto, LeftToRight, RightToLeft } -pub struct Page<'a> { +pub struct Page { + context : InContext, language : AttributeValue, direction : AttributeValue, title : AttributeValue, description : AttributeValue, - context : InContext, - regions : HashMap<&'a str, ComponentsBundle>, body_classes: Classes, + regions : HashMap<&'static str, ComponentsBundle>, template : String, } -impl<'a> Page<'a> { +impl Page { pub fn new() -> Self { Page { + context : InContext::new(), language : match &*DEFAULT_LANGUAGE { Some(language) => AttributeValue::new_with_value(language), _ => AttributeValue::new(), @@ -61,44 +62,49 @@ impl<'a> Page<'a> { }, title : AttributeValue::new(), description : AttributeValue::new(), - context : InContext::new(), - regions : common_components(), body_classes: Classes::new_with_default("body"), + regions : common_components(), template : "default".to_owned(), } } // Page BUILDER. - pub fn with_language(&mut self, language: &str) -> &mut Self { - self.language.with_value(language); + pub fn with_context(mut self, op: InContextOp) -> Self { + self.alter_context(op); self } - pub fn with_direction(&mut self, dir: TextDirection) -> &mut Self { - self.direction.with_value(match dir { - TextDirection::Auto => "auto", - TextDirection::LeftToRight => "ltr", - TextDirection::RightToLeft => "rtl", - }); + pub fn with_language(mut self, language: &str) -> Self { + self.alter_language(language); self } - pub fn with_title(&mut self, title: &str) -> &mut Self { - self.title.with_value(title); + pub fn with_direction(mut self, dir: TextDirection) -> Self { + self.alter_direction(dir); self } - pub fn with_description(&mut self, description: &str) -> &mut Self { - self.description.with_value(description); + pub fn with_title(mut self, title: &str) -> Self { + self.alter_title(title); + self + } + + pub fn with_description(mut self, description: &str) -> Self { + self.alter_description(description); + self + } + + pub fn with_body_classes(mut self, op: ClassesOp, classes: &str) -> Self { + self.alter_body_classes(op, classes); self } pub fn add_to( - &mut self, - region: &'a str, + mut self, + region: &'static str, component: impl ComponentTrait - ) -> &mut Self { + ) -> Self { if let Some(regions) = self.regions.get_mut(region) { regions.add(component); } else { @@ -107,18 +113,58 @@ impl<'a> Page<'a> { self } + pub fn using_template(mut self, template: &str) -> Self { + self.alter_template(template); + self + } + + // Page ALTER. + + pub fn alter_context(&mut self, op: InContextOp) -> &mut Self { + self.context.alter(op); + self + } + + pub fn alter_language(&mut self, language: &str) -> &mut Self { + self.language.with_value(language); + self + } + + pub fn alter_direction(&mut self, dir: TextDirection) -> &mut Self { + self.direction.with_value(match dir { + TextDirection::Auto => "auto", + TextDirection::LeftToRight => "ltr", + TextDirection::RightToLeft => "rtl", + }); + self + } + + pub fn alter_title(&mut self, title: &str) -> &mut Self { + self.title.with_value(title); + self + } + + pub fn alter_description(&mut self, description: &str) -> &mut Self { + self.description.with_value(description); + self + } + pub fn alter_body_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self { self.body_classes.alter(op, classes); self } - pub fn using_template(&mut self, template: &str) -> &mut Self { + pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self } // Page GETTERS. + pub fn context(&mut self) -> &mut InContext { + &mut self.context + } + pub fn language(&self) -> &AttributeValue { &self.language } @@ -135,10 +181,6 @@ impl<'a> Page<'a> { &self.description } - pub fn context(&mut self) -> &mut InContext { - &mut self.context - } - pub fn body_classes(&self) -> &Classes { &self.body_classes } @@ -184,8 +226,4 @@ impl<'a> Page<'a> { // Page EXTRAS. - pub fn using_theme(&mut self, theme_name: &str) -> &mut Self { - self.context.using_theme(theme_name); - self - } }