232 lines
6.1 KiB
JavaScript
232 lines
6.1 KiB
JavaScript
document.addEventListener("DOMContentLoaded", () => {
|
|
const $navbarBurgers = Array.prototype.slice.call(
|
|
document.querySelectorAll(".navbar-burger"),
|
|
0
|
|
);
|
|
|
|
if ($navbarBurgers.length > 0) {
|
|
$navbarBurgers.forEach((el) => {
|
|
el.addEventListener("click", () => {
|
|
const target = el.dataset.target;
|
|
const $target = document.getElementById(target);
|
|
|
|
el.classList.toggle("is-active");
|
|
$target.classList.toggle("is-active");
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|
|
function debounce(func, wait) {
|
|
var timeout;
|
|
|
|
return function () {
|
|
var context = this;
|
|
var args = arguments;
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(function () {
|
|
timeout = null;
|
|
func.apply(context, args);
|
|
}, wait);
|
|
};
|
|
}
|
|
|
|
function makeTeaser(body, terms) {
|
|
var TERM_WEIGHT = 40;
|
|
var NORMAL_WORD_WEIGHT = 2;
|
|
var FIRST_WORD_WEIGHT = 8;
|
|
var TEASER_MAX_WORDS = 10;
|
|
|
|
var stemmedTerms = terms.map(function (w) {
|
|
return elasticlunr.stemmer(w.toLowerCase());
|
|
});
|
|
var termFound = false;
|
|
var index = 0;
|
|
var weighted = []; // contains elements of ['word', weight, index_in_document]
|
|
|
|
// split in sentences, then words
|
|
var sentences = body.toLowerCase().split(". ");
|
|
|
|
for (var i in sentences) {
|
|
var words = sentences[i].split(" ");
|
|
var value = FIRST_WORD_WEIGHT;
|
|
|
|
for (var j in words) {
|
|
var word = words[j];
|
|
|
|
if (word.length > 0) {
|
|
for (var k in stemmedTerms) {
|
|
if (elasticlunr.stemmer(word).startsWith(stemmedTerms[k])) {
|
|
value = TERM_WEIGHT;
|
|
termFound = true;
|
|
}
|
|
}
|
|
weighted.push([word, value, index]);
|
|
value = NORMAL_WORD_WEIGHT;
|
|
}
|
|
|
|
index += word.length;
|
|
index += 1; // ' ' or '.' if last word in sentence
|
|
}
|
|
|
|
index += 1; // because we split at a two-char boundary '. '
|
|
}
|
|
|
|
if (weighted.length === 0) {
|
|
return body;
|
|
}
|
|
|
|
var windowWeights = [];
|
|
var windowSize = Math.min(weighted.length, TEASER_MAX_WORDS);
|
|
// We add a window with all the weights first
|
|
var curSum = 0;
|
|
for (var i = 0; i < windowSize; i++) {
|
|
curSum += weighted[i][1];
|
|
}
|
|
windowWeights.push(curSum);
|
|
|
|
for (var i = 0; i < weighted.length - windowSize; i++) {
|
|
curSum -= weighted[i][1];
|
|
curSum += weighted[i + windowSize][1];
|
|
windowWeights.push(curSum);
|
|
}
|
|
|
|
// If we didn't find the term, just pick the first window
|
|
var maxSumIndex = 0;
|
|
if (termFound) {
|
|
var maxFound = 0;
|
|
// backwards
|
|
for (var i = windowWeights.length - 1; i >= 0; i--) {
|
|
if (windowWeights[i] > maxFound) {
|
|
maxFound = windowWeights[i];
|
|
maxSumIndex = i;
|
|
}
|
|
}
|
|
}
|
|
|
|
var teaser = [];
|
|
var startIndex = weighted[maxSumIndex][2];
|
|
for (var i = maxSumIndex; i < maxSumIndex + windowSize; i++) {
|
|
var word = weighted[i];
|
|
if (startIndex < word[2]) {
|
|
// missing text from index to start of `word`
|
|
teaser.push(body.substring(startIndex, word[2]));
|
|
startIndex = word[2];
|
|
}
|
|
|
|
// add <em/> around search terms
|
|
if (word[1] === TERM_WEIGHT) {
|
|
teaser.push("<b>");
|
|
}
|
|
startIndex = word[2] + word[0].length;
|
|
teaser.push(body.substring(word[2], startIndex));
|
|
|
|
if (word[1] === TERM_WEIGHT) {
|
|
teaser.push("</b>");
|
|
}
|
|
}
|
|
teaser.push("…");
|
|
return teaser.join("");
|
|
}
|
|
|
|
function formatSearchResultItem(item, terms) {
|
|
return (
|
|
`<article class='box'>` +
|
|
`<h1 class='title is-4'>` +
|
|
`<a class='link' class='link' href='${item.ref}'>${item.doc.title}</a>` +
|
|
`</h1>` +
|
|
`<div class='content mt-2'>` +
|
|
`${makeTeaser(item.doc.body, terms)}` +
|
|
`<a class='read-more' href='${item.ref}'>` +
|
|
`Read More` +
|
|
`</a>` +
|
|
`</div>` +
|
|
`</article>`
|
|
);
|
|
}
|
|
|
|
function initSearch() {
|
|
var $searchInput = document.getElementById("search");
|
|
var $searchResults = document.querySelector(".search-results");
|
|
var $searchResultsItems = document.querySelector(".search-results__items");
|
|
var MAX_ITEMS = 10;
|
|
|
|
var options = {
|
|
bool: "AND",
|
|
fields: {
|
|
title: { boost: 2 },
|
|
body: { boost: 1 },
|
|
},
|
|
};
|
|
var currentTerm = "";
|
|
var index = elasticlunr.Index.load(window.searchIndex);
|
|
|
|
$searchInput.addEventListener(
|
|
"keyup",
|
|
debounce(function () {
|
|
var term = $searchInput.value.trim();
|
|
if (term === currentTerm || !index) {
|
|
return;
|
|
}
|
|
$searchResults.style.display = term === "" ? "none" : "block";
|
|
$searchResultsItems.innerHTML = "";
|
|
if (term === "") {
|
|
return;
|
|
}
|
|
|
|
var results = index.search(term, options);
|
|
if (results.length === 0) {
|
|
$searchResults.style.display = "none";
|
|
return;
|
|
}
|
|
|
|
currentTerm = term;
|
|
for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
|
|
var item = document.createElement("div");
|
|
item.classList.add("navbar-item");
|
|
item.innerHTML = formatSearchResultItem(results[i], term.split(" "));
|
|
$searchResultsItems.appendChild(item);
|
|
var item = document.createElement("hr");
|
|
item.classList.add("navbar-divider");
|
|
$searchResultsItems.appendChild(item);
|
|
}
|
|
}, 150)
|
|
);
|
|
}
|
|
|
|
if (
|
|
document.readyState === "complete" ||
|
|
(document.readyState !== "loading" && !document.documentElement.doScroll)
|
|
) {
|
|
initSearch();
|
|
} else {
|
|
document.addEventListener("DOMContentLoaded", initSearch);
|
|
}
|
|
|
|
document
|
|
.querySelector("#search-dialog")
|
|
.addEventListener("click", function (event) {
|
|
event.preventDefault();
|
|
var modal = document.querySelector(".modal"); // assuming you have only 1
|
|
var html = document.querySelector("html");
|
|
modal.classList.add("is-active");
|
|
html.classList.add("is-clipped");
|
|
document.getElementById("search").focus();
|
|
document.getElementById("search").select();
|
|
|
|
modal
|
|
.querySelector(".modal-background")
|
|
.addEventListener("click", function (e) {
|
|
e.preventDefault();
|
|
modal.classList.remove("is-active");
|
|
html.classList.remove("is-clipped");
|
|
});
|
|
|
|
document.querySelector(".delete").addEventListener("click", function (e) {
|
|
e.preventDefault();
|
|
modal.classList.remove("is-active");
|
|
html.classList.remove("is-clipped");
|
|
});
|
|
});
|