Skip to content

Commit 4da4597

Browse files
committed
Removed the stale code and fixed few errors
1 parent 05aac4f commit 4da4597

File tree

2 files changed

+70
-100
lines changed

2 files changed

+70
-100
lines changed

assets/js_files/cursor.js

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -82,48 +82,4 @@ function applyDarkModePreference() {
8282
document.body.classList.remove('dark-mode');
8383
document.getElementById('theme-icon').src = 'assets/images/moon_solid.svg';
8484
}
85-
}
86-
87-
// Function to set dark mode preference
88-
document.addEventListener('DOMContentLoaded', () => {
89-
const currentTheme = localStorage.getItem('theme');
90-
const switchCheckbox = document.getElementById('switch'); // Define switchCheckbox here
91-
const starRating = document.querySelector('.star_rating');
92-
const thankYouMessage = document.querySelector(".thank_you_message");
93-
if (switchCheckbox) { // Check if switchCheckbox is not null
94-
function applyDarkModeStyles() {
95-
document.body.classList.remove('light-mode');
96-
document.body.classList.add('dark-mode');
97-
starRating.style.backgroundColor = '#2d2828';
98-
thankYouMessage.style.backgroundColor = '#2d2828';
99-
}
100-
101-
function applyLightModeStyles() {
102-
document.body.classList.remove('dark-mode');
103-
document.body.classList.add('light-mode');
104-
starRating.style.backgroundColor = 'white';
105-
thankYouMessage.style.backgroundColor = 'white';
106-
}
107-
108-
if (currentTheme) {
109-
if (currentTheme === 'dark-mode') {
110-
applyDarkModeStyles();
111-
switchCheckbox.checked = true;
112-
} else {
113-
applyLightModeStyles();
114-
}
115-
}
116-
117-
switchCheckbox.addEventListener('change', () => {
118-
if (switchCheckbox.checked) {
119-
applyDarkModeStyles();
120-
localStorage.setItem('theme', 'dark-mode');
121-
} else {
122-
applyLightModeStyles();
123-
localStorage.setItem('theme', 'light-mode');
124-
}
125-
});
126-
} else {
127-
console.error("Switch checkbox not found!"); // Log an error if switchCheckbox is null
128-
}
129-
});
85+
}

