use super::{layout::into_node, render_child};
use crate::render::{Render, escape};
use crate::screen::{Component, Node};
use serde_json::{Value as Json, json};
into_node!(Live);
pub struct Live {
topic: &'static str,
nodes: Vec<Node>,
}
impl Live {
pub fn new(topic: &'static str) -> Self {
Live {
topic,
nodes: Vec::new(),
}
}
#[allow(
clippy::should_implement_trait,
reason = "a builder method that appends a child component; it is not arithmetic and `add` is the public name"
)]
pub fn add(mut self, node: impl Into<Node>) -> Self {
self.nodes.push(node.into());
self
}
}
impl Component for Live {
fn render(&self, out: &mut String, r: &Render) {
out.push_str(&format!(
r#"<div class="z-live" data-zeyn-live="{}">"#,
escape(self.topic)
));
for (i, node) in self.nodes.iter().enumerate() {
render_child(out, r, i, node);
}
out.push_str("</div>");
}
fn schema(&self) -> Json {
json!({ "type": "live", "topic": self.topic, "nodes": self.nodes.iter().map(Node::schema).collect::<Vec<_>>() })
}
fn children(&self) -> &[Node] {
&self.nodes
}
}