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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use sauron_vdom::Callback;
use sauron_vdom::{self, diff};
use std::ops::Deref;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::JsCast;
use web_sys::{self, Element, EventTarget, Node, Text};
use apply_patches::patch;
use std::collections::HashMap;
use std::sync::Mutex;
use web_sys::{Event, KeyboardEvent, MouseEvent};
use web_sys::{HtmlInputElement, HtmlTextAreaElement};
mod apply_patches;
use lazy_static::lazy_static;
lazy_static! {
static ref ELEM_UNIQUE_ID: Mutex<u32> = Mutex::new(0);
}
pub type ActiveClosure = HashMap<u32, Vec<Closure<Fn(Event)>>>;
pub struct CreatedNode<T> {
pub node: T,
closures: ActiveClosure,
}
pub struct DomUpdater {
current_vdom: crate::Node,
root_node: Node,
pub active_closures: ActiveClosure,
}
impl<T> CreatedNode<T> {
pub fn without_closures<N: Into<T>>(node: N) -> Self {
CreatedNode {
node: node.into(),
closures: HashMap::with_capacity(0),
}
}
pub fn create_text_node(text: &sauron_vdom::Text) -> Text {
let document = web_sys::window().unwrap().document().unwrap();
document.create_text_node(&text.text)
}
pub fn create_dom_node(vnode: &crate::Node) -> CreatedNode<Node> {
match vnode {
crate::Node::Text(text_node) => {
CreatedNode::without_closures(Self::create_text_node(text_node))
}
crate::Node::Element(element_node) => {
let created_element: CreatedNode<Node> =
Self::create_element_node(element_node).into();
created_element
}
}
}
pub fn create_element_node(velem: &crate::Element) -> CreatedNode<Element> {
let document = web_sys::window().unwrap().document().unwrap();
let element = if let Some(ref namespace) = velem.namespace {
document
.create_element_ns(Some(namespace), &velem.tag)
.unwrap()
} else {
document.create_element(&velem.tag).unwrap()
};
let mut closures = HashMap::new();
velem.attrs.iter().for_each(|(name, value)| {
element
.set_attribute(name, &value.to_string())
.expect("Set element attribute in create element");
});
if !velem.events.is_empty() {
let unique_id = create_unique_identifier();
element
.set_attribute("data-sauron_vdom-id", &unique_id.to_string())
.expect("Could not set attribute on element");
closures.insert(unique_id, vec![]);
velem.events.iter().for_each(
|(event_str, callback): (&String, &Callback<sauron_vdom::Event>)| {
let current_elem: &EventTarget = element.dyn_ref().unwrap();
let closure_wrap: Closure<Fn(Event)> = create_closure_wrap(&callback);
current_elem
.add_event_listener_with_callback(
event_str,
closure_wrap.as_ref().unchecked_ref(),
)
.unwrap();
closures.get_mut(&unique_id).unwrap().push(closure_wrap);
},
);
}
let mut previous_node_was_text = false;
velem.children.iter().for_each(|child| {
match child {
crate::Node::Text(text_node) => {
let current_node = element.as_ref() as &web_sys::Node;
if previous_node_was_text {
let separator = document.create_comment("ptns");
current_node
.append_child(separator.as_ref() as &web_sys::Node)
.unwrap();
}
current_node
.append_child(&Self::create_text_node(&text_node))
.unwrap();
previous_node_was_text = true;
}
crate::Node::Element(element_node) => {
previous_node_was_text = false;
let child = Self::create_element_node(element_node);
let child_elem: Element = child.node;
closures.extend(child.closures);
element.append_child(&child_elem).unwrap();
}
}
});
CreatedNode {
node: element,
closures,
}
}
}
fn create_closure_wrap(callback: &Callback<sauron_vdom::Event>) -> Closure<Fn(Event)> {
let callback_clone = callback.clone();
Closure::wrap(Box::new(move |event: Event| {
let mouse_event: Option<&MouseEvent> = event.dyn_ref();
let key_event: Option<&KeyboardEvent> = event.dyn_ref();
let target: Option<EventTarget> = event.target();
if let Some(mouse_event) = mouse_event {
if event.type_() == "click" {
callback_clone.emit(sauron_vdom::Event::MouseEvent(
sauron_vdom::MouseEvent::Press(
sauron_vdom::MouseButton::Left,
mouse_event.x() as u16,
mouse_event.y() as u16,
),
));
}
} else if let Some(key_event) = key_event {
callback_clone.emit(sauron_vdom::Event::KeyEvent(sauron_vdom::KeyEvent {
key: key_event.key(),
ctrl: key_event.ctrl_key(),
alt: key_event.alt_key(),
shift: key_event.shift_key(),
meta: key_event.meta_key(),
}));
} else if let Some(target) = target {
let input: Option<&HtmlInputElement> = target.dyn_ref();
let textarea: Option<&HtmlTextAreaElement> = target.dyn_ref();
if let Some(input) = input {
callback_clone.emit(sauron_vdom::Event::InputEvent(sauron_vdom::InputEvent {
value: input.value(),
}));
} else if let Some(textarea) = textarea {
callback_clone.emit(sauron_vdom::Event::InputEvent(sauron_vdom::InputEvent {
value: textarea.value(),
}));
} else {
callback_clone.emit(sauron_vdom::Event::Generic(event.type_()));
}
}
}))
}
impl DomUpdater {
pub fn new(current_vdom: crate::Node) -> DomUpdater {
let created_node = CreatedNode::<Node>::create_dom_node(¤t_vdom);
DomUpdater {
current_vdom,
root_node: created_node.node,
active_closures: created_node.closures,
}
}
pub fn new_append_to_mount(current_vdom: crate::Node, mount: &Element) -> DomUpdater {
let created_node: CreatedNode<Node> = CreatedNode::<Node>::create_dom_node(¤t_vdom);
mount
.append_child(&created_node.node)
.expect("Could not append child to mount");
DomUpdater {
current_vdom,
root_node: created_node.node,
active_closures: created_node.closures,
}
}
pub fn new_replace_mount(current_vdom: crate::Node, mount: Element) -> DomUpdater {
let created_node = CreatedNode::<Node>::create_dom_node(¤t_vdom);
mount
.replace_with_with_node_1(&created_node.node)
.expect("Could not replace mount element");
DomUpdater {
current_vdom,
root_node: created_node.node,
active_closures: created_node.closures,
}
}
pub fn update(&mut self, new_vdom: crate::Node) {
let patches = diff(&self.current_vdom, &new_vdom);
let active_closures =
patch(self.root_node.clone(), &self.active_closures, &patches).unwrap();
self.active_closures.extend(active_closures);
self.current_vdom = new_vdom;
}
pub fn root_node(&self) -> Node {
self.root_node.clone()
}
}
fn create_unique_identifier() -> u32 {
let mut elem_unique_id = ELEM_UNIQUE_ID.lock().unwrap();
*elem_unique_id += 1;
*elem_unique_id
}
impl From<CreatedNode<Element>> for CreatedNode<Node> {
fn from(other: CreatedNode<Element>) -> CreatedNode<Node> {
CreatedNode {
node: other.node.into(),
closures: other.closures,
}
}
}
impl<T> Deref for CreatedNode<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.node
}
}