Revert type for classes

This commit is contained in:
Manuel Cillero 2023-09-12 00:27:54 +02:00
parent 45d8c55e61
commit 330338c7ef
19 changed files with 152 additions and 153 deletions

View file

@ -41,5 +41,5 @@ impl ModuleTrait for Admin {
} }
fn before_prepare_body(page: &mut Page) { fn before_prepare_body(page: &mut Page) {
page.alter_body_classes(ClassesOp::Add, &["test-admin"]); page.alter_body_classes(ClassesOp::Add, "test-admin");
} }

View file

@ -53,8 +53,8 @@ impl ThemeTrait for Bulmix {
a.alter_classes( a.alter_classes(
ClassesOp::SetDefault, ClassesOp::SetDefault,
match a.anchor_type() { 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( h.alter_classes(
ClassesOp::SetDefault, ClassesOp::SetDefault,
match h.display() { match h.display() {
HeadingDisplay::XxLarge => &["title", "is-1"], HeadingDisplay::XxLarge => "title is-1",
HeadingDisplay::Large => &["title", "is-2"], HeadingDisplay::Large => "title is-2",
HeadingDisplay::Medium => &["title", "is-3"], HeadingDisplay::Medium => "title is-3",
HeadingDisplay::Small => &["title", "is-4"], HeadingDisplay::Small => "title is-4",
HeadingDisplay::XxSmall => &["title", "is-5"], HeadingDisplay::XxSmall => "title is-5",
HeadingDisplay::Normal => &["title"], HeadingDisplay::Normal => "title",
HeadingDisplay::Subtitle => &["subtitle"], HeadingDisplay::Subtitle => "subtitle",
}, },
); );
} }
@ -78,43 +78,41 @@ impl ThemeTrait for Bulmix {
p.alter_classes( p.alter_classes(
ClassesOp::SetDefault, ClassesOp::SetDefault,
match p.display() { match p.display() {
ParagraphDisplay::XxLarge => &["is-size-2"], ParagraphDisplay::XxLarge => "is-size-2",
ParagraphDisplay::Large => &["is-size-3"], ParagraphDisplay::Large => "is-size-3",
ParagraphDisplay::Medium => &["is-size-4"], ParagraphDisplay::Medium => "is-size-4",
ParagraphDisplay::Small => &["is-size-5"], ParagraphDisplay::Small => "is-size-5",
ParagraphDisplay::XxSmall => &["is-size-6"], ParagraphDisplay::XxSmall => "is-size-6",
ParagraphDisplay::Normal => &[], ParagraphDisplay::Normal => "",
}, },
); );
} }
grid::COMPONENT_GRID_COLUMN => { grid::COMPONENT_GRID_COLUMN => {
let col = component_as_mut::<grid::Column>(component); let col = component_as_mut::<grid::Column>(component);
col.alter_classes(ClassesOp::SetDefault, "column");
col.alter_classes( col.alter_classes(
ClassesOp::SetDefault, ClassesOp::AddDefault,
&[ match col.size() {
"column", grid::ColumnSize::Default => "",
match col.size() { grid::ColumnSize::Is1of12 => "is-1",
grid::ColumnSize::Default => "", grid::ColumnSize::Is2of12 => "is-2",
grid::ColumnSize::Is1of12 => "is-1", grid::ColumnSize::Is3of12 => "is-3",
grid::ColumnSize::Is2of12 => "is-2", grid::ColumnSize::Is4of12 => "is-4",
grid::ColumnSize::Is3of12 => "is-3", grid::ColumnSize::Is5of12 => "is-5",
grid::ColumnSize::Is4of12 => "is-4", grid::ColumnSize::Is6of12 => "is-6",
grid::ColumnSize::Is5of12 => "is-5", grid::ColumnSize::Is7of12 => "is-7",
grid::ColumnSize::Is6of12 => "is-6", grid::ColumnSize::Is8of12 => "is-8",
grid::ColumnSize::Is7of12 => "is-7", grid::ColumnSize::Is9of12 => "is-9",
grid::ColumnSize::Is8of12 => "is-8", grid::ColumnSize::Is10of12 => "is-10",
grid::ColumnSize::Is9of12 => "is-9", grid::ColumnSize::Is11of12 => "is-11",
grid::ColumnSize::Is10of12 => "is-10", grid::ColumnSize::IsFull => "is-12",
grid::ColumnSize::Is11of12 => "is-11", }
grid::ColumnSize::IsFull => "is-12",
},
"content",
]
); );
col.alter_classes(ClassesOp::AddDefault, "content");
} }
grid::COMPONENT_GRID_ROW => { grid::COMPONENT_GRID_ROW => {
let row = component_as_mut::<grid::Row>(component); let row = component_as_mut::<grid::Row>(component);
row.alter_classes(ClassesOp::SetDefault, &["columns"]); row.alter_classes(ClassesOp::SetDefault, "columns");
} }
_ => {} _ => {}
} }

