use itertools::Itertools;
use leptos::prelude::*;
use crate::{AdicEl, DisplayShape};
pub trait ComponentDisplay: DisplayShape {
fn create_component(
self,
class: Option<String>,
) -> impl IntoView
where Self: Sized {
let class_str = match class {
Some(c) => [self.default_class(), " ".to_string(), c].join(" "),
None => self.default_class(),
};
let viewbox_str = self.viewbox_str();
let comps = self.shape_view();
view!{
<svg class={class_str}
viewBox={viewbox_str}
xmlns="http://www.w3.org/2000/svg"
>
{comps}
</svg>
}
}
fn shape_view(
self,
) -> impl IntoView
where Self: Sized {
self.adic_els().map(|adic_el| match adic_el {
AdicEl::Circle(c) => view! {
<circle class={c.class} cx={c.cx} cy={c.cy} r={c.r}/>
}.into_any(),
AdicEl::Path(p) => view! {
<path class={p.class} d={p.d.into_iter().map(String::from).join(" ")}/>
}.into_any(),
AdicEl::Text(t) => view! {
<text class={t.class} style={t.style}
x={t.x} y={t.y} dx={t.dx} dy={t.dy}
>
{t.content}
</text>
}.into_any(),
}).collect_view()
}
}