2020-01-12 14:33:02 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
{{ $searchDataFile := printf "js/%s.search-data.js" .Language.Lang }}
|
|
|
|
{{ $searchData := resources.Get "js/search-data.js" | resources.ExecuteAsTemplate $searchDataFile . | resources.Minify | resources.Fingerprint }}
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
const input = document.querySelector('#gdoc-search-input');
|
|
|
|
const results = document.querySelector('#gdoc-search-results');
|
2020-12-20 19:09:49 +00:00
|
|
|
let showParent = false
|
|
|
|
|
|
|
|
{{ if .Site.Params.GeekdocSearchShowParent }}
|
|
|
|
showParent = true
|
|
|
|
{{ end }}
|
2020-01-12 14:33:02 +00:00
|
|
|
|
|
|
|
input.addEventListener('focus', init);
|
|
|
|
input.addEventListener('keyup', search);
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
input.removeEventListener('focus', init); // init once
|
|
|
|
input.required = true;
|
|
|
|
|
2020-12-02 14:21:34 +00:00
|
|
|
loadScript('{{ index .Site.Data.assets "js/flexsearch.min.js" | relURL }}');
|
2020-01-12 14:33:02 +00:00
|
|
|
loadScript('{{ $searchData.RelPermalink }}', function() {
|
|
|
|
input.required = false;
|
|
|
|
search();
|
|
|
|
});
|
2020-12-20 19:09:49 +00:00
|
|
|
loadScript('{{ index .Site.Data.assets "js/groupBy.min.js" | relURL }}');
|
2020-01-12 14:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function search() {
|
|
|
|
while (results.firstChild) {
|
|
|
|
results.removeChild(results.firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!input.value) {
|
|
|
|
console.log("empty")
|
|
|
|
results.classList.remove("has-hits");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:09:49 +00:00
|
|
|
let searchHits = window.geekdocSearchIndex.search(input.value, 10);
|
2020-01-12 14:33:02 +00:00
|
|
|
|
|
|
|
console.log(searchHits.length);
|
2020-12-20 19:09:49 +00:00
|
|
|
if (searchHits.length < 1) {
|
|
|
|
return results.classList.remove("has-hits");
|
|
|
|
}
|
|
|
|
|
|
|
|
results.classList.add("has-hits");
|
2020-12-21 12:14:51 +00:00
|
|
|
|
2020-12-20 19:09:49 +00:00
|
|
|
if (showParent) {
|
|
|
|
searchHits = groupBy(searchHits, hit => hit.parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
const items = [];
|
|
|
|
|
2020-12-21 12:14:51 +00:00
|
|
|
for (const section in searchHits) {
|
|
|
|
const item = document.createElement('li');
|
2020-12-20 19:09:49 +00:00
|
|
|
|
2020-12-21 12:14:51 +00:00
|
|
|
if (showParent) {
|
|
|
|
const title = item.appendChild(document.createElement('span'));
|
2020-12-20 19:09:49 +00:00
|
|
|
title.textContent = section;
|
|
|
|
}
|
2020-12-21 12:14:51 +00:00
|
|
|
|
|
|
|
const subList = item.appendChild(document.createElement('ul'));
|
|
|
|
createLinks(searchHits[section], subList);
|
|
|
|
|
|
|
|
items.push(item);
|
2020-01-12 14:33:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 19:09:49 +00:00
|
|
|
items.forEach(item => {
|
|
|
|
results.appendChild(item);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates links to given pages and either returns them in an array or attaches them to a target element
|
|
|
|
* @param {Object} pages Page to which the link should point to
|
|
|
|
* @param {HTMLElement} target Element to which the links should be attatched
|
|
|
|
* @returns {Array} If target is not specified, returns an array of built links
|
|
|
|
*/
|
|
|
|
function createLinks(pages, target) {
|
|
|
|
const items = [];
|
2020-01-12 14:33:02 +00:00
|
|
|
|
2020-12-20 19:09:49 +00:00
|
|
|
for (const page of pages) {
|
2020-12-21 12:14:51 +00:00
|
|
|
const item = document.createElement("li"),
|
|
|
|
entry = item.appendChild(document.createElement("span")),
|
|
|
|
a = entry.appendChild(document.createElement("a"));
|
|
|
|
|
|
|
|
entry.classList.add("flex")
|
2020-11-10 21:50:21 +00:00
|
|
|
|
2020-12-20 19:09:49 +00:00
|
|
|
a.href = page.href;
|
2020-01-12 14:33:02 +00:00
|
|
|
a.textContent = page.title;
|
2020-12-21 12:14:51 +00:00
|
|
|
a.classList.add("gdoc-search__entry")
|
2020-01-12 14:33:02 +00:00
|
|
|
|
2020-12-20 19:09:49 +00:00
|
|
|
if (target) {
|
|
|
|
target.appendChild(item);
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
items.push(item);
|
|
|
|
}
|
2020-01-12 14:33:02 +00:00
|
|
|
|
2020-12-20 19:09:49 +00:00
|
|
|
return items;
|
2020-01-12 14:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadScript(src, callback) {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.defer = true;
|
|
|
|
script.async = false;
|
|
|
|
script.src = src;
|
|
|
|
script.onload = callback;
|
|
|
|
|
|
|
|
document.head.appendChild(script);
|
|
|
|
}
|
|
|
|
})();
|