use crate::dom;
use std::ops::Deref;
#[derive(PartialEq)]
pub struct Attributes {
pub attributes: dom::Attributes,
last_attribute: String,
}
impl Deref for Attributes {
type Target = dom::Attributes;
fn deref(&self) -> &dom::Attributes {
&self.attributes
}
}
impl Into<dom::Attributes> for Attributes {
fn into(self) -> dom::Attributes {
self.attributes
}
}
impl Attributes {
pub fn new() -> Self {
Self {
attributes: dom::Attributes::new(),
last_attribute: "".to_string(),
}
}
pub fn string(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
let name = name.into();
self.last_attribute = name.clone();
self.attributes.add(name, dom::Value::Str(value.into()));
self
}
pub fn nut(mut self, name: impl Into<String>, value: u64) -> Self {
let name = name.into();
self.last_attribute = name.clone();
self.attributes.add(name, dom::Value::Nut(value));
self
}
pub fn int(mut self, name: impl Into<String>, value: i64) -> Self {
let name = name.into();
self.last_attribute = name.clone();
self.attributes.add(name, dom::Value::Int(value));
self
}
pub fn flag(mut self, name: impl Into<String>) -> Self {
let name = name.into();
self.last_attribute = name.clone();
self.attributes.set(name);
self
}
pub fn delimit_with(mut self, dlm: impl Into<String>) -> Self {
self.attributes.delimit(&self.last_attribute, dlm);
self
}
pub fn checked(self) -> Self {
self.flag("checked")
}
pub fn class(self, name: impl Into<String>) -> Self {
self.string("class", name)
}
pub fn draggable(self, val: bool) -> Self {
if val {
self.string("draggable", "true")
} else {
self.string("draggable", "false")
}
}
pub fn hidden(self) -> Self {
self.flag("hidden")
}
pub fn href(self, uri: impl Into<String>) -> Self {
self.string("href", uri)
}
pub fn id(self, name: impl Into<String>) -> Self {
self.string("id", name)
}
pub fn placeholder(self, value: impl Into<String>) -> Self {
self.string("placeholder", value)
}
pub fn selected(self) -> Self {
self.flag("selected")
}
pub fn style(self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.string("style", name.into() + ":" + &value.into())
}
pub fn title(self, name: impl Into<String>) -> Self {
self.string("title", name)
}
pub fn type_(self, name: impl Into<String>) -> Self {
self.string("type", name)
}
pub fn value(self, value: impl Into<String>) -> Self {
self.string("value", value)
}
}