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
use std::cell::RefCell;
use std::rc::Rc;
use console_error_panic_hook;
use js_sys::Reflect;
use log::Level;
use wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys;
use web_sys::Url;
use virtual_dom_rs::prelude::*;
use roost_app;
use roost_app::Msg;
use roost_app::{App, Store};
#[wasm_bindgen]
pub struct Client {
app: App,
dom_updater: DomUpdater,
}
#[wasm_bindgen]
extern "C" {
pub type GlobalJS;
pub static global_js: GlobalJS;
#[wasm_bindgen(method)]
pub fn update(this: &GlobalJS);
#[wasm_bindgen(js_name = "updateHljs")]
pub fn update_hljs();
}
#[wasm_bindgen]
impl Client {
#[wasm_bindgen(constructor)]
pub fn new(initial_state: &str) -> Client {
#[cfg(debug_assertions)]
console_log::init_with_level(Level::Debug).expect("Unable to enable console_log");
console_error_panic_hook::set_once();
let app = App::from_state_json(initial_state);
app.store.borrow_mut().subscribe(Box::new(|| {
web_sys::console::log_1(&"Updating state".into());
global_js.update();
}));
app.store.borrow_mut().set_after_route(Box::new(|new_path| {
let history = window().history().unwrap();
history
.push_state_with_url(&JsValue::null(), "Roost", Some(new_path))
.expect("Unable to push_state_with_url");
}));
let store = Rc::clone(&app.store);
let on_popstate = move |_: web_sys::Event| {
let path = location().pathname().unwrap() + &location().search().unwrap();
store.borrow_mut().msg(&Msg::SetPath(path))
};
let on_popstate = Box::new(on_popstate) as Box<FnMut(_)>;
let on_popstate = Closure::wrap(on_popstate);
window().set_onpopstate(Some(on_popstate.as_ref().unchecked_ref()));
on_popstate.forget();
let root_node = document()
.get_element_by_id("roost-content")
.expect("Unable to find repo-path <div>");
let dom_updater = DomUpdater::new_replace_mount(app.render(), root_node);
update_hljs();
let store = Rc::clone(&app.store);
intercept_relative_links(store);
Client { app, dom_updater }
}
pub fn render(&mut self) {
let vdom = self.app.render();
self.dom_updater.update(vdom);
}
}
fn intercept_relative_links(store: Rc<RefCell<Store>>) {
let on_anchor_click = move |event: web_sys::Event| {
let target = event
.target()
.unwrap()
.dyn_into::<web_sys::Element>()
.unwrap();
let tag_name = target.tag_name();
let tag_name = tag_name.as_str();
if tag_name.to_lowercase() == "a" {
let link = Reflect::get(&target, &"href".into())
.unwrap()
.as_string()
.unwrap();
let link_url = Url::new(link.as_str()).unwrap();
let hostname = location().hostname().unwrap();
let port = location().port().unwrap();
if link_url.hostname() == hostname && link_url.port() == port {
event.prevent_default();
let path = link_url.pathname();
if path.starts_with("/patch/") {
store
.borrow_mut()
.msg(&Msg::SetSelectedPatch(Some(path.split('/').collect::<Vec<&str>>()[2].to_string())));
} else {
store.borrow_mut().msg(&Msg::SetSelectedPatch(None));
}
store.borrow_mut().msg(&Msg::SetPath(path));
}
}
};
let on_anchor_click = Closure::wrap(Box::new(on_anchor_click) as Box<FnMut(_)>);
window()
.add_event_listener_with_callback("click", on_anchor_click.as_ref().unchecked_ref())
.unwrap();
on_anchor_click.forget();
}
fn window() -> web_sys::Window {
web_sys::window().unwrap()
}
fn document() -> web_sys::Document {
window().document().unwrap()
}
fn location() -> web_sys::Location {
document().location().unwrap()
}