|
1 | 1 | const input = document.getElementById("search"); |
2 | 2 | const instruction_list = document.getElementById("instruction_list"); |
3 | 3 | const filtersFieldset = document.getElementById("extension-filters-fieldset"); |
4 | | -// the blank area where we’ll insert checkboxes |
| 4 | +// The blank area where we’ll insert checkboxes |
5 | 5 | const filtersContainer = document.getElementById("extension-filters"); |
6 | | -const items = Array.from(instruction_list.querySelectorAll("li")); |
7 | | -const activeExtensions = new Set(); |
| 6 | +const items = Array.from(instruction_list.querySelectorAll("a")); |
| 7 | +const dropdowns = Array.from(document.querySelectorAll(".dropdown-callout")); |
| 8 | +const activeExtensionGroups = new Set(); |
8 | 9 |
|
9 | 10 | // Gather unique extension names from the rendered instruction <li> items. |
10 | | -const extensionSet = new Set(); |
11 | | -for (const li of items) { |
12 | | - const extension = li.dataset.extension; |
13 | | - extensionSet.add(extension); |
| 11 | +const extensionGroupSet = new Set(); |
| 12 | +for (const a of items) { |
| 13 | + const extension = a.dataset.extensionGroup; |
| 14 | + extensionGroupSet.add(extension); |
14 | 15 | } |
15 | 16 |
|
16 | | -const extensions = Array.from(extensionSet).sort(); |
17 | | -for (const extension of extensions) { |
18 | | - const id = `ext-${extension}`; |
19 | | - const label = document.createElement("label"); |
20 | | - label.className = "filter-option"; |
21 | | - |
22 | | - const checkbox = document.createElement("input"); |
23 | | - checkbox.type = "checkbox"; |
24 | | - checkbox.value = extension; |
25 | | - checkbox.id = id; |
26 | | - checkbox.addEventListener("change", () => { |
27 | | - if (checkbox.checked) { |
28 | | - activeExtensions.add(extension); |
| 17 | +const extensionGroups = Array.from(extensionGroupSet).sort(); |
| 18 | +for (const extensionGroup of extensionGroups) { |
| 19 | + const id = `ext-group-${extensionGroup}`; |
| 20 | + const label = document.createElement("label"); |
| 21 | + label.className = "filter-option"; |
| 22 | + |
| 23 | + const checkbox = document.createElement("input"); |
| 24 | + checkbox.type = "checkbox"; |
| 25 | + checkbox.value = extensionGroup; |
| 26 | + checkbox.id = id; |
| 27 | + |
| 28 | + checkbox.addEventListener("change", () => { |
| 29 | + if (checkbox.checked) activeExtensionGroups.add(extensionGroup); |
| 30 | + else activeExtensionGroups.delete(extensionGroup); |
| 31 | + applyFilters(); |
| 32 | + }); |
| 33 | + |
| 34 | + const text = document.createElement("span"); |
| 35 | + text.textContent = extensionGroup; |
| 36 | + |
| 37 | + label.appendChild(checkbox); |
| 38 | + label.appendChild(text); |
| 39 | + filtersContainer.appendChild(label); |
| 40 | +} |
| 41 | + |
| 42 | +function applyFilters() { |
| 43 | + const q = input.value.trim().toLowerCase(); |
| 44 | + const filtersActive = activeExtensionGroups.size > 0; |
| 45 | + |
| 46 | + if (q || filtersActive) { |
| 47 | + // If there’s a search query, hide the dropdowns to save space |
| 48 | + instruction_list.style.display = "grid"; |
| 49 | + for (const dropdown of dropdowns) { |
| 50 | + dropdown.style.display = "none"; |
| 51 | + } |
29 | 52 | } else { |
30 | | - activeExtensions.delete(extension); |
| 53 | + // If the search box is empty, show the dropdowns again |
| 54 | + instruction_list.style.display = "none"; |
| 55 | + for (const dropdown of dropdowns) { |
| 56 | + dropdown.style.display = "block"; |
| 57 | + } |
31 | 58 | } |
32 | | - applyFilters(); |
33 | | - }); |
34 | 59 |
|
35 | | - const text = document.createElement("span"); |
36 | | - text.textContent = extension; |
| 60 | + for (const a of items) { |
| 61 | + /* Check if the item matches the text search |
| 62 | + - If the search box is empty, match everything |
| 63 | + - Otherwise, match if the search string includes the query */ |
| 64 | + const matchesQuery = !q || (a.dataset.search || "").includes(q); |
37 | 65 |
|
38 | | - label.appendChild(checkbox); |
39 | | - label.appendChild(text); |
40 | | - filtersContainer.appendChild(label); |
41 | | -} |
| 66 | + /* Check if the item matches an active extension filter |
| 67 | + - If no filters are active, match everything |
| 68 | + - Otherwise, show only if its extension is selected */ |
| 69 | + const matchesExtensionGroup = !filtersActive || activeExtensionGroups.has(a.dataset.extensionGroup); |
42 | 70 |
|
43 | | -function applyFilters() { |
44 | | - const q = input.value.trim().toLowerCase(); |
45 | | - const filtersActive = activeExtensions.size > 0; |
46 | | - for (const li of items) { |
47 | | - /* Check if the item matches the text search |
48 | | - - If the search box is empty, match everything |
49 | | - - Otherwise, match if the search string includes the query */ |
50 | | - const matchesQuery = !q || (li.dataset.search || "").includes(q); |
51 | | - |
52 | | - /* Check if the item matches an active extension filter |
53 | | - - If no filters are active, match everything |
54 | | - - Otherwise, show only if its extension is selected */ |
55 | | - const matchesExtension = |
56 | | - !filtersActive || activeExtensions.has(li.dataset.extension); |
57 | | - |
58 | | - // Show or hide this <li> depending on whether both match conditions are true |
59 | | - li.style.display = matchesQuery && matchesExtension ? "" : "none"; |
60 | | - } |
| 71 | + // Show or hide this <li> depending on whether both match conditions are true |
| 72 | + a.style.display = matchesQuery && matchesExtensionGroup ? "grid" : "none"; |
| 73 | + } |
61 | 74 | } |
62 | 75 |
|
63 | 76 | input.addEventListener("input", applyFilters); |
64 | 77 |
|
65 | 78 | input.addEventListener("keydown", (e) => { |
66 | | - // Only act if Enter is pressed |
67 | | - if (e.key !== "Enter") return; |
| 79 | + // Only act if Enter is pressed |
| 80 | + if (e.key !== "Enter") return; |
68 | 81 |
|
69 | | - const q = input.value.trim().toLowerCase(); |
| 82 | + const q = input.value.trim().toLowerCase(); |
70 | 83 |
|
71 | | - // Do nothing if the box is empty |
72 | | - if (!q) return; |
| 84 | + // Do nothing if the box is empty |
| 85 | + if (!q) return; |
73 | 86 |
|
74 | | - const filtersActive = activeExtensions.size > 0; |
| 87 | + const filtersActive = activeExtensionGroups.size > 0; |
75 | 88 |
|
76 | | - /* Look for an exact mnemonic match among all instructions |
| 89 | + /* Look for an exact mnemonic match among all instructions |
77 | 90 | - If filters are active, also ensure it belongs to one of the selected extensions */ |
78 | | - const exact = items.find((li) => { |
79 | | - if (li.dataset.mnemonic !== q) return false; |
80 | | - if (!filtersActive) return true; |
81 | | - return activeExtensions.has(li.dataset.extension); |
82 | | - }); |
83 | | - |
84 | | - // If an exact match is found, redirect to that instruction's detail page |
85 | | - if (exact) { |
86 | | - const link = exact.querySelector("a").getAttribute("href"); |
87 | | - location.href = link; |
88 | | - } |
| 91 | + const exact = items.find((a) => { |
| 92 | + if (a.dataset.mnemonic !== q) return false; |
| 93 | + if (!filtersActive) return true; |
| 94 | + return activeExtensionGroups.has(a.dataset.extensionGroup); |
| 95 | + }); |
| 96 | + |
| 97 | + // If an exact match is found, redirect to that instruction's detail page |
| 98 | + if (exact) { |
| 99 | + const link = exact.getAttribute("href"); |
| 100 | + location.href = link; |
| 101 | + } |
89 | 102 | }); |
90 | 103 |
|
91 | 104 | applyFilters(); |
0 commit comments