orcinus-search/orcinus/js/search.js

50 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-04-13 12:27:41 +00:00
/* ***** Orcinus Site Search - Default Search Result Javascript **** */
// When offline, this script relies on being in the [root]/orcinus/js/
// directory in order for offline URLs to resolve correctly. If you
// have placed your Orcinus Site Search installation in a different
// location, you will need to edit the pathOffline value below.
let scrElem = document.currentScript;
let pathOnline = scrElem.src.replace(/\/js\/[^\/]+$/, '/search.php');
let pathOffline = scrElem.src.replace(/\/orcinus\/js\/[^\/]+$/, '');
2023-04-12 23:08:00 +00:00
let remoteValue;
if (typeof os_return_all !== 'function') {
function os_return_all() { return []; }
remoteValue = {
url: pathOnline + '?q=%QUERY&json',
wildcard: '%QUERY'
};
2023-04-12 23:08:00 +00:00
}
2023-04-13 01:08:53 +00:00
$('input.os_typeahead').attr('autocomplete', 'off').typeahead({
2023-04-12 23:08:00 +00:00
hint: true,
highlight: true,
minLength: 3
}, {
source: new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('title'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 5,
remote: remoteValue,
local: os_return_all
}),
2023-04-12 23:08:00 +00:00
display: 'title'
}).bind('typeahead:selected', function (obj, datum) {
// If we are online
if (window.location.protocol != 'file:') {
2023-06-16 18:47:51 +00:00
// On user click of a search suggestion, add this search query to
// the query log and cache
fetch(new Request(pathOnline), {
method: 'POST',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify({ q: datum.query, log: 'log' })
})
.then((response) => response.text())
.then((data) => { window.location.href = datum.url; });
// Else we are offline
} else window.location.href = pathOffline + datum.url;
2023-04-12 23:08:00 +00:00
});