-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsite-search.js
206 lines (161 loc) · 7.26 KB
/
site-search.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
/* -------------------------------------------------------------------------- */
/* SITE SEARCH
/* -------------------------------------------------------------------------- */
/* RIGHT SEARCH CURTAIN OVERLAY
Triggers the right search panel*/
function openSearch() {
document.body.style.overflow = 'hidden';
document.body.style.maxHeight = '100vh';
document.getElementById("right-search-overlay").style.width = "100%";
document.body.style.overflowY = "clip";
var modal = document.getElementById('right-search-overlay');
function modalShow() {
modal.setAttribute('tabindex', '0');
modal.focus();
}
function focusRestrict(event) {
document.addEventListener('focus', function(event) {
if (modalOpen && !modal.contains(event.target)) {
event.stopPropagation();
modal.focus();
}
}, true);
}
// Define placeholders for different languages
var placeholders = {
'en': 'Enter search term...',
'es': 'Introduzca el término de búsqueda...',
'fr': 'Entrez le terme de recherche...',
'id': 'Masukkan istilah pencarian...'
};
// Update checkbox states based on currentLanguage
var currentLanguage = window.currentLanguage || 'en'; // Default to English if undefined
var checkboxes = document.querySelectorAll('.search-row input[type="checkbox"]');
checkboxes.forEach(function(checkbox) {
checkbox.checked = false; // Reset all checkboxes
});
['en', 'es', 'fr', 'id'].forEach(function(lang) {
var isChecked = (currentLanguage === lang);
document.querySelectorAll(`input[name='searchIndex'][value='${lang}_site']`).forEach(function(checkbox) {
checkbox.checked = isChecked;
});
document.querySelectorAll(`input[name='searchIndex'][value='${lang}_glossary']`).forEach(function(checkbox) {
checkbox.checked = isChecked;
});
});
// Update placeholder text based on currentLanguage
var searchInput = document.getElementById('search_input');
searchInput.placeholder = placeholders[currentLanguage];
modalShow(); // Ensure the modal is shown correctly
}
/* Close when someone clicks on the "x" symbol inside the overlay */
function closeSearch() {
document.getElementById("right-search-overlay").style.width = "0%";
// Allow scrolling on the body again
document.body.style.overflow = '';
document.body.style.maxHeight = '';
}
/* MAIN SITE SEARCH FUNCTION */
function siteSearch() {
var query = document.getElementById("search_input").value.toLowerCase();
var checkboxes = document.querySelectorAll('input[name="searchIndex"]:checked');
var jsonFiles = [];
checkboxes.forEach(function(checkbox) {
switch (checkbox.value) {
case 'en_glossary':
jsonFiles.push('../en/indexes/glossary-ecobricks.json');
break;
case 'en_site':
jsonFiles.push('../en/indexes/pages.json?v=2');
break;
case 'fr_site':
jsonFiles.push('../fr/indexes/pages.json');
break;
case 'fr_glossary':
jsonFiles.push('../fr/indexes/glossary-ecobricks.json');
break;
case 'es_site':
jsonFiles.push('../es/indexes/pages.json');
break;
case 'es_glossary':
jsonFiles.push('../es/indexes/glossary-ecobricks.json');
break;
case 'id_site':
jsonFiles.push('../id/indexes/pages.json');
break;
case 'id_glossary':
jsonFiles.push('../id/indexes/glossary-ecobricks.json');
break;
}
});
var overlayContent = document.querySelector('.search-overlay-content');
overlayContent.style.height = 'fit-content';
overlayContent.style.marginTop = '8%';
var posts = [];
var numFilesLoaded = 0;
for (var i = 0; i < jsonFiles.length; i++) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
posts = posts.concat(data);
numFilesLoaded++;
if (numFilesLoaded == jsonFiles.length) {
presentSearchResults(posts, query);
}
}
};
xmlhttp.open("GET", jsonFiles[i], true);
xmlhttp.send();
}
}
function presentSearchResults(posts, query) {
// Implementation of presentSearchResults remains the same as previously provided
}
function presentSearchResults(posts, query) {
const noResultsTranslations = {
'en': '😕 Sorry, no results were found for ',
'es': '😕 Lo siento, no se encontraron resultados para ',
'fr': '😕 Désolé, aucun résultat trouvé pour ',
'id': '😕 Maaf, tidak ada hasil yang ditemukan untuk '
};
var isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
var outputPosts = [];
for (var j = 0; j < posts.length; j++) {
var post = posts[j];
if ((post.keywords && post.keywords.toLowerCase().includes(query)) || (post.description && post.description.toLowerCase().includes(query))) {
outputPosts.push(post);
}
}
var resultsContainer = document.getElementById("search_results");
resultsContainer.innerHTML = "";
if (outputPosts.length == 0) {
// Use the global currentLanguage to select the appropriate translation
var noResultsMessage = noResultsTranslations[currentLanguage] + "\"" + query + "\".</p>";
resultsContainer.innerHTML = "<p>" + noResultsMessage + "</p>";
} else {
for (var k = 0; k < outputPosts.length; k++) {
var currentPost = outputPosts[k];
var imageUrl = isDarkMode && currentPost.image_url_night ? currentPost.image_url_night : currentPost.image_url;
resultsContainer.innerHTML += "<div class=\"tc-item\"><div id='result_" + k + "' style=\"display:flex; text-align:left; padding: 15px;\"><div class=\"search_chapter_pic\" ><img src=\"" + imageUrl + "\"></div><div class=\"chapter-name-search\"><a href='" + currentPost.url + "'><div class=\"chapter-title-search\">" + currentPost.title + "</div><div class=\"description_search\">" + currentPost.description + "</div><div class=\"chapter-info-search\">" + currentPost.section + " | " + currentPost.language + "<br> ↳ ../" + currentPost.lang_key + "/" + currentPost.url + "</div></a></div>";
}
}
console.log('Is Dark Mode:', isDarkMode);
console.log('Night Image URL:', currentPost.image_url_night);
console.log('Day Image URL:', currentPost.image_url);
}
function handleKeyPress(event) {
if (event.keyCode === 13) { // 13 is the key code for the enter key
event.preventDefault(); // Prevent the default action to stop form submission
siteSearch(); // Call your search function without arguments
}
}
function clearResults() {
var searchInput = document.getElementById('search_input');
var resultsContainer = document.getElementById('search_results');
var overlayContent = document.querySelector('.search-overlay-content');
searchInput.value = '';
resultsContainer.innerHTML = '';
overlayContent.style.height = '';
overlayContent.style.marginTop = '';
}