-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (131 loc) · 4.96 KB
/
script.js
File metadata and controls
151 lines (131 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* GitHub Explorer Framework - Main Script
*
* Handles:
* - Navigation between content sections
* - Search functionality across all content
* - Submenu toggle functionality
*/
document.addEventListener('DOMContentLoaded', function () {
// DOM Elements
const navLinks = document.querySelectorAll('.heb-menu a');
const contentSections = document.querySelectorAll('.conteudo-lateral');
const searchInput = document.getElementById('searchInput');
const searchButton = document.getElementById('searchButton');
const submenuToggles = document.querySelectorAll('.toggle-submenu');
const sidebar = document.querySelector('.heb-bar'); // Elemento da sidebar
/**
* Initialize the page with default active section
*/
function initializePage() {
// Activate first nav link and content section by default
navLinks[0].classList.add('active');
contentSections[0].classList.add('active');
}
/**
* Handle navigation between content sections
*/
function setupNavigation() {
navLinks.forEach(link => {
link.addEventListener('click', function (e) {
e.preventDefault();
// Remove active class from all links and sections
navLinks.forEach(l => l.classList.remove('active'));
contentSections.forEach(c => c.classList.remove('active'));
// Add active class to clicked link and target section
this.classList.add('active');
const targetId = this.getAttribute('data-target');
document.getElementById(targetId).classList.add('active');
});
});
}
/**
* Implements the search functionality by displaying only the first matching content section.
*/
function setupSearch() {
// Stores the originally visible section before any search is performed
let originalActiveSection = null;
function performSearch() {
const searchTerm = searchInput.value.toLowerCase().trim();
// Removes any previous "no results" message
const existingMessage = document.querySelector('.no-results');
if (existingMessage) {
existingMessage.remove();
}
if (searchTerm === '') {
// If search is empty, restore the original section state
if (originalActiveSection) {
// Hide all content sections
contentSections.forEach(section => {
section.style.display = 'none';
});
// Show only the previously active section
originalActiveSection.style.display = 'block';
originalActiveSection = null; // Reset
}
return;
}
// On the first search, store the currently active section
if (!originalActiveSection) {
contentSections.forEach(section => {
if (section.classList.contains('active')) {
originalActiveSection = section;
}
});
}
// Hide all sections
contentSections.forEach(section => {
section.style.display = 'none';
});
let firstMatch = null;
// Find the first section that contains the search term
for (const section of contentSections) {
const sectionText = section.textContent.toLowerCase();
if (sectionText.includes(searchTerm)) {
firstMatch = section;
break;
}
}
if (firstMatch) {
// Show only the matched section
firstMatch.style.display = 'block';
firstMatch.scrollIntoView({ behavior: 'smooth', block: 'start' });
} else {
// Show a "no results found" message
const noResults = document.createElement('div');
noResults.className = 'no-results';
noResults.textContent = 'No results found for: ' + searchTerm;
document.querySelector('.container').prepend(noResults);
}
}
// Event listeners
searchButton.addEventListener('click', performSearch);
searchInput.addEventListener('keyup', function (e) {
if (e.key === 'Enter') {
performSearch();
}
});
}
/**
* Handle submenu toggle functionality
*/
function setupSubmenus() {
submenuToggles.forEach(toggle => {
toggle.addEventListener('click', function (e) {
e.preventDefault();
const parent = this.closest('.submenu');
parent.classList.toggle('active');
// Rotate chevron icon
const chevron = this.querySelector('.chevron-icon');
if (chevron) {
chevron.classList.toggle('rotate');
}
});
});
}
// Initialize all functionality
initializePage();
setupNavigation();
setupSearch();
setupSubmenus();
});