-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathfilter.astro
More file actions
213 lines (178 loc) · 6.73 KB
/
filter.astro
File metadata and controls
213 lines (178 loc) · 6.73 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
---
interface FilterSessionsProps {
allTracks?: string[];
allTypes?: string[];
allLevels?: string[];
}
const { allTracks = [], allTypes = [], allLevels = [] }: FilterSessionsProps = Astro.props;
import { Label } from "../form/label";
import { Select } from "../form/select";
---
<form class="mb-12 filter-sessions">
<div class="w-full mb-6">
<Label htmlFor="search">Search</Label>
<input
type="text"
id="search"
name="search"
class="block w-full bg-transparent text-lg h-16 py-2 pr-16 pl-4 border-[3px] border-primary appearance-none focus:outline-none focus:border-black focus-visible:bg-white"
placeholder="Search sessions..."
/>
</div>
<div class="flex flex-wrap gap-x-4 gap-y-4 mb-6">
{allTracks.length > 0 && (
<div>
<Label htmlFor="track">Track</Label>
<Select id="track" name="track">
<option value="">All</option>
{allTracks.map((track: string) => (
<option value={track}>{track}</option>
))}
</Select>
</div>
)}
{allTypes.length > 0 && (
<div>
<Label htmlFor="type">Session type</Label>
<Select id="type" name="type">
<option value="">All</option>
{allTypes.map((type: string) => (
<option value={type}>{type}</option>
))}
</Select>
</div>
)}
{allLevels.length > 0 && (
<div>
<Label htmlFor="level">Level</Label>
<Select id="level" name="level">
<option value="">All</option>
{allLevels.map((level: string) => (
<option value={level}>{level}</option>
))}
</Select>
</div>
)}
</div>
<button
type="button"
id="reset-filters"
class="btn underline font-bold hover:text-primary-hover"
>
Reset Filters
</button>
</form>
<!-- Results info -->
<div id="results-info" class="mb-6 text-lg font-medium"></div>
<script>
interface FilterParams {
track: string;
type: string;
level: string;
search: string;
}
document.addEventListener("DOMContentLoaded", () => {
const forms = document.querySelectorAll<HTMLFormElement>("form.filter-sessions");
const resultsInfo = document.getElementById("results-info");
function getUrlParams(): FilterParams {
const params = new URLSearchParams(window.location.search);
return {
track: params.get("track") || "",
type: params.get("type") || "",
level: params.get("level") || "",
search: params.get("search") || "",
};
}
function setSelectValues(): void {
const { track, type, level, search } = getUrlParams();
forms.forEach((form) => {
const trackSelect = form.querySelector<HTMLSelectElement>("#track");
const typeSelect = form.querySelector<HTMLSelectElement>("#type");
const levelSelect = form.querySelector<HTMLSelectElement>("#level");
const searchInput = form.querySelector<HTMLInputElement>("#search");
if (trackSelect) trackSelect.value = track;
if (typeSelect) typeSelect.value = type;
if (levelSelect) levelSelect.value = level;
if (searchInput) searchInput.value = search;
});
}
function updateUrlParams(track: string, type: string, level: string, search: string): void {
const params = new URLSearchParams();
if (track) params.set("track", track);
if (type) params.set("type", type);
if (level) params.set("level", level);
if (search) params.set("search", search);
const queryString = params.toString();
const newUrl = queryString
? `${window.location.pathname}?${queryString}`
: window.location.pathname;
window.history.pushState({}, "", newUrl);
}
function filterSessions(): void {
forms.forEach((form) => {
const trackSelect = form.querySelector<HTMLSelectElement>("#track");
const typeSelect = form.querySelector<HTMLSelectElement>("#type");
const levelSelect = form.querySelector<HTMLSelectElement>("#level");
const searchInput = form.querySelector<HTMLInputElement>("#search");
const track = trackSelect?.value || "";
const type = typeSelect?.value || "";
const level = levelSelect?.value || "";
const search = searchInput?.value.trim().toLowerCase() || "";
updateUrlParams(track, type, level, search);
const sessionItems = document.querySelectorAll<HTMLLIElement>("ol.sessions > li");
sessionItems.forEach((session) => {
const sessionTrack = session.getAttribute("data-track") || "";
const sessionType = session.getAttribute("data-type") || "";
const sessionLevel = session.getAttribute("data-level") || "";
const sessionText = session.textContent?.toLowerCase() || "";
const trackMatch = track === "" || sessionTrack === track;
const typeMatch = type === "" || sessionType === type;
const levelMatch = level === "" || sessionLevel === level;
const searchMatch = search === "" || sessionText.includes(search);
session.style.display =
trackMatch && typeMatch && levelMatch && searchMatch ? "block" : "none";
});
const trackGroups = document.querySelectorAll<HTMLDivElement>("div.track-group");
trackGroups.forEach((group) => {
const visibleSessions = group.querySelectorAll(
"ol.sessions > li:not([style*='display: none'])"
).length;
group.style.display = visibleSessions > 0 ? "block" : "none";
});
const visibleCount = document.querySelectorAll(
"ol.sessions > li:not([style*='display: none'])"
).length;
if (resultsInfo) {
resultsInfo.textContent = visibleCount > 0
? `${visibleCount} result${visibleCount === 1 ? "" : "s"} found.`
: "No results found.";
}
});
}
function applyFiltersFromUrl(): void {
setSelectValues();
filterSessions();
}
forms.forEach((form) => {
const selectElements = form.querySelectorAll<HTMLSelectElement>("select");
selectElements.forEach((select) => {
select.addEventListener("change", filterSessions);
});
const searchInput = form.querySelector<HTMLInputElement>("#search");
if (searchInput) {
searchInput.addEventListener("input", filterSessions);
}
const resetButton = form.querySelector<HTMLButtonElement>("#reset-filters");
if (resetButton) {
resetButton.addEventListener("click", () => {
selectElements.forEach((select) => {
select.value = "";
});
if (searchInput) searchInput.value = "";
filterSessions();
});
}
});
applyFiltersFromUrl();
});
</script>