-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathnavigation.rs
More file actions
189 lines (169 loc) · 5.28 KB
/
navigation.rs
File metadata and controls
189 lines (169 loc) · 5.28 KB
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
use maud::{html, Markup, Render};
use pointercrate_core::localization::LocaleConfiguration;
use unic_langid::subtags::Region;
pub struct TopLevelNavigationBarItem {
item: NavigationBarItem,
sub_levels: Vec<NavigationBarItem>,
}
impl TopLevelNavigationBarItem {
pub fn new(link: Option<&'static str>, content: Markup) -> Self {
TopLevelNavigationBarItem {
item: NavigationBarItem { link, content },
sub_levels: vec![],
}
}
pub fn with_sub_item(mut self, link: Option<&'static str>, content: Markup) -> Self {
self.sub_levels.push(NavigationBarItem { link, content });
self
}
}
struct NavigationBarItem {
content: Markup,
link: Option<&'static str>,
}
pub struct NavigationBar {
logo_path: &'static str,
items: Vec<TopLevelNavigationBarItem>,
}
impl NavigationBar {
pub fn new(logo_path: &'static str) -> Self {
NavigationBar { logo_path, items: vec![] }
}
pub fn with_item(mut self, item: TopLevelNavigationBarItem) -> Self {
self.items.push(item);
self
}
}
struct NavGroup<T> {
inner: T,
id: Option<&'static str>,
nohide: bool,
}
impl<T> NavGroup<T> {
pub fn new(inner: T) -> Self {
Self {
inner,
id: None,
nohide: false,
}
}
}
impl<T: Render> Render for NavGroup<T> {
fn render(&self) -> Markup {
html! {
div.nav-group.nav-nohide[self.nohide] id = [self.id] {
(self.inner)
}
}
}
}
impl Render for TopLevelNavigationBarItem {
fn render(&self) -> Markup {
html! {
a.nav-item.hover.white href = [self.item.link] {
(self.item.content)
@if !self.sub_levels.is_empty() {
i.fas.fa-sort-down style = "height: 50%; padding-left: 5px" {}
}
}
@if !self.sub_levels.is_empty() {
ul.nav-hover-dropdown {
@for sub_item in &self.sub_levels {
li {
a.white.hover href = [sub_item.link] { (sub_item.content)}
}
}
}
}
}
}
}
impl Render for NavigationBar {
fn render(&self) -> Markup {
html! {
header {
nav.center.collapse.underlined.see-through {
div.nav-icon.nav-nohide style = "margin-right: auto" {
a href = "/" aria-label = "Go to homepage" {
img src = (self.logo_path) style="height:15px" alt="Logo";
}
}
@for item in &self.items {
(NavGroup::new(item))
}
@if let Some(locales_dropdown) = locale_selection_dropdown() {
(locales_dropdown)
}
(NavGroup { // light-dark theme toggle
inner: TopLevelNavigationBarItem::new(
None,
html! {
span.fa {}
}
),
id: Some("theme-toggle"),
nohide: true,
})
div.nav-item.collapse-button.nav-nohide {
div.hamburger.hover {
input type="checkbox"{}
span{}
span{}
span{}
}
}
div.nav-drop-down {
@for item in &self.items {
(item)
}
}
}
div {} // artificial spacing
}
}
}
}
fn flag(region: Option<Region>) -> Markup {
html! [
@if let Some(region) = region {
span.flag-icon style = (format!(r#"background-image: url("/static/demonlist/images/flags/{}.svg");"#, region.as_str().to_ascii_lowercase())) {}
}
]
}
fn locale_selection_dropdown() -> Option<NavGroup<TopLevelNavigationBarItem>> {
let config = LocaleConfiguration::get();
let locales = config.locales();
let active_locale = config.active_locale();
if locales.len() < 2 {
return None;
}
let mut dropdown = TopLevelNavigationBarItem::new(
None,
html! {
span.flex {
(flag(active_locale.region))
span style = "margin-left: 8px" { (active_locale.language.as_str().to_uppercase()) }
}
},
);
for locale in locales {
if locale == active_locale {
// this locale is currently selected, don't add it to the dropdown
continue;
}
dropdown = dropdown.with_sub_item(
None,
html! {
span data-lang = (locale) {
(flag(locale.region))
span style = "margin-left: 10px" { (locale.language.as_str().to_uppercase()) }
}
},
);
}
Some(NavGroup {
inner: dropdown,
id: Some("language-selector"),
nohide: true,
})
}