aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--static/search.js32
1 files changed, 14 insertions, 18 deletions
diff --git a/static/search.js b/static/search.js
index 02f1814..be67f2d 100644
--- a/static/search.js
+++ b/static/search.js
@@ -11,12 +11,12 @@ const formatResultPreview = (body, terms) => {
// Update global index into body.
body_idx += word.length + 1 /* for the ' ' split char */;
- if (terms.some(term => { return word.startsWith(term); })) {
+ if (terms.some(term => word.startsWith(term))) {
return { idx : idx, end : idx + word.length };
} else {
return null;
}
- }).filter(match => { return match !== null; });
+ }).filter(match => match !== null);
// Format preview, by making each term bold and adding a short slice
// following after the term.
@@ -38,9 +38,7 @@ const formatResult = (result, terms) => {
+ "</div>";
}
-const evaluteSearch = (ctx) => {
- let term = ctx.input.value.trim();
-
+const evaluteSearchTerm = (term, ctx) => {
// Nothing todo if current search term is the same as last one.
if (term === ctx.last_search) {
return;
@@ -91,21 +89,19 @@ const debounce = (func, wait) => {
}
window.onload = () => {
- let ctx = new class {
- constructor() {
- // Get HTML DOM elements.
- this.input = document.getElementById("search");
- this.results = document.getElementById("search-results");
- this.results_items = document.getElementById("search-results-items");
- this.pages = document.getElementById("pages");
- // Load search index.
- this.search_index = elasticlunr.Index.load(window.searchIndex);
- // Last search term.
- this.last_search = "";
- }
+ let ctx = {
+ // Get HTML DOM elements.
+ results : document.getElementById("search-results"),
+ results_items : document.getElementById("search-results-items"),
+ pages : document.getElementById("pages"),
+ // Load search index.
+ search_index : elasticlunr.Index.load(window.searchIndex),
+ // Last search term.
+ last_search : null,
};
if (ctx.search_index) {
- ctx.input.onkeyup = debounce(() => { evaluteSearch(ctx); }, 200);
+ const input = document.getElementById("search");
+ input.onkeyup = debounce(() => evaluteSearchTerm(input.value.trim(), ctx), 200);
}
}