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
use self::SignatureCollapse::*;
use super::*;
pub trait SignatureContainer {
fn get_signature(&self) -> Html;
fn get_id(&self) -> String;
fn get_class(&self) -> String;
fn get_children(&self) -> Option<Html> {
None
}
fn get_source(&self) -> String {
String::new()
}
fn get_link(&self) -> String {
String::new()
}
}
#[derive(Clone, Debug, Properties)]
pub struct SignatureData<T: Clone + SignatureContainer> {
pub data: T,
}
#[derive(Clone, Debug)]
pub struct SignatureComponent<T>
where
T: 'static + Clone + SignatureContainer,
{
data: T,
link: ComponentLink<Self>,
collapse: Option<bool>,
children: Option<Html>,
}
#[derive(Copy, Clone, Debug)]
pub enum SignatureCollapse {
CollapseClick,
}
impl<T> Component for SignatureComponent<T>
where
T: 'static + SignatureContainer + Clone,
{
type Message = SignatureCollapse;
type Properties = SignatureData<T>;
fn create(data: Self::Properties, link: ComponentLink<Self>) -> Self {
let children = data.data.get_children();
let collapse = match children {
Some(_) => Some(true),
None => None,
};
Self { data: data.data, link, collapse, children }
}
fn update(&mut self, _: Self::Message) -> ShouldRender {
if let Some(state) = self.collapse {
self.collapse = Some(!state);
}
true
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
let id = self.data.get_id();
let class = self.data.get_class();
let subclass = format!("{}-items", class);
let src = SourceLink(self.data.get_source());
let code = self.data.get_signature();
let anchor = Anchor(format!("#{}", id));
let collapse = match self.collapse {
None => html! {
<span class="collapse-toggle">{"[x]"}</span>
},
Some(true) => html! {
<span class="collapse-toggle clickable" onclick=self.link.callback(|_| CollapseClick)>{"[-]"}</span>
},
Some(false) => html! {
<span class="collapse-toggle clickable" onclick=self.link.callback(|_| CollapseClick)>{"[+]"}</span>
},
};
let style = match self.collapse {
None => "display: flex;",
Some(true) => "display: flex;",
Some(false) => "display: none;",
};
match self.children.clone() {
Some(inner) => html! {
<>
<div id=id class=class style="display: flex;">
{code}
{anchor}
{FlexCenterExpansion}
{src}
{collapse}
</div>
<div class=subclass style=style>{inner}</div>
</>
},
None => html! {
<div id=id class=class style="display: flex;">
{code}
{anchor}
{FlexCenterExpansion}
{src}
{collapse}
</div>
},
}
}
}