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
use std::sync::mpsc::{Sender, Receiver, channel};
use stdweb::Value;
use stdweb::web::{Element, INode, EventListenerHandle, document};
use stdweb::web::event::{IMouseEvent, IKeyboardEvent};
use virtual_dom::{VNode, VTag, Listener};
fn clear_element(element: &Element) {
while let Some(child) = element.last_child() {
element.remove_child(&child).expect("can't remove a child");
}
}
pub struct AppSender<MSG> {
tx: Sender<MSG>,
bind: Value,
}
impl<MSG> Clone for AppSender<MSG> {
fn clone(&self) -> Self {
AppSender {
tx: self.tx.clone(),
bind: self.bind.clone(),
}
}
}
impl<MSG> AppSender<MSG> {
pub fn send(&mut self, msg: MSG) {
self.tx.send(msg).expect("App lost the receiver!");
let bind = &self.bind;
js! {
var bind = @{bind};
setTimeout(bind.loop);
}
}
}
pub struct App<MSG> {
tx: Sender<MSG>,
rx: Option<Receiver<MSG>>,
bind: Value,
}
impl<MSG: 'static> App<MSG> {
pub fn new() -> Self {
let bind = js! {
return { "loop": function() { } };
};
let (tx, rx) = channel();
App {
tx,
rx: Some(rx),
bind,
}
}
pub fn sender(&mut self) -> AppSender<MSG> {
AppSender {
tx: self.tx.clone(),
bind: self.bind.clone(),
}
}
pub fn mount<CTX, MOD, U, V>(&mut self, context: CTX, model: MOD, update: U, view: V)
where
CTX: 'static,
MOD: 'static,
U: Fn(&mut CTX, &mut MOD, MSG) + 'static,
V: Fn(&MOD) -> Html<MSG> + 'static,
{
self.mount_to("body", context, model, update, view)
}
pub fn mount_to<CTX, MOD, U, V>(&mut self, selector: &str, mut context: CTX, mut model: MOD, update: U, view: V)
where
CTX: 'static,
MOD: 'static,
U: Fn(&mut CTX, &mut MOD, MSG) + 'static,
V: Fn(&MOD) -> Html<MSG> + 'static,
{
let element = document().query_selector(selector)
.expect(format!("can't get node with selector `{}` for rendering", selector).as_str());
clear_element(&element);
let mut messages = Vec::new();
let mut last_frame = VNode::from(view(&model));
last_frame.apply(&element, None, self.sender());
let mut last_frame = Some(last_frame);
let rx = self.rx.take().expect("application runned without a receiver");
let bind = self.bind.clone();
let sender = self.sender();
let mut callback = move || {
messages.extend(rx.try_iter());
for msg in messages.drain(..) {
update(&mut context, &mut model, msg);
}
let mut next_frame = VNode::from(view(&model));
next_frame.apply(&element, last_frame.take(), sender.clone());
last_frame = Some(next_frame);
};
callback();
js! {
var bind = @{bind};
var callback = @{callback};
bind.loop = callback;
}
}
}
pub type Html<MSG> = VTag<MSG>;
macro_rules! impl_action {
($($action:ident($event:ident : $type:ident) -> $ret:ty => $convert:expr)*) => {$(
pub mod $action {
use stdweb::web::{IEventTarget, Element};
use stdweb::web::event::{IEvent, $type};
use super::*;
pub struct Wrapper<F>(Option<F>);
pub type Event = $ret;
impl<F, MSG> From<F> for Wrapper<F>
where
MSG: 'static,
F: Fn($ret) -> MSG + 'static,
{
fn from(handler: F) -> Self {
Wrapper(Some(handler))
}
}
impl<T, MSG> Listener<MSG> for Wrapper<T>
where
MSG: 'static,
T: Fn($ret) -> MSG + 'static,
{
fn kind(&self) -> &'static str {
stringify!($action)
}
fn attach(&mut self, element: &Element, mut sender: AppSender<MSG>)
-> EventListenerHandle {
let handler = self.0.take().expect("tried to attach listener twice");
let this = element.clone();
let listener = move |event: $type| {
debug!("Event handler: {}", stringify!($type));
event.stop_propagation();
let handy_event: $ret = $convert(&this, event);
let msg = handler(handy_event);
sender.send(msg);
};
element.add_event_listener(listener)
}
}
}
)*};
}
impl_action! {
onclick(event: ClickEvent) -> MouseData => |_, event| { MouseData::from(event) }
ondoubleclick(event: DoubleClickEvent) -> MouseData => |_, event| { MouseData::from(event) }
onkeypress(event: KeypressEvent) -> KeyData => |_, event| { KeyData::from(event) }
oninput(event: InputEvent) -> InputData => |this: &Element, _| {
use stdweb::web::html_element::InputElement;
use stdweb::unstable::TryInto;
let input: InputElement = this.clone().try_into().expect("only an InputElement can have an oninput event listener");
let value = input.value().into_string().unwrap_or_else(|| "".into());
InputData { value }
}
}
#[derive(Debug)]
pub struct MouseData {
pub screen_x: f64,
pub screen_y: f64,
pub client_x: f64,
pub client_y: f64,
}
impl<T: IMouseEvent> From<T> for MouseData {
fn from(event: T) -> Self {
MouseData {
screen_x: event.screen_x(),
screen_y: event.screen_y(),
client_x: event.client_x(),
client_y: event.client_y(),
}
}
}
#[derive(Debug)]
pub struct InputData {
pub value: String,
}
#[derive(Debug)]
pub struct KeyData {
pub key: String,
}
impl<T: IKeyboardEvent> From<T> for KeyData {
fn from(event: T) -> Self {
KeyData { key: event.key() }
}
}
#[derive(Debug)]
pub struct Href {
link: String,
}
impl From<String> for Href {
fn from(link: String) -> Self {
Href { link }
}
}
impl<'a> From<&'a str> for Href {
fn from(link: &'a str) -> Self {
Href { link: link.to_owned() }
}
}
impl ToString for Href {
fn to_string(&self) -> String {
self.link.to_owned()
}
}