2020-01-12 14:33:02 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-05 15:40:02 +00:00
|
|
|
{{ $searchDataFile := printf "%s.search-data.json" .Language.Lang }}
|
|
|
|
{{ $searchData := resources.Get "search-data.json" | resources.ExecuteAsTemplate $searchDataFile . | resources.Minify }}
|
2020-01-12 14:33:02 +00:00
|
|
|
|
|
|
|
(function() {
|
|
|
|
const input = document.querySelector('#gdoc-search-input');
|
|
|
|
const results = document.querySelector('#gdoc-search-results');
|
2021-01-05 15:40:02 +00:00
|
|
|
let showParent = {{ if .Site.Params.GeekdocSearchShowParent }}true{{ else }}false{{ end }}
|
2020-01-12 14:33:02 +00:00
|
|
|
|
2021-05-18 21:10:42 +00:00
|
|
|
if (input) {
|
|
|
|
input.addEventListener('focus', init);
|
|
|
|
input.addEventListener('keyup', search);
|
|
|
|
}
|
2020-01-12 14:33:02 +00:00
|
|
|
|
|
|
|
function init() {
|
|
|
|
input.removeEventListener('focus', init); // init once
|
|
|
|
|
2020-12-23 15:43:41 +00:00
|
|
|
loadScript('{{ index .Site.Data.assets "js/groupBy.min.js" | relURL }}');
|
2021-01-05 15:40:02 +00:00
|
|
|
loadScript('{{ index .Site.Data.assets "js/flexsearch.min.js" | relURL }}', function() {
|
2021-06-14 20:15:32 +00:00
|
|
|
const indexCfgDefaults = {
|
|
|
|
tokenize: 'forward'
|
|
|
|
}
|
|
|
|
const indexCfg = {{ with .Scratch.Get "geekdocSearchConfig" }}{{ . | jsonify }}{{ else }}indexCfgDefaults{{ end }};
|
|
|
|
const dataUrl = '{{ $searchData.RelPermalink }}'
|
2021-01-05 15:40:02 +00:00
|
|
|
|
2021-06-14 20:15:32 +00:00
|
|
|
indexCfg.document = {
|
|
|
|
key: 'id',
|
|
|
|
index: ['title', 'content'],
|
2021-01-05 15:40:02 +00:00
|
|
|
store: ['title', 'href', 'parent'],
|
|
|
|
};
|
|
|
|
|
2021-06-14 20:15:32 +00:00
|
|
|
const index = new FlexSearch.Document(indexCfg);
|
2021-01-05 15:40:02 +00:00
|
|
|
window.geekdocSearchIndex = index;
|
|
|
|
|
|
|
|
getJson(dataUrl, function(data) {
|
|
|
|
data.forEach(obj => {
|
|
|
|
window.geekdocSearchIndex.add(obj);
|
|
|
|
});
|
|
|
|
});
|
2020-01-12 14:33:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function search() {
|
2021-06-14 20:15:32 +00:00
|
|
|
const searchCfg = {
|
|
|
|
enrich: true,
|
|
|
|
limit: 10
|
|
|
|
};
|
|
|
|
|
2020-01-12 14:33:02 +00:00
|
|
|
while (results.firstChild) {
|
|
|
|
results.removeChild(results.firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!input.value) {
|
2021-06-14 20:15:32 +00:00
|
|
|
return results.classList.remove('has-hits');
|
2020-01-12 14:33:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:15:32 +00:00
|
|
|
let searchHits = flattenHits(window.geekdocSearchIndex.search(input.value, searchCfg));
|
2020-12-20 19:09:49 +00:00
|
|
|
if (searchHits.length < 1) {
|
2021-06-14 20:15:32 +00:00
|
|
|
return results.classList.remove('has-hits');
|
2020-12-20 19:09:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:15:32 +00:00
|
|
|
results.classList.add('has-hits');
|
2020-12-21 12:14:51 +00:00
|
|
|
|
2021-01-05 15:40:02 +00:00
|
|
|
if (showParent === true) {
|
2020-12-20 19:09:49 +00:00
|
|
|
searchHits = groupBy(searchHits, hit => hit.parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
const items = [];
|
|
|
|
|
2021-01-05 15:40:02 +00:00
|
|
|
if (showParent === true) {
|
2020-12-23 15:43:41 +00:00
|
|
|
for (const section in searchHits) {
|
|
|
|
const item = document.createElement('li'),
|
|
|
|
title = item.appendChild(document.createElement('span')),
|
|
|
|
subList = item.appendChild(document.createElement('ul'));
|
2020-12-20 19:09:49 +00:00
|
|
|
|
|
|
|
title.textContent = section;
|
2020-12-23 15:43:41 +00:00
|
|
|
createLinks(searchHits[section], subList);
|
|
|
|
|
|
|
|
items.push(item);
|
2020-12-20 19:09:49 +00:00
|
|
|
}
|
2020-12-23 15:43:41 +00:00
|
|
|
} else {
|
|
|
|
const item = document.createElement('li'),
|
|
|
|
title = item.appendChild(document.createElement('span')),
|
|
|
|
subList = item.appendChild(document.createElement('ul'));
|
2020-12-21 12:14:51 +00:00
|
|
|
|
2021-06-14 20:15:32 +00:00
|
|
|
title.textContent = 'Results';
|
2020-12-23 15:43:41 +00:00
|
|
|
createLinks(searchHits, subList);
|
2020-12-21 12:14:51 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-14 20:15:32 +00:00
|
|
|
* Creates links to given fields and either returns them in an array or attaches them to a target element
|
|
|
|
* @param {Object} fields Page to which the link should point to
|
2020-12-20 19:09:49 +00:00
|
|
|
* @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"));
|
|
|
|
|
2021-06-14 20:15:32 +00:00
|
|
|
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;
|
2021-06-14 20:15:32 +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
|
|
|
}
|
|
|
|
|
2021-01-05 15:40:02 +00:00
|
|
|
function fetchErrors(response) {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw Error(response.statusText);
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getJson(src, callback) {
|
|
|
|
fetch(src)
|
|
|
|
.then(fetchErrors)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(json => callback(json))
|
|
|
|
.catch(function(error) {
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:15:32 +00:00
|
|
|
function flattenHits(results) {
|
|
|
|
const items = [];
|
|
|
|
const map = new Map();
|
|
|
|
|
|
|
|
for (const field of results) {
|
|
|
|
for (const page of field.result) {
|
|
|
|
if(!map.has(page.doc.href)){
|
|
|
|
map.set(page.doc.href, true);
|
|
|
|
items.push(page.doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
2020-01-12 14:33:02 +00:00
|
|
|
function loadScript(src, callback) {
|
2020-12-23 15:43:41 +00:00
|
|
|
let script = document.createElement('script');
|
2020-01-12 14:33:02 +00:00
|
|
|
script.defer = true;
|
2020-12-23 15:58:56 +00:00
|
|
|
script.async = false;
|
2020-01-12 14:33:02 +00:00
|
|
|
script.src = src;
|
|
|
|
script.onload = callback;
|
|
|
|
|
2020-12-23 15:43:41 +00:00
|
|
|
document.body.appendChild(script);
|
2020-01-12 14:33:02 +00:00
|
|
|
}
|
|
|
|
})();
|