2023-04-13 12:27:41 +00:00
|
|
|
/* ***** Orcinus Site Search - Default Search Result Javascript **** */
|
2023-06-21 19:07:57 +00:00
|
|
|
|
|
|
|
// 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 []; }
|
2023-06-21 19:07:57 +00:00
|
|
|
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
|
|
|
|
}, {
|
2023-06-21 19:07:57 +00:00
|
|
|
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) {
|
2023-06-15 13:48:28 +00:00
|
|
|
|
2023-06-21 19:07:57 +00:00
|
|
|
// If we are online
|
|
|
|
if (window.location.protocol != 'file:') {
|
2023-06-16 18:38:24 +00:00
|
|
|
|
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
|
2023-06-21 19:07:57 +00:00
|
|
|
fetch(new Request(pathOnline), {
|
2023-06-16 18:38:24 +00:00
|
|
|
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; });
|
2023-06-21 19:07:57 +00:00
|
|
|
|
|
|
|
// Else we are offline
|
|
|
|
} else window.location.href = pathOffline + datum.url;
|
2023-04-12 23:08:00 +00:00
|
|
|
});
|