-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
321 lines (275 loc) · 9.18 KB
/
functions.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
314
315
316
317
318
319
320
321
// Set focus on the search input
document.querySelector('input[type="search"]').focus();
// Reset the search when the reset button is clicked
document.addEventListener('click', function(e) {
if (e.target.matches('.reset-search')) {
document.querySelector('input[type="search"]').value = '';
document.querySelectorAll('.char-cards .char').forEach(function(char) {
char.style.display = 'block';
});
e.target.classList.add('hidden');
e.preventDefault();
}
});
// Listen for key events to activate search input
document.addEventListener('keyup', function(e) {
if (e.keyCode === 83 || e.keyCode === 70) { // s(earch), f(ind), or esc
if (document.querySelector('input[type="search"]') !== document.activeElement) {
document.querySelector('input[type="search"]').value = '';
document.querySelector('input[type="search"]').focus();
}
}
if (e.keyCode === 27) { // esc
document.querySelector('input[type="search"]').value = '';
document.querySelector('input[type="search"]').focus();
}
});
document.querySelectorAll('input[type="search"]').forEach(function(input) {
input.addEventListener('keyup', function() {
var search_term = this.value.toLowerCase();
document.querySelectorAll('.char-cards .char').forEach(function(char) {
if (char.getAttribute('data-search-term').toLowerCase().includes(search_term) || search_term.length < 1) {
char.style.display = 'block';
} else {
char.style.display = 'none';
}
});
window.scrollTo(0, 0);
if (this.value !== '') {
document.querySelector('.reset-search').classList.remove('hidden');
}
});
});
/**
* Darkmode.
*/
(() => {
const modeToggle = document.getElementById( 'dark-mode-toggle' );
if (!modeToggle) {
return;
}
let activeMode = localStorage.getItem( 'charcopy-mode' );
const setMode = (isDark, isUserTriggered) => {
localStorage.setItem('charcopy-mode', isDark ? 'dark' : 'light');
modeToggle.checked = isDark ? true : false;
const applyModeClasses = () => {
if (isDark) {
document.body.classList.add( 'dark-mode' );
document.body.classList.remove( 'light-mode' );
} else {
document.body.classList.add( 'light-mode' );
document.body.classList.remove( 'dark-mode' );
}
};
if (isUserTriggered) {
document.body.classList.add( 'mode-switching' );
setTimeout(() => {
document.body.classList.remove( 'mode-switching' );
}, 1000);
setTimeout( applyModeClasses, 400 );
} else {
applyModeClasses();
}
};
// When no mode was set, get current system default.
if (!activeMode) {
activeMode = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
// Set mode based on localStorage or default system setting.
setMode('dark' === activeMode, false);
// Set mode based on toggle.
modeToggle.addEventListener('click', (e) => {
setMode(e.target.checked, true);
});
})();
// define the color options
const colorOptions = {
yellow: 'color-scheme-yellow',
green: 'color-scheme-green',
blue: 'color-scheme-blue',
purple: 'color-scheme-purple',
pink: 'color-scheme-pink'
};
// Define the favicon options
const faviconOptions = {
yellow: 'favicon-yellow.ico',
green: 'favicon-green.ico',
blue: 'favicon-blue.ico',
purple: 'favicon-purple.ico',
pink: 'favicon-pink.ico'
};
// check if there is a color preference stored in localStorage
const colorPref = localStorage.getItem( 'colorPref' );
// if there is a color preference, use it, otherwise use the default color
const initialColor = colorPref ? colorOptions[colorPref] : colorOptions.yellow;
// set the initial color
setTheme( initialColor );
// attach event listeners to the color switcher buttons
Object.keys(colorOptions).forEach(color => {
const button = document.getElementById(`${color}-button`);
button.addEventListener('click', () => {
localStorage.setItem('colorPref', color);
updateFavicon( color );
setTimeout(() => {
setTheme(colorOptions[color]);
}, 400);
document.body.classList.add( 'mode-switching' );
setTimeout(() => {
document.body.classList.remove( 'mode-switching' );
}, 1000);
Object.keys(colorOptions).forEach(otherColor => {
const otherButton = document.getElementById(`${otherColor}-button`);
if (color === otherColor) {
otherButton.classList.add('current');
} else {
otherButton.classList.remove('current');
}
});
});
});
// set the theme of the page
function setTheme(theme) {
document.body.classList.forEach(className => {
if (className.startsWith( 'color-scheme-' )) {
document.body.classList.remove(className);
}
});
document.body.classList.add(theme);
Object.keys(colorOptions).forEach(color => {
const button = document.getElementById(`${color}-button`);
if (theme === colorOptions[color]) {
button.classList.add('current');
} else {
button.classList.remove('current');
}
});
}
// Update the favicon according to the current color
function updateFavicon(color) {
const favicon = document.querySelector("link[rel*='icon']");
favicon.href = faviconOptions[color];
}
// Update the initial favicon
updateFavicon(colorPref ? colorPref : 'yellow');
// Load characters dynamically
const charactersContainer = document.querySelector( "#characters-container" );
const loadingMessage = document.querySelector( "#loading-message" );
function renderCharacters(characters) {
loadingMessage.style.display = "none";
charactersContainer.classList.remove( "loading" );
characters.forEach(character => {
const charElement = document.createElement("div");
charElement.classList.add("char");
if (character.searchTerm === "shrug") {
charElement.classList.add("shrug");
}
charElement.setAttribute("data-search-term", character.searchTerm);
const charButton = document.createElement("button");
charButton.classList.add("char-button");
charButton.setAttribute("data-clipboard-text", character.clipboardText);
charButton.innerHTML = character.character;
charElement.appendChild(charButton);
charactersContainer.appendChild(charElement);
});
}
fetch( "characters.json" )
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Network response was not ok');
}
})
.then(characters => {
// Cache the characters.json file
caches.open('offline-cache').then(cache => {
cache.put('characters.json', new Response(JSON.stringify(characters)));
});
// Render the characters on the page
renderCharacters(characters);
})
.catch(error => {
console.log('Fetch error:', error);
// If the user is offline, try to get the characters.json file from the cache
caches.match('characters.json').then(response => {
if (response) {
return response.json();
}
}).then(characters => {
// If the characters are found in the cache, render them on the page
if (characters) {
renderCharacters(characters);
} else {
// If the characters are not found in the cache, display an error message
loadingMessage.innerHTML = "Unable to load characters";
}
});
});
// Copy characters to clipboard
charactersContainer.addEventListener("click", function(event) {
if (event.target.classList.contains("char-button")) {
var charText = event.target.getAttribute("data-clipboard-text");
navigator.clipboard
.writeText(charText)
.then(function() {
var confirmationText =
'<span class="character">' +
charText +
'</span><span class="message"> copied to clipboard</span>';
var confirmationElement = document.getElementById("confirmation-message");
var confirmationInnerElement = confirmationElement.querySelector(
".copy-confirmation-inner"
);
confirmationInnerElement.innerHTML = confirmationText;
confirmationElement.classList.add("visible");
setTimeout(function() {
confirmationElement.classList.remove("visible");
}, 750); // Remove "visible" class after 750 ms
})
.catch(function() {
console.error("Failed to copy to clipboard");
});
}
});
// Offline Mode
document.addEventListener('DOMContentLoaded', () => {
function updateOnlineStatus() {
const offlineTextDiv = document.querySelector('.offline-text');
const body = document.body;
if (navigator.onLine) {
body.classList.remove('offline-mode');
// Remove the offline-text div when online
if (offlineTextDiv && body) {
body.removeChild(offlineTextDiv);
}
} else {
body.classList.add('offline-mode');
// Add the offline-text div when offline
if (!offlineTextDiv && body) {
const newOfflineTextDiv = document.createElement('div');
newOfflineTextDiv.classList.add('offline-text');
newOfflineTextDiv.innerText = 'You are offline. But no worries, the app still works!';
body.insertBefore(newOfflineTextDiv, body.firstChild);
}
}
}
window.addEventListener('online', updateOnlineStatus);
window.addEventListener('offline', updateOnlineStatus);
updateOnlineStatus();
});
// Clear Cache
const clearCacheButton = document.querySelector( "#clear-cache-button" );
if (
window.location.search === "?debug" ||
window.location.search === "?cache" ||
window.location.search === "?clear"
) {
clearCacheButton.style.display = "block";
}
clearCacheButton.addEventListener("click", function() {
caches.delete( "offline-cache" ).then(function() {
console.log("Cache cleared successfully");
window.location.reload();
window.location.search = "";
});
});