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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::{collections::HashMap, fmt::Debug, sync::Arc};

use crate::{DomRef, EventHandler};

/// Represents an html tag such as `<div>`, `<span>`, etc.
pub struct HtmlElement {
    pub(crate) tag_name: &'static str,
    pub(crate) attributes: HashMap<String, String>,
    pub(crate) callbacks: HashMap<String, EventHandler>,
    pub(crate) dom_ref: Option<Arc<DomRef>>,
}

impl Debug for HtmlElement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HtmlElement")
            .field("tag_name", &self.tag_name)
            .field("attributes", &self.attributes)
            .field("callbacks", &"Callbacks")
            .field("dom_ref", &self.dom_ref)
            .finish()
    }
}

impl HtmlElement {
    /// Build a new HtmlElement
    /// ```
    /// HtmlElement::new().tag_name("div").build();
    /// ```
    pub fn new() -> HtmlElementBuilder<()> {
        HtmlElementBuilder {
            tag_name: (),
            attributes: HashMap::new(),
            callbacks: HashMap::new(),
            dom_ref: None,
        }
    }

    pub fn tag_name(&self) -> &str {
        &self.tag_name
    }

    /// get a map of all the attributes:
    /// For a `<div id="foo" class="bar">` this would return
    /// `{"id": "foo", "class": "bar"}`
    pub fn attributes(&self) -> &HashMap<String, String> {
        &self.attributes
    }

    /// get a map of all the callbacks / event handlers:
    /// For a `<div on_click=|_| { alert!("clicked")}>` this would return
    /// `{ "click": |event| { alert!("clicked") } }`
    pub fn callbacks(&self) -> &HashMap<String, EventHandler> {
        &self.callbacks
    }

    /// get the dom reference of the element
    pub fn dom_ref(&self) -> &Option<Arc<DomRef>> {
        &self.dom_ref
    }
}

pub struct HtmlElementBuilder<T> {
    pub(crate) tag_name: T,
    pub(crate) attributes: HashMap<String, String>,
    pub(crate) callbacks: HashMap<String, EventHandler>,
    pub(crate) dom_ref: Option<Arc<DomRef>>,
}

impl HtmlElementBuilder<()> {
    pub fn tag_name(self, tag_name: &'static str) -> HtmlElementBuilder<&'static str> {
        HtmlElementBuilder {
            tag_name,
            attributes: self.attributes,
            callbacks: self.callbacks,
            dom_ref: self.dom_ref,
        }
    }
}

impl<T> HtmlElementBuilder<T> {
    /// set one specific attribute of the element:
    /// ```
    /// HtmlElement::new().tag_name("div").attr("id", "foo").build();
    /// ```
    /// builds a `<div id="foo">`
    pub fn attr<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Into<String>,
        V: Into<String>,
        V: Into<String>,
    {
        self.attributes.insert(key.into(), value.into());
        self
    }

    /// Set one specific callback / event handler:
    /// ```
    /// HtmlElement::new().tag_name("div").on("click", |event| { alert!("clicked") }).build();
    /// ```
    /// builds a div that will send an alert on the "click"-event
    pub fn on<K, C>(mut self, key: K, handler: C) -> Self
    where
        K: Into<String>,
        C: Fn(web_sys::Event) + Send + Sync + 'static,
    {
        self.callbacks.insert(key.into(), Box::new(handler));
        self
    }

    /// Get a dom reference to the element:
    /// Use `use_ref::<DomRef>()` to obtain the `Arc<DomRef>` to pass to this method.
    pub fn dom_ref(mut self, dom_ref: Arc<DomRef>) -> Self {
        self.dom_ref = Some(dom_ref);
        self
    }
}

impl HtmlElementBuilder<&'static str> {
    pub fn build(self) -> HtmlElement {
        HtmlElement {
            tag_name: self.tag_name,
            attributes: self.attributes,
            callbacks: self.callbacks,
            dom_ref: self.dom_ref,
        }
    }
}

impl PartialEq for HtmlElement {
    fn eq(&self, other: &Self) -> bool {
        self.tag_name == other.tag_name
            && self.attributes == other.attributes
            && self.callbacks.is_empty()
            && other.callbacks.is_empty()
            && self.dom_ref == other.dom_ref
    }
}