|
202 | 202 | 2. Category Filter Pills (homepage) |
203 | 203 | ========================================================== */ |
204 | 204 | const initFilters = () => { |
205 | | - const pills = document.querySelectorAll('.filter-pill'); |
| 205 | + const pills = document.querySelectorAll('#categoryFilter .filter-pill'); |
| 206 | + const jdkPills = document.querySelectorAll('#jdkFilter .filter-pill'); |
206 | 207 | const cards = document.querySelectorAll('.tip-card'); |
207 | 208 | if (!pills.length || !cards.length) return; |
208 | 209 |
|
| 210 | + let activeCategory = null; |
| 211 | + let activeJdk = null; |
| 212 | + |
| 213 | + // LTS cycle ranges: each entry covers all versions introduced since the previous LTS |
| 214 | + const LTS_RANGES = { |
| 215 | + '11': [9, 11], |
| 216 | + '17': [12, 17], |
| 217 | + '21': [18, 21], |
| 218 | + '25': [22, 25] |
| 219 | + }; |
| 220 | + |
| 221 | + const applyFilters = () => { |
| 222 | + cards.forEach(card => { |
| 223 | + const matchesCategory = !activeCategory || card.dataset.category === activeCategory; |
| 224 | + let matchesJdk = true; |
| 225 | + if (activeJdk) { |
| 226 | + const version = parseInt(card.dataset.jdk, 10); |
| 227 | + const range = LTS_RANGES[activeJdk]; |
| 228 | + matchesJdk = range && version >= range[0] && version <= range[1]; |
| 229 | + } |
| 230 | + card.classList.toggle('filter-hidden', !(matchesCategory && matchesJdk)); |
| 231 | + }); |
| 232 | + |
| 233 | + if (window.updateViewToggleState) { |
| 234 | + window.updateViewToggleState(); |
| 235 | + } |
| 236 | + }; |
| 237 | + |
209 | 238 | pills.forEach(pill => { |
210 | 239 | pill.addEventListener('click', () => { |
211 | 240 | const category = pill.dataset.filter || 'all'; |
|
215 | 244 | pills.forEach(p => p.classList.remove('active')); |
216 | 245 | if (!wasActive) pill.classList.add('active'); |
217 | 246 |
|
218 | | - const showAll = (!wasActive && category === 'all'); |
219 | | - const showCategory = (!wasActive && category !== 'all') ? category : null; |
220 | | - |
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 | | - } |
228 | | - }); |
| 247 | + activeCategory = (!wasActive && category !== 'all') ? category : null; |
229 | 248 |
|
230 | 249 | // 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); |
| 250 | + if (activeCategory) { |
| 251 | + history.replaceState(null, '', '#' + activeCategory); |
234 | 252 | } else { |
235 | 253 | history.replaceState(null, '', window.location.pathname + window.location.search); |
236 | 254 | } |
237 | 255 |
|
238 | | - // Update view toggle button state |
239 | | - if (window.updateViewToggleState) { |
240 | | - window.updateViewToggleState(); |
241 | | - } |
| 256 | + applyFilters(); |
| 257 | + }); |
| 258 | + }); |
| 259 | + |
| 260 | + jdkPills.forEach(pill => { |
| 261 | + pill.addEventListener('click', () => { |
| 262 | + const version = pill.dataset.jdkFilter || 'all'; |
| 263 | + const wasActive = pill.classList.contains('active'); |
| 264 | + |
| 265 | + // Update active JDK pill (toggle off if re-clicked) |
| 266 | + jdkPills.forEach(p => p.classList.remove('active')); |
| 267 | + if (!wasActive) pill.classList.add('active'); |
| 268 | + |
| 269 | + activeJdk = (!wasActive && version !== 'all') ? version : null; |
| 270 | + |
| 271 | + applyFilters(); |
242 | 272 | }); |
243 | 273 | }); |
244 | 274 |
|
245 | 275 | // Apply filter from a given category string (or "all" / empty for no filter) |
246 | 276 | const applyHashFilter = (category) => { |
247 | 277 | const target = category |
248 | | - ? document.querySelector(`.filter-pill[data-filter="${category}"]`) |
| 278 | + ? document.querySelector(`#categoryFilter .filter-pill[data-filter="${category}"]`) |
249 | 279 | : null; |
250 | 280 | if (target) { |
251 | 281 | target.click(); |
252 | 282 | // Scroll the filter section into view |
253 | 283 | const section = document.getElementById('all-comparisons'); |
254 | 284 | if (section) section.scrollIntoView({ behavior: 'smooth' }); |
255 | 285 | } else { |
256 | | - const allButton = document.querySelector('.filter-pill[data-filter="all"]'); |
| 286 | + const allButton = document.querySelector('#categoryFilter .filter-pill[data-filter="all"]'); |
257 | 287 | if (allButton) allButton.click(); |
258 | 288 | } |
259 | 289 | }; |
|
0 commit comments