From d854b9c47bced5067b4e75f18f6f3068431ccdcf Mon Sep 17 00:00:00 2001 From: johannst Date: Tue, 11 May 2021 21:49:13 +0200 Subject: simplify one-line lambdas, pull input out of context --- static/search.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'static') 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) => { + ""; } -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); } } -- cgit v1.2.3