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
use super::{VComp, VDiff, VList, VTag, VText};
use html::{Component, Renderable, Scope};
use std::cmp::PartialEq;
use std::fmt;
use stdweb::web::{INode, Node};
pub enum VNode<COMP: Component> {
VTag(VTag<COMP>),
VText(VText<COMP>),
VComp(VComp<COMP>),
VList(VList<COMP>),
VRef(Node),
}
impl<COMP: Component> VDiff for VNode<COMP> {
type Component = COMP;
fn detach(&mut self, parent: &Node) -> Option<Node> {
match *self {
VNode::VTag(ref mut vtag) => vtag.detach(parent),
VNode::VText(ref mut vtext) => vtext.detach(parent),
VNode::VComp(ref mut vcomp) => vcomp.detach(parent),
VNode::VList(ref mut vlist) => vlist.detach(parent),
VNode::VRef(ref node) => {
let sibling = node.next_sibling();
parent
.remove_child(node)
.expect("can't remove node by VRef");
sibling
}
}
}
fn apply(
&mut self,
parent: &Node,
precursor: Option<&Node>,
ancestor: Option<VNode<Self::Component>>,
env: &Scope<Self::Component>,
) -> Option<Node> {
match *self {
VNode::VTag(ref mut vtag) => vtag.apply(parent, precursor, ancestor, env),
VNode::VText(ref mut vtext) => vtext.apply(parent, precursor, ancestor, env),
VNode::VComp(ref mut vcomp) => vcomp.apply(parent, precursor, ancestor, env),
VNode::VList(ref mut vlist) => vlist.apply(parent, precursor, ancestor, env),
VNode::VRef(ref mut node) => {
let sibling = match ancestor {
Some(mut n) => n.detach(parent),
None => None,
};
if let Some(sibling) = sibling {
parent
.insert_before(node, &sibling)
.expect("can't insert element before sibling");
} else {
parent.append_child(node);
}
Some(node.to_owned())
}
}
}
}
impl<COMP: Component> From<VText<COMP>> for VNode<COMP> {
fn from(vtext: VText<COMP>) -> Self {
VNode::VText(vtext)
}
}
impl<COMP: Component> From<VList<COMP>> for VNode<COMP> {
fn from(vlist: VList<COMP>) -> Self {
VNode::VList(vlist)
}
}
impl<COMP: Component> From<VTag<COMP>> for VNode<COMP> {
fn from(vtag: VTag<COMP>) -> Self {
VNode::VTag(vtag)
}
}
impl<COMP: Component> From<VComp<COMP>> for VNode<COMP> {
fn from(vcomp: VComp<COMP>) -> Self {
VNode::VComp(vcomp)
}
}
impl<COMP: Component, T: ToString> From<T> for VNode<COMP> {
fn from(value: T) -> Self {
VNode::VText(VText::new(value.to_string()))
}
}
impl<'a, COMP: Component> From<&'a dyn Renderable<COMP>> for VNode<COMP> {
fn from(value: &'a dyn Renderable<COMP>) -> Self {
value.view()
}
}
impl<COMP: Component> fmt::Debug for VNode<COMP> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
VNode::VTag(ref vtag) => vtag.fmt(f),
VNode::VText(ref vtext) => vtext.fmt(f),
VNode::VComp(_) => "Component<>".fmt(f),
VNode::VList(_) => "List<>".fmt(f),
VNode::VRef(_) => "NodeReference<>".fmt(f),
}
}
}
impl<COMP: Component> PartialEq for VNode<COMP> {
fn eq(&self, other: &VNode<COMP>) -> bool {
match *self {
VNode::VTag(ref vtag_a) => match *other {
VNode::VTag(ref vtag_b) => vtag_a == vtag_b,
_ => false,
},
VNode::VText(ref vtext_a) => match *other {
VNode::VText(ref vtext_b) => vtext_a == vtext_b,
_ => false,
},
_ => {
false
}
}
}
}