Skip to content

Commit 7b2d956

Browse files
authored
Merge pull request #44 from EXtremeExploit/medals-search-tags
medals: Make searching tag based
2 parents caa1ebd + 3fcfa58 commit 7b2d956

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

medals/js/functions.js

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ document.addEventListener("DOMContentLoaded", function () {
2828
document.getElementById("styled-checkbox-1").checked = true;
2929
}
3030
if (new URLSearchParams(window.location.search).get("medal") == null) landingPage();
31-
requestMedals(true, "");
31+
requestMedals(true);
3232
});
3333

3434
window.addEventListener('popstate', function (event) {
@@ -151,26 +151,63 @@ async function initColMedals() {
151151
});
152152
}
153153

154-
async function requestMedals(init, strValue) {
154+
async function requestMedals(init, strValue = '') {
155155
if (init || Object.values(colMedals).length == 0) // Init the colMedals object
156156
await initColMedals();
157157

158+
let query = strValue.trim();
159+
while (query.includes(' ')) {
160+
query = query.replace(' ', ' ');
161+
query = query.trim();
162+
}
163+
158164
let filteredMedalsArrayByGroup = [];
159165
for (let v of Object.values(colMedals)) {
160-
let doModsMatch = false;
161-
for (const word of strValue.split(' ')) {
162-
if (v.Mods == '' || v.Mods == null) break;
163-
doModsMatch = v.Mods.replace(',', '').toUpperCase().includes(word.toUpperCase());
164-
if (doModsMatch) break;
166+
let medalMatches = false;
167+
if (query == '') {
168+
medalMatches = true;
169+
} else {
170+
let wordMatches = [];
171+
for (const word of query.split(' ')) {
172+
thisWordMatches = false;
173+
if (v.Mods == null) v.Mods = '';
174+
thisWordMatches = v.Mods.replace(',', '').toUpperCase().includes(word.toUpperCase());
175+
if (thisWordMatches) {
176+
wordMatches.push(true);
177+
continue;
178+
}
179+
180+
thisWordMatches = v.Name.toLowerCase().includes(word.toLowerCase());
181+
if (thisWordMatches) {
182+
wordMatches.push(true);
183+
continue;
184+
}
185+
thisWordMatches = v.Solution?.toLowerCase().includes(word.toLowerCase());
186+
if (thisWordMatches) {
187+
wordMatches.push(true);
188+
continue;
189+
}
190+
thisWordMatches = v.Description?.toLowerCase().includes(word.toLowerCase());
191+
if (thisWordMatches) {
192+
wordMatches.push(true);
193+
continue;
194+
}
195+
thisWordMatches = v.Instructions?.toLowerCase().includes(word.toLowerCase());
196+
if (thisWordMatches) {
197+
wordMatches.push(true);
198+
continue;
199+
}
200+
thisWordMatches = v.MedalID == parseInt(word);
201+
if (thisWordMatches) {
202+
wordMatches.push(true);
203+
continue;
204+
}
205+
wordMatches.push(false);
206+
}
207+
medalMatches = !wordMatches.includes(false);
165208
}
166209

167-
// Match Name, Solution, Description, Instructions and medal id
168-
if (v.Name.toLowerCase().includes(strValue.toLowerCase()) ||
169-
v.Solution?.toLowerCase().includes(strValue.toLowerCase()) ||
170-
v.Description?.toLowerCase().includes(strValue.toLowerCase()) ||
171-
v.Instructions?.toLowerCase().includes(strValue.toLowerCase()) ||
172-
doModsMatch ||
173-
v.MedalID == parseInt(strValue)) {
210+
if (medalMatches) {
174211
if (filteredMedalsArrayByGroup[v.Grouping] == null) filteredMedalsArrayByGroup[v.Grouping] = [];
175212
filteredMedalsArrayByGroup[v.Grouping].push(v);
176213
}

0 commit comments

Comments
 (0)