hugo-theme-zzo/layouts/partials/sidebar/site-search.html

169 lines
5.4 KiB
HTML
Raw Normal View History

2019-11-04 13:09:44 +00:00
{{ if $.Param "enableSearch" }}
<div class="search">
<span class="icon">
{{ partial "svgs/etc/search.svg" (dict "width" 22 "height" 22) }}
</span>
<input id="search" class="input" type="text" placeholder="{{T "search-placeholder"}}" autocomplete="off">
<div id="search-results" class="dropdown">
<div id="search-menu" class="dropdown-menu" role="menu">
</div>
</div>
</div>
{{ partial "sidebar/site-mark" . }}
2019-11-05 08:51:53 +00:00
{{ $lunr := resources.Get "js/lunr.min.js" | resources.Fingerprint }}
2019-11-05 09:22:06 +00:00
<script defer src="{{ $lunr.RelPermalink }}"></script>
2019-11-04 13:09:44 +00:00
{{ $stemmer := .Site.Data.lib.js.stemmer }}
{{ printf "<script defer src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\" title=\"%s\"></script>" $stemmer.url $stemmer.sri $stemmer.code | safeHTML }}
{{ $multi := .Site.Data.lib.js.multi_language }}
{{ printf "<script defer src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\" title=\"%s\"></script>" $multi.url $multi.sri $multi.code | safeHTML }}
2019-11-14 08:55:50 +00:00
{{ $searchLanguages := .Site.Params.searchLanguages }}
{{ range .Site.Data.lib.lunr }}
2019-11-14 08:55:50 +00:00
{{ if in $searchLanguages .code }}
{{ printf "<script defer src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\" title=\"%s\"></script>" .url .sri .code | safeHTML }}
{{ end }}
{{ end }}
2019-11-04 13:09:44 +00:00
<script>
{{ if .Site.IsMultiLingual }}
var baseurl = "{{.Site.BaseURL}}{{.Site.LanguagePrefix}}";
{{ else }}
var baseurl = "{{.Site.BaseURL}}";
{{ end }}
2019-11-05 08:11:56 +00:00
2019-11-05 09:22:06 +00:00
$(document).ready(function () {
{{ $searchLanguages := .Site.Params.searchLanguages }}
var searchLanguages = JSON.parse({{ $searchLanguages | jsonify }});
if (!searchLanguages) {
searchLanguages = ['en'];
}
2019-11-05 09:22:06 +00:00
var lunrIndex = null;
var pagesIndex = null;
var searchResults = null;
var searchMenu = null;
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
2019-11-05 08:11:56 +00:00
2019-11-05 09:22:06 +00:00
function initLunr() {
if (!endsWith(baseurl, "/")) {
baseurl = baseurl + '/';
};
$.getJSON(baseurl + "index.json")
.done(function (index) {
pagesIndex = index;
lunrIndex = lunr(function () {
this.use(lunr.multiLanguage(...searchLanguages));
2019-11-05 09:22:06 +00:00
this.ref('uri');
this.field('title');
this.field('description');
this.field('content');
2019-11-05 09:22:06 +00:00
//this.field('tags');
//this.field('series');
//this.field('categories');
var that = this;
index.forEach(function (page) {
that.add(page);
});
2019-11-04 13:09:44 +00:00
});
2019-11-05 09:22:06 +00:00
})
.fail(function (jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.error("Error getting Hugo index file:", err);
2019-11-04 13:09:44 +00:00
});
2019-11-05 09:22:06 +00:00
}
2019-11-05 08:11:56 +00:00
2019-11-05 09:22:06 +00:00
function search(query) {
return lunrIndex.search(query).map(function (result) {
return pagesIndex.filter(function (page) {
return page.uri === result.ref;
})[0];
});
}
2019-11-05 08:11:56 +00:00
2019-11-05 09:22:06 +00:00
function renderSearchResults(results) {
searchResults = document.getElementById('search-results');
searchMenu = document.getElementById('search-menu');
searchResults.setAttribute('class', 'dropdown is-active');
var content = document.createElement('div');
content.setAttribute('class', 'dropdown-content search-content');
if (results.length > 0) {
results.forEach(function (result) {
var item = document.createElement('a');
item.setAttribute('href', result.uri);
item.setAttribute('class', 'dropdown-item');
item.innerHTML = `<div class="menu-item"><div class="menu-item__title">» ${result.title}</div><div class="menu-item__desc">${result.description ? result.description : result.content}</div></div>`;
content.appendChild(item);
});
} else {
var item = document.createElement('span');
2019-11-04 13:09:44 +00:00
item.setAttribute('class', 'dropdown-item');
2019-11-05 09:22:06 +00:00
item.innerText = 'No results found';
2019-11-04 13:09:44 +00:00
content.appendChild(item);
2019-11-05 09:22:06 +00:00
}
2019-11-04 13:09:44 +00:00
2019-11-05 09:22:06 +00:00
while (searchMenu.hasChildNodes()) {
searchMenu.removeChild(
searchMenu.lastChild
);
}
searchMenu.appendChild(content);
2019-11-05 08:11:56 +00:00
}
2019-11-04 13:09:44 +00:00
2019-11-05 09:22:06 +00:00
initLunr();
2019-11-04 13:09:44 +00:00
2019-11-05 09:22:06 +00:00
$("#search").on('input', function (e) {
if (!e.target.value) {
$('#search-results').attr('class', 'dropdown');
return null;
2019-11-04 13:09:44 +00:00
}
2019-11-05 09:22:06 +00:00
var results = search(e.target.value);
renderSearchResults(results);
});
$('#search').on('blur', function () {
setTimeout(function () {
$('#search-results').attr('class', 'dropdown');
}, 100);
});
$('#search').on('click', function (e) {
if (!e.target.value) {
$('#search-results').attr('class', 'dropdown');
return null;
}
var results = search(e.target.value);
renderSearchResults(results);
});
$('#search').on('keydown', function (e) {
var items = $('#search-menu .dropdown-item');
var activeIndex = $('#search-menu .dropdown-item.is-active').index();
items.removeClass('is-active');
if (e.key === 'ArrowDown') {
items.eq(activeIndex + 1).addClass('is-active');
} else if (e.key === 'ArrowUp') {
items.eq(activeIndex - 1).addClass('is-active');
} else if (e.key === 'Enter') {
var currentItemLink = items.eq(activeIndex).attr('href');
if (currentItemLink) {
location.href = currentItemLink;
}
}
});
2019-11-05 09:26:22 +00:00
});
2019-11-04 13:09:44 +00:00
</script>
{{ end }}