|
4 | 4 | const prevButton = document.getElementById('prev-button'); |
5 | 5 | const nextButton = document.getElementById('next-button'); |
6 | 6 | const revealButton = document.getElementById('reveal-button'); |
| 7 | + const categoryList = document.getElementById('joke-categories'); |
7 | 8 |
|
8 | 9 | if (!setupEl || !punchlineEl || !prevButton || !nextButton || !revealButton) { |
9 | 10 | return; |
|
17 | 18 | let index = 0; |
18 | 19 | let punchlineVisible = false; |
19 | 20 |
|
| 21 | + const categorize = |
| 22 | + window.jokeCategoryHelper && typeof window.jokeCategoryHelper.categorize === 'function' |
| 23 | + ? window.jokeCategoryHelper.categorize |
| 24 | + : null; |
| 25 | + |
| 26 | + const fallbackCategory = |
| 27 | + window.jokeCategoryHelper && typeof window.jokeCategoryHelper.fallback === 'string' |
| 28 | + ? window.jokeCategoryHelper.fallback |
| 29 | + : 'Classic Dad'; |
| 30 | + |
| 31 | + function renderCategories(values) { |
| 32 | + if (!categoryList) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + categoryList.textContent = ''; |
| 37 | + |
| 38 | + const items = Array.isArray(values) && values.length ? values : [fallbackCategory]; |
| 39 | + |
| 40 | + items.forEach((label) => { |
| 41 | + const pill = document.createElement('li'); |
| 42 | + pill.className = 'category-pill'; |
| 43 | + pill.textContent = label; |
| 44 | + categoryList.appendChild(pill); |
| 45 | + }); |
| 46 | + |
| 47 | + categoryList.hidden = false; |
| 48 | + } |
| 49 | + |
20 | 50 | function shuffle(array) { |
21 | 51 | const copy = array.slice(); |
22 | 52 | for (let i = copy.length - 1; i > 0; i -= 1) { |
|
51 | 81 | revealButton.hidden = true; |
52 | 82 | prevButton.disabled = true; |
53 | 83 | nextButton.disabled = true; |
| 84 | + if (categoryList) { |
| 85 | + categoryList.textContent = ''; |
| 86 | + categoryList.hidden = true; |
| 87 | + } |
54 | 88 | return; |
55 | 89 | } |
56 | 90 |
|
57 | 91 | ensureDeck(); |
58 | 92 | const current = deck[index]; |
59 | 93 | const hasPunchline = typeof current.punchline === 'string' && current.punchline.trim().length > 0; |
60 | 94 | const punchlineText = hasPunchline ? current.punchline : '💩'; |
| 95 | + const categories = categorize |
| 96 | + ? categorize(current.joke, current.punchline) |
| 97 | + : [fallbackCategory]; |
61 | 98 |
|
62 | 99 | setupEl.textContent = current.joke; |
63 | 100 | punchlineEl.textContent = punchlineText; |
64 | 101 | revealButton.hidden = false; |
65 | 102 |
|
66 | 103 | setPunchlineVisible(false); |
| 104 | + renderCategories(categories); |
67 | 105 |
|
68 | 106 | const buttonsDisabled = deck.length <= 1; |
69 | 107 | prevButton.disabled = buttonsDisabled; |
|
0 commit comments