* Setup language and tag filters
()
| 96 | * Setup language and tag filters |
| 97 | */ |
| 98 | function setupFilters(): void { |
| 99 | if (!samplesData) return; |
| 100 | |
| 101 | // Language filter |
| 102 | const languageSelect = document.getElementById( |
| 103 | "filter-language" |
| 104 | ) as HTMLSelectElement; |
| 105 | if (languageSelect) { |
| 106 | // Get unique languages across all cookbooks |
| 107 | const languages = new Map<string, Language>(); |
| 108 | samplesData.cookbooks.forEach((cookbook) => { |
| 109 | cookbook.languages.forEach((lang) => { |
| 110 | if (!languages.has(lang.id)) { |
| 111 | languages.set(lang.id, lang); |
| 112 | } |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | languageSelect.innerHTML = '<option value="">All Languages</option>'; |
| 117 | languages.forEach((lang, id) => { |
| 118 | const option = document.createElement("option"); |
| 119 | option.value = id; |
| 120 | option.textContent = lang.name; |
| 121 | languageSelect.appendChild(option); |
| 122 | }); |
| 123 | |
| 124 | languageSelect.addEventListener("change", () => { |
| 125 | selectedLanguage = languageSelect.value || null; |
| 126 | renderCookbooks(); |
| 127 | updateResultsCount(); |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | // Tag filter (multi-select with Choices.js) |
| 132 | const tagSelect = document.getElementById("filter-tag") as HTMLSelectElement; |
| 133 | if (tagSelect && samplesData.filters.tags.length > 0) { |
| 134 | // Initialize Choices.js |
| 135 | tagChoices = createChoices("#filter-tag", { placeholderValue: "All Tags" }); |
| 136 | tagChoices.setChoices( |
| 137 | samplesData.filters.tags.map((tag) => ({ value: tag, label: tag })), |
| 138 | "value", |
| 139 | "label", |
| 140 | true |
| 141 | ); |
| 142 | |
| 143 | tagSelect.addEventListener("change", () => { |
| 144 | selectedTags = getChoicesValues(tagChoices!); |
| 145 | renderCookbooks(); |
| 146 | updateResultsCount(); |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | // Clear filters button |
| 151 | const clearBtn = document.getElementById("clear-filters"); |
| 152 | clearBtn?.addEventListener("click", clearFilters); |
| 153 | } |
| 154 | |
| 155 | /** |
no test coverage detected