hirola_core/
generic_node.rs

1use std::{cell::RefCell, future::Future};
2
3use crate::prelude::Render;
4
5pub trait GenericNode: std::fmt::Debug + Clone + PartialEq + std::cmp::Eq + 'static {
6    /// Create a new element node.
7    fn element(tag: &str) -> Self;
8
9    /// Create a new text node.
10    fn text_node(text: &str) -> Self;
11
12    /// Create a new fragment (list of nodes). A fragment is not necessarily wrapped around by an element.
13    fn fragment() -> Self;
14
15    /// Create a marker (dummy) node. For [`DomNode`], this is implemented by creating an empty comment node.
16    /// This is used, for example, in [`Keyed`] and [`Indexed`] for scenarios where you want to push a new item to the
17    /// end of the list. If the list is empty, a dummy node is needed to store the position of the component.
18    fn marker() -> Self;
19
20    /// Sets an attribute on a node.
21    fn set_attribute(&self, name: &str, value: &str);
22
23    /// Appends a child to the node's children.
24    fn append_child(&self, child: &Self);
25
26    /// Insert a new child node to this node's children. If `reference_node` is `Some`, the child will be inserted
27    /// before the reference node. Else if `None`, the child will be inserted at the end.
28    fn insert_child_before(&self, new_node: &Self, reference_node: Option<&Self>);
29
30    /// Remove a child node from this node's children.
31    fn remove_child(&self, child: &Self);
32
33    /// Replace a child node from this node's children with a new child node.
34    fn replace_child(&self, old: &Self, new: &Self);
35
36    /// Insert a new node before this node.
37    fn insert_sibling_before(&self, child: &Self);
38
39    /// Returns the parent node, or `None` if detached.
40    fn parent_node(&self) -> Option<Self>;
41
42    /// Returns the next sibling, or `None` if this node is the last sibling.
43    fn next_sibling(&self) -> Option<Self>;
44
45    /// Remove this node from the tree.
46    fn remove_self(&self);
47
48    /// Update inner text of the node. If the node has elements, all the elements are replaced with a new text node.
49    fn update_inner_text(&self, text: &str);
50
51    /// Replace all the children in a node with a new node
52    fn replace_children_with(&self, node: &Self);
53
54    fn effect(&self, future: impl Future<Output = ()> + 'static);
55
56    fn children(&self) -> RefCell<Vec<Self>>;
57
58    fn append_render(&self, render: impl Render<Self> + 'static) {
59        Box::new(render).render_into(self).unwrap();
60    }
61}
62
63pub trait EventListener<Handler> {
64    fn event(&self, name: &str, handler: Handler);
65}
66
67/// Allows you to acquire a node during template processing
68pub trait NodeReference {
69    type Target;
70    fn try_get(&self) -> Option<Self::Target>;
71    fn set(&self, node: Self::Target);
72}