1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
pub mod vcomponent;
pub mod velement;
pub mod vlist;
pub mod vnode;
pub mod vtext;

pub use self::vcomponent::VComponent;
pub use self::velement::VElement;
pub use self::vlist::VList;
pub use self::vnode::VNode;
pub use self::vtext::VText;

pub(crate) mod dom {
    use std::borrow::Cow;

    use gloo::events::EventListener;
    use gloo::utils::{body, document};
    use web_sys::{Element, Event, Node, Text};

    use crate::utils::debug;

    pub const ROOT_ELEMENT_ID: &str = "walrust-root";

    pub fn get_root_element() -> Node {
        Node::from(
            document()
                .get_element_by_id(ROOT_ELEMENT_ID)
                .unwrap_or_else(|| {
                    let message = format!(
                        "There was no '{}' element, adding default one",
                        ROOT_ELEMENT_ID
                    );
                    debug::log(message);
                    let root = document().create_element("div").unwrap();
                    set_attribute(&root, "id", ROOT_ELEMENT_ID);
                    append_child(&body(), &root);
                    root
                }),
        )
    }

    pub fn create_element(local_name: &str) -> Element {
        document()
            .create_element(local_name)
            .expect("Couldnt create new element")
    }

    pub fn create_text_node(data: &str) -> Text {
        document().create_text_node(data)
    }

    pub fn remove_node(node: &Node) {
        let ancestor = node.parent_node().expect("Node does not have a parent");
        self::remove_child(&ancestor, node);
    }

    pub fn append_child(ancestor: &Node, child: &Node) -> Node {
        ancestor
            .append_child(child)
            .expect("Couldnt append child to node")
    }

    pub fn replace_child(ancestor: &Node, old_child: &Node, child: &Node) -> Node {
        ancestor
            .replace_child(child, old_child)
            .expect("Couldnt replace child with a new node")
    }

    pub fn remove_child(ancestor: &Node, child: &Node) -> Node {
        ancestor.remove_child(child).expect("Couldnt remove child")
    }

    pub fn set_attribute(el: &Element, name: &str, value: &str) {
        el.set_attribute(name, value)
            .expect("Couldnt set attribute")
    }

    pub fn remove_attribute(el: &Element, name: &str) {
        el.remove_attribute(name).expect("Couldnt remove attribute")
    }

    pub fn create_event_listener<F>(
        element: &Element,
        event_type: Cow<'static, str>,
        callback: F,
    ) -> EventListener
    where
        F: FnMut(&Event) + 'static,
    {
        EventListener::new(element, event_type, callback)
    }
}