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) {
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(
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::<grid::Column>(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::<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(
"/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",

View file

@ -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<String>) -> &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
}

View file

@ -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<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}

View file

@ -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<String>) -> &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<String>) -> &mut Self {
self.inner_classes.alter_value(op, classes);
self
}

View file

@ -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<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}

View file

@ -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<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}

View file

@ -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<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}

View file

@ -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<String>) -> &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
}

View file

@ -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<String>) -> &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
}

View file

@ -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<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}

View file

@ -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<String>) -> &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;

View file

@ -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<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}

View file

@ -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<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}

View file

@ -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<String>) -> &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;

View file

@ -45,5 +45,5 @@ async fn node(request: service::HttpRequest) -> ResultPage<Markup, FatalError> {
}
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).
//!
//! 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 (<https://stackoverflow.com/a/1321712>), 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<String>) -> &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;
}
}

View file

@ -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<String>) -> &mut Self {
self.body_classes.alter_value(op, classes);
self
}