2022-11-17 21:13:11 +00:00
|
|
|
window.relearn = window.relearn || {};
|
|
|
|
|
|
|
|
window.relearn.runInitialSearch = function(){
|
|
|
|
if( window.relearn.isSearchInit && window.relearn.isLunrInit ){
|
2023-09-23 08:19:35 +00:00
|
|
|
var input = document.querySelector('#R-search-by-detail');
|
2023-02-12 08:36:45 +00:00
|
|
|
if( !input ){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var value = input.value;
|
|
|
|
searchDetail( value );
|
2022-11-17 21:13:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 23:18:04 +00:00
|
|
|
var lunrIndex, pagesIndex;
|
|
|
|
|
2022-11-01 23:17:10 +00:00
|
|
|
function initLunrIndex( index ){
|
|
|
|
pagesIndex = index;
|
2023-01-30 17:00:33 +00:00
|
|
|
// Set up Lunr by declaring the fields we use
|
2022-11-01 23:17:10 +00:00
|
|
|
// Also provide their boost level for the ranking
|
|
|
|
lunrIndex = lunr(function() {
|
|
|
|
this.use(lunr.multiLanguage.apply(null, contentLangs));
|
|
|
|
this.ref('index');
|
|
|
|
this.field('title', {
|
|
|
|
boost: 15
|
|
|
|
});
|
|
|
|
this.field('tags', {
|
|
|
|
boost: 10
|
|
|
|
});
|
|
|
|
this.field('content', {
|
|
|
|
boost: 5
|
|
|
|
});
|
2021-10-16 23:18:04 +00:00
|
|
|
|
2022-11-01 23:17:10 +00:00
|
|
|
this.pipeline.remove(lunr.stemmer);
|
|
|
|
this.searchPipeline.remove(lunr.stemmer);
|
2021-10-16 23:18:04 +00:00
|
|
|
|
2023-01-30 17:00:33 +00:00
|
|
|
// Feed Lunr with each file and let LUnr actually index them
|
2022-11-01 23:17:10 +00:00
|
|
|
pagesIndex.forEach(function(page, idx) {
|
|
|
|
page.index = idx;
|
|
|
|
this.add(page);
|
|
|
|
}, this);
|
2022-11-17 21:13:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
window.relearn.isLunrInit = true;
|
|
|
|
window.relearn.runInitialSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
function triggerSearch(){
|
2023-09-23 08:19:35 +00:00
|
|
|
var input = document.querySelector('#R-search-by-detail');
|
2022-11-18 08:03:15 +00:00
|
|
|
if( !input ){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var value = input.value;
|
2023-02-12 08:36:45 +00:00
|
|
|
searchDetail( value );
|
|
|
|
|
|
|
|
// add a new entry to the history after the user
|
|
|
|
// changed the term; this does not reload the page
|
|
|
|
// but will add to the history and update the address bar URL
|
2022-11-18 08:03:15 +00:00
|
|
|
var url = new URL( window.location );
|
2023-02-12 08:36:45 +00:00
|
|
|
var oldValue = url.searchParams.get( 'search-by' );
|
2022-11-18 08:03:15 +00:00
|
|
|
if( value != oldValue ){
|
2023-02-12 08:36:45 +00:00
|
|
|
var state = window.history.state || {};
|
|
|
|
state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} );
|
|
|
|
url.searchParams.set( 'search-by', value );
|
|
|
|
state.search = url.toString();
|
|
|
|
// with normal pages, this is handled by the 'pagehide' event, but this
|
|
|
|
// doesn't fire in case of pushState, so we have to do the same thing
|
|
|
|
// here, too
|
2023-02-12 13:11:53 +00:00
|
|
|
state.contentScrollTop = +elc.scrollTop;
|
2023-02-12 08:36:45 +00:00
|
|
|
window.history.pushState( state, '', url );
|
2022-11-17 21:13:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 08:36:45 +00:00
|
|
|
window.addEventListener( 'popstate', function ( event ){
|
2024-04-12 15:30:57 +00:00
|
|
|
// restart search if browsed through history
|
2023-02-12 08:36:45 +00:00
|
|
|
if( event.state ){
|
|
|
|
var state = window.history.state || {};
|
|
|
|
state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} );
|
|
|
|
if( state.search ) {
|
|
|
|
var url = new URL( state.search );
|
|
|
|
if( url.searchParams.has('search-by') ){
|
|
|
|
var search = url.searchParams.get( 'search-by' );
|
|
|
|
|
|
|
|
// we have to insert the old search term into the inputs
|
|
|
|
var inputs = document.querySelectorAll( 'input.search-by' );
|
|
|
|
inputs.forEach( function( e ){
|
|
|
|
e.value = search;
|
|
|
|
var event = document.createEvent( 'Event' );
|
|
|
|
event.initEvent( 'input', false, false );
|
|
|
|
e.dispatchEvent( event );
|
|
|
|
});
|
|
|
|
|
|
|
|
// recreate the last search results and eventually
|
|
|
|
// restore the previous scrolling position
|
|
|
|
searchDetail( search );
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 21:13:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-09-23 08:19:35 +00:00
|
|
|
var input = document.querySelector('#R-search-by-detail');
|
2022-11-17 21:13:11 +00:00
|
|
|
if( input ){
|
|
|
|
input.addEventListener( 'keydown', function(event) {
|
|
|
|
// if we are pressing ESC in the searchdetail our focus will
|
|
|
|
// be stolen by the other event handlers, so we have to refocus
|
|
|
|
// here after a short while
|
|
|
|
if (event.key == "Escape") {
|
|
|
|
setTimeout( function(){ input.focus(); }, 0 );
|
|
|
|
}
|
|
|
|
});
|
2022-11-01 23:17:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initLunrJs() {
|
|
|
|
// new way to load our search index
|
|
|
|
if( window.index_js_url ){
|
|
|
|
var js = document.createElement("script");
|
|
|
|
js.src = index_js_url;
|
|
|
|
js.setAttribute("async", "");
|
|
|
|
js.onload = function(){
|
2024-10-13 14:26:50 +00:00
|
|
|
initLunrIndex(relearn_searchindex);
|
2022-11-01 23:17:10 +00:00
|
|
|
};
|
|
|
|
js.onerror = function(e){
|
|
|
|
console.error('Error getting Hugo index file');
|
|
|
|
};
|
|
|
|
document.head.appendChild(js);
|
|
|
|
}
|
2021-10-16 23:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-30 17:00:33 +00:00
|
|
|
* Trigger a search in Lunr and transform the result
|
2021-10-16 23:18:04 +00:00
|
|
|
*
|
|
|
|
* @param {String} term
|
|
|
|
* @return {Array} results
|
|
|
|
*/
|
|
|
|
function search(term) {
|
2023-01-30 17:00:33 +00:00
|
|
|
// Find the item in our index corresponding to the Lunr one to have more info
|
2021-10-16 23:18:04 +00:00
|
|
|
// Remove Lunr special search characters: https://lunrjs.com/guides/searching.html
|
2023-08-31 10:35:36 +00:00
|
|
|
term = term.replace( /[*:^~+-]/g, ' ' );
|
|
|
|
var searchTerm = lunr.tokenizer( term ).reduce( function(a,token){return a.concat(searchPatterns(token.str))}, []).join(' ');
|
2022-11-20 22:01:48 +00:00
|
|
|
return !searchTerm || !lunrIndex ? [] : lunrIndex.search(searchTerm).map(function(result) {
|
2021-10-16 23:18:04 +00:00
|
|
|
return { index: result.ref, matches: Object.keys(result.matchData.metadata) }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function searchPatterns(word) {
|
2022-11-29 18:26:43 +00:00
|
|
|
// for short words high amounts of typos doesn't make sense
|
|
|
|
// for long words we allow less typos because this largly increases search time
|
|
|
|
var typos = [
|
|
|
|
{ len: -1, typos: 1 },
|
|
|
|
{ len: 60, typos: 2 },
|
|
|
|
{ len: 40, typos: 3 },
|
|
|
|
{ len: 20, typos: 4 },
|
|
|
|
{ len: 16, typos: 3 },
|
|
|
|
{ len: 12, typos: 2 },
|
|
|
|
{ len: 8, typos: 1 },
|
|
|
|
{ len: 4, typos: 0 },
|
|
|
|
];
|
2021-10-16 23:18:04 +00:00
|
|
|
return [
|
|
|
|
word + '^100',
|
|
|
|
word + '*^10',
|
|
|
|
'*' + word + '^10',
|
2022-11-29 18:26:43 +00:00
|
|
|
word + '~' + typos.reduce( function( a, c, i ){ return word.length < c.len ? c : a; } ).typos + '^1'
|
2021-10-16 23:18:04 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-11-17 21:13:11 +00:00
|
|
|
|
|
|
|
function resolvePlaceholders( s, args ) {
|
|
|
|
var args = args || [];
|
|
|
|
// use replace to iterate over the string
|
|
|
|
// select the match and check if the related argument is present
|
|
|
|
// if yes, replace the match with the argument
|
|
|
|
return s.replace(/{([0-9]+)}/g, function (match, index) {
|
|
|
|
// check if the argument is present
|
|
|
|
return typeof args[index] == 'undefined' ? match : args[index];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-12 08:36:45 +00:00
|
|
|
function searchDetail( value ) {
|
2023-09-23 08:19:35 +00:00
|
|
|
var results = document.querySelector('#R-searchresults');
|
2022-11-17 21:13:11 +00:00
|
|
|
var hint = document.querySelector('.searchhint');
|
|
|
|
hint.innerText = '';
|
|
|
|
results.textContent = '';
|
|
|
|
var a = search( value );
|
|
|
|
if( a.length ){
|
|
|
|
hint.innerText = resolvePlaceholders( window.T_N_results_found, [ value, a.length ] );
|
|
|
|
a.forEach( function(item){
|
|
|
|
var page = pagesIndex[item.index];
|
|
|
|
var numContextWords = 10;
|
|
|
|
var contextPattern = '(?:\\S+ +){0,' + numContextWords + '}\\S*\\b(?:' +
|
|
|
|
item.matches.map( function(match){return match.replace(/\W/g, '\\$&')} ).join('|') +
|
|
|
|
')\\b\\S*(?: +\\S+){0,' + numContextWords + '}';
|
|
|
|
var context = page.content.match(new RegExp(contextPattern, 'i'));
|
|
|
|
var divsuggestion = document.createElement('a');
|
|
|
|
divsuggestion.className = 'autocomplete-suggestion';
|
|
|
|
divsuggestion.setAttribute('data-term', value);
|
|
|
|
divsuggestion.setAttribute('data-title', page.title);
|
2024-03-18 20:59:19 +00:00
|
|
|
divsuggestion.setAttribute('href', window.relearn.relBaseUri + page.uri);
|
2022-11-17 21:13:11 +00:00
|
|
|
divsuggestion.setAttribute('data-context', context);
|
2023-09-23 22:28:49 +00:00
|
|
|
var divtitle = document.createElement('div');
|
|
|
|
divtitle.className = 'title';
|
|
|
|
divtitle.innerText = '» ' + page.title;
|
|
|
|
divsuggestion.appendChild( divtitle );
|
|
|
|
var divbreadcrumb = document.createElement('div');
|
|
|
|
divbreadcrumb.className = 'breadcrumbs';
|
|
|
|
divbreadcrumb.innerText = (page.breadcrumb || '');
|
|
|
|
divsuggestion.appendChild( divbreadcrumb );
|
|
|
|
if( context ){
|
|
|
|
var divcontext = document.createElement('div');
|
|
|
|
divcontext.className = 'context';
|
|
|
|
divcontext.innerText = (context || '');
|
|
|
|
divsuggestion.appendChild( divcontext );
|
|
|
|
}
|
2022-11-17 21:13:11 +00:00
|
|
|
results.appendChild( divsuggestion );
|
|
|
|
});
|
|
|
|
window.relearn.markSearch();
|
|
|
|
}
|
|
|
|
else if( value.length ) {
|
|
|
|
hint.innerText = resolvePlaceholders( window.T_No_results_found, [ value ] );
|
|
|
|
}
|
|
|
|
input.focus();
|
|
|
|
setTimeout( adjustContentWidth, 0 );
|
2023-02-12 08:36:45 +00:00
|
|
|
|
|
|
|
// if we are initiating search because of a browser history
|
|
|
|
// operation, we have to restore the scrolling postion the
|
|
|
|
// user previously has used; if this search isn't initiated
|
|
|
|
// by a browser history operation, it simply does nothing
|
|
|
|
var state = window.history.state || {};
|
|
|
|
state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} );
|
2023-02-12 13:11:53 +00:00
|
|
|
if( state.hasOwnProperty( 'contentScrollTop' ) ){
|
2023-02-12 08:36:45 +00:00
|
|
|
window.setTimeout( function(){
|
2023-02-12 13:11:53 +00:00
|
|
|
elc.scrollTop = +state.contentScrollTop;
|
2023-02-12 08:36:45 +00:00
|
|
|
}, 10 );
|
|
|
|
return;
|
|
|
|
}
|
2022-11-17 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 23:17:10 +00:00
|
|
|
initLunrJs();
|
2023-01-29 23:31:15 +00:00
|
|
|
|
|
|
|
function startSearch(){
|
2023-09-23 08:19:35 +00:00
|
|
|
var input = document.querySelector('#R-search-by-detail');
|
2023-02-12 08:36:45 +00:00
|
|
|
if( input ){
|
|
|
|
var state = window.history.state || {};
|
|
|
|
state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} );
|
|
|
|
state.search = window.location.toString();
|
|
|
|
window.history.replaceState( state, '', window.location );
|
|
|
|
}
|
2022-11-17 21:13:11 +00:00
|
|
|
|
2021-10-16 23:18:04 +00:00
|
|
|
var searchList = new autoComplete({
|
|
|
|
/* selector for the search box element */
|
2024-04-04 15:50:01 +00:00
|
|
|
selectorToInsert: 'search:has(.searchbox)',
|
2023-09-23 08:19:35 +00:00
|
|
|
selector: '#R-search-by',
|
2021-10-16 23:18:04 +00:00
|
|
|
/* source is the callback to perform the search */
|
|
|
|
source: function(term, response) {
|
|
|
|
response(search(term));
|
|
|
|
},
|
|
|
|
/* renderItem displays individual search results */
|
|
|
|
renderItem: function(item, term) {
|
|
|
|
var page = pagesIndex[item.index];
|
|
|
|
var numContextWords = 2;
|
|
|
|
var contextPattern = '(?:\\S+ +){0,' + numContextWords + '}\\S*\\b(?:' +
|
|
|
|
item.matches.map( function(match){return match.replace(/\W/g, '\\$&')} ).join('|') +
|
|
|
|
')\\b\\S*(?: +\\S+){0,' + numContextWords + '}';
|
|
|
|
var context = page.content.match(new RegExp(contextPattern, 'i'));
|
|
|
|
var divsuggestion = document.createElement('div');
|
|
|
|
divsuggestion.className = 'autocomplete-suggestion';
|
|
|
|
divsuggestion.setAttribute('data-term', term);
|
|
|
|
divsuggestion.setAttribute('data-title', page.title);
|
2024-03-18 20:59:19 +00:00
|
|
|
divsuggestion.setAttribute('data-uri', window.relearn.relBaseUri + page.uri);
|
2021-10-16 23:18:04 +00:00
|
|
|
divsuggestion.setAttribute('data-context', context);
|
2023-09-23 22:28:49 +00:00
|
|
|
var divtitle = document.createElement('div');
|
|
|
|
divtitle.className = 'title';
|
|
|
|
divtitle.innerText = '» ' + page.title;
|
|
|
|
divsuggestion.appendChild( divtitle );
|
|
|
|
if( context ){
|
|
|
|
var divcontext = document.createElement('div');
|
|
|
|
divcontext.className = 'context';
|
|
|
|
divcontext.innerText = (context || '');
|
|
|
|
divsuggestion.appendChild( divcontext );
|
|
|
|
}
|
2021-10-16 23:18:04 +00:00
|
|
|
return divsuggestion.outerHTML;
|
|
|
|
},
|
|
|
|
/* onSelect callback fires when a search suggestion is chosen */
|
|
|
|
onSelect: function(e, term, item) {
|
|
|
|
location.href = item.getAttribute('data-uri');
|
2022-11-17 16:48:48 +00:00
|
|
|
e.preventDefault();
|
2021-10-16 23:18:04 +00:00
|
|
|
}
|
|
|
|
});
|
2023-01-29 23:31:15 +00:00
|
|
|
};
|
2021-10-16 23:18:04 +00:00
|
|
|
|
2023-01-29 23:31:15 +00:00
|
|
|
ready( startSearch );
|