Allow multi form elements

This commit is contained in:
Austen Adler 2021-08-22 14:17:59 -04:00
parent 8a7453d12c
commit 47114718a1
8 changed files with 66 additions and 30 deletions

View File

@ -22,56 +22,92 @@ pub trait FormRender {
fn render(&self) -> String; fn render(&self) -> String;
} }
pub trait InputRender { pub trait InputRender {
fn render(&self, name: &str) -> String; fn render(&self, name: &str, multi_index: Option<usize>) -> String;
} }
impl InputRender for bool { impl InputRender for bool {
fn render(&self, name: &str) -> String { fn render(&self, name: &str, multi_index: Option<usize>) -> String {
format!( format!(
r#"<label for="{name}">{name}</label><input type="checkbox" name="{name}" {}/>"#, r#"<label for="{name}">{name}</label><input type="checkbox" name="{name}" {}{}/>"#,
if *self { " checked" } else { "" }, if *self { " checked" } else { "" },
name = name, if let Some(i) = multi_index {
format!(
r#" name="{name}-{i}" rust-form-multi="{name}""#,
name = name,
i = i
)
} else {
format!(r#" name="{name}""#, name = name)
},
name = name
) )
} }
} }
impl InputRender for Rgb { impl InputRender for Rgb {
fn render(&self, name: &str) -> String { fn render(&self, name: &str, multi_index: Option<usize>) -> String {
format!( format!(
r#"<label for="{name}">{name}</label><input type="color" value="{}" name="{name}" />"#, r#"<label for="{name}">{name}</label><input type="color" value="{}" name="{name}" {}/>"#,
self.to_hex_str(), self.to_hex_str(),
name = name, if let Some(i) = multi_index {
format!(
r#" name="{name}-{i}" rust-form-multi="{name}""#,
name = name,
i = i
)
} else {
format!(r#" name="{name}""#, name = name)
},
name = name
) )
} }
} }
impl InputRender for Vec<Rgb> { impl InputRender for Vec<Rgb> {
fn render(&self, name: &str) -> String { fn render(&self, name: &str, _multi_index: Option<usize>) -> String {
self.iter() self.iter()
.chain(iter::once(&Rgb::default())) .chain(iter::once(&Rgb::default()))
.enumerate() .enumerate()
.fold(String::new(), |acc, (i, c)| { .fold(String::new(), |acc, (i, c)| {
acc + &c.render(&format!("{}-{}", name, i)) + "\n" acc + &c.render(name, Some(i)) + "\n"
}) })
} }
} }
impl InputRender for u8 { impl InputRender for u8 {
fn render(&self, name: &str) -> String { fn render(&self, name: &str, multi_index: Option<usize>) -> String {
format!( format!(
r#"<label for="{name}">{name}</label><input type="number" max="255" min="0" step="1" value="{}" name="{name}" />"#, r#"<label for="{name}">{name}</label><input type="number" max="255" min="0" step="1" value="{}" name="{name}" {}/>"#,
self, self,
name = name, if let Some(i) = multi_index {
format!(
r#" name="{name}-{i}" rust-form-multi="{name}""#,
name = name,
i = i
)
} else {
format!(r#" name="{name}""#, name = name)
},
name = name
) )
} }
} }
impl InputRender for u16 { impl InputRender for u16 {
fn render(&self, name: &str) -> String { fn render(&self, name: &str, multi_index: Option<usize>) -> String {
format!( format!(
r#"<label for="{name}">{name}</label><input type="number" max="65535" min="0" step="1" value="{}" name="{name}" />"#, r#"<label for="{name}">{name}</label><input type="number" max="65535" min="0" step="1" value="{}" name="{name}" {}/>"#,
self, self,
name = name, if let Some(i) = multi_index {
format!(
r#" name="{name}-{i}" rust-form-multi="{name}""#,
name = name,
i = i
)
} else {
format!(r#" name="{name}""#, name = name)
},
name = name
) )
} }
} }

View File

@ -25,9 +25,9 @@ impl Default for CollideParams {
impl FormRender for CollideParams { impl FormRender for CollideParams {
fn render(&self) -> String { fn render(&self) -> String {
[ [
self.left_color.render("left_color"), self.left_color.render("left_color", None),
self.right_color.render("right_color"), self.right_color.render("right_color", None),
self.conjoined_color.render("conjoined_color"), self.conjoined_color.render("conjoined_color", None),
] ]
.concat() .concat()
} }

View File

@ -19,7 +19,7 @@ impl Default for FadeParams {
impl FormRender for FadeParams { impl FormRender for FadeParams {
fn render(&self) -> String { fn render(&self) -> String {
self.color.render("color") self.color.render("color", None)
} }
} }

View File

@ -27,9 +27,9 @@ impl Default for FlashingParams {
impl FormRender for FlashingParams { impl FormRender for FlashingParams {
fn render(&self) -> String { fn render(&self) -> String {
[ [
self.colors.render("colors"), self.colors.render("colors", None),
self.width.render("width"), self.width.render("width", None),
self.tick_rate.render("tick_rate"), self.tick_rate.render("tick_rate", None),
] ]
.concat() .concat()
} }

View File

@ -19,7 +19,7 @@ impl Default for MovingPixelParams {
impl FormRender for MovingPixelParams { impl FormRender for MovingPixelParams {
fn render(&self) -> String { fn render(&self) -> String {
self.color.render("color") self.color.render("color", None)
} }
} }

View File

@ -25,9 +25,9 @@ impl Default for MovingRainbowParams {
impl FormRender for MovingRainbowParams { impl FormRender for MovingRainbowParams {
fn render(&self) -> String { fn render(&self) -> String {
[ [
self.width.render("width"), self.width.render("width", None),
self.forward.render("forward"), self.forward.render("forward", None),
self.skip.render("skip"), self.skip.render("skip", None),
] ]
.concat() .concat()
} }

View File

@ -25,9 +25,9 @@ impl Default for OrbParams {
impl FormRender for OrbParams { impl FormRender for OrbParams {
fn render(&self) -> String { fn render(&self) -> String {
[ [
self.color.render("color"), self.color.render("color", None),
self.center_width.render("center_width"), self.center_width.render("center_width", None),
self.backoff_width.render("backoff_width"), self.backoff_width.render("backoff_width", None),
] ]
.concat() .concat()
} }

View File

@ -19,7 +19,7 @@ impl Default for SolidParams {
impl FormRender for SolidParams { impl FormRender for SolidParams {
fn render(&self) -> String { fn render(&self) -> String {
self.color.render("color") self.color.render("color", None)
} }
} }