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
use crate::{util, App};
use std::collections::HashMap;
use wasm_bindgen::{closure::Closure, JsCast, JsValue};
fn get_path() -> String {
let path = util::window()
.location()
.pathname()
.expect("Can't find pathname");
path[1..path.len()].to_string()
}
pub fn initial<Ms, Mdl>(app: App<Ms, Mdl>, routes: HashMap<String, Ms>) -> App<Ms, Mdl>
where
Ms: Clone + 'static,
Mdl: Clone + 'static,
{
for (route, route_message) in routes.into_iter() {
if route == get_path() {
app.update(route_message);
break;
}
}
app
}
pub fn update_popstate_listener<Ms, Mdl>(app: &App<Ms, Mdl>, routes: HashMap<String, Ms>)
where
Ms: Clone,
Mdl: Clone,
{
let window = util::window();
if let Some(ps_closure) = app.data.popstate_closure.borrow().as_ref() {
(window.as_ref() as &web_sys::EventTarget)
.remove_event_listener_with_callback("popstate", ps_closure.as_ref().unchecked_ref())
.expect("Problem removing old popstate listener");
}
let app_for_closure = app.clone();
let closure = Closure::wrap(Box::new(move |_| {
if let Some(route_message) = routes.get(&get_path()) {
app_for_closure.update(route_message.clone());
}
}) as Box<FnMut(web_sys::Event) + 'static>);
(window.as_ref() as &web_sys::EventTarget)
.add_event_listener_with_callback("popstate", closure.as_ref().unchecked_ref())
.expect("Problem adding popstate listener");
app.data.popstate_closure.replace(Some(closure));
}
pub fn push_route<Ms, Mdl>(app: crate::vdom::App<Ms, Mdl>, path: &str, message: Ms) -> Mdl
where Ms: Clone + 'static, Mdl: 'static + Clone
{
app.add_route(path, message.clone());
let history = util::window().history().expect("Can't find history");
let path = &(String::from("/") + path);
history
.push_state_with_url(&JsValue::null(), "", Some(path))
.expect("Problem pushing state");
let up = (app.cfg.update)(message, app.data.model.borrow().clone());
match up {
crate::vdom::Update::Render(model) => model,
crate::vdom::Update::Skip(model) => model,
}
}