-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathlearning-paths.astro
212 lines (171 loc) · 5.11 KB
/
learning-paths.astro
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
---
import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
import { getCollection, type CollectionEntry } from "astro:content";
import { marked } from "marked";
import { CardGrid, LinkTitleCard, Description } from "~/components";
const frontmatter = {
title: "Learning paths",
description:
"Learning paths guide you through modules and projects so you can get started with Cloudflare as quickly as possible.",
template: "splash",
} as const;
const learningPaths = await getCollection("learning-paths");
function lpGroups(paths: Array<CollectionEntry<"learning-paths">>) {
const groups = paths.flatMap((p) => p.data.product_group ?? []);
const additional = paths.flatMap((p) => p.data.additional_groups ?? []);
const unique = [...new Set(groups.concat(additional))];
return unique.sort();
}
function lpProducts(paths: Array<CollectionEntry<"learning-paths">>) {
const products = paths.flatMap((p) => p.data.products ?? []);
const unique = [...new Set(products)];
return unique.sort();
}
const groups = lpGroups(learningPaths);
const products = lpProducts(learningPaths);
---
<StarlightPage frontmatter={frontmatter}>
<Description>
{frontmatter.description}
</Description>
<div class="flex">
<div class="w-1/4 pr-4 hidden lg:block">
<div class="border-b-2 border-accent-600 dark:border-accent-200">
<h2>Filters</h2>
</div>
<div id="areas-filter">
<p>
<strong>Product areas</strong>
</p>
{
groups.map((group) => (
<>
<input type="checkbox" id={group} />
<label for={group}>{group}</label>
<br />
</>
))
}
</div>
<div id="products-filter">
<p>
<strong>Products</strong>
</p>
{
products.map((product) => (
<>
<input type="checkbox" id={product} />
<label for={product}>{product}</label>
<br />
</>
))
}
</div>
</div>
<div id="lp-grid" class="w-full">
<CardGrid>
{
learningPaths
.sort((a, b) => a.data.priority - b.data.priority)
.map((lp) => (
<div
class="[&>article]:h-full [&>article]:w-full"
data-groups={JSON.stringify(
[lp.data.product_group].concat(
lp.data.additional_groups ?? [],
),
)}
data-products={JSON.stringify(lp.data.products)}
>
<LinkTitleCard
title={lp.data.title}
href={lp.data.path}
set:html={marked.parse(lp.data.description)}
/>
</div>
))
}
</CardGrid>
</div>
</div>
</StarlightPage>
<script>
function getAreaFilters(): NodeListOf<HTMLInputElement> | undefined {
const groupFiltersContainer =
document.querySelector<HTMLDivElement>("#areas-filter");
if (!groupFiltersContainer) return undefined;
const groupFilters =
groupFiltersContainer.querySelectorAll<HTMLInputElement>("input");
if (!groupFilters) return undefined;
return groupFilters;
}
function getProductFilters(): NodeListOf<HTMLInputElement> | undefined {
const groupFiltersContainer =
document.querySelector<HTMLDivElement>("#products-filter");
if (!groupFiltersContainer) return undefined;
const groupFilters =
groupFiltersContainer.querySelectorAll<HTMLInputElement>("input");
if (!groupFilters) return undefined;
return groupFilters;
}
function getLearningPaths(): NodeListOf<HTMLDivElement> | undefined {
const learningPathsContainer =
document.querySelector<HTMLDivElement>("#lp-grid");
if (!learningPathsContainer) return undefined;
const learningPaths =
learningPathsContainer.querySelectorAll<HTMLDivElement>("[data-groups]");
if (!learningPaths) return undefined;
return learningPaths;
}
function filterProducts() {
const areaFilters = getAreaFilters();
const productFilters = getProductFilters();
if (!areaFilters || !productFilters) return;
const checkedAreas: Array<string> = [];
const checkedProducts: Array<string> = [];
for (const filter of areaFilters) {
if (filter.checked) {
checkedAreas.push(filter.id);
}
}
for (const filter of productFilters) {
if (filter.checked) {
checkedProducts.push(filter.id);
}
}
const learningPaths = getLearningPaths();
if (!learningPaths) return;
if (checkedAreas.length === 0 && checkedProducts.length === 0) {
learningPaths.forEach((card) => {
card.style.display = "";
});
return;
}
for (const learningPath of learningPaths) {
if (!learningPath.dataset.groups || !learningPath.dataset.products)
continue;
const groups: Array<string> = JSON.parse(learningPath.dataset.groups);
const products: Array<string> = JSON.parse(learningPath.dataset.products);
let show = true;
show = checkedAreas.some((v) => {
return groups.includes(v);
});
if (!show) {
show = checkedProducts.some((v) => {
return products.includes(v);
});
}
if (show) {
learningPath.style.display = "";
} else {
learningPath.style.display = "none";
}
}
}
getAreaFilters()?.forEach((input) => {
input.addEventListener("change", filterProducts);
});
getProductFilters()?.forEach((input) => {
input.addEventListener("change", filterProducts);
});
</script>