|
199 | 199 | }; |
200 | 200 |
|
201 | 201 | /* ========================================================== |
202 | | - 2. Category Filter Pills (homepage) |
| 202 | + 2. Category + JDK Dropdown Filters (homepage) |
203 | 203 | ========================================================== */ |
204 | 204 | const initFilters = () => { |
205 | | - const pills = document.querySelectorAll('.filter-pill'); |
206 | 205 | const cards = document.querySelectorAll('.tip-card'); |
207 | | - if (!pills.length || !cards.length) return; |
| 206 | + if (!cards.length) return; |
208 | 207 |
|
209 | | - pills.forEach(pill => { |
210 | | - pill.addEventListener('click', () => { |
211 | | - const category = pill.dataset.filter || 'all'; |
212 | | - const wasActive = pill.classList.contains('active'); |
| 208 | + let activeCategory = null; |
| 209 | + let activeJdk = null; |
213 | 210 |
|
214 | | - // Update active pill (toggle off if re-clicked) |
215 | | - pills.forEach(p => p.classList.remove('active')); |
216 | | - if (!wasActive) pill.classList.add('active'); |
| 211 | + // LTS cycle ranges: each entry covers all versions introduced since the previous LTS |
| 212 | + const LTS_RANGES = { |
| 213 | + '11': [9, 11], |
| 214 | + '17': [12, 17], |
| 215 | + '21': [18, 21], |
| 216 | + '25': [22, 25] |
| 217 | + }; |
217 | 218 |
|
218 | | - const showAll = (!wasActive && category === 'all'); |
219 | | - const showCategory = (!wasActive && category !== 'all') ? category : null; |
| 219 | + const applyFilters = () => { |
| 220 | + cards.forEach(card => { |
| 221 | + const matchesCategory = !activeCategory || card.dataset.category === activeCategory; |
| 222 | + let matchesJdk = true; |
| 223 | + if (activeJdk) { |
| 224 | + const version = parseInt(card.dataset.jdk, 10); |
| 225 | + const range = LTS_RANGES[activeJdk]; |
| 226 | + matchesJdk = range && version >= range[0] && version <= range[1]; |
| 227 | + } |
| 228 | + card.classList.toggle('filter-hidden', !(matchesCategory && matchesJdk)); |
| 229 | + }); |
220 | 230 |
|
221 | | - // Filter cards |
222 | | - cards.forEach(card => { |
223 | | - if (showAll || card.dataset.category === showCategory) { |
224 | | - card.classList.remove('filter-hidden'); |
225 | | - } else { |
226 | | - card.classList.add('filter-hidden'); |
227 | | - } |
| 231 | + if (window.updateViewToggleState) { |
| 232 | + window.updateViewToggleState(); |
| 233 | + } |
| 234 | + }; |
| 235 | + |
| 236 | + // Generic helper to wire up a dropdown |
| 237 | + const initDropdown = (dropdownEl, onSelect) => { |
| 238 | + if (!dropdownEl) return; |
| 239 | + const toggleBtn = dropdownEl.querySelector('.jdk-dropdown-toggle'); |
| 240 | + const labelEl = dropdownEl.querySelector('.jdk-label'); |
| 241 | + const list = dropdownEl.querySelector('ul'); |
| 242 | + |
| 243 | + const openDropdown = () => { |
| 244 | + list.style.display = 'block'; |
| 245 | + toggleBtn.setAttribute('aria-expanded', 'true'); |
| 246 | + }; |
| 247 | + |
| 248 | + const closeDropdown = () => { |
| 249 | + list.style.display = 'none'; |
| 250 | + toggleBtn.setAttribute('aria-expanded', 'false'); |
| 251 | + }; |
| 252 | + |
| 253 | + const selectItem = (li) => { |
| 254 | + list.querySelectorAll('li').forEach(l => l.classList.remove('active')); |
| 255 | + li.classList.add('active'); |
| 256 | + if (labelEl) labelEl.textContent = li.textContent.trim(); |
| 257 | + }; |
| 258 | + |
| 259 | + toggleBtn.addEventListener('click', (e) => { |
| 260 | + e.stopPropagation(); |
| 261 | + list.style.display === 'block' ? closeDropdown() : openDropdown(); |
| 262 | + }); |
| 263 | + |
| 264 | + document.addEventListener('click', closeDropdown); |
| 265 | + document.addEventListener('keydown', (e) => { |
| 266 | + if (e.key === 'Escape') closeDropdown(); |
| 267 | + }); |
| 268 | + |
| 269 | + list.querySelectorAll('li').forEach(li => { |
| 270 | + li.addEventListener('click', (e) => { |
| 271 | + e.stopPropagation(); |
| 272 | + closeDropdown(); |
| 273 | + selectItem(li); |
| 274 | + onSelect(li, toggleBtn); |
228 | 275 | }); |
| 276 | + }); |
229 | 277 |
|
230 | | - // Update URL hash to reflect active filter |
231 | | - const activeFilter = pill.classList.contains('active') ? category : null; |
232 | | - if (activeFilter && activeFilter !== 'all') { |
233 | | - history.replaceState(null, '', '#' + activeFilter); |
234 | | - } else { |
235 | | - history.replaceState(null, '', window.location.pathname + window.location.search); |
| 278 | + return { closeDropdown, setActive: (value) => { |
| 279 | + const target = list.querySelector(`li[data-filter="${value}"]`); |
| 280 | + if (target) { |
| 281 | + selectItem(target); |
| 282 | + toggleBtn.classList.toggle('has-filter', value !== 'all'); |
236 | 283 | } |
| 284 | + }}; |
| 285 | + }; |
237 | 286 |
|
238 | | - // Update view toggle button state |
239 | | - if (window.updateViewToggleState) { |
240 | | - window.updateViewToggleState(); |
241 | | - } |
242 | | - }); |
| 287 | + // Category dropdown |
| 288 | + const categoryDropdown = document.getElementById('categoryDropdown'); |
| 289 | + const catDropdownCtrl = initDropdown(categoryDropdown, (li, toggleBtn) => { |
| 290 | + const category = li.dataset.filter; |
| 291 | + activeCategory = category !== 'all' ? category : null; |
| 292 | + toggleBtn.classList.toggle('has-filter', !!activeCategory); |
| 293 | + if (activeCategory) { |
| 294 | + history.replaceState(null, '', '#' + activeCategory); |
| 295 | + } else { |
| 296 | + history.replaceState(null, '', window.location.pathname + window.location.search); |
| 297 | + } |
| 298 | + applyFilters(); |
| 299 | + }); |
| 300 | + |
| 301 | + // JDK dropdown |
| 302 | + const jdkDropdown = document.getElementById('jdkDropdown'); |
| 303 | + initDropdown(jdkDropdown, (li, toggleBtn) => { |
| 304 | + const version = li.dataset.jdkFilter; |
| 305 | + activeJdk = version !== 'all' ? version : null; |
| 306 | + toggleBtn.classList.toggle('has-filter', !!activeJdk); |
| 307 | + applyFilters(); |
243 | 308 | }); |
244 | 309 |
|
245 | 310 | // Apply filter from a given category string (or "all" / empty for no filter) |
246 | 311 | const applyHashFilter = (category) => { |
247 | | - const target = category |
248 | | - ? document.querySelector(`.filter-pill[data-filter="${category}"]`) |
249 | | - : null; |
250 | | - if (target) { |
251 | | - target.click(); |
252 | | - // Scroll the filter section into view |
| 312 | + if (category && catDropdownCtrl) { |
| 313 | + catDropdownCtrl.setActive(category); |
| 314 | + activeCategory = category; |
| 315 | + applyFilters(); |
253 | 316 | const section = document.getElementById('all-comparisons'); |
254 | 317 | if (section) section.scrollIntoView({ behavior: 'smooth' }); |
255 | | - } else { |
256 | | - const allButton = document.querySelector('.filter-pill[data-filter="all"]'); |
257 | | - if (allButton) allButton.click(); |
| 318 | + } else if (catDropdownCtrl) { |
| 319 | + catDropdownCtrl.setActive('all'); |
| 320 | + activeCategory = null; |
| 321 | + applyFilters(); |
258 | 322 | } |
259 | 323 | }; |
260 | 324 |
|
|
0 commit comments