From 330338c7ef514773ebd6e74b32159e8dcf733729 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Tue, 12 Sep 2023 00:27:54 +0200 Subject: [PATCH] =?UTF-8?q?=E2=8F=AA=20Revert=20type=20for=20classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop-admin/src/lib.rs | 2 +- pagetop-bulmix/src/lib.rs | 70 +++++++++---------- pagetop-homedemo/src/lib.rs | 24 +++---- pagetop-minimal/src/component/anchor.rs | 12 ++-- pagetop-minimal/src/component/block.rs | 4 +- pagetop-minimal/src/component/container.rs | 16 ++--- .../src/component/form_element/button.rs | 16 ++--- .../src/component/form_element/date.rs | 6 +- .../src/component/form_element/form.rs | 4 +- .../src/component/form_element/input.rs | 23 +++--- pagetop-minimal/src/component/grid/column.rs | 37 +++++----- pagetop-minimal/src/component/grid/row.rs | 4 +- pagetop-minimal/src/component/heading.rs | 18 ++--- pagetop-minimal/src/component/icon.rs | 9 +-- pagetop-minimal/src/component/image.rs | 4 +- pagetop-minimal/src/component/paragraph.rs | 16 ++--- pagetop-node/src/lib.rs | 2 +- pagetop/src/html/classes.rs | 34 ++++++--- pagetop/src/response/page.rs | 4 +- 19 files changed, 152 insertions(+), 153 deletions(-) diff --git a/pagetop-admin/src/lib.rs b/pagetop-admin/src/lib.rs index 3e667c3d..f2651b82 100644 --- a/pagetop-admin/src/lib.rs +++ b/pagetop-admin/src/lib.rs @@ -41,5 +41,5 @@ impl ModuleTrait for Admin { } fn before_prepare_body(page: &mut Page) { - page.alter_body_classes(ClassesOp::Add, &["test-admin"]); + page.alter_body_classes(ClassesOp::Add, "test-admin"); } diff --git a/pagetop-bulmix/src/lib.rs b/pagetop-bulmix/src/lib.rs index 159e627b..83a649f1 100644 --- a/pagetop-bulmix/src/lib.rs +++ b/pagetop-bulmix/src/lib.rs @@ -53,8 +53,8 @@ impl ThemeTrait for Bulmix { a.alter_classes( ClassesOp::SetDefault, match a.anchor_type() { - AnchorType::Button => &["button", "is-primary"], - _ => &[], + AnchorType::Button => "button is-primary", + _ => "", }, ); } @@ -63,13 +63,13 @@ impl ThemeTrait for Bulmix { h.alter_classes( ClassesOp::SetDefault, match h.display() { - HeadingDisplay::XxLarge => &["title", "is-1"], - HeadingDisplay::Large => &["title", "is-2"], - HeadingDisplay::Medium => &["title", "is-3"], - HeadingDisplay::Small => &["title", "is-4"], - HeadingDisplay::XxSmall => &["title", "is-5"], - HeadingDisplay::Normal => &["title"], - HeadingDisplay::Subtitle => &["subtitle"], + HeadingDisplay::XxLarge => "title is-1", + HeadingDisplay::Large => "title is-2", + HeadingDisplay::Medium => "title is-3", + HeadingDisplay::Small => "title is-4", + HeadingDisplay::XxSmall => "title is-5", + HeadingDisplay::Normal => "title", + HeadingDisplay::Subtitle => "subtitle", }, ); } @@ -78,43 +78,41 @@ impl ThemeTrait for Bulmix { p.alter_classes( ClassesOp::SetDefault, match p.display() { - ParagraphDisplay::XxLarge => &["is-size-2"], - ParagraphDisplay::Large => &["is-size-3"], - ParagraphDisplay::Medium => &["is-size-4"], - ParagraphDisplay::Small => &["is-size-5"], - ParagraphDisplay::XxSmall => &["is-size-6"], - ParagraphDisplay::Normal => &[], + ParagraphDisplay::XxLarge => "is-size-2", + ParagraphDisplay::Large => "is-size-3", + ParagraphDisplay::Medium => "is-size-4", + ParagraphDisplay::Small => "is-size-5", + ParagraphDisplay::XxSmall => "is-size-6", + ParagraphDisplay::Normal => "", }, ); } grid::COMPONENT_GRID_COLUMN => { let col = component_as_mut::(component); + col.alter_classes(ClassesOp::SetDefault, "column"); col.alter_classes( - ClassesOp::SetDefault, - &[ - "column", - match col.size() { - grid::ColumnSize::Default => "", - grid::ColumnSize::Is1of12 => "is-1", - grid::ColumnSize::Is2of12 => "is-2", - grid::ColumnSize::Is3of12 => "is-3", - grid::ColumnSize::Is4of12 => "is-4", - grid::ColumnSize::Is5of12 => "is-5", - grid::ColumnSize::Is6of12 => "is-6", - grid::ColumnSize::Is7of12 => "is-7", - grid::ColumnSize::Is8of12 => "is-8", - grid::ColumnSize::Is9of12 => "is-9", - grid::ColumnSize::Is10of12 => "is-10", - grid::ColumnSize::Is11of12 => "is-11", - grid::ColumnSize::IsFull => "is-12", - }, - "content", - ] + ClassesOp::AddDefault, + match col.size() { + grid::ColumnSize::Default => "", + grid::ColumnSize::Is1of12 => "is-1", + grid::ColumnSize::Is2of12 => "is-2", + grid::ColumnSize::Is3of12 => "is-3", + grid::ColumnSize::Is4of12 => "is-4", + grid::ColumnSize::Is5of12 => "is-5", + grid::ColumnSize::Is6of12 => "is-6", + grid::ColumnSize::Is7of12 => "is-7", + grid::ColumnSize::Is8of12 => "is-8", + grid::ColumnSize::Is9of12 => "is-9", + grid::ColumnSize::Is10of12 => "is-10", + grid::ColumnSize::Is11of12 => "is-11", + grid::ColumnSize::IsFull => "is-12", + } ); + col.alter_classes(ClassesOp::AddDefault, "content"); } grid::COMPONENT_GRID_ROW => { let row = component_as_mut::(component); - row.alter_classes(ClassesOp::SetDefault, &["columns"]); + row.alter_classes(ClassesOp::SetDefault, "columns"); } _ => {} } diff --git a/pagetop-homedemo/src/lib.rs b/pagetop-homedemo/src/lib.rs index fba96421..5c8f4a07 100644 --- a/pagetop-homedemo/src/lib.rs +++ b/pagetop-homedemo/src/lib.rs @@ -38,7 +38,7 @@ async fn demo(request: service::HttpRequest) -> ResultPage { .with_context(ContextOp::AddStyleSheet(StyleSheet::at( "/homedemo/css/styles.css", ))) - .with_body_classes(ClassesOp::Add, &["default-homepage"]) + .with_body_classes(ClassesOp::Add, "default-homepage") .with_in("content", hello_world()) .with_in("content", welcome()) .with_in("content", about_pagetop()) @@ -52,7 +52,7 @@ fn hello_world() -> Container { grid::Row::new() .with_column( grid::Column::new() - .with_classes(ClassesOp::Add, &["hello-col-text"]) + .with_classes(ClassesOp::Add, "hello-col-text") .with_size(grid::ColumnSize::Is5of12) .with_component( Heading::h1(L10n::t("page_title", &LOCALES_HOMEDEMO)) @@ -84,17 +84,17 @@ fn hello_world() -> Container { ) .with_target(AnchorTarget::Blank) .with_left_icon(Icon::with("git")) - .with_classes(ClassesOp::Add, &["code-link"]), + .with_classes(ClassesOp::Add, "code-link"), ) .with_component( Anchor::link("#welcome", L10n::t("hello_welcome", &LOCALES_HOMEDEMO)) .with_left_icon(Icon::with("arrow-down-circle-fill")) - .with_classes(ClassesOp::Add, &["welcome-link"]), + .with_classes(ClassesOp::Add, "welcome-link"), ), ) .with_column( grid::Column::new() - .with_classes(ClassesOp::Add, &["hello-col-image"]) + .with_classes(ClassesOp::Add, "hello-col-image") .with_component(Image::with("/homedemo/images/header.svg")), ), ) @@ -103,7 +103,7 @@ fn hello_world() -> Container { fn welcome() -> Container { Container::section() .with_id("welcome") - .with_classes(ClassesOp::Add, &["welcome-col-text"]) + .with_classes(ClassesOp::Add, "welcome-col-text") .with_component(Heading::h2(L10n::t("welcome_page", &LOCALES_HOMEDEMO))) .with_component( Heading::h3(L10n::e("welcome_subtitle", &LOCALES_HOMEDEMO).with_arg( @@ -127,13 +127,13 @@ fn about_pagetop() -> Container { grid::Row::new() .with_column( grid::Column::new() - .with_classes(ClassesOp::Add, &["pagetop-col-image"]) + .with_classes(ClassesOp::Add, "pagetop-col-image") .with_size(grid::ColumnSize::Is5of12) .with_component(Image::with("/homedemo/images/about.svg")), ) .with_column( grid::Column::new() - .with_classes(ClassesOp::Add, &["pagetop-col-text"]) + .with_classes(ClassesOp::Add, "pagetop-col-text") .with_component(Heading::h2(L10n::t("pagetop_title", &LOCALES_HOMEDEMO))) .with_component( Paragraph::with(L10n::t("pagetop_text1", &LOCALES_HOMEDEMO)) @@ -153,7 +153,7 @@ fn promo_pagetop() -> Container { grid::Row::new() .with_column( grid::Column::new() - .with_classes(ClassesOp::Add, &["promo-col-text"]) + .with_classes(ClassesOp::Add, "promo-col-text") .with_component(Heading::h2(L10n::t( "pagetop_promo_title", &LOCALES_HOMEDEMO, @@ -173,7 +173,7 @@ fn promo_pagetop() -> Container { ) .with_column( grid::Column::new() - .with_classes(ClassesOp::Add, &["promo-col-image"]) + .with_classes(ClassesOp::Add, "promo-col-image") .with_size(grid::ColumnSize::Is6of12) .with_component(Image::with("/homedemo/images/pagetop.png")), ), @@ -185,12 +185,12 @@ fn reporting_issues() -> Container { grid::Row::new() .with_column( grid::Column::new() - .with_classes(ClassesOp::Add, &["reporting-col-image"]) + .with_classes(ClassesOp::Add, "reporting-col-image") .with_component(Image::with("/homedemo/images/support.jpg")), ) .with_column( grid::Column::new() - .with_classes(ClassesOp::Add, &["reporting-col-text"]) + .with_classes(ClassesOp::Add, "reporting-col-text") .with_size(grid::ColumnSize::Is6of12) .with_component(Heading::h2(L10n::t( "report_problems_title", diff --git a/pagetop-minimal/src/component/anchor.rs b/pagetop-minimal/src/component/anchor.rs index 7c86ddbe..753cbaf7 100644 --- a/pagetop-minimal/src/component/anchor.rs +++ b/pagetop-minimal/src/component/anchor.rs @@ -123,21 +123,21 @@ impl Anchor { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } #[fn_builder] pub fn alter_type(&mut self, anchor_type: AnchorType) -> &mut Self { - self.anchor_type = anchor_type; - self.classes.alter_value( + self.alter_classes( ClassesOp::SetDefault, - match self.anchor_type { - AnchorType::Button => &["btn", "btn-primary"], - _ => &[], + match anchor_type { + AnchorType::Button => "btn btn-primary", + _ => "", }, ); + self.anchor_type = anchor_type; self } diff --git a/pagetop-minimal/src/component/block.rs b/pagetop-minimal/src/component/block.rs index 76213fa3..ef84a263 100644 --- a/pagetop-minimal/src/component/block.rs +++ b/pagetop-minimal/src/component/block.rs @@ -18,7 +18,7 @@ pub struct Block { impl ComponentTrait for Block { fn new() -> Self { - Block::default().with_classes(ClassesOp::SetDefault, &["block"]) + Block::default().with_classes(ClassesOp::SetDefault, "block") } fn handle(&self) -> Handle { @@ -82,7 +82,7 @@ impl Block { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } diff --git a/pagetop-minimal/src/component/container.rs b/pagetop-minimal/src/component/container.rs index 95c1e5a7..344e20c2 100644 --- a/pagetop-minimal/src/component/container.rs +++ b/pagetop-minimal/src/component/container.rs @@ -30,8 +30,8 @@ pub struct Container { impl ComponentTrait for Container { fn new() -> Self { Container::default() - .with_classes(ClassesOp::SetDefault, &["container"]) - .with_inner_classes(ClassesOp::SetDefault, &["container"]) + .with_classes(ClassesOp::SetDefault, "container") + .with_inner_classes(ClassesOp::SetDefault, "container") } fn handle(&self) -> Handle { @@ -99,25 +99,25 @@ impl ComponentTrait for Container { impl Container { pub fn header() -> Self { - let mut c = Container::new().with_classes(ClassesOp::SetDefault, &["header"]); + let mut c = Container::new().with_classes(ClassesOp::SetDefault, "header"); c.container_type = ContainerType::Header; c } pub fn footer() -> Self { - let mut c = Container::new().with_classes(ClassesOp::SetDefault, &["footer"]); + let mut c = Container::new().with_classes(ClassesOp::SetDefault, "footer"); c.container_type = ContainerType::Footer; c } pub fn main() -> Self { - let mut c = Container::new().with_classes(ClassesOp::SetDefault, &["main"]); + let mut c = Container::new().with_classes(ClassesOp::SetDefault, "main"); c.container_type = ContainerType::Main; c } pub fn section() -> Self { - let mut c = Container::new().with_classes(ClassesOp::SetDefault, &["section"]); + let mut c = Container::new().with_classes(ClassesOp::SetDefault, "section"); c.container_type = ContainerType::Section; c } @@ -143,13 +143,13 @@ impl Container { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } #[fn_builder] - pub fn alter_inner_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_inner_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.inner_classes.alter_value(op, classes); self } diff --git a/pagetop-minimal/src/component/form_element/button.rs b/pagetop-minimal/src/component/form_element/button.rs index b40ccbcc..a8f4da4b 100644 --- a/pagetop-minimal/src/component/form_element/button.rs +++ b/pagetop-minimal/src/component/form_element/button.rs @@ -28,9 +28,7 @@ pub struct Button { impl ComponentTrait for Button { fn new() -> Self { - Button::default() - .with_classes(ClassesOp::SetDefault, &["btn", "btn-primary"]) - .with_classes(ClassesOp::Add, &["form-button"]) + Button::default().with_classes(ClassesOp::SetDefault, "btn btn-primary form-button") } fn handle(&self) -> Handle { @@ -76,10 +74,7 @@ impl Button { pub fn submit(value: L10n) -> Self { let mut button = Button::new() - .with_classes( - ClassesOp::Replace("form-button".to_owned()), - &["form-submit"], - ) + .with_classes(ClassesOp::Replace("form-button".to_owned()), "form-submit") .with_value(value); button.button_type = ButtonType::Submit; button @@ -87,10 +82,7 @@ impl Button { pub fn reset(value: L10n) -> Self { let mut button = Button::new() - .with_classes( - ClassesOp::Replace("form-button".to_owned()), - &["form-reset"], - ) + .with_classes(ClassesOp::Replace("form-button".to_owned()), "form-reset") .with_value(value); button.button_type = ButtonType::Reset; button @@ -111,7 +103,7 @@ impl Button { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } diff --git a/pagetop-minimal/src/component/form_element/date.rs b/pagetop-minimal/src/component/form_element/date.rs index cac1d8b7..18b304a9 100644 --- a/pagetop-minimal/src/component/form_element/date.rs +++ b/pagetop-minimal/src/component/form_element/date.rs @@ -23,9 +23,7 @@ pub struct Date { impl ComponentTrait for Date { fn new() -> Self { - Date::default() - .with_classes(ClassesOp::SetDefault, &["form-item"]) - .with_classes(ClassesOp::Add, &["form-type-date"]) + Date::default().with_classes(ClassesOp::SetDefault, "form-item form-type-date") } fn handle(&self) -> Handle { @@ -90,7 +88,7 @@ impl Date { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } diff --git a/pagetop-minimal/src/component/form_element/form.rs b/pagetop-minimal/src/component/form_element/form.rs index 1efd2935..862b69ed 100644 --- a/pagetop-minimal/src/component/form_element/form.rs +++ b/pagetop-minimal/src/component/form_element/form.rs @@ -28,7 +28,7 @@ pub struct Form { impl ComponentTrait for Form { fn new() -> Self { Form::default() - .with_classes(ClassesOp::SetDefault, &["form"]) + .with_classes(ClassesOp::SetDefault, "form") .with_charset("UTF-8") } @@ -97,7 +97,7 @@ impl Form { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } diff --git a/pagetop-minimal/src/component/form_element/input.rs b/pagetop-minimal/src/component/form_element/input.rs index 22c3e3fe..2bcd6329 100644 --- a/pagetop-minimal/src/component/form_element/input.rs +++ b/pagetop-minimal/src/component/form_element/input.rs @@ -42,8 +42,7 @@ pub struct Input { impl ComponentTrait for Input { fn new() -> Self { Input::default() - .with_classes(ClassesOp::SetDefault, &["form-item"]) - .with_classes(ClassesOp::Add, &["form-type-textfield"]) + .with_classes(ClassesOp::SetDefault, "form-item form-type-textfield") .with_size(Some(60)) .with_maxlength(Some(128)) } @@ -116,7 +115,7 @@ impl Input { pub fn password() -> Self { let mut input = Input::new().with_classes( ClassesOp::Replace("form-type-textfield".to_owned()), - &["form-type-password"], + "form-type-password", ); input.input_type = InputType::Password; input @@ -125,7 +124,7 @@ impl Input { pub fn search() -> Self { let mut input = Input::new().with_classes( ClassesOp::Replace("form-type-textfield".to_owned()), - &["form-type-search"], + "form-type-search", ); input.input_type = InputType::Search; input @@ -134,7 +133,7 @@ impl Input { pub fn email() -> Self { let mut input = Input::new().with_classes( ClassesOp::Replace("form-type-textfield".to_owned()), - &["form-type-email"], + "form-type-email", ); input.input_type = InputType::Email; input @@ -143,7 +142,7 @@ impl Input { pub fn telephone() -> Self { let mut input = Input::new().with_classes( ClassesOp::Replace("form-type-textfield".to_owned()), - &["form-type-telephone"], + "form-type-telephone", ); input.input_type = InputType::Telephone; input @@ -152,7 +151,7 @@ impl Input { pub fn url() -> Self { let mut input = Input::new().with_classes( ClassesOp::Replace("form-type-textfield".to_owned()), - &["form-type-url"], + "form-type-url", ); input.input_type = InputType::Url; input @@ -173,18 +172,18 @@ impl Input { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } #[fn_builder] pub fn alter_name(&mut self, name: &str) -> &mut Self { + if let Some(previous) = self.name.get() { + self.alter_classes(ClassesOp::Remove, concat_string!("form-item-", previous)); + } + self.alter_classes(ClassesOp::AddDefault, concat_string!("form-item-", name)); self.name.alter_value(name); - self.alter_classes( - ClassesOp::SetDefault, - &[concat_string!("form-item form-item-", name)], - ); self } diff --git a/pagetop-minimal/src/component/grid/column.rs b/pagetop-minimal/src/component/grid/column.rs index a455ec3d..9acbe438 100644 --- a/pagetop-minimal/src/component/grid/column.rs +++ b/pagetop-minimal/src/component/grid/column.rs @@ -50,7 +50,7 @@ pub struct Column { impl ComponentTrait for Column { fn new() -> Self { - Column::default().with_classes(ClassesOp::SetDefault, &[SIZE_DEFAULT]) + Column::default().with_classes(ClassesOp::SetDefault, SIZE_DEFAULT) } fn handle(&self) -> Handle { @@ -108,7 +108,7 @@ impl Column { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } @@ -116,21 +116,24 @@ impl Column { #[rustfmt::skip] #[fn_builder] pub fn alter_size(&mut self, size: ColumnSize) -> &mut Self { - match size { - ColumnSize::Default => self.alter_classes(ClassesOp::SetDefault, &[SIZE_DEFAULT]), - ColumnSize::Is1of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_1_OF_12]), - ColumnSize::Is2of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_2_OF_12]), - ColumnSize::Is3of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_3_OF_12]), - ColumnSize::Is4of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_4_OF_12]), - ColumnSize::Is5of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_5_OF_12]), - ColumnSize::Is6of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_6_OF_12]), - ColumnSize::Is7of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_7_OF_12]), - ColumnSize::Is8of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_8_OF_12]), - ColumnSize::Is9of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_9_OF_12]), - ColumnSize::Is10of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_10_OF_12]), - ColumnSize::Is11of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_11_OF_12]), - ColumnSize::IsFull => self.alter_classes(ClassesOp::SetDefault, &[SIZE_12_OF_12]), - }; + self.alter_classes( + ClassesOp::SetDefault, + match size { + ColumnSize::Default => SIZE_DEFAULT, + ColumnSize::Is1of12 => SIZE_1_OF_12, + ColumnSize::Is2of12 => SIZE_2_OF_12, + ColumnSize::Is3of12 => SIZE_3_OF_12, + ColumnSize::Is4of12 => SIZE_4_OF_12, + ColumnSize::Is5of12 => SIZE_5_OF_12, + ColumnSize::Is6of12 => SIZE_6_OF_12, + ColumnSize::Is7of12 => SIZE_7_OF_12, + ColumnSize::Is8of12 => SIZE_8_OF_12, + ColumnSize::Is9of12 => SIZE_9_OF_12, + ColumnSize::Is10of12 => SIZE_10_OF_12, + ColumnSize::Is11of12 => SIZE_11_OF_12, + ColumnSize::IsFull => SIZE_12_OF_12, + } + ); self.size = size; self } diff --git a/pagetop-minimal/src/component/grid/row.rs b/pagetop-minimal/src/component/grid/row.rs index 8090c553..e2be6ca7 100644 --- a/pagetop-minimal/src/component/grid/row.rs +++ b/pagetop-minimal/src/component/grid/row.rs @@ -19,7 +19,7 @@ pub struct Row { impl ComponentTrait for Row { fn new() -> Self { - Row::default().with_classes(ClassesOp::SetDefault, &["row"]) + Row::default().with_classes(ClassesOp::SetDefault, "row") } fn handle(&self) -> Handle { @@ -77,7 +77,7 @@ impl Row { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } diff --git a/pagetop-minimal/src/component/heading.rs b/pagetop-minimal/src/component/heading.rs index 3bea3c39..e4b8ae3e 100644 --- a/pagetop-minimal/src/component/heading.rs +++ b/pagetop-minimal/src/component/heading.rs @@ -133,7 +133,7 @@ impl Heading { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } @@ -153,16 +153,16 @@ impl Heading { #[rustfmt::skip] #[fn_builder] pub fn alter_display(&mut self, display: HeadingDisplay) -> &mut Self { - self.classes.alter_value( + self.alter_classes( ClassesOp::SetDefault, match display { - HeadingDisplay::XxLarge => &["display-2"], - HeadingDisplay::Large => &["display-3"], - HeadingDisplay::Medium => &["display-4"], - HeadingDisplay::Small => &["display-5"], - HeadingDisplay::XxSmall => &["display-6"], - HeadingDisplay::Normal => &[], - HeadingDisplay::Subtitle => &[], + HeadingDisplay::XxLarge => "display-2", + HeadingDisplay::Large => "display-3", + HeadingDisplay::Medium => "display-4", + HeadingDisplay::Small => "display-5", + HeadingDisplay::XxSmall => "display-6", + HeadingDisplay::Normal => "", + HeadingDisplay::Subtitle => "", }, ); self.display = display; diff --git a/pagetop-minimal/src/component/icon.rs b/pagetop-minimal/src/component/icon.rs index ce055742..6d5a1a68 100644 --- a/pagetop-minimal/src/component/icon.rs +++ b/pagetop-minimal/src/component/icon.rs @@ -13,7 +13,7 @@ pub struct Icon { impl ComponentTrait for Icon { fn new() -> Self { - Icon::default().with_classes(ClassesOp::SetDefault, &["bi-question-circle-fill"]) + Icon::default().with_classes(ClassesOp::SetDefault, "bi-question-circle-fill") } fn handle(&self) -> Handle { @@ -60,16 +60,13 @@ impl Icon { #[fn_builder] pub fn alter_icon_name(&mut self, name: &str) -> &mut Self { + self.alter_classes(ClassesOp::SetDefault, concat_string!("bi-", name)); self.icon_name = name.to_owned(); - self.alter_classes( - ClassesOp::SetDefault, - &[concat_string!("bi-", self.icon_name)], - ); self } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } diff --git a/pagetop-minimal/src/component/image.rs b/pagetop-minimal/src/component/image.rs index e4ee58eb..d3e6d9df 100644 --- a/pagetop-minimal/src/component/image.rs +++ b/pagetop-minimal/src/component/image.rs @@ -24,7 +24,7 @@ pub struct Image { impl ComponentTrait for Image { fn new() -> Self { - Image::default().with_classes(ClassesOp::SetDefault, &["img-fluid"]) + Image::default().with_classes(ClassesOp::SetDefault, "img-fluid") } fn handle(&self) -> Handle { @@ -93,7 +93,7 @@ impl Image { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } diff --git a/pagetop-minimal/src/component/paragraph.rs b/pagetop-minimal/src/component/paragraph.rs index 474e0c1f..f5fd3c67 100644 --- a/pagetop-minimal/src/component/paragraph.rs +++ b/pagetop-minimal/src/component/paragraph.rs @@ -84,7 +84,7 @@ impl Paragraph { } #[fn_builder] - pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.classes.alter_value(op, classes); self } @@ -103,15 +103,15 @@ impl Paragraph { #[rustfmt::skip] #[fn_builder] pub fn alter_display(&mut self, display: ParagraphDisplay) -> &mut Self { - self.classes.alter_value( + self.alter_classes( ClassesOp::SetDefault, match display { - ParagraphDisplay::XxLarge => &["fs-2"], - ParagraphDisplay::Large => &["fs-3"], - ParagraphDisplay::Medium => &["fs-4"], - ParagraphDisplay::Small => &["fs-5"], - ParagraphDisplay::XxSmall => &["fs-6"], - ParagraphDisplay::Normal => &[], + ParagraphDisplay::XxLarge => "fs-2", + ParagraphDisplay::Large => "fs-3", + ParagraphDisplay::Medium => "fs-4", + ParagraphDisplay::Small => "fs-5", + ParagraphDisplay::XxSmall => "fs-6", + ParagraphDisplay::Normal => "", }, ); self.display = display; diff --git a/pagetop-node/src/lib.rs b/pagetop-node/src/lib.rs index 3634706f..96d1c813 100644 --- a/pagetop-node/src/lib.rs +++ b/pagetop-node/src/lib.rs @@ -45,5 +45,5 @@ async fn node(request: service::HttpRequest) -> ResultPage { } fn before_prepare_body(page: &mut Page) { - page.alter_body_classes(ClassesOp::Add, &["test-node"]); + page.alter_body_classes(ClassesOp::Add, "test-node"); } diff --git a/pagetop/src/html/classes.rs b/pagetop/src/html/classes.rs index b1bc7f63..9f06d38e 100644 --- a/pagetop/src/html/classes.rs +++ b/pagetop/src/html/classes.rs @@ -4,8 +4,8 @@ //! by the theme) and user classes (for customizing components based on application styles). //! //! Default classes can be added using [SetDefault] and [AddDefault], while user classes can be -//! added using [Add]. Operations to [Remove] or [Replace] any class, as well as to [Reset] user -//! classes, are also provided. +//! added using [Add]. Operations to [Remove], [Replace] or [ReplaceIfExists] a class, as well as +//! to [Reset] user classes, are also provided. //! //! Although the order of the classes is irrelevant (), default //! classes will be presented before user classes and duplicate classes will not be allowed. @@ -18,6 +18,7 @@ pub enum ClassesOp { Add, Remove, Replace(String), + ReplaceIfExists(String), Reset, } @@ -38,21 +39,24 @@ impl Classes { // Classes BUILDER. #[fn_builder] - pub fn alter_value(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_value(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { + let classes: String = classes.into(); + let classes: Vec<&str> = classes.split_ascii_whitespace().collect(); + match op { ClassesOp::SetDefault => { self.0.retain(|(_, t)| t.ne(&ClassType::Default)); - self.add(classes, 0, ClassType::Default); + self.add(&classes, 0, ClassType::Default); } ClassesOp::AddDefault => { let pos = match self.0.iter().position(|(_, t)| t.eq(&ClassType::User)) { Some(pos) => pos, None => self.0.len(), }; - self.add(classes, pos, ClassType::Default); + self.add(&classes, pos, ClassType::Default); } ClassesOp::Add => { - self.add(classes, self.0.len(), ClassType::User); + self.add(&classes, self.0.len(), ClassType::User); } ClassesOp::Remove => { for name in classes { @@ -60,9 +64,18 @@ impl Classes { } } ClassesOp::Replace(class) => { + match self.0.iter().position(|(c, _)| c.eq(&class)) { + Some(pos) => { + let (_, class_type) = self.0.remove(pos); + self.add(&classes, pos, class_type); + } + None => self.add(&classes, self.0.len(), ClassType::User), + }; + } + ClassesOp::ReplaceIfExists(class) => { if let Some(pos) = self.0.iter().position(|(c, _)| c.eq(&class)) { let (_, class_type) = self.0.remove(pos); - self.add(classes, pos, class_type); + self.add(&classes, pos, class_type); } } ClassesOp::Reset => { @@ -73,11 +86,10 @@ impl Classes { } #[inline] - fn add(&mut self, classes: &[impl ToString], mut pos: usize, class_type: ClassType) { + fn add(&mut self, classes: &Vec<&str>, mut pos: usize, class_type: ClassType) { for class in classes { - let class: String = class.to_string(); - if !class.is_empty() && self.0.iter().position(|(c, _)| c.eq(&class)).is_none() { - self.0.insert(pos, (class, class_type.clone())); + if !class.is_empty() && self.0.iter().position(|(c, _)| c.eq(class)).is_none() { + self.0.insert(pos, (class.to_string(), class_type.clone())); pos = pos + 1; } } diff --git a/pagetop/src/response/page.rs b/pagetop/src/response/page.rs index fce44032..6510534a 100644 --- a/pagetop/src/response/page.rs +++ b/pagetop/src/response/page.rs @@ -38,7 +38,7 @@ impl Page { properties : Vec::new(), favicon : None, context : Context::new(request), - body_classes: Classes::new().with_value(ClassesOp::SetDefault, &["body"]), + body_classes: Classes::new().with_value(ClassesOp::SetDefault, "body"), regions : ComponentsRegions::new(), template : "default".to_owned(), } @@ -83,7 +83,7 @@ impl Page { } #[fn_builder] - pub fn alter_body_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self { + pub fn alter_body_classes(&mut self, op: ClassesOp, classes: impl Into) -> &mut Self { self.body_classes.alter_value(op, classes); self }