💥 New names for optional attributes

This commit is contained in:
Manuel Cillero 2023-10-08 21:04:05 +02:00
parent 7a5c86ac9a
commit 33dff8f085
18 changed files with 167 additions and 143 deletions

View file

@ -28,10 +28,10 @@ type AnchorHtml = TypedComponent<L10n>;
pub struct Anchor {
weight : Weight,
renderable : Renderable,
id : IdentifierValue,
classes : Classes,
id : OptionId,
classes : OptionClasses,
anchor_type: AnchorType,
href : AttributeValue,
href : OptionString,
html : AnchorHtml,
left_icon : AnchorIcon,
right_icon : AnchorIcon,
@ -85,18 +85,18 @@ impl ComponentTrait for Anchor {
}
impl Anchor {
pub fn link(href: &str, html: L10n) -> Self {
pub fn link(href: impl Into<String>, html: L10n) -> Self {
Anchor::new().with_href(href).with_html(html)
}
pub fn button(href: &str, html: L10n) -> Self {
pub fn button(href: impl Into<String>, html: L10n) -> Self {
Anchor::new()
.with_type(AnchorType::Button)
.with_href(href)
.with_html(html)
}
pub fn location(id: &str) -> Self {
pub fn location(id: impl Into<String>) -> Self {
Anchor::new().with_type(AnchorType::Location).with_id(id)
}
@ -115,7 +115,7 @@ impl Anchor {
}
#[fn_builder]
pub fn alter_id(&mut self, id: &str) -> &mut Self {
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.id.alter_value(id);
self
}
@ -140,7 +140,7 @@ impl Anchor {
}
#[fn_builder]
pub fn alter_href(&mut self, href: &str) -> &mut Self {
pub fn alter_href(&mut self, href: impl Into<String>) -> &mut Self {
self.href.alter_value(href);
self
}
@ -177,7 +177,7 @@ impl Anchor {
// Anchor GETTERS.
pub fn classes(&self) -> &Classes {
pub fn classes(&self) -> &OptionClasses {
&self.classes
}
@ -185,7 +185,7 @@ impl Anchor {
&self.anchor_type
}
pub fn href(&self) -> &AttributeValue {
pub fn href(&self) -> &OptionString {
&self.href
}

View file

@ -4,14 +4,16 @@ new_handle!(COMPONENT_BLOCK);
actions_for_component!(Block);
type BlockTitle = TypedComponent<L10n>;
#[rustfmt::skip]
#[derive(Default)]
pub struct Block {
weight : Weight,
renderable: Renderable,
id : IdentifierValue,
classes : Classes,
title : AttributeValue,
id : OptionId,
classes : OptionClasses,
title : BlockTitle,
stuff : ArcComponents,
template : String,
}
@ -45,7 +47,7 @@ impl ComponentTrait for Block {
let id = cx.required_id::<Block>(self.id());
PrepareMarkup::With(html! {
div id=(id) class=[self.classes().get()] {
@if let Some(title) = self.title().get() {
@if let Some(title) = self.title().get().into_string(cx) {
h2 class="block-title" { (title) }
}
div class="block-body" {
@ -64,7 +66,7 @@ impl Block {
// Block BUILDER.
#[fn_builder]
pub fn alter_id(&mut self, id: &str) -> &mut Self {
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.id.alter_value(id);
self
}
@ -88,8 +90,8 @@ impl Block {
}
#[fn_builder]
pub fn alter_title(&mut self, title: &str) -> &mut Self {
self.title.alter_value(title);
pub fn alter_title(&mut self, title: L10n) -> &mut Self {
self.title.set(title);
self
}
@ -112,11 +114,11 @@ impl Block {
// Block GETTERS.
pub fn classes(&self) -> &Classes {
pub fn classes(&self) -> &OptionClasses {
&self.classes
}
pub fn title(&self) -> &AttributeValue {
pub fn title(&self) -> &BlockTitle {
&self.title
}

View file

@ -17,12 +17,12 @@ type ButtonValue = TypedComponent<L10n>;
pub struct Button {
weight : Weight,
renderable : Renderable,
classes : Classes,
classes : OptionClasses,
button_type: ButtonType,
name : AttributeValue,
name : OptionString,
value : ButtonValue,
autofocus : AttributeValue,
disabled : AttributeValue,
autofocus : OptionString,
disabled : OptionString,
template : String,
}
@ -146,7 +146,7 @@ impl Button {
// Button GETTERS.
pub fn classes(&self) -> &Classes {
pub fn classes(&self) -> &OptionClasses {
&self.classes
}
@ -154,7 +154,7 @@ impl Button {
&self.button_type
}
pub fn name(&self) -> &AttributeValue {
pub fn name(&self) -> &OptionString {
&self.name
}
@ -162,11 +162,11 @@ impl Button {
&self.value
}
pub fn autofocus(&self) -> &AttributeValue {
pub fn autofocus(&self) -> &OptionString {
&self.autofocus
}
pub fn disabled(&self) -> &AttributeValue {
pub fn disabled(&self) -> &OptionString {
&self.disabled
}

View file

@ -7,17 +7,17 @@ new_handle!(COMPONENT_DATE);
pub struct Date {
weight : Weight,
renderable : Renderable,
classes : Classes,
name : AttributeValue,
value : AttributeValue,
label : AttributeValue,
placeholder : AttributeValue,
autofocus : AttributeValue,
autocomplete: AttributeValue,
disabled : AttributeValue,
readonly : AttributeValue,
required : AttributeValue,
help_text : AttributeValue,
classes : OptionClasses,
name : OptionString,
value : OptionString,
label : OptionString,
placeholder : OptionString,
autofocus : OptionString,
autocomplete: OptionString,
disabled : OptionString,
readonly : OptionString,
required : OptionString,
help_text : OptionString,
template : String,
}
@ -176,47 +176,47 @@ impl Date {
// Date GETTERS.
pub fn classes(&self) -> &Classes {
pub fn classes(&self) -> &OptionClasses {
&self.classes
}
pub fn name(&self) -> &AttributeValue {
pub fn name(&self) -> &OptionString {
&self.name
}
pub fn value(&self) -> &AttributeValue {
pub fn value(&self) -> &OptionString {
&self.value
}
pub fn label(&self) -> &AttributeValue {
pub fn label(&self) -> &OptionString {
&self.label
}
pub fn placeholder(&self) -> &AttributeValue {
pub fn placeholder(&self) -> &OptionString {
&self.placeholder
}
pub fn autofocus(&self) -> &AttributeValue {
pub fn autofocus(&self) -> &OptionString {
&self.autofocus
}
pub fn autocomplete(&self) -> &AttributeValue {
pub fn autocomplete(&self) -> &OptionString {
&self.autocomplete
}
pub fn disabled(&self) -> &AttributeValue {
pub fn disabled(&self) -> &OptionString {
&self.disabled
}
pub fn readonly(&self) -> &AttributeValue {
pub fn readonly(&self) -> &OptionString {
&self.readonly
}
pub fn required(&self) -> &AttributeValue {
pub fn required(&self) -> &OptionString {
&self.required
}
pub fn help_text(&self) -> &AttributeValue {
pub fn help_text(&self) -> &OptionString {
&self.help_text
}

View file

@ -16,10 +16,10 @@ pub enum FormMethod {
pub struct Form {
weight : Weight,
renderable: Renderable,
id : IdentifierValue,
classes : Classes,
action : AttributeValue,
charset : AttributeValue,
id : OptionId,
classes : OptionClasses,
action : OptionString,
charset : OptionString,
method : FormMethod,
stuff : ArcComponents,
template : String,
@ -91,7 +91,7 @@ impl Form {
}
#[fn_builder]
pub fn alter_id(&mut self, id: &str) -> &mut Self {
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.id.alter_value(id);
self
}
@ -139,15 +139,15 @@ impl Form {
// Form GETTERS.
pub fn classes(&self) -> &Classes {
pub fn classes(&self) -> &OptionClasses {
&self.classes
}
pub fn action(&self) -> &AttributeValue {
pub fn action(&self) -> &OptionString {
&self.action
}
pub fn charset(&self) -> &AttributeValue {
pub fn charset(&self) -> &OptionString {
&self.charset
}

View file

@ -6,8 +6,8 @@ new_handle!(COMPONENT_HIDDEN);
#[derive(Default)]
pub struct Hidden {
weight: Weight,
name : NameValue,
value : AttributeValue,
name : OptionName,
value : OptionString,
}
impl ComponentTrait for Hidden {
@ -58,11 +58,11 @@ impl Hidden {
// Hidden GETTERS.
pub fn name(&self) -> &NameValue {
pub fn name(&self) -> &OptionName {
&self.name
}
pub fn value(&self) -> &AttributeValue {
pub fn value(&self) -> &OptionString {
&self.value
}
}

View file

@ -21,20 +21,20 @@ type InputHelpText = TypedComponent<L10n>;
pub struct Input {
weight : Weight,
renderable : Renderable,
classes : Classes,
classes : OptionClasses,
input_type : InputType,
name : NameValue,
value : AttributeValue,
name : OptionName,
value : OptionString,
label : InputLabel,
size : Option<u16>,
minlength : Option<u16>,
maxlength : Option<u16>,
placeholder : AttributeValue,
autofocus : AttributeValue,
autocomplete: AttributeValue,
disabled : AttributeValue,
readonly : AttributeValue,
required : AttributeValue,
placeholder : OptionString,
autofocus : OptionString,
autocomplete: OptionString,
disabled : OptionString,
readonly : OptionString,
required : OptionString,
help_text : InputHelpText,
template : String,
}
@ -282,7 +282,7 @@ impl Input {
// Input GETTERS.
pub fn classes(&self) -> &Classes {
pub fn classes(&self) -> &OptionClasses {
&self.classes
}
@ -290,11 +290,11 @@ impl Input {
&self.input_type
}
pub fn name(&self) -> &NameValue {
pub fn name(&self) -> &OptionName {
&self.name
}
pub fn value(&self) -> &AttributeValue {
pub fn value(&self) -> &OptionString {
&self.value
}
@ -314,27 +314,27 @@ impl Input {
self.maxlength
}
pub fn placeholder(&self) -> &AttributeValue {
pub fn placeholder(&self) -> &OptionString {
&self.placeholder
}
pub fn autofocus(&self) -> &AttributeValue {
pub fn autofocus(&self) -> &OptionString {
&self.autofocus
}
pub fn autocomplete(&self) -> &AttributeValue {
pub fn autocomplete(&self) -> &OptionString {
&self.autocomplete
}
pub fn disabled(&self) -> &AttributeValue {
pub fn disabled(&self) -> &OptionString {
&self.disabled
}
pub fn readonly(&self) -> &AttributeValue {
pub fn readonly(&self) -> &OptionString {
&self.readonly
}
pub fn required(&self) -> &AttributeValue {
pub fn required(&self) -> &OptionString {
&self.required
}

View file

@ -32,8 +32,8 @@ type HeadingText = TypedComponent<L10n>;
pub struct Heading {
weight : Weight,
renderable : Renderable,
id : IdentifierValue,
classes : Classes,
id : OptionId,
classes : OptionClasses,
heading_type: HeadingType,
text : HeadingText,
display : HeadingDisplay,
@ -127,7 +127,7 @@ impl Heading {
}
#[fn_builder]
pub fn alter_id(&mut self, id: &str) -> &mut Self {
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.id.alter_value(id);
self
}
@ -177,7 +177,7 @@ impl Heading {
// Paragraph GETTERS.
pub fn classes(&self) -> &Classes {
pub fn classes(&self) -> &OptionClasses {
&self.classes
}

View file

@ -8,7 +8,7 @@ pub struct Icon {
weight : Weight,
renderable: Renderable,
icon_name : String,
classes : Classes,
classes : OptionClasses,
}
impl ComponentTrait for Icon {
@ -73,7 +73,7 @@ impl Icon {
self.icon_name.as_str()
}
pub fn classes(&self) -> &Classes {
pub fn classes(&self) -> &OptionClasses {
&self.classes
}
}

View file

@ -20,9 +20,9 @@ pub enum ImageSize {
pub struct Image {
weight : Weight,
renderable: Renderable,
id : IdentifierValue,
classes : Classes,
source : AttributeValue,
id : OptionId,
classes : OptionClasses,
source : OptionString,
size : ImageSize,
}
@ -101,7 +101,7 @@ impl Image {
}
#[fn_builder]
pub fn alter_id(&mut self, id: &str) -> &mut Self {
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.id.alter_value(id);
self
}
@ -126,11 +126,11 @@ impl Image {
// Image GETTERS.
pub fn classes(&self) -> &Classes {
pub fn classes(&self) -> &OptionClasses {
&self.classes
}
pub fn source(&self) -> &AttributeValue {
pub fn source(&self) -> &OptionString {
&self.source
}

View file

@ -18,8 +18,8 @@ pub enum ParagraphDisplay {
pub struct Paragraph {
weight : Weight,
renderable: Renderable,
id : IdentifierValue,
classes : Classes,
id : OptionId,
classes : OptionClasses,
stuff : ArcComponents,
display : ParagraphDisplay,
template : String,
@ -78,7 +78,7 @@ impl Paragraph {
}
#[fn_builder]
pub fn alter_id(&mut self, id: &str) -> &mut Self {
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.id.alter_value(id);
self
}
@ -126,7 +126,7 @@ impl Paragraph {
// Paragraph GETTERS.
pub fn classes(&self) -> &Classes {
pub fn classes(&self) -> &OptionClasses {
&self.classes
}

View file

@ -19,9 +19,9 @@ pub enum WrapperType {
pub struct Wrapper {
weight : Weight,
renderable : Renderable,
id : IdentifierValue,
classes : Classes,
inner_classes: Classes,
id : OptionId,
classes : OptionClasses,
inner_classes: OptionClasses,
wrapper_type : WrapperType,
stuff : ArcComponents,
template : String,
@ -137,7 +137,7 @@ impl Wrapper {
}
#[fn_builder]
pub fn alter_id(&mut self, id: &str) -> &mut Self {
pub fn alter_id(&mut self, id: impl Into<String>) -> &mut Self {
self.id.alter_value(id);
self
}
@ -173,11 +173,11 @@ impl Wrapper {
// Wrapper GETTERS.
pub fn classes(&self) -> &Classes {
pub fn classes(&self) -> &OptionClasses {
&self.classes
}
pub fn inner_classes(&self) -> &Classes {
pub fn inner_classes(&self) -> &OptionClasses {
&self.inner_classes
}

View file

@ -13,17 +13,17 @@ pub use assets::Assets;
mod favicon;
pub use favicon::Favicon;
mod identifier;
pub use identifier::IdentifierValue;
mod opt_id;
pub use opt_id::OptionId;
mod name;
pub use name::NameValue;
mod opt_name;
pub use opt_name::OptionName;
mod attribute;
pub use attribute::AttributeValue;
mod opt_string;
pub use opt_string::OptionString;
mod classes;
pub use classes::{Classes, ClassesOp};
mod opt_classes;
pub use opt_classes::{ClassesOp, OptionClasses};
pub mod unit;
@ -45,7 +45,13 @@ impl PrepareMarkup {
pub fn into_string(self) -> Option<String> {
match self {
PrepareMarkup::None => None,
PrepareMarkup::Text(text) => Some(text.to_string()),
PrepareMarkup::Text(text) => {
if text.is_empty() {
None
} else {
Some(text.to_string())
}
}
PrepareMarkup::With(markup) => Some(markup.into_string()),
}
}

View file

@ -1,4 +1,4 @@
//! **Classes** implements a *helper* for dynamically adding class names to components.
//! **OptionClasses** 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).
@ -18,7 +18,9 @@ pub enum ClassesOp {
Add,
Remove,
Replace(String),
Toggle,
Reset,
Clear,
}
#[derive(Clone, PartialEq)]
@ -28,14 +30,14 @@ enum ClassType {
}
#[derive(Default)]
pub struct Classes(Vec<(String, ClassType)>);
pub struct OptionClasses(Vec<(String, ClassType)>);
impl Classes {
impl OptionClasses {
pub fn new() -> Self {
Classes::default()
OptionClasses::default()
}
// Classes BUILDER.
// OptionClasses BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
@ -58,8 +60,8 @@ impl Classes {
self.add(&classes, self.0.len(), ClassType::User);
}
ClassesOp::Remove => {
for name in classes {
self.0.retain(|(c, _)| c.ne(&name.to_string()));
for class in classes {
self.0.retain(|(c, _)| c.ne(&class.to_string()));
}
}
ClassesOp::Replace(classes_to_replace) => {
@ -79,9 +81,23 @@ impl Classes {
}
self.add(&classes, pos, class_type);
}
ClassesOp::Toggle => {
for class in classes {
if !class.is_empty() {
if let Some(pos) = self.0.iter().position(|(c, _)| c.eq(class)) {
self.0.remove(pos);
} else {
self.0.push((class.to_string(), ClassType::User));
}
}
}
}
ClassesOp::Reset => {
self.0.retain(|(_, t)| t.ne(&ClassType::User));
}
ClassesOp::Clear => {
self.0.clear();
}
}
self
}
@ -96,7 +112,7 @@ impl Classes {
}
}
// Classes GETTERS.
// OptionClasses GETTERS.
pub fn exists(&self, class: impl Into<String>) -> bool {
let class: String = class.into();

View file

@ -1,22 +1,22 @@
use crate::fn_builder;
#[derive(Default)]
pub struct NameValue(String);
pub struct OptionId(String);
impl NameValue {
impl OptionId {
pub fn new() -> Self {
NameValue::default()
OptionId::default()
}
// NameValue BUILDER.
// OptionId BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, value: &str) -> &mut Self {
self.0 = value.trim().replace(' ', "_");
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
self.0 = value.into().trim().replace(' ', "_");
self
}
// NameValue GETTERS.
// OptionId GETTERS.
pub fn get(&self) -> Option<String> {
if self.0.is_empty() {

View file

@ -1,22 +1,22 @@
use crate::fn_builder;
#[derive(Default)]
pub struct AttributeValue(String);
pub struct OptionName(String);
impl AttributeValue {
impl OptionName {
pub fn new() -> Self {
AttributeValue::default()
OptionName::default()
}
// AttributeValue BUILDER.
// OptionName BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, value: &str) -> &mut Self {
self.0 = value.trim().to_owned();
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
self.0 = value.into().trim().replace(' ', "_");
self
}
// AttributeValue GETTERS.
// OptionName GETTERS.
pub fn get(&self) -> Option<String> {
if self.0.is_empty() {

View file

@ -1,22 +1,22 @@
use crate::fn_builder;
#[derive(Default)]
pub struct IdentifierValue(String);
pub struct OptionString(String);
impl IdentifierValue {
impl OptionString {
pub fn new() -> Self {
IdentifierValue::default()
OptionString::default()
}
// IdentifierValue BUILDER.
// OptionString BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, value: &str) -> &mut Self {
self.0 = value.trim().replace(' ', "_");
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
self.0 = value.into().trim().to_owned();
self
}
// IdentifierValue GETTERS.
// OptionString GETTERS.
pub fn get(&self) -> Option<String> {
if self.0.is_empty() {

View file

@ -3,7 +3,7 @@ use crate::base::component::L10n;
use crate::core::component::{ArcComponent, ComponentTrait, TypedComponent};
use crate::core::component::{Context, ContextOp};
use crate::core::theme::ComponentsRegions;
use crate::html::{html, Classes, ClassesOp, Favicon, Markup, DOCTYPE};
use crate::html::{html, ClassesOp, Favicon, Markup, OptionClasses, DOCTYPE};
use crate::response::fatal_error::FatalError;
use crate::{fn_builder, service};
@ -22,7 +22,7 @@ pub struct Page {
properties : Vec<(&'static str, &'static str)>,
favicon : Option<Favicon>,
context : Context,
body_classes: Classes,
body_classes: OptionClasses,
regions : ComponentsRegions,
template : String,
}
@ -37,7 +37,7 @@ impl Page {
properties : Vec::new(),
favicon : None,
context : Context::new(request),
body_classes: Classes::new().with_value(ClassesOp::SetDefault, "body"),
body_classes: OptionClasses::new().with_value(ClassesOp::SetDefault, "body"),
regions : ComponentsRegions::new(),
template : "default".to_owned(),
}
@ -125,7 +125,7 @@ impl Page {
&mut self.context
}
pub fn body_classes(&self) -> &Classes {
pub fn body_classes(&self) -> &OptionClasses {
&self.body_classes
}