78 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2023-01-24 19:02:00 +01:00
function GeneralLoadErrorText() {
return "Could not load data from the server in a timely manner. Please try again or contact us";
}
// click on search suggestion
function displayItem (id) {
document.location.href='/displayItem?pre=search&id=' + id;
}
// display (or hide) search suggestions box
function displaySuggestions (visibility) {
console.log('(un)hiding suggestion');
if (visibility == 0 && $('#SuggestionsCnt').is(':visible')) {
$('#SuggestionsCnt').hide();
}
else if (visibility == 1 && $('#SuggestionsCnt').is(':hidden')) {
$('#SuggestionsCnt').show();
}
}
// bold their text inside returned data (label / name)
function highlight (text, what)
{
return text.replace( new RegExp( '(' + what + ')', 'gi' ), "<strong>$1</strong>" );
}
$(document).ready(function () {
// search suggestions
$('#q').keyup(function () {
$.ajax ({
method: "POST",
url: API_URL,
data: {
w: 'search_suggestions',
txt: $('#q').val()
}
})
.done (function (msg) {
console.log(msg);
if (msg == '') {
displaySuggestions(0);
}
else {
// if everything is OK
if (msg['status'] == "200") {
// we received assoc json with id, name, label, block.
var suggestions = "";
for (let i = 0; i < msg['data'].length; i++) {
suggestions += '<div><a onclick="displayItem(\''
+ msg['data'][i]['id'] + '\');">'
+ highlight(msg['data'][i]['name'], $('#q').val()) + ': '
+ highlight(msg['data'][i]['label'], $('#q').val())
+ '</a></div>';
}
$('#SuggestionsCnt').html('<div id="Suggestions">' + suggestions + '</div>');
if (suggestions.length > 0) {
displaySuggestions(1);
}
else {
displaySuggestions(0);
}
}
else {
console.log(msg);
displaySuggestions(0);
}
}
});
});
});