Skip to content

Commit 43efe1a

Browse files
committed
Search string is now remebered between page reloads as well
Signed-off-by: Hofi <[email protected]>
1 parent dfc3dbb commit 43efe1a

File tree

1 file changed

+78
-28
lines changed

1 file changed

+78
-28
lines changed

_js/lunr/lunr-en.js

Lines changed: 78 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,52 +30,102 @@ function removeExtension(url) {
3030
return url; // If no extension found, return the original URL
3131
}
3232

33+
// Function to set a cookie
34+
function setCookie(name, value, days) {
35+
var expires = "";
36+
if (days) {
37+
var date = new Date();
38+
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
39+
expires = "; expires=" + date.toUTCString();
40+
}
41+
document.cookie = name + "=" + (value || "") + expires + "; path=/; SameSite=Strict";
42+
}
43+
44+
// Function to get a cookie value
45+
function getCookie(name) {
46+
var nameEQ = name + "=";
47+
var ca = document.cookie.split(';');
48+
for (var i = 0; i < ca.length; i++) {
49+
var c = ca[i];
50+
while (c.charAt(0) == ' ') {
51+
c = c.substring(1, c.length);
52+
}
53+
if (c.indexOf(nameEQ) == 0) {
54+
return c.substring(nameEQ.length, c.length);
55+
}
56+
}
57+
return null;
58+
}
59+
3360
$(document).ready(function() {
34-
$('input#search').on('keyup', function () {
61+
62+
// Function to restore input field value from cookie
63+
function restoreInputValue() {
64+
var searchInput = document.getElementById('search');
65+
var searchValue = getCookie('search');
66+
if (searchValue) {
67+
searchInput.value = searchValue;
68+
onKeyUp();
69+
}
70+
}
71+
72+
// Function to save input field value to cookie
73+
function saveInputValue() {
74+
var searchInput = document.getElementById('search');
75+
setCookie('search', searchInput.value, 365);
76+
}
77+
78+
function onKeyUp() {
79+
saveInputValue();
80+
3581
var resultdiv = $('#results');
36-
var query = $(this).val().toLowerCase();
82+
var searchInput = document.getElementById('search');
83+
var query = searchInput.value.toLowerCase();
3784
var result =
3885
idx.query(function (q) {
3986
query.split(lunr.tokenizer.separator).forEach(function (term) {
4087
q.term(term, { boost: 100 })
41-
if(query.lastIndexOf(" ") != query.length-1){
42-
q.term(term, { usePipeline: false, wildcard: lunr.Query.wildcard.TRAILING, boost: 10 })
88+
if (query.lastIndexOf(" ") != query.length - 1) {
89+
q.term(term, { usePipeline: false, wildcard: lunr.Query.wildcard.TRAILING, boost: 10 })
4390
}
44-
if (term != ""){
45-
q.term(term, { usePipeline: false, editDistance: 1, boost: 1 })
91+
if (term != "") {
92+
q.term(term, { usePipeline: false, editDistance: 1, boost: 1 })
4693
}
4794
})
4895
});
4996
resultdiv.empty();
50-
resultdiv.prepend('<p class="results__found">'+result.length+' {{ site.data.ui-text[site.locale].results_found | default: "Result(s) found" }}</p>');
97+
resultdiv.prepend('<p class="results__found">' + result.length + ' {{ site.data.ui-text[site.locale].results_found | default: "Result(s) found" }}</p>');
5198
for (var item in result) {
5299
var ref = result[item].ref;
53-
if(store[ref].teaser){
100+
if (store[ref].teaser) {
54101
var searchitem =
55-
'<div class="list__item">'+
56-
'<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">'+
57-
'<h2 class="archive__item-title" itemprop="headline">'+
58-
'<a href="'+removeExtension(tore[ref].url)+'" rel="permalink">'+store[ref].title+'</a>'+
59-
'</h2>'+
60-
'<div class="archive__item-teaser">'+
61-
'<img src="'+store[ref].teaser+'" alt="">'+
62-
'</div>'+
63-
'<p class="archive__item-excerpt" itemprop="description">'+store[ref].excerpt.split(" ").splice(0,20).join(" ")+'...</p>'+
64-
'</article>'+
102+
'<div class="list__item">' +
103+
'<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">' +
104+
'<h2 class="archive__item-title" itemprop="headline">' +
105+
'<a href="' + removeExtension(tore[ref].url) + '" rel="permalink">' + store[ref].title + '</a>' +
106+
'</h2>' +
107+
'<div class="archive__item-teaser">' +
108+
'<img src="' + store[ref].teaser + '" alt="">' +
109+
'</div>' +
110+
'<p class="archive__item-excerpt" itemprop="description">' + store[ref].excerpt.split(" ").splice(0, 20).join(" ") + '...</p>' +
111+
'</article>' +
65112
'</div>';
66113
}
67-
else{
68-
var searchitem =
69-
'<div class="list__item">'+
70-
'<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">'+
71-
'<h2 class="archive__item-title" itemprop="headline">'+
72-
'<a href="'+removeExtension(store[ref].url)+'" rel="permalink">'+store[ref].title+'</a>'+
73-
'</h2>'+
74-
'<p class="archive__item-excerpt" itemprop="description">'+store[ref].excerpt.split(" ").splice(0,20).join(" ")+'...</p>'+
75-
'</article>'+
114+
else {
115+
var searchitem =
116+
'<div class="list__item">' +
117+
'<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">' +
118+
'<h2 class="archive__item-title" itemprop="headline">' +
119+
'<a href="' + removeExtension(store[ref].url) + '" rel="permalink">' + store[ref].title + '</a>' +
120+
'</h2>' +
121+
'<p class="archive__item-excerpt" itemprop="description">' + store[ref].excerpt.split(" ").splice(0, 20).join(" ") + '...</p>' +
122+
'</article>' +
76123
'</div>';
77124
}
78125
resultdiv.append(searchitem);
79126
}
80-
});
127+
}
128+
$('input#search').on('keyup', onKeyUp);
129+
130+
restoreInputValue();
81131
});

0 commit comments

Comments
 (0)