2022-06-15 07:51:47 +00:00
|
|
|
var suggestions = document.getElementById('suggestions');
|
|
|
|
var search = document.getElementById('search');
|
|
|
|
|
|
|
|
if (search !== null) {
|
|
|
|
document.addEventListener('keydown', inputFocus);
|
|
|
|
}
|
|
|
|
|
|
|
|
function inputFocus(e) {
|
|
|
|
if (e.ctrlKey && e.key === '/' ) {
|
|
|
|
e.preventDefault();
|
|
|
|
search.focus();
|
|
|
|
}
|
|
|
|
if (e.key === 'Escape' ) {
|
|
|
|
search.blur();
|
|
|
|
suggestions.classList.add('d-none');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('click', function(event) {
|
|
|
|
|
|
|
|
var isClickInsideElement = suggestions.contains(event.target);
|
|
|
|
|
|
|
|
if (!isClickInsideElement) {
|
|
|
|
suggestions.classList.add('d-none');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
Source:
|
|
|
|
- https://dev.to/shubhamprakash/trap-focus-using-javascript-6a3
|
|
|
|
*/
|
|
|
|
|
|
|
|
document.addEventListener('keydown',suggestionFocus);
|
|
|
|
|
|
|
|
function suggestionFocus(e) {
|
|
|
|
const suggestionsHidden = suggestions.classList.contains('d-none');
|
|
|
|
if (suggestionsHidden) return;
|
|
|
|
|
|
|
|
const focusableSuggestions= [...suggestions.querySelectorAll('a')];
|
|
|
|
if (focusableSuggestions.length === 0) return;
|
|
|
|
|
|
|
|
const index = focusableSuggestions.indexOf(document.activeElement);
|
|
|
|
|
|
|
|
if (e.key === "ArrowUp") {
|
|
|
|
e.preventDefault();
|
|
|
|
const nextIndex = index > 0 ? index - 1 : 0;
|
|
|
|
focusableSuggestions[nextIndex].focus();
|
|
|
|
}
|
|
|
|
else if (e.key === "ArrowDown") {
|
|
|
|
e.preventDefault();
|
|
|
|
const nextIndex= index + 1 < focusableSuggestions.length ? index + 1 : index;
|
|
|
|
focusableSuggestions[nextIndex].focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Source:
|
|
|
|
- https://github.com/nextapps-de/flexsearch#index-documents-field-search
|
|
|
|
- https://raw.githack.com/nextapps-de/flexsearch/master/demo/autocomplete.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
var index = new FlexSearch.Document({
|
|
|
|
tokenize: "forward",
|
|
|
|
cache: 100,
|
|
|
|
document: {
|
|
|
|
id: 'id',
|
|
|
|
store: [
|
|
|
|
"href", "title", "description"
|
|
|
|
],
|
|
|
|
index: ["title", "description", "content"]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-28 05:27:14 +00:00
|
|
|
{{ $list := slice }}
|
|
|
|
{{- if and (isset .Site.Params.options "searchsectionsindex") (not (eq (len .Site.Params.options.searchSectionsIndex) 0)) }}
|
|
|
|
{{- if eq .Site.Params.options.searchSectionsIndex "ALL" }}
|
|
|
|
{{- $list = .Site.Pages }}
|
|
|
|
{{- else }}
|
|
|
|
{{- $list = (where (where .Site.Pages "Type" "in" .Site.Params.options.searchSectionsIndex) ".Params.search.index" "!=" "false") }}
|
|
|
|
{{- if (in .Site.Params.options.searchSectionsIndex "HomePage") }}
|
|
|
|
{{ $list = $list | append .Site.Home }}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
{{- else }}
|
|
|
|
{{- $list = (where .Site.Pages "Section" "docs") }}
|
|
|
|
{{- end }}
|
2022-06-15 07:51:47 +00:00
|
|
|
|
|
|
|
{{ $len := (len $list) -}}
|
|
|
|
|
2022-07-14 04:29:54 +00:00
|
|
|
{{ range $index, $element := $list -}}
|
|
|
|
index.add(
|
2022-06-15 07:51:47 +00:00
|
|
|
{
|
|
|
|
id: {{ $index }},
|
|
|
|
href: "{{ .RelPermalink }}",
|
|
|
|
title: {{ .Title | jsonify }},
|
|
|
|
{{ with .Description -}}
|
|
|
|
description: {{ . | jsonify }},
|
|
|
|
{{ else -}}
|
|
|
|
description: {{ .Summary | plainify | jsonify }},
|
|
|
|
{{ end -}}
|
|
|
|
content: {{ .Plain | jsonify }}
|
2022-07-14 04:29:54 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
{{ end -}}
|
2022-06-15 07:51:47 +00:00
|
|
|
|
|
|
|
search.addEventListener('input', show_results, true);
|
|
|
|
|
|
|
|
function show_results(){
|
|
|
|
const maxResult = 5;
|
2022-06-28 05:27:14 +00:00
|
|
|
const searchQuery = this.value;
|
|
|
|
const results = index.search(searchQuery, {limit: maxResult, enrich: true});
|
2022-06-15 07:51:47 +00:00
|
|
|
|
|
|
|
// flatten results since index.search() returns results for each indexed field
|
|
|
|
const flatResults = new Map(); // keyed by href to dedupe results
|
|
|
|
for (const result of results.flatMap(r => r.result)) {
|
|
|
|
if (flatResults.has(result.doc.href)) continue;
|
|
|
|
flatResults.set(result.doc.href, result.doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestions.innerHTML = "";
|
|
|
|
suggestions.classList.remove('d-none');
|
|
|
|
|
|
|
|
// inform user that no results were found
|
|
|
|
if (flatResults.size === 0 && searchQuery) {
|
|
|
|
const noResultsMessage = document.createElement('div')
|
|
|
|
noResultsMessage.innerHTML = `No results for "<strong>${searchQuery}</strong>"`
|
|
|
|
noResultsMessage.classList.add("suggestion__no-results");
|
|
|
|
suggestions.appendChild(noResultsMessage);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct a list of suggestions
|
|
|
|
for(const [href, doc] of flatResults) {
|
|
|
|
const entry = document.createElement('div');
|
|
|
|
suggestions.appendChild(entry);
|
|
|
|
|
|
|
|
const a = document.createElement('a');
|
|
|
|
a.href = href;
|
|
|
|
entry.appendChild(a);
|
|
|
|
|
|
|
|
const title = document.createElement('span');
|
|
|
|
title.textContent = doc.title;
|
|
|
|
title.classList.add("suggestion__title");
|
|
|
|
a.appendChild(title);
|
|
|
|
|
|
|
|
const description = document.createElement('span');
|
|
|
|
description.textContent = doc.description;
|
|
|
|
description.classList.add("suggestion__description");
|
|
|
|
a.appendChild(description);
|
|
|
|
|
|
|
|
suggestions.appendChild(entry);
|
|
|
|
|
2022-06-28 05:27:14 +00:00
|
|
|
if(suggestions.childElementCount === maxResult) break;
|
2022-06-15 07:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}());
|