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
#![allow(unused_macros)]
use std::collections::HashMap;
use std::panic;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
pub mod dom_types;
pub mod fetch;
#[macro_use]
pub mod shortcuts;
pub mod storage;
mod vdom;
mod websys_bridge;
#[macro_use]
extern crate serde_derive;
pub fn to_input(target: &web_sys::EventTarget ) -> &web_sys::HtmlInputElement {
target.dyn_ref::<web_sys::HtmlInputElement>().expect("Unable to cast as an input element")
}
pub fn to_textarea(target: &web_sys::EventTarget ) -> &web_sys::HtmlTextAreaElement {
target.dyn_ref::<web_sys::HtmlTextAreaElement>().expect("Unable to cast as a textarea element")
}
pub fn to_select(target: &web_sys::EventTarget ) -> &web_sys::HtmlSelectElement {
target.dyn_ref::<web_sys::HtmlSelectElement>().expect("Unable to cast as a select element")
}
pub fn to_html_el(target: &web_sys::EventTarget ) -> &web_sys::HtmlElement {
target.dyn_ref::<web_sys::HtmlElement>().expect("Unable to cast as an HTML element")
}
pub fn to_kbevent(event: &web_sys::Event ) -> &web_sys::KeyboardEvent {
event.dyn_ref::<web_sys::KeyboardEvent>().expect("Unable to cast as a keyboard event")
}
pub fn document() -> web_sys::Document {
web_sys::window()
.expect("Can't find window")
.document()
.expect("Can't find document")
}
pub fn run<Ms, Mdl>(model: Mdl, update: fn(Ms, Mdl) -> Mdl,
view: fn(Mdl) -> dom_types::El<Ms>, mount_point_id: &str, route_map: Option<HashMap<&str, Ms>>)
where Ms: Clone + Sized + 'static, Mdl: Clone + Sized + 'static
{
let app = vdom::App::new(model.clone(), update, view, mount_point_id);
let mut topel_vdom = (app.data.view)(model);
app.setup_vdom(&mut topel_vdom, 0, 0);
vdom::attach_listeners(&mut topel_vdom, app.mailbox());
websys_bridge::attach_els(&mut topel_vdom, &app.data.mount_point);
app.data.main_el_vdom.replace(topel_vdom);
if let Some(r_map) = route_map {
let window = web_sys::window().expect("no global `window` exists");
let path = window.location().pathname().expect("Can't find pathname");
for (route, route_message) in r_map.clone().into_iter() {
if route == &path {
app.update_dom(route_message);
break;
}
}
let mut r_map2 = HashMap::new();
for (route, msg) in r_map {
r_map2.insert(route.to_string(), msg);
}
let history_closure = Closure::wrap(
Box::new(move |event: web_sys::Event| {
let event = event.dyn_into::<web_sys::PopStateEvent>()
.expect("Unable to cast as a PopStateEvent");
let window = web_sys::window().expect("no global `window` exists");
let path = window.location().pathname().expect("Can't find pathname");
let path_trimmed = &path[1..path.len()].to_string();
if let Some(route_message) = r_map2.get(path_trimmed) {
app.update_dom(route_message.clone());
}
})
as Box<FnMut(web_sys::Event) + 'static>,
);
(window.as_ref() as &web_sys::EventTarget)
.add_event_listener_with_callback("popstate", history_closure.as_ref().unchecked_ref())
.expect("Problem adding popstate listener");
history_closure.forget(); }
panic::set_hook(Box::new(console_error_panic_hook::hook));
}
pub fn push_route(path: &str) {
let window = web_sys::window().expect("No global window exists");
let history = window.history().expect("Can't find history");
history.push_state_with_url(&JsValue::from_str(""), "", Some(path));
}
pub fn empty<Ms: Clone>() -> dom_types::El<Ms> {
let mut el = dom_types::El::empty(dom_types::Tag::Del);
el.add_attr("dummy-element".into(), "true".into());
el
}
pub fn log<S: ToString>(text: S) {
web_sys::console::log_1(&text.to_string().into());
}
pub mod prelude {
pub use crate::dom_types::{El, UpdateEl, simple_ev, input_ev, keyboard_ev, raw_ev};
pub use std::collections::HashMap;
}