@@ -30,52 +30,102 @@ function removeExtension(url) {
30
30
return url ; // If no extension found, return the original URL
31
31
}
32
32
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
+
33
60
$ ( 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
+
35
81
var resultdiv = $ ( '#results' ) ;
36
- var query = $ ( this ) . val ( ) . toLowerCase ( ) ;
82
+ var searchInput = document . getElementById ( 'search' ) ;
83
+ var query = searchInput . value . toLowerCase ( ) ;
37
84
var result =
38
85
idx . query ( function ( q ) {
39
86
query . split ( lunr . tokenizer . separator ) . forEach ( function ( term ) {
40
87
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 } )
43
90
}
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 } )
46
93
}
47
94
} )
48
95
} ) ;
49
96
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>' ) ;
51
98
for ( var item in result ) {
52
99
var ref = result [ item ] . ref ;
53
- if ( store [ ref ] . teaser ) {
100
+ if ( store [ ref ] . teaser ) {
54
101
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>' +
65
112
'</div>' ;
66
113
}
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>' +
76
123
'</div>' ;
77
124
}
78
125
resultdiv . append ( searchitem ) ;
79
126
}
80
- } ) ;
127
+ }
128
+ $ ( 'input#search' ) . on ( 'keyup' , onKeyUp ) ;
129
+
130
+ restoreInputValue ( ) ;
81
131
} ) ;
0 commit comments