forked from JayShukla8/Quotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
259 lines (218 loc) · 8.82 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
document.addEventListener("DOMContentLoaded", function () {
fetchQuotes();
// 'Back to top' button functionality
const backToTopButton = document.getElementById("back-to-top");
backToTopButton.addEventListener("click", function () {
window.scrollTo({
top: 0,
behavior: "smooth",
});
});
// Show the 'back to top' button when the user scrolls down
window.addEventListener("scroll", function () {
if (window.scrollY > 100) {
backToTopButton.style.display = "block";
} else {
backToTopButton.style.display = "none";
}
});
});
const fetchQuotes = () => {
fetch("data.json")
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
const tags = new Set();
const shuffledQuotes = data.sort(() => Math.random() - 0.5);
// Extract unique tags from quotes
shuffledQuotes.forEach(quote => {
quote.tags.forEach(tag => tags.add(tag));
});
const select = document.getElementById("tags");
// Add the "All" option first
const allOption = document.createElement('option');
allOption.value = "all";
allOption.textContent = "All";
select.appendChild(allOption);
// Populate the select dropdown with unique tags, capitalizing the first letter and sorting them
Array.from(tags)
.map(tag => tag.charAt(0).toUpperCase() + tag.slice(1)) // Capitalize the first letter
.sort() // Sort tags alphabetically
.forEach((val) => {
const option = document.createElement('option');
option.value = val.toLowerCase(); // Set value to lowercase to maintain consistency
option.textContent = val;
select.appendChild(option);
});
select.onchange = (e) => {
const tag = e.target.value;
// If "All" is selected, render all quotes
if (tag === "all") {
renderQuotes(shuffledQuotes);
} else {
// Filter the quotes by the selected tag
const newQuotes = shuffledQuotes.filter((quote) =>
quote.tags.includes(tag.toLowerCase()) // Match case-insensitively
);
renderQuotes(newQuotes);
}
};
// Initially render all shuffled quotes
renderQuotes(shuffledQuotes);
})
.catch((error) => {
console.error("Error fetching the JSON data:", error);
});
};
const renderQuotes = (shuffledQuotes) => {
const quotesContainer = document.getElementById("quotes-container");
quotesContainer.innerHTML = ""; // Clear existing quotes
let likedQuotes = JSON.parse(localStorage.getItem("likedQuotes")) || [];
// Iterate over the shuffled quotes
shuffledQuotes.forEach((quoteData) => {
// Create a div for each quote block
const quoteBlock = document.createElement("div");
quoteBlock.className = "quote-block";
// Add the quote text
const quoteText = document.createElement("p");
quoteText.className = "quote-text";
quoteText.textContent = quoteData.quote || quoteData.text; // Handle both "quote" and "text" keys
// Add the author
const quoteAuthor = document.createElement("p");
quoteAuthor.className = "quote-author";
quoteAuthor.textContent = `— ${quoteData.author}`;
// Add the source
const quoteSource = document.createElement("p");
quoteSource.className = "quote-source";
quoteSource.textContent = `Source: ${quoteData.source}`;
// Create a container for icons
const iconsContainer = document.createElement("div");
iconsContainer.style.width = "100%";
iconsContainer.className = "icons-container";
// Create a container for icon wrappers
const iconsWrapper = document.createElement("div");
iconsWrapper.className = "icons-wrapper";
// Create a container for quote title and ellipsis icon
const cardTop = document.createElement("div");
cardTop.className = "card-top";
// Create a container for source and like icon
const cardBottom = document.createElement("div");
cardBottom.className = "card-bottom";
// Add icons
// Add copy icon
const copyIcon = document.createElement("i");
copyIcon.className = "icon fas fa-copy"; // FontAwesome copy icon
// Add share icon
const shareIcon = document.createElement("i");
shareIcon.className = "icon fas fa-share"; // FontAwesome share icon
// Add voice icon
const voiceIcon = document.createElement("i");
voiceIcon.className = "icon fas fa-volume-up"; // FontAwesome voice icon
const likeIcon = document.createElement("i");
likeIcon.className = "icon fas fa-heart";
// Add ellipse icon
const ellipsisIcon = document.createElement("i");
ellipsisIcon.className = "toggle-icon fa-solid fa-ellipsis-vertical";
const saveImageIcon = document.createElement("i");
saveImageIcon.className = "icon fas fa-image";
saveImageIcon.title = "Save as Image";
if (likedQuotes.includes(quoteText.textContent)) {
likeIcon.classList.add("liked");
}
// Append icons to the container
iconsContainer.appendChild(copyIcon);
iconsContainer.appendChild(shareIcon);
iconsContainer.appendChild(voiceIcon);
iconsContainer.appendChild(likeIcon);
iconsContainer.appendChild(saveImageIcon);
iconsContainer.appendChild(iconsWrapper);
iconsWrapper.appendChild(copyIcon);
iconsWrapper.appendChild(shareIcon);
iconsWrapper.appendChild(voiceIcon);
iconsWrapper.appendChild(saveImageIcon);
iconsContainer.appendChild(iconsWrapper);
// Append like icon and source text to card bottom
cardBottom.appendChild(quoteSource);
cardBottom.appendChild(likeIcon);
cardTop.appendChild(quoteText);
cardTop.appendChild(ellipsisIcon);
// Append elements to the quote block
quoteBlock.appendChild(cardTop);
quoteBlock.appendChild(iconsContainer);
quoteBlock.appendChild(quoteAuthor);
quoteBlock.appendChild(cardBottom);
// Append the quote block to the container
quotesContainer.appendChild(quoteBlock);
// Toggle functionality
ellipsisIcon.addEventListener("click", () => {
iconsContainer.classList.toggle("icons-container");
});
// Copy functionality
copyIcon.addEventListener("click", function () {
navigator.clipboard
.writeText(`${quoteText.textContent} ${quoteAuthor.textContent}`)
.then(() => {
// Change the copy icon to a checkmark
copyIcon.className = "icon fas fa-check";
// Revert the icon back to copy after a delay (e.g., 10 seconds)
setTimeout(() => {
copyIcon.className = "icon fas fa-copy";
iconsContainer.classList.toggle("icons-container");
}, 10000);
})
.catch((err) => {
iconsContainer.classList.toggle("icons-container");
console.log("Error copying text: ", err);
});
});
// Share functionality
shareIcon.addEventListener("click", function () {
if (navigator.share) {
navigator
.share({
title: "Quote", // Title for the shared content
text: `${quoteText.textContent} ${quoteAuthor.textContent}\n\nRead more at: ${window.location.href}`,
})
.then(() => {
iconsContainer.classList.toggle("icons-container");
console.log("Quote shared!");
})
.catch((error) => {
iconsContainer.classList.toggle("icons-container");
console.log("Error sharing:", error);
});
} else {
alert("Sharing is not supported in this browser.");
}
});
// Voice functionality
voiceIcon.addEventListener("click", function () {
const utterance = new SpeechSynthesisUtterance(
`${quoteText.textContent} by ${quoteAuthor.textContent}`
);
speechSynthesis.speak(utterance);
iconsContainer.classList.toggle("icons-container");
});
// Like functionality
likeIcon.addEventListener("click", function () {
if (likeIcon.classList.contains("liked")) {
likeIcon.classList.remove("liked");
likedQuotes = likedQuotes.filter(
(quote) => quote !== quoteText.textContent
);
} else {
likeIcon.classList.add("liked");
likedQuotes.push(quoteText.textContent);
}
localStorage.setItem("likedQuotes", JSON.stringify(likedQuotes));
});
// Save as image functionality
saveImageIcon.addEventListener("click", function () {
saveQuoteAsImage(quoteBlock);
});
});
};