1- const input = document . getElementById ( "search" ) ;
2- const instruction_list = document . getElementById ( "instruction_list" ) ;
3- const filtersFieldset = document . getElementById ( "extension-filters-fieldset" ) ;
4- // the blank area where we’ll insert checkboxes
5- const filtersContainer = document . getElementById ( "extension-filters" ) ;
6- const items = Array . from ( instruction_list . querySelectorAll ( "li" ) ) ;
7- const activeExtensions = new Set ( ) ;
8-
9- // Gather unique extension names from the rendered instruction <li> items.
10- const extensionSet = new Set ( ) ;
1+ // Obtaining relevant DOM elements
2+ const input = document . getElementById ( "search" ) as HTMLInputElement ;
3+ const instructionList = document . getElementById ( "instruction_list" ) as HTMLUListElement ;
4+ const filtersContainer = document . getElementById ( "extension-filters" ) as HTMLDivElement ;
5+ const items = Array . from ( instructionList . querySelectorAll ( "li" ) ) ;
6+
7+
8+ // Extracting unique extensions to filter
9+ const activeExtensions = new Set < string > ( ) ;
10+ const extensionSet = new Set < string > ( ) ;
1111for ( const li of items ) {
1212 const extension = li . dataset . extension ;
13- extensionSet . add ( extension ) ;
13+ if ( extension ) extensionSet . add ( extension ) ;
1414}
15-
1615const extensions = Array . from ( extensionSet ) . sort ( ) ;
16+
17+
18+ // Creating a checkbox for each unique extension
1719for ( const extension of extensions ) {
1820 const id = `ext-${ extension } ` ;
1921 const label = document . createElement ( "label" ) ;
@@ -23,12 +25,10 @@ for (const extension of extensions) {
2325 checkbox . type = "checkbox" ;
2426 checkbox . value = extension ;
2527 checkbox . id = id ;
28+
2629 checkbox . addEventListener ( "change" , ( ) => {
27- if ( checkbox . checked ) {
28- activeExtensions . add ( extension ) ;
29- } else {
30- activeExtensions . delete ( extension ) ;
31- }
30+ if ( checkbox . checked ) activeExtensions . add ( extension ) ;
31+ else activeExtensions . delete ( extension ) ;
3232 applyFilters ( ) ;
3333 } ) ;
3434
@@ -40,9 +40,12 @@ for (const extension of extensions) {
4040 filtersContainer . appendChild ( label ) ;
4141}
4242
43+
44+ // Function to apply both text and extension filters to the instruction list
4345function applyFilters ( ) {
4446 const q = input . value . trim ( ) . toLowerCase ( ) ;
4547 const filtersActive = activeExtensions . size > 0 ;
48+
4649 for ( const li of items ) {
4750 /* Check if the item matches the text search
4851 - If the search box is empty, match everything
@@ -52,25 +55,21 @@ function applyFilters() {
5255 /* Check if the item matches an active extension filter
5356 - If no filters are active, match everything
5457 - Otherwise, show only if its extension is selected */
55- const matchesExtension =
56- ! filtersActive || activeExtensions . has ( li . dataset . extension ) ;
58+ const matchesExtension = ! filtersActive || activeExtensions . has ( li . dataset . extension ) ;
5759
5860 // Show or hide this <li> depending on whether both match conditions are true
5961 li . style . display = matchesQuery && matchesExtension ? "" : "none" ;
6062 }
6163}
6264
63- input . addEventListener ( "input" , applyFilters ) ;
6465
66+ // Set up event listeners for the search input
67+ input . addEventListener ( "input" , applyFilters ) ;
6568input . addEventListener ( "keydown" , ( e ) => {
6669 // Only act if Enter is pressed
6770 if ( e . key !== "Enter" ) return ;
68-
6971 const q = input . value . trim ( ) . toLowerCase ( ) ;
70-
71- // Do nothing if the box is empty
72- if ( ! q ) return ;
73-
72+ if ( ! q ) return ; // Do nothing if the box is empty
7473 const filtersActive = activeExtensions . size > 0 ;
7574
7675 /* Look for an exact mnemonic match among all instructions
@@ -88,4 +87,6 @@ input.addEventListener("keydown", (e) => {
8887 }
8988} ) ;
9089
90+
91+ // Apply filters on page load
9192applyFilters ( ) ;
0 commit comments