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
#[macro_export]
macro_rules! elem {
($elem:expr, { $($methods:tt)* }) => {
dominator::apply_methods!(dominator::DomBuilder::new($elem), { $($methods)* }).into_dom()
};
}
#[macro_export]
macro_rules! with_query {
($this:ident, $query:expr => $t:ty, { $($methods:tt)* }) => {
dominator::with_node!($this, element => {
.__internal_transfer_callbacks({
let error_msg = &format!("unable to get element for {}", $query);
let child = element.query_selector($query).expect_throw(error_msg).expect_throw(error_msg);
let child: $t = wasm_bindgen::JsCast::dyn_into(child).expect_throw(error_msg);
dominator::apply_methods!(dominator::DomBuilder::new(child), { $($methods)* })
})
})
};
($this:ident, $query:expr, { $($methods:tt)* }) => {
$crate::with_query!($this, $query => web_sys::HtmlElement, { $($methods)* })
};
}
#[macro_export]
macro_rules! with_data_id {
($this:ident, $id:expr => $t:ty, { $($methods:tt)* }) => {
$crate::with_query!($this, &format!("[data-id='{}']", $id) => $t, { $($methods)* })
};
($this:ident, $id:expr, { $($methods:tt)* }) => {
$crate::with_data_id!($this, $id => web_sys::HtmlElement, { $($methods)* })
};
}
#[macro_export]
macro_rules! dynamic_class_signal {
($this:ident, $signal:expr) => {
dominator::with_node!($this, element => {
.__internal_transfer_callbacks({
dominator::apply_methods!(dominator::DomBuilder::new(element.clone()), {
.future({
let mut old = None;
$signal.for_each(move |class| {
if let Some(old) = old.as_deref() {
element.class_list().remove_1(old).unwrap();
}
if let Some(name) = class.as_deref() {
element.class_list().add_1(&name).unwrap();
}
old = class;
async {}
})
})
})
})
})
};
}
#[macro_export]
macro_rules! html_at_slot {
($name:expr, $slot:expr, { $($rest:tt)* }) => {
dominator::html!($name, {
.attribute("slot", $slot)
$($rest)*
})
};
}
#[macro_export]
macro_rules! make_ts_event {
($literal:literal, $data:ident) => {
paste::item! {
make_custom_event_serde!($literal, [<$data Event>], $data);
cfg_if::cfg_if! {
if #[cfg(feature = "ts_test")] {
use web_sys::CustomEvent;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn [<check_rust_event_ $data>](event:CustomEvent) -> Result<JsValue, JsValue> {
let literal = event.type_();
let event:[<$data Event>] = unsafe {
std::mem::transmute::<CustomEvent, [<$data Event>]>(event)
};
if literal == $literal {
let data:$data = event.data();
let expected = serde_json::to_string(&$data::default()).unwrap();
let got = serde_json::to_string(&data).unwrap();
if expected != got {
Err(JsValue::from_str(&format!("did not match default! should be {} but is {}", expected, got)))
} else {
Ok(JsValue::from_str(&got))
}
} else {
Err(JsValue::from_str(&format!("wrong type! should be {} but is {}", $literal, literal)))
}
}
}
}
}
}
}
#[macro_export]
macro_rules! make_custom_event {
($name:ident, $type:literal) => {
dominator::make_event!($name, $type => web_sys::CustomEvent);
impl $name {
pub fn detail(&self) -> JsValue { self.event.detail() }
}
}
}
#[macro_export]
macro_rules! make_custom_event_serde {
($type:literal, $name:ident, $data:ident) => {
make_custom_event!($name, $type);
impl $name {
pub fn data(&self) -> $data {
serde_wasm_bindgen::from_value(self.detail()).unwrap()
}
}
}
}