forked from fitzgen/dodrio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.rs
229 lines (204 loc) · 7.29 KB
/
todo.rs
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
//! Type definition and `dodrio::Render` implementation for a single todo item.
use crate::keys;
use dodrio::{Cached, Node, Render, RenderContext, RootRender, VdomWeak};
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use wasm_bindgen::{prelude::*, JsCast};
/// A single todo item.
#[derive(Default)]
pub struct Todo<C> {
inner: Cached<TodoInner<C>>,
}
#[derive(Serialize, Deserialize)]
struct TodoInner<C> {
id: usize,
title: String,
completed: bool,
#[serde(skip)]
edits: Option<String>,
#[serde(skip)]
_controller: PhantomData<C>,
}
/// Actions on a single todo item that can be triggered from the UI.
pub trait TodoActions {
/// Toggle the completion state of the todo item with the given id.
fn toggle_completed(root: &mut dyn RootRender, vdom: VdomWeak, id: usize);
/// Delete the todo item with the given id.
fn delete(root: &mut dyn RootRender, vdom: VdomWeak, id: usize);
/// Begin editing the todo item with the given id.
fn begin_editing(root: &mut dyn RootRender, vdom: VdomWeak, id: usize);
/// Update the edits for the todo with the given id.
fn update_edits(root: &mut dyn RootRender, vdom: VdomWeak, id: usize, edits: String);
/// Finish editing the todo with the given id.
fn finish_edits(root: &mut dyn RootRender, vdom: VdomWeak, id: usize);
/// Cancel editing the todo with the given id.
fn cancel_edits(root: &mut dyn RootRender, vdom: VdomWeak, id: usize);
}
impl<C> Todo<C> {
/// Construct a new `Todo` with the given identifier and title.
pub fn new<S: Into<String>>(id: usize, title: S) -> Self {
let title = title.into();
let completed = false;
let edits = None;
Todo {
inner: Cached::new(TodoInner {
id,
title,
completed,
edits,
_controller: PhantomData,
}),
}
}
/// Set this todo item's id.
pub fn set_id(&mut self, id: usize) {
if id != self.inner.id {
Cached::invalidate(&self.inner);
}
self.inner.id = id;
}
/// Is this `Todo` complete?
pub fn is_complete(&self) -> bool {
self.inner.completed
}
/// Mark the `Todo` as complete or not.
pub fn set_complete(&mut self, to: bool) {
if to != self.inner.completed {
Cached::invalidate(&self.inner);
}
self.inner.completed = to;
}
/// Get this todo's title.
pub fn title(&self) -> &str {
&self.inner.title
}
/// Set this todo item's title.
pub fn set_title<S: Into<String>>(&mut self, title: S) {
let title = title.into();
if title != self.inner.title {
Cached::invalidate(&self.inner);
}
self.inner.title = title;
}
/// Set the edits for this todo.
pub fn set_edits<S: Into<String>>(&mut self, edits: Option<S>) {
let edits = edits.map(Into::into);
if edits != self.inner.edits {
Cached::invalidate(&self.inner);
}
self.inner.edits = edits;
}
/// Take this todo's edits, leaving `None` in their place.
pub fn take_edits(&mut self) -> Option<String> {
if self.inner.edits.is_some() {
Cached::invalidate(&self.inner);
}
self.inner.edits.take()
}
}
impl<'a, C: TodoActions> Render<'a> for Todo<C> {
fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
use dodrio::{
builder::*,
bumpalo::{self, collections::String},
};
let id = self.inner.id;
let title = self.inner.edits.as_ref().unwrap_or(&self.inner.title);
let title = bumpalo::format!(in cx.bump, "{}", title).into_bump_str();
li(&cx)
.attr("class", {
let mut class = String::new_in(cx.bump);
if self.inner.completed {
class.push_str("completed ");
}
if self.inner.edits.is_some() {
class.push_str("editing");
}
class.into_bump_str()
})
.children([
div(&cx)
.attr("class", "view")
.children([
input(&cx)
.attr("class", "toggle")
.attr("type", "checkbox")
.bool_attr("checked", self.inner.completed)
.on("click", move |root, vdom, _event| {
C::toggle_completed(root, vdom, id);
})
.finish(),
label(&cx)
.on("dblclick", move |root, vdom, _event| {
C::begin_editing(root, vdom, id);
})
.children([text(title)])
.finish(),
button(&cx)
.attr("class", "destroy")
.on("click", move |root, vdom, _event| {
C::delete(root, vdom, id);
})
.finish(),
])
.finish(),
input(&cx)
.attr("class", "edit")
.attr("value", title)
.attr("name", "title")
.attr(
"id",
bumpalo::format!(in cx.bump, "todo-{}", id).into_bump_str(),
)
.on("input", move |root, vdom, event| {
let input = event
.target()
.unwrap_throw()
.unchecked_into::<web_sys::HtmlInputElement>();
C::update_edits(root, vdom, id, input.value());
})
.on("blur", move |root, vdom, _event| {
C::finish_edits(root, vdom, id);
})
.on("keydown", move |root, vdom, event| {
let event = event.unchecked_into::<web_sys::KeyboardEvent>();
match event.key_code() {
keys::ENTER => C::finish_edits(root, vdom, id),
keys::ESCAPE => C::cancel_edits(root, vdom, id),
_ => {}
}
})
.finish(),
])
.finish()
}
}
impl<C> Default for TodoInner<C> {
fn default() -> Self {
TodoInner {
id: Default::default(),
title: Default::default(),
completed: Default::default(),
edits: Default::default(),
_controller: PhantomData,
}
}
}
impl<C> serde::Serialize for Todo<C> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.inner.serialize(serializer)
}
}
impl<'de, C> Deserialize<'de> for Todo<C> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Todo {
inner: <Cached<TodoInner<C>>>::deserialize(deserializer)?,
})
}
}