portails/assets/js/index.js

163 lines
4.0 KiB
JavaScript
Raw Normal View History

2020-12-06 12:03:41 +01:00
var suggestions = document.getElementById('suggestions');
2021-09-07 12:08:46 +02:00
var search = document.getElementById('search');
2020-12-06 12:03:41 +01:00
2021-09-07 12:08:46 +02:00
if (search !== null) {
document.addEventListener('keydown', inputFocus);
}
2020-12-06 12:03:41 +01:00
function inputFocus(e) {
2021-09-07 12:08:46 +02:00
if (e.ctrlKey && e.key === '/' ) {
2020-12-06 12:03:41 +01:00
e.preventDefault();
2021-09-07 12:08:46 +02:00
search.focus();
2020-12-06 12:03:41 +01:00
}
2021-09-07 12:08:46 +02:00
if (e.key === 'Escape' ) {
search.blur();
2020-12-06 12:03:41 +01:00
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 focusableSuggestions= suggestions.querySelectorAll('a');
const focusable= [...focusableSuggestions];
const index = focusable.indexOf(document.activeElement);
2021-09-07 12:08:46 +02:00
const keyDefault = suggestions.classList.contains('d-none');
2020-12-06 12:03:41 +01:00
let nextIndex = 0;
2021-09-07 12:08:46 +02:00
if ((e.keyCode === 38) && (!keyDefault)) {
2020-12-06 12:03:41 +01:00
e.preventDefault();
nextIndex= index > 0 ? index-1 : 0;
focusableSuggestions[nextIndex].focus();
}
2021-09-07 12:08:46 +02:00
else if ((e.keyCode === 40) && (!keyDefault)) {
2020-12-06 12:03:41 +01:00
e.preventDefault();
nextIndex= index+1 < focusable.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(){
2021-06-25 13:29:22 +02:00
var index = new FlexSearch.Document({
tokenize: "forward",
cache: 100,
document: {
id: 'id',
store: [
"href", "title", "description"
],
index: ["title", "description", "content"]
}
2020-12-06 12:03:41 +01:00
});
2021-06-25 13:29:22 +02:00
// Not yet supported: https://github.com/nextapps-de/flexsearch#complex-documents
/*
2020-12-06 12:03:41 +01:00
var docs = [
{{ range $index, $page := (where .Site.Pages "Section" "docs") -}}
{
id: {{ $index }},
2021-06-25 13:29:22 +02:00
href: "{{ .Permalink }}",
2020-12-06 12:03:41 +01:00
title: {{ .Title | jsonify }},
description: {{ .Params.description | jsonify }},
content: {{ .Content | jsonify }}
},
{{ end -}}
];
2021-06-25 13:29:22 +02:00
*/
2020-12-06 12:03:41 +01:00
2021-06-25 13:29:22 +02:00
// https://discourse.gohugo.io/t/range-length-or-last-element/3803/2
{{ $list := (where .Site.Pages "Section" "docs") -}}
{{ $len := (len $list) -}}
index.add(
{{ range $index, $element := $list -}}
{
id: {{ $index }},
2021-09-28 16:26:15 +02:00
href: "{{ .RelPermalink }}",
2021-06-25 13:29:22 +02:00
title: {{ .Title | jsonify }},
description: {{ .Params.description | jsonify }},
content: {{ .Content | jsonify }}
})
{{ if ne (add $index 1) $len -}}
.add(
{{ end -}}
{{ end -}}
;
2020-12-06 12:03:41 +01:00
2021-09-07 12:08:46 +02:00
search.addEventListener('input', show_results, true);
2020-12-06 12:03:41 +01:00
suggestions.addEventListener('click', accept_suggestion, true);
function show_results(){
const maxResult = 5;
2020-12-06 12:03:41 +01:00
var value = this.value;
var results = index.search(value, {limit: maxResult, enrich: true});
2020-12-06 12:03:41 +01:00
suggestions.classList.remove('d-none');
suggestions.innerHTML = "";
//flatSearch now returns results for each index field. create a single list
const flatResults = {}; //keyed by href to dedupe results
results.forEach(result=>{
result.result.forEach(r=>{
flatResults[r.doc.href] = r.doc;
});
});
2020-12-06 12:03:41 +01:00
//construct a list of suggestions list
for(const href in flatResults) {
const doc = flatResults[href];
2020-12-06 12:03:41 +01:00
const entry = document.createElement('div');
entry.innerHTML = '<a href><span></span><span></span></a>';
2020-12-06 12:03:41 +01:00
entry.querySelector('a').href = href;
entry.querySelector('span:first-child').textContent = doc.title;
entry.querySelector('span:nth-child(2)').textContent = doc.description;
2020-12-06 12:03:41 +01:00
suggestions.appendChild(entry);
if(suggestions.childElementCount == maxResult) break;
2020-12-06 12:03:41 +01:00
}
}
function accept_suggestion(){
while(suggestions.lastChild){
suggestions.removeChild(suggestions.lastChild);
}
return false;
}
}());