Skip to content

Commit 5ac76de

Browse files
authored
fix bugs in seach
1 parent 08bc74b commit 5ac76de

File tree

1 file changed

+43
-66
lines changed

1 file changed

+43
-66
lines changed

script.js

Lines changed: 43 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -48,80 +48,57 @@ document.addEventListener('DOMContentLoaded', function () {
4848
* Search functionality that searches through all visible content
4949
*/
5050
function setupSearch() {
51-
// Get all searchable elements (cards, list items, etc.)
52-
const searchableElements = document.querySelectorAll('[data-search], .conteudo-lateral');
51+
function performSearch() {
52+
const searchTerm = searchInput.value.toLowerCase().trim();
5353

54-
function performSearch() {
55-
const searchTerm = searchInput.value.toLowerCase().trim();
56-
57-
if (searchTerm === '') {
58-
// If search is empty, show all content
59-
contentSections.forEach(section => section.style.display = 'block');
60-
document.querySelectorAll('[data-search]').forEach(el => el.style.display = 'block');
61-
return;
62-
}
54+
// Remove previous messages
55+
const existingMessage = document.querySelector('.no-results');
56+
if (existingMessage) {
57+
existingMessage.remove();
58+
}
6359

64-
// First hide all content sections
65-
contentSections.forEach(section => section.style.display = 'none');
66-
67-
// Search through all elements with data-search attribute
68-
let foundResults = false;
69-
70-
document.querySelectorAll('[data-search]').forEach(element => {
71-
const searchText = element.getAttribute('data-search').toLowerCase();
72-
if (searchText.includes(searchTerm)) {
73-
element.style.display = 'block';
74-
// Show the parent section
75-
const parentSection = element.closest('.conteudo-lateral');
76-
if (parentSection) {
77-
parentSection.style.display = 'block';
78-
foundResults = true;
79-
}
80-
} else {
81-
element.style.display = 'none';
82-
}
60+
if (searchTerm === '') {
61+
// If empty, show everything
62+
contentSections.forEach(section => {
63+
section.style.display = 'block';
8364
});
65+
return;
66+
}
8467

85-
// Search through content text (for deeper content searching)
86-
if (!foundResults) {
87-
contentSections.forEach(section => {
88-
const sectionText = section.textContent.toLowerCase();
89-
if (sectionText.includes(searchTerm)) {
90-
section.style.display = 'block';
91-
foundResults = true;
92-
}
93-
});
94-
}
68+
const words = searchTerm.split(' ');
69+
const pageText = document.body.textContent.toLowerCase();
9570

96-
// If no results found, show a message
97-
if (!foundResults) {
98-
const noResults = document.createElement('div');
99-
noResults.className = 'no-results';
100-
noResults.textContent = 'No results found for: ' + searchTerm;
101-
102-
// Check if message already exists
103-
const existingMessage = document.querySelector('.no-results');
104-
if (!existingMessage) {
105-
document.querySelector('.container').prepend(noResults);
106-
}
107-
} else {
108-
// Remove no results message if it exists
109-
const existingMessage = document.querySelector('.no-results');
110-
if (existingMessage) {
111-
existingMessage.remove();
112-
}
113-
}
114-
}
71+
const allWordsFound = words.every(word => pageText.includes(word));
11572

116-
// Event listeners for search
117-
searchButton.addEventListener('click', performSearch);
118-
searchInput.addEventListener('keyup', function (e) {
119-
if (e.key === 'Enter') {
120-
performSearch();
121-
}
73+
// Hide all content first
74+
contentSections.forEach(section => {
75+
section.style.display = 'none';
12276
});
77+
78+
if (allWordsFound) {
79+
// If something was found, show all sections (or customize this)
80+
contentSections.forEach(section => {
81+
section.style.display = 'block';
82+
});
83+
} else {
84+
// Display "no results" message
85+
const noResults = document.createElement('div');
86+
noResults.className = 'no-results';
87+
noResults.textContent = 'No results found for: ' + searchTerm;
88+
document.querySelector('.container').prepend(noResults);
89+
}
12390
}
12491

92+
// Events
93+
searchButton.addEventListener('click', performSearch);
94+
searchInput.addEventListener('keyup', function (e) {
95+
if (e.key === 'Enter') {
96+
performSearch();
97+
}
98+
});
99+
}
100+
101+
125102
/**
126103
* Handle submenu toggle functionality
127104
*/
@@ -146,4 +123,4 @@ document.addEventListener('DOMContentLoaded', function () {
146123
setupNavigation();
147124
setupSearch();
148125
setupSubmenus();
149-
});
126+
});

0 commit comments

Comments
 (0)