-
Notifications
You must be signed in to change notification settings - Fork 384
/
script.js
313 lines (260 loc) · 10.9 KB
/
script.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Progress Bar
const updateProgress = () => {
const {
scrollTop,
scrollHeight
} = document.documentElement;
const scrollPercent = `${(scrollTop/(scrollHeight-window.innerHeight)) * 100}%`;
document.querySelector('#progress-bar').style.setProperty('--progress', scrollPercent);
}
document.addEventListener('scroll', updateProgress);
// Highlight active section in navigation on scroll
window.addEventListener('scroll', () => {
const sections = document.querySelectorAll('.containers');
const navLinks = document.querySelectorAll('.nav-links');
let currentSection = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
if (scrollY >= sectionTop - 50) {
currentSection = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.setAttribute('id', '');
if (link.getAttribute('href') === `#${currentSection}`) {
link.setAttribute('id', 'active1');
console.log(currentSection);
}
});
});
// Toggle mobile menu visibility
const hamburger = document.querySelector(".hamburger");
const navMenu = document.querySelector(".nav-menu");
document.addEventListener("DOMContentLoaded", function () {
setTimeout(function () {
document.querySelector("body").classList.add("loaded");
}, 500);
});
hamburger.addEventListener("click", mobileMenu);
function mobileMenu() {
hamburger.classList.toggle("active");
navMenu.classList.toggle("active");
}
const navLink = document.querySelectorAll(".nav-link");
navLink.forEach(n => n.addEventListener("click", closeMenu));
function closeMenu() {
hamburger.classList.remove("active");
navMenu.classList.remove("active");
}
// Hide or show scroll progress indicator
let calcScrollValue = () => {
let scrollProg = document.getElementById("progress");
let pos = document.documentElement.scrollTop;
let calcHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
let scrollValue = Math.round((pos * 100) / calcHeight);
if (pos > 100) {
scrollProg.style.display = "grid";
} else {
scrollProg.style.display = "none";
}
scrollProg.addEventListener("click", () => {
document.documentElement.scrollTop = 0;
});
scrollProg.style.background = `conic-gradient(#0063ba ${scrollValue}%, #d499de ${scrollValue}%)`;
};
window.addEventListener('scroll', function () {
var scrollToTopButton = document.getElementById('progress');
if (window.pageYOffset > 200) {
scrollToTopButton.style.display = 'block';
} else {
scrollToTopButton.style.display = 'none';
}
});
window.onscroll = calcScrollValue;
window.onload = calcScrollValue;
// Pagination functionality for calculators
const input = document.getElementById('calculatorSearch');
const paginationControls = document.getElementById('pagination-controls');
let calculators = document.querySelectorAll('.container .box');
let h2TextContents = [];
calculators.forEach(calculator => {
let h2Element = calculator.querySelector('h2');
if (h2Element) {
h2TextContents.push(h2Element.textContent);
}
});
// Show page based on current page number
const itemsPerPage = 10;
let currentPage = 1;
function showPage(page) {
const boxes = document.querySelectorAll('.container .box');
const totalPages = Math.ceil(boxes.length / itemsPerPage);
currentPage = Math.max(1, Math.min(page, totalPages));
boxes.forEach((box, index) => {
box.style.display = (index >= (currentPage - 1) * itemsPerPage && index < currentPage * itemsPerPage) ? 'block' : 'none';
});
document.getElementById('page-info').innerText = `Page ${currentPage} of ${totalPages}`;
document.getElementById('prev').style.display = totalPages > 1 ? 'inline' : 'none';
document.getElementById('next').style.display = totalPages > 1 ? 'inline' : 'none';
}
function changePage(direction) {
showPage(currentPage + direction);
}
// Filter calculators based on search input
function filterCalculators() {
var input, filter, calculators, i;
input = document.getElementById('calculatorSearch');
filter = input.value.toUpperCase();
calculators = document.querySelectorAll('.container .box');
var noResults = document.getElementById('noResults');
var paginationControls = document.getElementById('pagination-controls');
var hasResults = false;
for (i = 0; i < calculators.length; i++) {
var calculator = calculators[i];
var h2 = calculator.querySelector('h2');
var h3 = calculator.querySelector('h3');
var calculatorName = h2.textContent || h2.innerText;
var calculatorDescription = h3.textContent || h3.innerText;
if ((calculatorName.toUpperCase().indexOf(filter) > -1) || (calculatorDescription.toUpperCase().indexOf(filter) > -1)) {
calculator.style.display = "flex";
hasResults = true;
} else {
calculator.style.display = "none";
}
}
if (hasResults) {
noResults.style.display = 'none';
paginationControls.style.display = 'none'; // Hide pagination controls during search
} else {
noResults.style.display = 'block';
paginationControls.style.display = 'none'; // Hide pagination controls if no results
}
if (filter === "") {
paginationControls.style.display = 'flex'; // Show pagination controls when search input is cleared
showPage(currentPage); // Reset pagination
}
}
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('noResults').style.display = 'none';
showPage(currentPage);
});
var input1 = document.getElementById('calculatorSearch');
input1.addEventListener('input', (e) => {
const searchTerm = e.target.value.toLowerCase();
let hasResults = false;
calculators.forEach(calculator => {
const h2 = calculator.querySelector('h2');
const h3 = calculator.querySelector('h3');
const h2Text = h2 ? h2.textContent.toLowerCase() : '';
const h3Text = h3 ? h3.textContent.toLowerCase() : '';
if (h2Text.includes(searchTerm) || h3Text.includes(searchTerm)) {
calculator.style.display = 'flex';
hasResults = true;
} else {
calculator.style.display = 'none';
}
});
// Show or hide pagination controls based on search input
if (hasResults) {
noResults.style.display = 'none';
paginationControls.style.display = 'none'; // Hide pagination controls during search
} else {
noResults.style.display = 'block';
paginationControls.style.display = 'none'; // Hide pagination controls if no results
}
if (searchTerm.length === 0) {
paginationControls.style.display = 'flex'; // Show pagination controls when search input is cleared
showPage(currentPage); // Reset pagination
}
});
// Display search results in a dropdown list
let search_input_container = document.querySelector('.search-input-container');
let calculatorSearch = document.getElementById('calculatorSearch');
input.addEventListener('input', (e) => {
let searchResultsContainer = document.getElementById('searchResults_Container') || false;
if (searchResultsContainer) {
search_input_container.removeChild(searchResultsContainer);
}
searchResultsContainer = document.createElement('div');
let div = document.createElement('div');
searchResultsContainer.setAttribute('id', 'searchResults_Container');
let filtered = h2TextContents.filter(ele => {
const searchTerm = e.target.value.toLowerCase();
const elementText = ele.toLowerCase();
return elementText.includes(searchTerm);
});
if (filtered && e.target.value.length > 0) {
filtered.forEach((item, index) => {
let p = document.createElement('p');
p.textContent = item;
p.setAttribute('key', index);
div.appendChild(p);
p.addEventListener('click', () => {
calculatorSearch.value = item;
searchResultsContainer.removeChild(div);
});
});
searchResultsContainer.appendChild(div);
search_input_container.appendChild(searchResultsContainer);
}
if (e.target.value.length === 0) {
searchResultsContainer.removeChild(div);
}
});
// Voice command feature in search bar
const searchBar = document.querySelector("#searchBar");
const searchBarInput = searchBar.querySelector("input");
const pagination = document.getElementById("pagination-controls");
// The speech recognition interface lives on the browser’s window object
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (SpeechRecognition) {
console.log("Your Browser supports speech Recognition");
const recognition = new SpeechRecognition();
recognition.continuous = true;
searchBar.insertAdjacentHTML("beforeend", '<button type="button"><i class="fas fa-microphone"></i></button>');
const micBtn = searchBar.querySelector("button");
const micIcon = micBtn.firstElementChild;
micBtn.addEventListener("click", micBtnClick);
function micBtnClick() {
if (micIcon.classList.contains("fa-microphone")) { // Start Voice Recognition
recognition.start(); // First time you have to allow access to mic!
} else {
recognition.stop();
}
}
recognition.addEventListener("start", startSpeechRecognition);
function startSpeechRecognition() {
micIcon.classList.remove("fa-microphone");
micIcon.classList.add("fa-microphone-slash");
searchBarInput.focus();
console.log("Voice activated, SPEAK");
}
recognition.addEventListener("end", endSpeechRecognition);
function endSpeechRecognition() {
micIcon.classList.remove("fa-microphone-slash");
micIcon.classList.add("fa-microphone");
searchBarInput.focus();
console.log("Speech recognition service disconnected");
}
recognition.addEventListener("result", resultOfSpeechRecognition);
function resultOfSpeechRecognition(event) {
const current = event.resultIndex;
const transcript = event.results[current][0].transcript;
const newTranscript = transcript.endsWith('.') ? transcript.slice(0, -1) : transcript;
console.log(newTranscript);
searchBarInput.value = newTranscript;
filterCalculators();
}
} else {
info.textContent = "Your Browser does not support Speech Recognition";
}
// Validate input for name fields
function validateName(inputId) {
let input = document.getElementById(inputId);
let value = input.value;
let regex = /^[A-Za-z ]+$/;
if (!regex.test(value)) {
alert("Please enter only characters in the name field.");
input.value = value.replace(/[^A-Za-z ]/g, ''); // Remove any non-alphabetic characters
}
}