script.js

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function filterComponents() {
6565

6666
// Voice command in search bar feature
6767
const searchBar = document.querySelector("#searchBar");
68-
const searchBarInput = searchBar.querySelector("input");
68+
const searchBarInput = searchBar ? searchBar.querySelector("input") : null;
6969

7070
// The speech recognition interface lives on the browser’s window object
7171
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
@@ -76,49 +76,53 @@ if (SpeechRecognition) {
7676
const recognition = new SpeechRecognition();
7777
recognition.continuous = true;
7878

79-
searchBar.insertAdjacentHTML("beforeend", '<button type="button"><i class="fas fa-microphone"></i></button>');
80-
searchBarInput.style.paddingRight = "50px";
79+
if (searchBar && searchBarInput) {
80+
searchBar.insertAdjacentHTML("beforeend", '<button type="button"><i class="fas fa-microphone"></i></button>');
81+
searchBarInput.style.paddingRight = "50px";
8182

82-
const micBtn = searchBar.querySelector("button");
83-
const micIcon = micBtn.firstElementChild;
83+
const micBtn = searchBar.querySelector("button");
84+
const micIcon = micBtn.firstElementChild;
8485

85-
micBtn.addEventListener("click", micBtnClick);
86-
function micBtnClick() {
87-
if (micIcon.classList.contains("fa-microphone")) { // Start Voice Recognition
88-
recognition.start(); // First time you have to allow access to mic!
86+
micBtn.addEventListener("click", micBtnClick);
87+
88+
function micBtnClick() {
89+
if (micIcon.classList.contains("fa-microphone")) { // Start Voice Recognition
90+
recognition.start(); // First time you have to allow access to mic!
91+
} else {
92+
recognition.stop();
93+
}
8994
}
90-
else {
91-
recognition.stop();
95+
96+
recognition.addEventListener("start", startSpeechRecognition);
97+
98+
function startSpeechRecognition() {
99+
micIcon.classList.remove("fa-microphone");
100+
micIcon.classList.add("fa-microphone-slash");
101+
searchBarInput.focus();
102+
console.log("Voice activated, SPEAK");
92103
}
93-
}
94104

95-
recognition.addEventListener("start", startSpeechRecognition);
96-
function startSpeechRecognition() {
97-
micIcon.classList.remove("fa-microphone");
98-
micIcon.classList.add("fa-microphone-slash");
99-
searchFormInput.focus();
100-
console.log("Voice activated, SPEAK");
101-
}
105+
recognition.addEventListener("end", endSpeechRecognition);
102106

103-
recognition.addEventListener("end", endSpeechRecognition);
104-
function endSpeechRecognition() {
105-
micIcon.classList.remove("fa-microphone-slash");
106-
micIcon.classList.add("fa-microphone");
107-
searchBarInput.focus();
108-
console.log("Speech recognition service disconnected");
109-
}
107+
function endSpeechRecognition() {
108+
micIcon.classList.remove("fa-microphone-slash");
109+
micIcon.classList.add("fa-microphone");
110+
searchBarInput.focus();
111+
console.log("Speech recognition service disconnected");
112+
}
113+
114+
recognition.addEventListener("result", resultOfSpeechRecognition);
110115

111-
recognition.addEventListener("result", resultOfSpeechRecognition);
112-
function resultOfSpeechRecognition(event) {
113-
const current = event.resultIndex;
114-
const transcript = event.results[current][0].transcript;
115-
newtranscript = transcript.endsWith('.') ? transcript.slice(0, -1) : transcript;
116-
console.log(newtranscript)
117-
searchBarInput.value = newtranscript;
118-
filterComponents();
116+
function resultOfSpeechRecognition(event) {
117+
const current = event.resultIndex;
118+
const transcript = event.results[current][0].transcript;
119+
newtranscript = transcript.endsWith('.') ? transcript.slice(0, -1) : transcript;
120+
console.log(newtranscript)
121+
searchBarInput.value = newtranscript;
122+
filterComponents();
123+
}
119124
}
120-
}
121-
else {
125+
} else {
122126
console.log("Your Browser does not support speech Recognition");
123127
info.textContent = "Your Browser does not support Speech Recognition";
124128
}
@@ -128,28 +132,38 @@ document.addEventListener("DOMContentLoaded", function () {
128132
const homeLink = document.querySelector('.nav-menu a[href="#home"]');
129133
const componentsLink = document.querySelector('.nav-menu a[href="#components"]');
130134

135+
// Check if elements exist before adding event listeners
131136
const searchBarSection = document.getElementById('searchBar');
132137
const componentsSection = document.getElementById('components');
133138

134-
componentsLink.addEventListener('click', (event) => {
135-
event.preventDefault();
136-
componentsSection.scrollIntoView({ behavior: 'smooth' });
137-
componentsLink.style.color = 'red';
138-
homeLink.style.color = '';
139-
});
140-
141-
window.addEventListener('scroll', () => {
142-
const currentScroll = window.pageYOffset;
143-
144-
// Adjust the offset accordingly
145-
const componentsSectionTop = componentsSection.offsetTop - 50;
146-
147-
if (currentScroll >= componentsSectionTop) {
139+
if (homeLink && componentsLink && componentsSection) {
140+
componentsLink.addEventListener('click', (event) => {
141+
event.preventDefault();
142+
componentsSection.scrollIntoView({
143+
behavior: 'smooth'
144+
});
148145
componentsLink.style.color = 'red';
149146
homeLink.style.color = '';
150-
} else {
151-
componentsLink.style.color = '';
152-
homeLink.style.color = 'red';
153-
}
154-
});
147+
});
148+
149+
window.addEventListener('scroll', () => {
150+
const currentScroll = window.pageYOffset;
151+
152+
// Adjust the offset accordingly
153+
const componentsSectionTop = componentsSection.offsetTop - 50;
154+
155+
if (currentScroll >= componentsSectionTop) {
156+
componentsLink.style.color = 'red';
157+
homeLink.style.color = '';
158+
} else {
159+
componentsLink.style.color = '';
160+
homeLink.style.color = 'red';
161+
}
162+
});
163+
} else {
164+
// If any of the navbar elements are missing, log a warning but don't stop the script
165+
if (!homeLink) console.warn("Home link not found");
166+
if (!componentsLink) console.warn("Components link not found");
167+
if (!componentsSection) console.warn("Components section not found");
168+
}
155169
});

0 commit comments

Comments
 (0)