-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
64 lines (59 loc) · 1.85 KB
/
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
let searchIndex = null;
fetch("/api/search-index.json")
.then(function (response) {
return response.json();
})
.then(function (data) {
searchIndex = data;
});
function submitSearch(event) {
event.preventDefault();
const search = document.getElementById("search");
let value = search.value;
// matomo search tracking
if (window._paq) {
window._paq.push([
"trackSiteSearch",
// Search keyword searched for
value,
// Search category selected in your search engine. If you do not need this, set to false
false,
// Number of results on the Search results page. Zero indicates a 'No Result Search Keyword'. Set to false if you don't know
false,
]);
}
if (value.length > 0) {
let results = searchPersons(searchIndex, value);
const searchResults = document.getElementById("search-results");
document.getElementById("persons").style.display = "none";
searchResults.style.display = "flex";
searchResults.innerHTML = results
.map((person) => document.getElementById(person.id).outerHTML)
.join("");
window.lazyLoadInstance.update();
} else {
document.getElementById("persons").style.display = "flex";
document.getElementById("search-results").style.display = "none";
}
}
function searchPersons(searchIndex, text) {
let persons = searchIndex.filter((person) => {
let match = false;
person._exactMatch = false;
let textLowerCased = text.toLowerCase().trim();
person.keywords.forEach((keyword) => {
keyword = keyword.toLowerCase();
if (keyword.indexOf(textLowerCased) > -1) {
match = true;
if (textLowerCased.length === keyword.length) {
person._exactMatch = true;
}
}
});
return match;
});
persons = persons.sort((a, b) => {
return b._exactMatch - a._exactMatch;
});
return persons;
}