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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#[macro_use]
extern crate cfg_if;

use std::collections::BTreeMap;

use maplit::*;
use wasm_bindgen::prelude::*;
use web_sys::{Document, Node};

cfg_if! {
    // When the `console_error_panic_hook` feature is enabled, we can call the
    // `set_panic_hook` function to get better error messages if we ever panic.
    if #[cfg(feature = "console_error_panic_hook")] {
        extern crate console_error_panic_hook;
        use console_error_panic_hook::set_once as set_panic_hook;
    }
}

cfg_if! {
    // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
    // allocator.
    if #[cfg(feature = "wee_alloc")] {
        extern crate wee_alloc;
        #[global_allocator]
        static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
    }
}

// Called by our JS entry point
#[wasm_bindgen]
pub fn run() {
    use std::borrow::Borrow;
    let document: Document = web_sys::window().unwrap().document().unwrap();
    let body = document.borrow().body().unwrap();
    let body: &web_sys::Node = body.as_ref();

    /*
    let element: Element = ui! {
        <div id="container">
            <input value="foo" type="text"/>
            <a href="/bar"/>
            <span>hello world now</span>
        </div>
    };
    */

    let element = Element {
        ty: "div".to_string(),
        inner: Inner::Children(vec![
            Element {
                ty: "input".to_string(),
                props: Properties {
                    attrs: btreemap! {
                        "value".to_string() => "foo".to_string(),
                        "type".to_string() => "text".to_string(),
                    },
                },
                ..Default::default()
            },
            Element {
                ty: "a".to_string(),
                props: Properties {
                    attrs: btreemap! {
                        "href".to_string() => "/bar".to_string(),
                    },
                },
                ..Default::default()
            },
            Element {
                ty: "span".to_string(),
                inner: Inner::Text("hello world now".to_string()),
                ..Default::default()
            },
        ]),
        props: Properties {
            attrs: btreemap! {
                "id".to_string() => "container".to_string(),
            },
        },
        ..Default::default()
    };

    render(element, &body);
}

fn render(element: Element, container: &Node) {
    let instance: Instance = element.into();
    instance.reconcile(container);

    /*
    let rootInstance = null;
    
    function render(element, container) {
      const prevInstance = rootInstance;
      const nextInstance = reconcile(container, prevInstance, element);
      rootInstance = nextInstance;
    }
    
    */
}

#[derive(Default)]
pub struct Element {
    ty: String,
    props: Properties,
    inner: Inner<Element, String>,
}

enum Inner<SELF, TEXT> {
    Children(Vec<SELF>),
    Text(TEXT),
}

impl<S, T> Default for Inner<S, T> {
    fn default() -> Self {
        Inner::Children(vec![])
    }
}

#[derive(Default)]
struct Properties {
    attrs: BTreeMap<String, String>,
    // TODO(anp) add listeners
}

use web_sys::Element as DomElement;

impl Properties {
    fn set(&self, elem: &DomElement) {
        for (key, value) in &self.attrs {
            elem.set_attribute(key, value).unwrap();
        }
    }

    fn remove(&self, elem: &DomElement) {
        for key in self.attrs.keys() {
            elem.remove_attribute(key).unwrap();
        }
    }
}

pub struct Instance {
    ty: String,
    props: Properties,

    dom: web_sys::Element,
    inner: Inner<Instance, web_sys::Text>,
}

impl Instance {
    fn reconcile(&self, parent_dom: &Node) {
        // function reconcile(parentDom, instance, element) {
        //   if (instance == null) {
        //     // Create instance
        //     const newInstance = instantiate(element);
        //     parentDom.appendChild(newInstance.dom);
        //     return newInstance;
        //   } else if (element == null) {
        //     // Remove instance
        //     parentDom.removeChild(instance.dom);
        //     return null;
        //   } else if (instance.element.type === element.type) {
        //     // Update instance
        //     updateDomProperties(instance.dom, instance.element.props, element.props);
        //     instance.childInstances = reconcileChildren(instance, element);
        //     instance.element = element;
        //     return instance;
        //   } else {
        //     // Replace instance
        //     const newInstance = instantiate(element);
        //     parentDom.replaceChild(newInstance.dom, instance.dom);
        //     return newInstance;
        //   }
        // }

        // function reconcileChildren(instance, element) {
        //   const dom = instance.dom;
        //   const childInstances = instance.childInstances;
        //   const nextChildElements = element.props.children || [];
        //   const newChildInstances = [];
        //   const count = Math.max(childInstances.length, nextChildElements.length);
        //   for (let i = 0; i < count; i++) {
        //     const childInstance = childInstances[i];
        //     const childElement = nextChildElements[i];
        //     const newChildInstance = reconcile(dom, childInstance, childElement);
        //     newChildInstances.push(newChildInstance);
        //   }
        //   return newChildInstances.filter(instance => instance != null);
        // }
    }
}

impl From<Element> for Instance {
    fn from(elem: Element) -> Self {
        let Element { ty, props, inner } = elem;
        let document: Document = web_sys::window().unwrap().document().unwrap();
        let dom: web_sys::Element = document.create_element(&ty).unwrap().into();
        let dom_node: &Node = dom.as_ref();

        props.set(&dom);

        let inner = match inner {
            Inner::Children(children) => Inner::Children(
                children
                    .into_iter()
                    .map(|child| {
                        let instance: Instance = child.into();
                        dom_node.append_child(instance.dom.as_ref()).unwrap();
                        instance
                    })
                    .collect::<Vec<Instance>>(),
            ),
            Inner::Text(text) => {
                let text: web_sys::Text = document.create_text_node(&text);
                let text_node: &Node = text.as_ref();
                let dom_node: &Node = dom.as_ref();
                dom_node.append_child(&text_node).unwrap();

                Inner::Text(text)
            }
        };

        Instance {
            ty,
            props,
            dom,
            inner,
        }
    }
}