@@ -14,6 +14,7 @@ document.addEventListener('DOMContentLoaded', function () {
1414 const searchInput = document . getElementById ( 'searchInput' ) ;
1515 const searchButton = document . getElementById ( 'searchButton' ) ;
1616 const submenuToggles = document . querySelectorAll ( '.toggle-submenu' ) ;
17+ const sidebar = document . querySelector ( '.heb-bar' ) ; // Elemento da sidebar
1718
1819 /**
1920 * Initialize the page with default active section
@@ -44,56 +45,75 @@ document.addEventListener('DOMContentLoaded', function () {
4445 } ) ;
4546 }
4647
47- /**
48- * Search functionality that searches through all visible content
49- */
50- function setupSearch ( ) {
48+ /**
49+ * Implements the search functionality by displaying only the first matching content section.
50+ */
51+ function setupSearch ( ) {
52+ // Stores the originally visible section before any search is performed
53+ let originalActiveSection = null ;
54+
5155 function performSearch ( ) {
5256 const searchTerm = searchInput . value . toLowerCase ( ) . trim ( ) ;
5357
54- // Remove old messages
58+ // Removes any previous "no results" message
5559 const existingMessage = document . querySelector ( '.no-results' ) ;
5660 if ( existingMessage ) {
5761 existingMessage . remove ( ) ;
5862 }
5963
6064 if ( searchTerm === '' ) {
61- // If empty, show everything
62- contentSections . forEach ( section => {
63- section . style . display = 'block' ;
64- } ) ;
65+ // If search is empty, restore the original section state
66+ if ( originalActiveSection ) {
67+ // Hide all content sections
68+ contentSections . forEach ( section => {
69+ section . style . display = 'none' ;
70+ } ) ;
71+ // Show only the previously active section
72+ originalActiveSection . style . display = 'block' ;
73+ originalActiveSection = null ; // Reset
74+ }
6575 return ;
6676 }
6777
68- const words = searchTerm . split ( ' ' ) ;
69- let foundSections = [ ] ;
78+ // On the first search, store the currently active section
79+ if ( ! originalActiveSection ) {
80+ contentSections . forEach ( section => {
81+ if ( section . classList . contains ( 'active' ) ) {
82+ originalActiveSection = section ;
83+ }
84+ } ) ;
85+ }
7086
71- // Hide everything first
87+ // Hide all sections
7288 contentSections . forEach ( section => {
7389 section . style . display = 'none' ;
90+ } ) ;
7491
75- const sectionText = section . textContent . toLowerCase ( ) ;
76- const match = words . every ( word => sectionText . includes ( word ) ) ;
92+ let firstMatch = null ;
7793
78- if ( match ) {
79- section . style . display = 'block' ;
80- foundSections . push ( section ) ;
94+ // Find the first section that contains the search term
95+ for ( const section of contentSections ) {
96+ const sectionText = section . textContent . toLowerCase ( ) ;
97+ if ( sectionText . includes ( searchTerm ) ) {
98+ firstMatch = section ;
99+ break ;
81100 }
82- } ) ;
101+ }
83102
84- if ( foundSections . length > 0 ) {
85- // Scroll to the first matched section
86- foundSections [ 0 ] . scrollIntoView ( { behavior : 'smooth' } ) ;
103+ if ( firstMatch ) {
104+ // Show only the matched section
105+ firstMatch . style . display = 'block' ;
106+ firstMatch . scrollIntoView ( { behavior : 'smooth' , block : 'start' } ) ;
87107 } else {
88- // Show "no results" message
108+ // Show a "no results found " message
89109 const noResults = document . createElement ( 'div' ) ;
90110 noResults . className = 'no-results' ;
91111 noResults . textContent = 'No results found for: ' + searchTerm ;
92112 document . querySelector ( '.container' ) . prepend ( noResults ) ;
93113 }
94114 }
95115
96- // Events
116+ // Event listeners
97117 searchButton . addEventListener ( 'click' , performSearch ) ;
98118 searchInput . addEventListener ( 'keyup' , function ( e ) {
99119 if ( e . key === 'Enter' ) {
@@ -103,6 +123,7 @@ document.addEventListener('DOMContentLoaded', function () {
103123}
104124
105125
126+
106127 /**
107128 * Handle submenu toggle functionality
108129 */
@@ -127,4 +148,4 @@ document.addEventListener('DOMContentLoaded', function () {
127148 setupNavigation ( ) ;
128149 setupSearch ( ) ;
129150 setupSubmenus ( ) ;
130- } ) ;
151+ } ) ;
0 commit comments