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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! Implement a virtual html element
use super::NodeList;
use crate::app::{Application, WeakMain};
use crate::element_events::{EventHandler, Listener};
use wasm_bindgen::JsCast;
enum AttributeValue {
String(String),
// Usize(usize),
Bool(bool),
EventListener(Box<Listener>),
None,
}
/// A node that is rendered as any HTML Element such as `div`, `span`...
pub struct Element {
real_element: web_sys::Element,
attributes: Vec<AttributeValue>,
node_list: NodeList,
}
impl Element {
/// Create new virtual element with given tag.
/// The equivalent real element is also created, but not inserted in real DOM yet.
pub fn new(tag: &str) -> Self {
Self {
real_element: crate::interop::document()
.create_element(tag)
.expect("Create new real element"),
attributes: Vec::new(),
node_list: NodeList::new(),
}
}
/// Reserve memory with given capacities.
pub fn reserve(&mut self, attributes_capacity: usize, nodes_capacity: usize) {
if attributes_capacity > self.attributes.len() {
let more_count = attributes_capacity - self.attributes.len();
self.attributes.reserve(more_count);
}
if nodes_capacity > self.node_list.len() {
let more_count = nodes_capacity - self.node_list.len();
self.node_list.nodes_mut().reserve(more_count);
}
}
/// Borrow mutable self.node_list, and immutable self.real_element
pub fn node_list_mut_and_real_node(&mut self) -> (&mut NodeList, &web_sys::Node) {
(&mut self.node_list, self.real_element.as_ref())
}
/// Insert real node to correct place in its parent' node list
pub fn insert_to(&self, real_parent: &web_sys::Node, next_sibling: Option<&web_sys::Node>) {
crate::error::log_result_error(
real_parent.insert_before(&self.real_element.as_ref(), next_sibling),
);
}
/// Number of attributes
pub fn attribute_count(&self) -> usize {
self.attributes.len()
}
/// Number of child nodes
pub fn node_count(&self) -> usize {
self.node_list.len()
}
/// Set a bool attribute
pub fn no_update_bool_attribute(&mut self, name: &str, value: bool) {
if value {
crate::error::log_result_error(self.real_element.set_attribute(name, ""));
}
}
/// Set/update a bool attribute
pub fn bool_attribute(&mut self, index: usize, name: &str, value: bool) {
if index >= self.attributes.len() {
self.attributes.push(AttributeValue::Bool(value));
if value {
crate::error::log_result_error(self.real_element.set_attribute(name, ""));
}
} else {
let a = unsafe {
// It is safe here because
// index < self.attributes.len()
// is true here
self.attributes.get_unchecked_mut(index)
};
if let AttributeValue::Bool(ref mut current_value) = a {
if *current_value != value {
*current_value = value;
if value {
crate::error::log_result_error(self.real_element.set_attribute(name, ""));
} else {
crate::error::log_result_error(self.real_element.remove_attribute(name));
}
}
} else {
log::error!("Attribute at given index is not a bool")
}
}
}
/// Set a string attribute
pub fn no_update_literal_string_attribute(&mut self, name: &str, value: &str) {
crate::error::log_result_error(self.real_element.set_attribute(name, value));
}
/// Set a string attribute
pub fn no_update_string_attribute(&mut self, name: &str, value: &impl ToString) {
crate::error::log_result_error(self.real_element.set_attribute(name, &value.to_string()));
}
/// Set/update a string attribute
pub fn string_attribute(&mut self, index: usize, name: &str, value: &impl ToString) {
let value = value.to_string();
if index >= self.attributes.len() {
crate::error::log_result_error(self.real_element.set_attribute(name, &value));
self.attributes.push(AttributeValue::String(value));
} else {
let a = unsafe {
// It is safe here because
// index < self.attributes.len()
// is true here
self.attributes.get_unchecked_mut(index)
};
if let AttributeValue::String(ref mut current_value) = a {
if *current_value != value {
crate::error::log_result_error(self.real_element.set_attribute(name, &value));
*current_value = value;
}
}
}
}
// Do not have `no_update_input_checked` here,
// use `no_update_bool_attribute` instead
/// Set/update checkbox's checked
pub fn input_checked(&mut self, index: usize, value: bool) {
if index >= self.attributes.len() {
if value {
crate::error::log_result_error(self.real_element.set_attribute("checked", ""));
}
self.attributes.push(AttributeValue::Bool(value));
} else {
let a = unsafe {
// It is safe here because
// index < self.attributes.len()
// is true here
self.attributes.get_unchecked_mut(index)
};
if let AttributeValue::Bool(ref mut current_value) = a {
if *current_value != value {
*current_value = value;
let input: &web_sys::HtmlInputElement = self.real_element.unchecked_ref();
input.set_checked(value);
}
} else {
log::error!("Attribute at given index is not a bool")
}
}
}
/// Set/update input's value
pub fn input_value(&mut self, index: usize, value: &impl ToString) {
let value = value.to_string();
if index >= self.attributes.len() {
crate::error::log_result_error(self.real_element.set_attribute("value", &value));
self.attributes.push(AttributeValue::String(value));
} else {
let a = unsafe {
// It is safe here because
// index < self.attributes.len()
// is true here
self.attributes.get_unchecked_mut(index)
};
if let AttributeValue::String(ref mut current_value) = a {
if *current_value != value {
let input: &web_sys::HtmlInputElement = self.real_element.unchecked_ref();
input.set_value(&value);
*current_value = value;
}
} else {
log::error!("Attribute at given index is not a string")
}
}
}
/// Set/update select's value
pub fn select_value(&mut self, index: usize, value: &impl ToString) {
if index >= self.attributes.len() {
// For now, just store AttributeValue::None as a placeholder
self.attributes.push(AttributeValue::None);
}
let select: &web_sys::HtmlSelectElement = self.real_element.unchecked_ref();
select.set_value(&value.to_string());
// For now, value for <select> will always be setted.
// If the <select> is created with an empty list of <option>, but with some `value`, then filled, later, with
// a list of <option>s loaded from network (but the `value` is not changed) then the commented code below may
// not work.
/*
let value = value.to_string();
if index >= self.attributes.len() {
crate::error::log_result_error(self.real_element.set_attribute("value", &value));
self.attributes.push(AttributeValue::String(value));
} else {
let a = unsafe {
// It is safe here because
// index < self.attributes.len()
// is true here
self.attributes.get_unchecked_mut(index)
};
if let AttributeValue::String(ref mut current_value) = a {
if *current_value != value {
let select: &web_sys::HtmlSelectElement = self.real_element.unchecked_ref();
select.set_value(&value);
*current_value = value;
}
} else {
log::error!("Attribute at given index is not a string")
}
}*/
}
/// Set/update select's value
pub fn select_optional_value(&mut self, index: usize, value: &Option<String>) {
if index >= self.attributes.len() {
// For now, just store AttributeValue::None as a placeholder
self.attributes.push(AttributeValue::None);
}
let select: &web_sys::HtmlSelectElement = self.real_element.unchecked_ref();
match value {
None => {
select.set_selected_index(-1);
}
Some(value) => {
select.set_value(value);
}
}
// For now, value for <select> will always be setted.
// If the <select> is created with an empty list of <option>, but with some `value`, then filled, later, with
// a list of <option>s loaded from network (but the `value` is not changed) then the commented code below may
// not work.
/*
if index >= self.attributes.len() {
let select: &web_sys::HtmlSelectElement = self.real_element.unchecked_ref();
log::info!("select.options = {}", select.options().length());
match value {
None => {
select.set_selected_index(-1);
self.attributes.push(AttributeValue::None);
}
Some(value) => {
select.set_value(value);
//crate::error::log_result_error(
// self.real_element.set_attribute("value", &value),
//);
self.attributes
.push(AttributeValue::String(value.to_string()));
}
}
} else {
let a = unsafe {
// It is safe here because
// index < self.attributes.len()
// is true here
self.attributes.get_unchecked_mut(index)
};
match (value, &a) {
(None, AttributeValue::String(_)) => {
let select: &web_sys::HtmlSelectElement = self.real_element.unchecked_ref();
select.set_selected_index(-1);
*a = AttributeValue::None;
//log::info!("select.set_selected_index(-1);");
}
(Some(value), AttributeValue::None) => {
let select: &web_sys::HtmlSelectElement = self.real_element.unchecked_ref();
select.set_value(value);
*a = AttributeValue::String(value.to_string());
//log::info!("select.set_value(value);");
}
(Some(value), AttributeValue::String(old_value)) => {
if value != old_value {
let select: &web_sys::HtmlSelectElement = self.real_element.unchecked_ref();
select.set_value(value);
*a = AttributeValue::String(value.to_string());
//log::info!("value != old_value");
} else {
//log::info!("No change");
}
}
(None, AttributeValue::None) => {
// Nothing to do
//log::info!("");
}
_ => {
log::error!("select_optional_value: Attribute type mismatched");
}
}
}*/
}
/// Set/update select's value
pub fn select_index(&mut self, index: usize, value: usize) {
if index >= self.attributes.len() {
// For now, just store AttributeValue::None as a placeholder
self.attributes.push(AttributeValue::None);
}
let select: &web_sys::HtmlSelectElement = self.real_element.unchecked_ref();
select.set_selected_index(value as i32);
}
/// Set/update select's value
pub fn select_optional_index(&mut self, index: usize, value: &Option<usize>) {
if index >= self.attributes.len() {
// For now, just store AttributeValue::None as a placeholder
self.attributes.push(AttributeValue::None);
}
let select: &web_sys::HtmlSelectElement = self.real_element.unchecked_ref();
match value {
None => {
select.set_selected_index(-1);
}
Some(value) => {
select.set_selected_index(*value as i32);
}
}
}
/// Set value for textarea
pub fn no_update_textarea_value(&mut self, value: &str) {
let ta: &web_sys::HtmlTextAreaElement = self.real_element.unchecked_ref();
ta.set_value(value);
}
/// Set/update textarea's value
pub fn textarea_value(&mut self, index: usize, value: &str) {
// Text inside a textarea tends to be big!
// Is there a better way to implement this?
// Maybe: not store it in the self.attributes?
// But check against the self.real_element.value() directly?
if index >= self.attributes.len() {
let ta: &web_sys::HtmlTextAreaElement = self.real_element.unchecked_ref();
ta.set_value(&value);
self.attributes
.push(AttributeValue::String(value.to_string()));
} else {
let a = unsafe {
// It is safe here because
// index < self.attributes.len()
// is true here
self.attributes.get_unchecked_mut(index)
};
if let AttributeValue::String(ref mut current_value) = a {
if *current_value != value {
let input: &web_sys::HtmlTextAreaElement = self.real_element.unchecked_ref();
input.set_value(value);
*current_value = value.to_string();
}
} else {
log::error!("Attribute at given index is not a string")
}
}
}
/// Set a conditional class
pub fn no_update_conditional_class(&mut self, class: &str, on: bool) {
if on {
crate::error::log_result_error(self.real_element.class_list().add_1(class));
}
}
/// Set/update the conditional class
pub fn conditional_class(&mut self, index: usize, class: &str, on: bool) {
if index >= self.attributes.len() {
if on {
crate::error::log_result_error(self.real_element.class_list().add_1(class));
}
self.attributes.push(AttributeValue::Bool(on));
} else {
let a = unsafe {
// It is safe here because
// index < self.attributes.len()
// is true here
self.attributes.get_unchecked_mut(index)
};
if let AttributeValue::Bool(ref mut current_value) = a {
if *current_value != on {
*current_value = on;
if on {
crate::error::log_result_error(self.real_element.class_list().add_1(class));
} else {
crate::error::log_result_error(
self.real_element.class_list().remove_1(class),
);
}
}
} else {
log::error!("Attribute (expected a conditional class) at given index is not a bool")
}
}
}
/// Attach/update event handler
pub fn event_handler<A: Application>(
&mut self,
index: usize,
main: &WeakMain<A>,
mut event_handler: Box<EventHandler<A>>,
) {
let et: &web_sys::EventTarget = self.real_element.as_ref();
if index >= self.attributes.len() {
let el = event_handler.create_event_listener(main);
crate::error::log_result_error(
et.add_event_listener_with_callback(el.event_name(), el.js_function()),
);
self.attributes.push(AttributeValue::EventListener(el));
} else {
let a = unsafe {
// It is safe here because
// index < self.attributes.len()
// is true here
self.attributes.get_unchecked_mut(index)
};
if let AttributeValue::EventListener(ref mut current_listener) = a {
crate::error::log_result_error(et.remove_event_listener_with_callback(
current_listener.event_name(),
current_listener.js_function(),
));
let new_listener = event_handler.create_event_listener(main);
crate::error::log_result_error(et.add_event_listener_with_callback(
new_listener.event_name(),
new_listener.js_function(),
));
*current_listener = new_listener;
} else {
log::error!("Not an AttributeValue::EventListener");
}
}
}
// Remove real node from real dom and return the next sibling
pub(crate) fn remove_and_get_next_sibling(
&mut self,
real_parent: &web_sys::Node,
) -> Option<web_sys::Node> {
let next_sibling: Option<web_sys::Node> = {
let node: &web_sys::Node = self.real_element.as_ref();
node.next_sibling()
};
crate::error::log_result_error(real_parent.remove_child(self.real_element.as_ref()));
next_sibling
}
// Remove real node from real dom
pub(crate) fn remove_real_node(&mut self, real_parent: &web_sys::Node) {
crate::error::log_result_error(real_parent.remove_child(&self.real_element.as_ref()));
}
/// Get next sibling of the last node
pub fn get_next_sibling(&self) -> Option<web_sys::Node> {
let node: &web_sys::Node = self.real_element.as_ref();
node.next_sibling()
}
/// Get first real node
pub fn get_first_real_node(&self) -> Option<&web_sys::Node> {
Some(self.real_element.as_ref())
}
/// Clone everything except for tracked attributes, and for-loop content
pub(super) fn clone(&self, real_parent: &web_sys::Node) -> Self {
let clone = self.start_clone();
crate::error::log_result_error(real_parent.append_child(clone.real_element.as_ref()));
clone
}
/// Start cloning self and all childs
pub(super) fn start_clone(&self) -> Self {
let real_element: web_sys::Element = self
.real_element
.clone_node_with_deep(false)
.expect("clone real_element")
.unchecked_into();
let node_list = self.node_list.clone(real_element.as_ref());
Self {
real_element,
attributes: Vec::with_capacity(self.attributes.capacity()),
node_list,
}
}
}