2020-07-11 11:48:27 +02:00
|
|
|
var request = null;
|
2020-08-31 13:34:35 +02:00
|
|
|
var timeoutID = null;
|
2020-07-11 11:48:27 +02:00
|
|
|
|
|
|
|
$('#search').on('input', function () {
|
|
|
|
var query = $(this).val();
|
2020-08-31 13:34:35 +02:00
|
|
|
var timeoutMs = 100;
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
if (timeoutID !== null) {
|
|
|
|
window.clearTimeout(timeoutID);
|
|
|
|
}
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
timeoutID = window.setTimeout(function () {
|
|
|
|
request = $.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: "query",
|
|
|
|
contentType: 'application/json',
|
|
|
|
data: JSON.stringify({ 'query': query }),
|
|
|
|
contentType: 'application/json',
|
|
|
|
success: function (data, textStatus, request) {
|
|
|
|
let httpResults = Papa.parse(data, { delimiter: ",", header: true, skipEmptyLines: true });
|
|
|
|
results.innerHTML = '';
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
let timeSpent = request.getResponseHeader('Time-Ms');
|
|
|
|
let numberOfDocuments = httpResults.data.length;
|
|
|
|
count.innerHTML = `${numberOfDocuments}`;
|
|
|
|
time.innerHTML = `${timeSpent}ms`;
|
|
|
|
time.classList.remove('fade-in-out');
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
for (element of httpResults.data) {
|
|
|
|
const elem = document.createElement('li');
|
|
|
|
elem.classList.add("document");
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
const ol = document.createElement('ol');
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
for (const prop in element) {
|
|
|
|
const field = document.createElement('li');
|
|
|
|
field.classList.add("field");
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
const attribute = document.createElement('div');
|
|
|
|
attribute.classList.add("attribute");
|
|
|
|
attribute.innerHTML = prop;
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
const content = document.createElement('div');
|
|
|
|
content.classList.add("content");
|
|
|
|
content.innerHTML = element[prop];
|
|
|
|
|
|
|
|
field.appendChild(attribute);
|
|
|
|
field.appendChild(content);
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
ol.appendChild(field);
|
|
|
|
}
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
elem.appendChild(ol);
|
|
|
|
results.appendChild(elem)
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
beforeSend: function () {
|
|
|
|
if (request !== null) {
|
|
|
|
request.abort();
|
|
|
|
time.classList.add('fade-in-out');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}, timeoutMs);
|
2020-07-11 11:48:27 +02:00
|
|
|
});
|
2020-07-11 14:17:37 +02:00
|
|
|
|
|
|
|
// Make the number of document a little bit prettier
|
|
|
|
$('#docs-count').text(function(index, text) {
|
|
|
|
return parseInt(text).toLocaleString()
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make the database a little bit easier to read
|
|
|
|
$('#db-size').text(function(index, text) {
|
2020-08-28 15:38:05 +02:00
|
|
|
return filesize(parseInt(text))
|
2020-07-11 14:17:37 +02:00
|
|
|
});
|
2020-10-06 14:52:05 +02:00
|
|
|
|
|
|
|
// We trigger the input when we load the script, this way
|
|
|
|
// we execute a placeholder search when the input is empty.
|
|
|
|
$(window).on('load', function () {
|
|
|
|
$('#search').trigger('input');
|
|
|
|
});
|