💥 Consolidate component class name management
This commit is contained in:
parent
d1bfb2a799
commit
45d8c55e61
19 changed files with 178 additions and 165 deletions
|
|
@ -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"]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,12 +78,12 @@ 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 => &[],
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
@ -91,31 +91,30 @@ impl ThemeTrait for Bulmix {
|
|||
let col = component_as_mut::<grid::Column>(component);
|
||||
col.alter_classes(
|
||||
ClassesOp::SetDefault,
|
||||
concat_string!(
|
||||
&[
|
||||
"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",
|
||||
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"
|
||||
)
|
||||
.as_str(),
|
||||
"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"]);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -123,7 +123,7 @@ impl Anchor {
|
|||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
@ -134,8 +134,8 @@ impl Anchor {
|
|||
self.classes.alter_value(
|
||||
ClassesOp::SetDefault,
|
||||
match self.anchor_type {
|
||||
AnchorType::Button => "btn btn-primary",
|
||||
_ => "",
|
||||
AnchorType::Button => &["btn", "btn-primary"],
|
||||
_ => &[],
|
||||
},
|
||||
);
|
||||
self
|
||||
|
|
|
|||
|
|
@ -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: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_inner_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
|
||||
pub fn alter_inner_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.inner_classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,8 +29,8 @@ 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")
|
||||
.with_classes(ClassesOp::SetDefault, &["btn", "btn-primary"])
|
||||
.with_classes(ClassesOp::Add, &["form-button"])
|
||||
}
|
||||
|
||||
fn handle(&self) -> Handle {
|
||||
|
|
@ -76,7 +76,10 @@ 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
|
||||
|
|
@ -84,7 +87,10 @@ 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
|
||||
|
|
@ -105,7 +111,7 @@ impl Button {
|
|||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,8 +24,8 @@ 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")
|
||||
.with_classes(ClassesOp::SetDefault, &["form-item"])
|
||||
.with_classes(ClassesOp::Add, &["form-type-date"])
|
||||
}
|
||||
|
||||
fn handle(&self) -> Handle {
|
||||
|
|
@ -90,7 +90,7 @@ impl Date {
|
|||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,8 +42,8 @@ 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"])
|
||||
.with_classes(ClassesOp::Add, &["form-type-textfield"])
|
||||
.with_size(Some(60))
|
||||
.with_maxlength(Some(128))
|
||||
}
|
||||
|
|
@ -116,7 +116,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 +125,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 +134,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 +143,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 +152,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,7 +173,7 @@ impl Input {
|
|||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
@ -183,7 +183,7 @@ impl Input {
|
|||
self.name.alter_value(name);
|
||||
self.alter_classes(
|
||||
ClassesOp::SetDefault,
|
||||
concat_string!("form-item form-item-", name).as_str(),
|
||||
&[concat_string!("form-item form-item-", name)],
|
||||
);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
@ -117,19 +117,19 @@ impl Column {
|
|||
#[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),
|
||||
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.size = size;
|
||||
self
|
||||
|
|
|
|||
|
|
@ -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: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -133,7 +133,7 @@ impl Heading {
|
|||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
@ -153,19 +153,19 @@ impl Heading {
|
|||
#[rustfmt::skip]
|
||||
#[fn_builder]
|
||||
pub fn alter_display(&mut self, display: HeadingDisplay) -> &mut Self {
|
||||
self.display = display;
|
||||
self.classes.alter_value(
|
||||
ClassesOp::SetDefault,
|
||||
match self.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 => "",
|
||||
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 => &[],
|
||||
},
|
||||
);
|
||||
self.display = display;
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
@ -63,13 +63,13 @@ impl Icon {
|
|||
self.icon_name = name.to_owned();
|
||||
self.alter_classes(
|
||||
ClassesOp::SetDefault,
|
||||
concat_string!("bi-", self.icon_name).as_str(),
|
||||
&[concat_string!("bi-", self.icon_name)],
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ impl Paragraph {
|
|||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
|
||||
pub fn alter_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
@ -103,18 +103,18 @@ impl Paragraph {
|
|||
#[rustfmt::skip]
|
||||
#[fn_builder]
|
||||
pub fn alter_display(&mut self, display: ParagraphDisplay) -> &mut Self {
|
||||
self.display = display;
|
||||
self.classes.alter_value(
|
||||
ClassesOp::SetDefault,
|
||||
match self.display {
|
||||
ParagraphDisplay::XxLarge => "fs-2",
|
||||
ParagraphDisplay::Large => "fs-3",
|
||||
ParagraphDisplay::Medium => "fs-4",
|
||||
ParagraphDisplay::Small => "fs-5",
|
||||
ParagraphDisplay::XxSmall => "fs-6",
|
||||
ParagraphDisplay::Normal => "",
|
||||
match display {
|
||||
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;
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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"]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,27 @@
|
|||
//! **Classes** implements a *helper* for dynamically adding class names to components.
|
||||
//!
|
||||
//! This *helper* differentiates between default classes (generally associated with styles provided
|
||||
//! 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.
|
||||
//!
|
||||
//! 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.
|
||||
|
||||
use crate::fn_builder;
|
||||
|
||||
pub enum ClassesOp {
|
||||
SetDefault,
|
||||
AddDefault,
|
||||
Add,
|
||||
Remove,
|
||||
Replace(String),
|
||||
Reset,
|
||||
SetDefault,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
#[derive(Clone, PartialEq)]
|
||||
enum ClassType {
|
||||
Default,
|
||||
User,
|
||||
|
|
@ -25,56 +38,51 @@ impl Classes {
|
|||
// Classes BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_value(&mut self, op: ClassesOp, classes: &str) -> &mut Self {
|
||||
let classes: Vec<String> = classes
|
||||
.split_ascii_whitespace()
|
||||
.map(|c| c.to_owned())
|
||||
.collect();
|
||||
|
||||
pub fn alter_value(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
match op {
|
||||
ClassesOp::Add => {
|
||||
for class in classes {
|
||||
if self.0.iter().position(|(c, _)| c.eq(&class)).is_none() {
|
||||
self.0.push((class, ClassType::User));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ClassesOp::Remove => {
|
||||
for class in classes {
|
||||
self.0
|
||||
.retain(|(c, t)| c.ne(&class) || t.ne(&ClassType::User));
|
||||
}
|
||||
}
|
||||
|
||||
ClassesOp::Replace(value) => {
|
||||
for class in classes {
|
||||
match self.0.iter().position(|(c, _)| c.eq(&value)) {
|
||||
Some(pos) => {
|
||||
self.0.remove(pos);
|
||||
self.0.insert(pos, (class, ClassType::User));
|
||||
}
|
||||
_ => self.0.push((class, ClassType::User)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ClassesOp::Reset => self.0.retain(|(_, t)| t.eq(&ClassType::Default)),
|
||||
|
||||
ClassesOp::SetDefault => {
|
||||
self.0.retain(|(_, t)| t.eq(&ClassType::User));
|
||||
let mut pos = 0;
|
||||
for class in classes {
|
||||
if self.0.iter().position(|(c, _)| c.eq(&class)).is_none() {
|
||||
self.0.insert(pos, (class, ClassType::Default));
|
||||
pos = pos + 1;
|
||||
}
|
||||
self.0.retain(|(_, t)| t.ne(&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);
|
||||
}
|
||||
ClassesOp::Add => {
|
||||
self.add(classes, self.0.len(), ClassType::User);
|
||||
}
|
||||
ClassesOp::Remove => {
|
||||
for name in classes {
|
||||
self.0.retain(|(c, _)| c.ne(&name.to_string()));
|
||||
}
|
||||
}
|
||||
ClassesOp::Replace(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);
|
||||
}
|
||||
}
|
||||
ClassesOp::Reset => {
|
||||
self.0.retain(|(_, t)| t.ne(&ClassType::User));
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn add(&mut self, classes: &[impl ToString], 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()));
|
||||
pos = pos + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Classes GETTERS.
|
||||
|
||||
pub fn get(&self) -> Option<String> {
|
||||
|
|
|
|||
|
|
@ -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: &str) -> &mut Self {
|
||||
pub fn alter_body_classes(&mut self, op: ClassesOp, classes: &[impl ToString]) -> &mut Self {
|
||||
self.body_classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue