-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelement.rs
235 lines (216 loc) · 8.41 KB
/
element.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
230
231
232
233
234
235
use crate::dbg;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
/// Wrapper for `web_sys::Element` to simplify calling different interfaces
pub struct Element {
el: Option<web_sys::Element>,
}
impl From<Element> for Option<web_sys::Node> {
fn from(obj: Element) -> Option<web_sys::Node> {
if let Some(el) = obj.el {
Some(el.into())
} else {
None
}
}
}
impl From<Element> for Option<web_sys::EventTarget> {
fn from(obj: Element) -> Option<web_sys::EventTarget> {
if let Some(el) = obj.el {
Some(el.into())
} else {
None
}
}
}
impl Element {
pub fn qs(selector: &str) -> Option<Element> {
let body: web_sys::Element = web_sys::window()?.document()?.body()?.into();
let el = body.query_selector(selector).ok()?;
Some(Element { el })
}
/// Add event listener to this node
pub fn add_event_listener<T>(&mut self, event_name: &str, handler: T)
where
// TODO rewrite without static
T: 'static + FnMut(web_sys::Event) -> (),
{
let cb = Closure::wrap(Box::new(handler) as Box<FnMut(_)>);
if let Some(el) = self.el.take() {
let el_et: web_sys::EventTarget = el.into();
el_et.add_event_listener_with_callback(event_name, cb.as_ref().unchecked_ref());
cb.forget(); // TODO cycle collect this
if let Ok(el) = wasm_bindgen::JsCast::dyn_into(el_et): Result<web_sys::Element, _> {
self.el = Some(el);
}
}
}
// TODO fix static lifetimes
/// Delegate an event to a selector
pub fn delegate<T>(
&mut self,
selector: &'static str,
event: &str,
mut handler: T,
use_capture: bool,
) where
T: 'static + FnMut(web_sys::Event) -> (),
{
if let Some(el) = self.el.take() {
// if let Some(element_et) = self.into(): Option<web_sys::EventTarget> {
//let element_et: web_sys::EventTarget = el.into();
if let Some(dyn_el) = wasm_bindgen::JsCast::dyn_ref(&el): Option<&web_sys::EventTarget>
{
//let rc_el = Rc::new(el);
//let cl_el = rc_el.clone();
if let Some(window) = web_sys::window() {
if let Some(document) = window.document() {
//let tg_el: web_sys::Element = self.el;
// TODO document selector to the target element
let tg_el = document;
let cb = Closure::wrap(Box::new(move |event: web_sys::Event| {
dbg("got fn call delegated");
if let Some(target_element) = event.target() {
let dyn_target_el: Option<
&web_sys::Node,
> = wasm_bindgen::JsCast::dyn_ref(&target_element);
if let Some(target_element) = dyn_target_el {
if let Ok(potential_elements) =
tg_el.query_selector_all(selector)
{
let mut has_match = false;
/*
dbg(format!(
"len: {} {}",
potential_elements.length(),
selector
).as_str());
*/
for i in 0..potential_elements.length() {
if let Some(el) = potential_elements.get(i) {
if target_element.is_equal_node(Some(&el)) {
has_match = true;
}
break;
}
}
if has_match {
handler(event);
}
}
}
}
}) as Box<FnMut(_)>);
dyn_el.add_event_listener_with_callback_and_bool(
//element_et.add_event_listener_with_callback_and_bool(
event,
cb.as_ref().unchecked_ref(),
use_capture,
);
cb.forget(); // TODO cycle collect
}
}
}
self.el = Some(el);
}
}
/// Find child `Element`s from this node
pub fn qs_from(&mut self, selector: &str) -> Option<Element> {
let mut found_el = None;
if let Some(el) = self.el.take() {
found_el = Some(Element {
el: el.query_selector(selector).ok()?,
});
self.el = Some(el);
}
found_el
}
/// Sets the inner HTML of the `self.el` element
pub fn set_inner_html(&mut self, value: String) {
if let Some(el) = self.el.take() {
el.set_inner_html(&value);
self.el = Some(el);
}
}
/// Sets the text content of the `self.el` element
pub fn set_text_content(&mut self, value: String) {
if let Some(el) = self.el.take() {
if let Some(node) = wasm_bindgen::JsCast::dyn_ref(&el): Option<&web_sys::Node> {
node.set_text_content(Some(&value));
}
self.el = Some(el);
}
}
/// Removes a class list item from the element
///
/// ```
/// e.class_list_remove(String::from("clickable"));
/// // removes the class 'clickable' from e.el
/// ```
pub fn class_list_remove(&mut self, value: String) {
if let Some(el) = self.el.take() {
el.class_list().remove_1(&value);
self.el = Some(el);
}
}
/// Given another `Element` it will remove that child from the DOM from this element
pub fn remove_child(&mut self, mut child: Element) {
if let Some(child_el) = child.el.take() {
if let Some(el) = self.el.take() {
if let Some(el_node) = wasm_bindgen::JsCast::dyn_ref(&el): Option<&web_sys::Node> {
let child_node: web_sys::Node = child_el.into();
el_node.remove_child(&child_node);
}
self.el = Some(el);
}
}
}
/// Sets the whole class value for `self.el`
pub fn set_class_name(&mut self, class_name: String) {
if let Some(el) = self.el.take() {
el.set_class_name(&class_name);
self.el = Some(el);
}
}
/// Sets the visibility for the element in `self.el`
pub fn set_visibility(&mut self, visible: bool) {
if let Some(el) = self.el.take() {
{
let dyn_el: Option<&web_sys::HtmlElement> = wasm_bindgen::JsCast::dyn_ref(&el);
if let Some(el) = dyn_el {
el.set_hidden(!visible);
}
}
self.el = Some(el);
}
}
/// Sets the visibility for the element in `self.el` (The element must be an input)
pub fn set_value(&mut self, value: String) {
if let Some(el) = self.el.take() {
if let Some(el) = wasm_bindgen::JsCast::dyn_ref::<web_sys::HtmlInputElement>(&el) {
el.set_value(&value);
}
self.el = Some(el);
}
}
/* TODO use
pub fn focus(&self) {
if let Some(el) = self.el.take() {
if let Some(el) = wasm_bindgen::JsCast::dyn_ref::<web_sys::HtmlElement>(&el) {
el.focus();
}
self.el = Some(el);
}
}
*/
/// Sets the checked state for the element in `self.el` (The element must be an input)
pub fn set_checked(&mut self, checked: bool) {
if let Some(el) = self.el.take() {
if let Some(el) = wasm_bindgen::JsCast::dyn_ref::<web_sys::HtmlInputElement>(&el) {
el.set_checked(checked);
}
self.el = Some(el);
}
}
}