-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
search.js
127 lines (101 loc) · 3.79 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
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
/**
* @fileoverview Search functionality
* @author Nicholas C. Zakas
*/
//-----------------------------------------------------------------------------
// Imports
//-----------------------------------------------------------------------------
import algoliasearch from "./algoliasearch.js";
//-----------------------------------------------------------------------------
// Initialization
//-----------------------------------------------------------------------------
// search
const client = algoliasearch('L633P0C2IR', 'bb6bbd2940351f3afc18844a6b06a6e8');
const index = client.initIndex('eslint');
// page
const searchInputs = document.querySelectorAll('input.search__input');
//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------
/**
* Executes a search against the Algolia index.
* @param {string} query The search query to execute.
* @returns {Promise<Array<object>>} The search results.
*/
function fetchSearchResults(query) {
return index.search(query, {
facetFilters: ["tags:docs"]
}).then(({ hits }) => hits);
}
/**
* Removes any current search results from the display.
* @param {Element} resultsElement The HTML element used to display search results.
* @returns {void}
*/
function clearSearchResults(resultsElement) {
while (resultsElement.firstChild) {
resultsElement.removeChild(resultsElement.firstChild);
}
}
/**
* Displays the given search results in the page.
* @param {Array<object>} results The search results to display.
* @param {Element} resultsElement The HTML element used to display search results.
* @returns {void}
*/
function displaySearchResults(results,resultsElement) {
clearSearchResults(resultsElement);
if (results.length) {
const list = document.createElement("ul");
resultsElement.append(list);
for (const result of results) {
const listItem = document.createElement('li');
listItem.innerHTML = `
<a href="${result.url}">${result.hierarchy.lvl0}</a><br>
${result._highlightResult.hierarchy.lvl0.value}
`.trim();
list.append(listItem);
}
} else {
resultsElement.innerHTML = "No results found.";
}
}
//-----------------------------------------------------------------------------
// Event Handlers
//-----------------------------------------------------------------------------
// listen for input changes
searchInputs.forEach((searchInput) => {
searchInput.addEventListener('keyup', function (event) {
const resultsElement = document.querySelector(`#${searchInput.id}-results`);
const query = searchInput.value;
if (query.length > 2) {
fetchSearchResults(query)
.then(function (results) { displaySearchResults(results, resultsElement) })
.catch(function () {
clearSearchResults(resultsElement);
});
} else {
clearSearchResults(resultsElement);
}
});
});
// add an event listenrer for a click on the search link
// btnHandler('#search-link', function(){
// // get the data
// fetch('/search.json').then(function(response) {
// return response.json();
// }).then(function(response) {
// searchIndex = response.search;
// });
// searchUI.toggleAttribute('hidden');
// searchInput.focus();
// // listen for input changes
// searchInput.addEventListener('keyup', function(event) {
// const str = searchInput.value;
// if(str.length > 2) {
// find(str);
// } else {
// clearResults();
// }
// });
// });