View file

@ -38,7 +38,7 @@ async fn demo(request: service::HttpRequest) -> ResultPage<Markup, FatalError> {
.with_context(ContextOp::AddStyleSheet(StyleSheet::at( .with_context(ContextOp::AddStyleSheet(StyleSheet::at(
"/homedemo/css/styles.css", "/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", hello_world())
.with_in("content", welcome()) .with_in("content", welcome())
.with_in("content", about_pagetop()) .with_in("content", about_pagetop())
@ -52,7 +52,7 @@ fn hello_world() -> Container {
grid::Row::new() grid::Row::new()
.with_column( .with_column(
grid::Column::new() grid::Column::new()
.with_classes(ClassesOp::Add, &["hello-col-text"]) .with_classes(ClassesOp::Add, "hello-col-text")
.with_size(grid::ColumnSize::Is5of12) .with_size(grid::ColumnSize::Is5of12)
.with_component( .with_component(
Heading::h1(L10n::t("page_title", &LOCALES_HOMEDEMO)) Heading::h1(L10n::t("page_title", &LOCALES_HOMEDEMO))
@ -84,17 +84,17 @@ fn hello_world() -> Container {
) )
.with_target(AnchorTarget::Blank) .with_target(AnchorTarget::Blank)
.with_left_icon(Icon::with("git")) .with_left_icon(Icon::with("git"))
.with_classes(ClassesOp::Add, &["code-link"]), .with_classes(ClassesOp::Add, "code-link"),
) )
.with_component( .with_component(
Anchor::link("#welcome", L10n::t("hello_welcome", &LOCALES_HOMEDEMO)) Anchor::link("#welcome", L10n::t("hello_welcome", &LOCALES_HOMEDEMO))
.with_left_icon(Icon::with("arrow-down-circle-fill")) .with_left_icon(Icon::with("arrow-down-circle-fill"))
.with_classes(ClassesOp::Add, &["welcome-link"]), .with_classes(ClassesOp::Add, "welcome-link"),
), ),
) )
.with_column( .with_column(
grid::Column::new() 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")), .with_component(Image::with("/homedemo/images/header.svg")),
), ),
) )
@ -103,7 +103,7 @@ fn hello_world() -> Container {
fn welcome() -> Container { fn welcome() -> Container {
Container::section() Container::section()
.with_id("welcome") .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::h2(L10n::t("welcome_page", &LOCALES_HOMEDEMO)))
.with_component( .with_component(
Heading::h3(L10n::e("welcome_subtitle", &LOCALES_HOMEDEMO).with_arg( Heading::h3(L10n::e("welcome_subtitle", &LOCALES_HOMEDEMO).with_arg(
@ -127,13 +127,13 @@ fn about_pagetop() -> Container {
grid::Row::new() grid::Row::new()
.with_column( .with_column(
grid::Column::new() grid::Column::new()
.with_classes(ClassesOp::Add, &["pagetop-col-image"]) .with_classes(ClassesOp::Add, "pagetop-col-image")
.with_size(grid::ColumnSize::Is5of12) .with_size(grid::ColumnSize::Is5of12)
.with_component(Image::with("/homedemo/images/about.svg")), .with_component(Image::with("/homedemo/images/about.svg")),
) )
.with_column( .with_column(
grid::Column::new() 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(Heading::h2(L10n::t("pagetop_title", &LOCALES_HOMEDEMO)))
.with_component( .with_component(
Paragraph::with(L10n::t("pagetop_text1", &LOCALES_HOMEDEMO)) Paragraph::with(L10n::t("pagetop_text1", &LOCALES_HOMEDEMO))
@ -153,7 +153,7 @@ fn promo_pagetop() -> Container {
grid::Row::new() grid::Row::new()
.with_column( .with_column(
grid::Column::new() grid::Column::new()
.with_classes(ClassesOp::Add, &["promo-col-text"]) .with_classes(ClassesOp::Add, "promo-col-text")
.with_component(Heading::h2(L10n::t( .with_component(Heading::h2(L10n::t(
"pagetop_promo_title", "pagetop_promo_title",
&LOCALES_HOMEDEMO, &LOCALES_HOMEDEMO,
@ -173,7 +173,7 @@ fn promo_pagetop() -> Container {
) )
.with_column( .with_column(
grid::Column::new() grid::Column::new()
.with_classes(ClassesOp::Add, &["promo-col-image"]) .with_classes(ClassesOp::Add, "promo-col-image")
.with_size(grid::ColumnSize::Is6of12) .with_size(grid::ColumnSize::Is6of12)
.with_component(Image::with("/homedemo/images/pagetop.png")), .with_component(Image::with("/homedemo/images/pagetop.png")),
), ),
@ -185,12 +185,12 @@ fn reporting_issues() -> Container {
grid::Row::new() grid::Row::new()
.with_column( .with_column(
grid::Column::new() 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_component(Image::with("/homedemo/images/support.jpg")),
) )
.with_column( .with_column(
grid::Column::new() grid::Column::new()
.with_classes(ClassesOp::Add, &["reporting-col-text"]) .with_classes(ClassesOp::Add, "reporting-col-text")
.with_size(grid::ColumnSize::Is6of12) .with_size(grid::ColumnSize::Is6of12)
.with_component(Heading::h2(L10n::t( .with_component(Heading::h2(L10n::t(
"report_problems_title", "report_problems_title",

View file

@ -123,21 +123,21 @@ impl Anchor {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_type(&mut self, anchor_type: AnchorType) -> &mut Self { pub fn alter_type(&mut self, anchor_type: AnchorType) -> &mut Self {
self.anchor_type = anchor_type; self.alter_classes(
self.classes.alter_value(
ClassesOp::SetDefault, ClassesOp::SetDefault,
match self.anchor_type { match anchor_type {
AnchorType::Button => &["btn", "btn-primary"], AnchorType::Button => "btn btn-primary",
_ => &[], _ => "",
}, },
); );
self.anchor_type = anchor_type;
self self
} }

View file

@ -18,7 +18,7 @@ pub struct Block {
impl ComponentTrait for Block { impl ComponentTrait for Block {
fn new() -> Self { fn new() -> Self {
Block::default().with_classes(ClassesOp::SetDefault, &["block"]) Block::default().with_classes(ClassesOp::SetDefault, "block")
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
@ -82,7 +82,7 @@ impl Block {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }

View file

@ -30,8 +30,8 @@ pub struct Container {
impl ComponentTrait for Container { impl ComponentTrait for Container {
fn new() -> Self { fn new() -> Self {
Container::default() Container::default()
.with_classes(ClassesOp::SetDefault, &["container"]) .with_classes(ClassesOp::SetDefault, "container")
.with_inner_classes(ClassesOp::SetDefault, &["container"]) .with_inner_classes(ClassesOp::SetDefault, "container")
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
@ -99,25 +99,25 @@ impl ComponentTrait for Container {
impl Container { impl Container {
pub fn header() -> Self { 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.container_type = ContainerType::Header;
c c
} }
pub fn footer() -> Self { 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.container_type = ContainerType::Footer;
c c
} }
pub fn main() -> Self { 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.container_type = ContainerType::Main;
c c
} }
pub fn section() -> Self { 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.container_type = ContainerType::Section;
c c
} }
@ -143,13 +143,13 @@ impl Container {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.inner_classes.alter_value(op, classes); self.inner_classes.alter_value(op, classes);
self self
} }

View file

@ -28,9 +28,7 @@ pub struct Button {
impl ComponentTrait for Button { impl ComponentTrait for Button {
fn new() -> Self { fn new() -> Self {
Button::default() Button::default().with_classes(ClassesOp::SetDefault, "btn btn-primary form-button")
.with_classes(ClassesOp::SetDefault, &["btn", "btn-primary"])
.with_classes(ClassesOp::Add, &["form-button"])
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
@ -76,10 +74,7 @@ impl Button {
pub fn submit(value: L10n) -> Self { pub fn submit(value: L10n) -> Self {
let mut button = Button::new() let mut button = Button::new()
.with_classes( .with_classes(ClassesOp::Replace("form-button".to_owned()), "form-submit")
ClassesOp::Replace("form-button".to_owned()),
&["form-submit"],
)
.with_value(value); .with_value(value);
button.button_type = ButtonType::Submit; button.button_type = ButtonType::Submit;
button button
@ -87,10 +82,7 @@ impl Button {
pub fn reset(value: L10n) -> Self { pub fn reset(value: L10n) -> Self {
let mut button = Button::new() let mut button = Button::new()
.with_classes( .with_classes(ClassesOp::Replace("form-button".to_owned()), "form-reset")
ClassesOp::Replace("form-button".to_owned()),
&["form-reset"],
)
.with_value(value); .with_value(value);
button.button_type = ButtonType::Reset; button.button_type = ButtonType::Reset;
button button
@ -111,7 +103,7 @@ impl Button {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }

View file

@ -23,9 +23,7 @@ pub struct Date {
impl ComponentTrait for Date { impl ComponentTrait for Date {
fn new() -> Self { fn new() -> Self {
Date::default() Date::default().with_classes(ClassesOp::SetDefault, "form-item form-type-date")
.with_classes(ClassesOp::SetDefault, &["form-item"])
.with_classes(ClassesOp::Add, &["form-type-date"])
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
@ -90,7 +88,7 @@ impl Date {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }

View file

@ -28,7 +28,7 @@ pub struct Form {
impl ComponentTrait for Form { impl ComponentTrait for Form {
fn new() -> Self { fn new() -> Self {
Form::default() Form::default()
.with_classes(ClassesOp::SetDefault, &["form"]) .with_classes(ClassesOp::SetDefault, "form")
.with_charset("UTF-8") .with_charset("UTF-8")
} }
@ -97,7 +97,7 @@ impl Form {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }

View file

@ -42,8 +42,7 @@ pub struct Input {
impl ComponentTrait for Input { impl ComponentTrait for Input {
fn new() -> Self { fn new() -> Self {
Input::default() Input::default()
.with_classes(ClassesOp::SetDefault, &["form-item"]) .with_classes(ClassesOp::SetDefault, "form-item form-type-textfield")
.with_classes(ClassesOp::Add, &["form-type-textfield"])
.with_size(Some(60)) .with_size(Some(60))
.with_maxlength(Some(128)) .with_maxlength(Some(128))
} }
@ -116,7 +115,7 @@ impl Input {
pub fn password() -> Self { pub fn password() -> Self {
let mut input = Input::new().with_classes( let mut input = Input::new().with_classes(
ClassesOp::Replace("form-type-textfield".to_owned()), ClassesOp::Replace("form-type-textfield".to_owned()),
&["form-type-password"], "form-type-password",
); );
input.input_type = InputType::Password; input.input_type = InputType::Password;
input input
@ -125,7 +124,7 @@ impl Input {
pub fn search() -> Self { pub fn search() -> Self {
let mut input = Input::new().with_classes( let mut input = Input::new().with_classes(
ClassesOp::Replace("form-type-textfield".to_owned()), ClassesOp::Replace("form-type-textfield".to_owned()),
&["form-type-search"], "form-type-search",
); );
input.input_type = InputType::Search; input.input_type = InputType::Search;
input input
@ -134,7 +133,7 @@ impl Input {
pub fn email() -> Self { pub fn email() -> Self {
let mut input = Input::new().with_classes( let mut input = Input::new().with_classes(
ClassesOp::Replace("form-type-textfield".to_owned()), ClassesOp::Replace("form-type-textfield".to_owned()),
&["form-type-email"], "form-type-email",
); );
input.input_type = InputType::Email; input.input_type = InputType::Email;
input input
@ -143,7 +142,7 @@ impl Input {
pub fn telephone() -> Self { pub fn telephone() -> Self {
let mut input = Input::new().with_classes( let mut input = Input::new().with_classes(
ClassesOp::Replace("form-type-textfield".to_owned()), ClassesOp::Replace("form-type-textfield".to_owned()),
&["form-type-telephone"], "form-type-telephone",
); );
input.input_type = InputType::Telephone; input.input_type = InputType::Telephone;
input input
@ -152,7 +151,7 @@ impl Input {
pub fn url() -> Self { pub fn url() -> Self {
let mut input = Input::new().with_classes( let mut input = Input::new().with_classes(
ClassesOp::Replace("form-type-textfield".to_owned()), ClassesOp::Replace("form-type-textfield".to_owned()),
&["form-type-url"], "form-type-url",
); );
input.input_type = InputType::Url; input.input_type = InputType::Url;
input input
@ -173,18 +172,18 @@ impl Input {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_name(&mut self, name: &str) -> &mut Self { 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.name.alter_value(name);
self.alter_classes(
ClassesOp::SetDefault,
&[concat_string!("form-item form-item-", name)],
);
self self
} }

View file

@ -50,7 +50,7 @@ pub struct Column {
impl ComponentTrait for Column { impl ComponentTrait for Column {
fn new() -> Self { fn new() -> Self {
Column::default().with_classes(ClassesOp::SetDefault, &[SIZE_DEFAULT]) Column::default().with_classes(ClassesOp::SetDefault, SIZE_DEFAULT)
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
@ -108,7 +108,7 @@ impl Column {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }
@ -116,21 +116,24 @@ impl Column {
#[rustfmt::skip] #[rustfmt::skip]
#[fn_builder] #[fn_builder]
pub fn alter_size(&mut self, size: ColumnSize) -> &mut Self { pub fn alter_size(&mut self, size: ColumnSize) -> &mut Self {
match size { self.alter_classes(
ColumnSize::Default => self.alter_classes(ClassesOp::SetDefault, &[SIZE_DEFAULT]), ClassesOp::SetDefault,
ColumnSize::Is1of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_1_OF_12]), match size {
ColumnSize::Is2of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_2_OF_12]), ColumnSize::Default => SIZE_DEFAULT,
ColumnSize::Is3of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_3_OF_12]), ColumnSize::Is1of12 => SIZE_1_OF_12,
ColumnSize::Is4of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_4_OF_12]), ColumnSize::Is2of12 => SIZE_2_OF_12,
ColumnSize::Is5of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_5_OF_12]), ColumnSize::Is3of12 => SIZE_3_OF_12,
ColumnSize::Is6of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_6_OF_12]), ColumnSize::Is4of12 => SIZE_4_OF_12,
ColumnSize::Is7of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_7_OF_12]), ColumnSize::Is5of12 => SIZE_5_OF_12,
ColumnSize::Is8of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_8_OF_12]), ColumnSize::Is6of12 => SIZE_6_OF_12,
ColumnSize::Is9of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_9_OF_12]), ColumnSize::Is7of12 => SIZE_7_OF_12,
ColumnSize::Is10of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_10_OF_12]), ColumnSize::Is8of12 => SIZE_8_OF_12,
ColumnSize::Is11of12 => self.alter_classes(ClassesOp::SetDefault, &[SIZE_11_OF_12]), ColumnSize::Is9of12 => SIZE_9_OF_12,
ColumnSize::IsFull => self.alter_classes(ClassesOp::SetDefault, &[SIZE_12_OF_12]), ColumnSize::Is10of12 => SIZE_10_OF_12,
}; ColumnSize::Is11of12 => SIZE_11_OF_12,
ColumnSize::IsFull => SIZE_12_OF_12,
}
);
self.size = size; self.size = size;
self self
} }

View file

@ -19,7 +19,7 @@ pub struct Row {
impl ComponentTrait for Row { impl ComponentTrait for Row {
fn new() -> Self { fn new() -> Self {
Row::default().with_classes(ClassesOp::SetDefault, &["row"]) Row::default().with_classes(ClassesOp::SetDefault, "row")
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
@ -77,7 +77,7 @@ impl Row {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }

View file

@ -133,7 +133,7 @@ impl Heading {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }
@ -153,16 +153,16 @@ impl Heading {
#[rustfmt::skip] #[rustfmt::skip]
#[fn_builder] #[fn_builder]
pub fn alter_display(&mut self, display: HeadingDisplay) -> &mut Self { pub fn alter_display(&mut self, display: HeadingDisplay) -> &mut Self {
self.classes.alter_value( self.alter_classes(
ClassesOp::SetDefault, ClassesOp::SetDefault,
match display { match display {
HeadingDisplay::XxLarge => &["display-2"], HeadingDisplay::XxLarge => "display-2",
HeadingDisplay::Large => &["display-3"], HeadingDisplay::Large => "display-3",
HeadingDisplay::Medium => &["display-4"], HeadingDisplay::Medium => "display-4",
HeadingDisplay::Small => &["display-5"], HeadingDisplay::Small => "display-5",
HeadingDisplay::XxSmall => &["display-6"], HeadingDisplay::XxSmall => "display-6",
HeadingDisplay::Normal => &[], HeadingDisplay::Normal => "",
HeadingDisplay::Subtitle => &[], HeadingDisplay::Subtitle => "",
}, },
); );
self.display = display; self.display = display;

View file

@ -13,7 +13,7 @@ pub struct Icon {
impl ComponentTrait for Icon { impl ComponentTrait for Icon {
fn new() -> Self { 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 { fn handle(&self) -> Handle {
@ -60,16 +60,13 @@ impl Icon {
#[fn_builder] #[fn_builder]
pub fn alter_icon_name(&mut self, name: &str) -> &mut Self { 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.icon_name = name.to_owned();
self.alter_classes(
ClassesOp::SetDefault,
&[concat_string!("bi-", self.icon_name)],
);
self self
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }

View file

@ -24,7 +24,7 @@ pub struct Image {
impl ComponentTrait for Image { impl ComponentTrait for Image {
fn new() -> Self { fn new() -> Self {
Image::default().with_classes(ClassesOp::SetDefault, &["img-fluid"]) Image::default().with_classes(ClassesOp::SetDefault, "img-fluid")
} }
fn handle(&self) -> Handle { fn handle(&self) -> Handle {
@ -93,7 +93,7 @@ impl Image {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }

View file

@ -84,7 +84,7 @@ impl Paragraph {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.classes.alter_value(op, classes); self.classes.alter_value(op, classes);
self self
} }
@ -103,15 +103,15 @@ impl Paragraph {
#[rustfmt::skip] #[rustfmt::skip]
#[fn_builder] #[fn_builder]
pub fn alter_display(&mut self, display: ParagraphDisplay) -> &mut Self { pub fn alter_display(&mut self, display: ParagraphDisplay) -> &mut Self {
self.classes.alter_value( self.alter_classes(
ClassesOp::SetDefault, ClassesOp::SetDefault,
match display { match display {
ParagraphDisplay::XxLarge => &["fs-2"], ParagraphDisplay::XxLarge => "fs-2",
ParagraphDisplay::Large => &["fs-3"], ParagraphDisplay::Large => "fs-3",
ParagraphDisplay::Medium => &["fs-4"], ParagraphDisplay::Medium => "fs-4",
ParagraphDisplay::Small => &["fs-5"], ParagraphDisplay::Small => "fs-5",
ParagraphDisplay::XxSmall => &["fs-6"], ParagraphDisplay::XxSmall => "fs-6",
ParagraphDisplay::Normal => &[], ParagraphDisplay::Normal => "",
}, },
); );
self.display = display; self.display = display;

View file

@ -45,5 +45,5 @@ async fn node(request: service::HttpRequest) -> ResultPage<Markup, FatalError> {
} }
fn before_prepare_body(page: &mut Page) { fn before_prepare_body(page: &mut Page) {
page.alter_body_classes(ClassesOp::Add, &["test-node"]); page.alter_body_classes(ClassesOp::Add, "test-node");
} }

View file

@ -4,8 +4,8 @@
//! by the theme) and user classes (for customizing components based on application styles). //! 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 //! 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 //! added using [Add]. Operations to [Remove], [Replace] or [ReplaceIfExists] a class, as well as
//! classes, are also provided. //! to [Reset] user classes, are also provided.
//! //!
//! Although the order of the classes is irrelevant (<https://stackoverflow.com/a/1321712>), default //! Although the order of the classes is irrelevant (<https://stackoverflow.com/a/1321712>), default
//! classes will be presented before user classes and duplicate classes will not be allowed. //! classes will be presented before user classes and duplicate classes will not be allowed.
@ -18,6 +18,7 @@ pub enum ClassesOp {
Add, Add,
Remove, Remove,
Replace(String), Replace(String),
ReplaceIfExists(String),
Reset, Reset,
} }
@ -38,21 +39,24 @@ impl Classes {
// Classes BUILDER. // Classes BUILDER.
#[fn_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<String>) -> &mut Self {
let classes: String = classes.into();
let classes: Vec<&str> = classes.split_ascii_whitespace().collect();
match op { match op {
ClassesOp::SetDefault => { ClassesOp::SetDefault => {
self.0.retain(|(_, t)| t.ne(&ClassType::Default)); self.0.retain(|(_, t)| t.ne(&ClassType::Default));
self.add(classes, 0, ClassType::Default); self.add(&classes, 0, ClassType::Default);
} }
ClassesOp::AddDefault => { ClassesOp::AddDefault => {
let pos = match self.0.iter().position(|(_, t)| t.eq(&ClassType::User)) { let pos = match self.0.iter().position(|(_, t)| t.eq(&ClassType::User)) {
Some(pos) => pos, Some(pos) => pos,
None => self.0.len(), None => self.0.len(),
}; };
self.add(classes, pos, ClassType::Default); self.add(&classes, pos, ClassType::Default);
} }
ClassesOp::Add => { ClassesOp::Add => {
self.add(classes, self.0.len(), ClassType::User); self.add(&classes, self.0.len(), ClassType::User);
} }
ClassesOp::Remove => { ClassesOp::Remove => {
for name in classes { for name in classes {
@ -60,9 +64,18 @@ impl Classes {
} }
} }
ClassesOp::Replace(class) => { 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)) { if let Some(pos) = self.0.iter().position(|(c, _)| c.eq(&class)) {
let (_, class_type) = self.0.remove(pos); let (_, class_type) = self.0.remove(pos);
self.add(classes, pos, class_type); self.add(&classes, pos, class_type);
} }
} }
ClassesOp::Reset => { ClassesOp::Reset => {
@ -73,11 +86,10 @@ impl Classes {
} }
#[inline] #[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 { for class in classes {
let class: String = class.to_string(); if !class.is_empty() && self.0.iter().position(|(c, _)| c.eq(class)).is_none() {
if !class.is_empty() && self.0.iter().position(|(c, _)| c.eq(&class)).is_none() { self.0.insert(pos, (class.to_string(), class_type.clone()));
self.0.insert(pos, (class, class_type.clone()));
pos = pos + 1; pos = pos + 1;
} }
} }

View file

@ -38,7 +38,7 @@ impl Page {
properties : Vec::new(), properties : Vec::new(),
favicon : None, favicon : None,
context : Context::new(request), 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(), regions : ComponentsRegions::new(),
template : "default".to_owned(), template : "default".to_owned(),
} }
@ -83,7 +83,7 @@ impl Page {
} }
#[fn_builder] #[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<String>) -> &mut Self {
self.body_classes.alter_value(op, classes); self.body_classes.alter_value(op, classes);
self self
} }