Añade estilos en línea para los componentes
This commit is contained in:
parent
2cb23317a8
commit
37dbcaa84a
4 changed files with 88 additions and 14 deletions
|
|
@ -77,7 +77,7 @@ impl ComponentTrait for Anchor {
|
|||
target=[target]
|
||||
{
|
||||
(self.left_icon().render(context))
|
||||
(" ")(*self.html())(" ")
|
||||
(*self.html())
|
||||
(self.right_icon().render(context))
|
||||
}
|
||||
}
|
||||
|
|
@ -195,13 +195,13 @@ impl Anchor {
|
|||
|
||||
pub fn alter_left_icon(&mut self, icon: Icon) -> &mut Self {
|
||||
self.left_icon.clear();
|
||||
self.left_icon.add(icon);
|
||||
self.left_icon.add(icon.with_inline_style("margin-right", Some("5px")));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn alter_right_icon(&mut self, icon: Icon) -> &mut Self {
|
||||
self.right_icon.clear();
|
||||
self.right_icon.add(icon);
|
||||
self.right_icon.add(icon.with_inline_style("margin-left", Some("5px")));
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,17 +3,19 @@ use crate::prelude::*;
|
|||
pub const ICON_COMPONENT: &str = "pagetop::component::icon";
|
||||
|
||||
pub struct Icon {
|
||||
renderable: fn() -> bool,
|
||||
weight : isize,
|
||||
icon_name : String,
|
||||
renderable : fn() -> bool,
|
||||
weight : isize,
|
||||
classes : Classes,
|
||||
inline_styles: InlineStyles,
|
||||
}
|
||||
|
||||
impl ComponentTrait for Icon {
|
||||
fn new() -> Self {
|
||||
Icon {
|
||||
renderable: render_always,
|
||||
weight : 0,
|
||||
icon_name : "question-circle-fill".to_owned(),
|
||||
renderable : render_always,
|
||||
weight : 0,
|
||||
classes : Classes::new_with_default("bi-question-circle-fill"),
|
||||
inline_styles: InlineStyles::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -35,8 +37,7 @@ impl ComponentTrait for Icon {
|
|||
"/theme/icons/bootstrap-icons.css?ver=1.8.2"
|
||||
));
|
||||
|
||||
let name = concat_string!("bi-", self.icon_name);
|
||||
html! { i class=(name) {}; }
|
||||
html! { i class=[self.classes()] style=[self.inline_styles()] {}; }
|
||||
}
|
||||
|
||||
fn as_ref_any(&self) -> &dyn AnyComponent {
|
||||
|
|
@ -70,6 +71,16 @@ impl Icon {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self {
|
||||
self.alter_classes(classes, op);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_inline_style(mut self, style: &str, value: Option<&str>) -> Self {
|
||||
self.alter_inline_style(style, value);
|
||||
self
|
||||
}
|
||||
|
||||
// Icon ALTER.
|
||||
|
||||
pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self {
|
||||
|
|
@ -83,13 +94,27 @@ impl Icon {
|
|||
}
|
||||
|
||||
pub fn alter_icon_name(&mut self, name: &str) -> &mut Self {
|
||||
self.icon_name = name.to_owned();
|
||||
self.classes.alter(concat_string!("bi-", name).as_str(), ClassesOp::SetDefault);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self {
|
||||
self.classes.alter(classes, op);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn alter_inline_style(&mut self, style: &str, value: Option<&str>) -> &mut Self {
|
||||
self.inline_styles.add_style(style, value);
|
||||
self
|
||||
}
|
||||
|
||||
// Icon GETTERS.
|
||||
|
||||
pub fn icon_name(&self) -> &str {
|
||||
&self.icon_name
|
||||
pub fn classes(&self) -> &Option<String> {
|
||||
self.classes.option()
|
||||
}
|
||||
|
||||
pub fn inline_styles(&self) -> Option<String> {
|
||||
self.inline_styles.option()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,3 +16,6 @@ pub use optattr::OptAttr;
|
|||
|
||||
mod classes;
|
||||
pub use classes::{Classes, ClassesOp};
|
||||
|
||||
mod inline_styles;
|
||||
pub use inline_styles::InlineStyles;
|
||||
|
|
|
|||
46
pagetop/src/html/inline_styles.rs
Normal file
46
pagetop/src/html/inline_styles.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
use crate::concat_string;
|
||||
|
||||
struct Style {
|
||||
property: String,
|
||||
inline : String,
|
||||
}
|
||||
|
||||
pub struct InlineStyles(Vec<Style>);
|
||||
|
||||
impl InlineStyles {
|
||||
pub fn new() -> Self {
|
||||
InlineStyles(Vec::new())
|
||||
}
|
||||
|
||||
pub fn add_style(&mut self, property: &str, value: Option<&str>) -> &Self {
|
||||
let property = property.trim();
|
||||
match self.0.iter().position(|s| s.property.eq(property)) {
|
||||
Some(pos) => {
|
||||
self.0.remove(pos);
|
||||
if let Some(value) = value {
|
||||
self.0.insert(pos, Style {
|
||||
property: property.to_owned(),
|
||||
inline : concat_string!(property, ":", value.trim(), ";"),
|
||||
});
|
||||
}
|
||||
},
|
||||
_ => if let Some(value) = value {
|
||||
self.0.push(Style {
|
||||
property: property.to_owned(),
|
||||
inline : concat_string!(property, ":", value.trim(), ";"),
|
||||
});
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn option(&self) -> Option<String> {
|
||||
if self.0.len() == 0 {
|
||||
None
|
||||
} else {
|
||||
let mut inline = "".to_owned();
|
||||
self.0.iter().for_each(|s| inline.push_str(s.inline.as_str()));
|
||||
Some(inline)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue