2020-05-16 16:20:04 +00:00
|
|
|
/* global initData, authorizationToken */
|
2020-05-15 23:24:56 +00:00
|
|
|
|
2020-05-16 16:20:04 +00:00
|
|
|
// List of HTML entities for escaping.
|
|
|
|
var htmlEscapes = {
|
|
|
|
'&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
'"': '"',
|
|
|
|
"'": ''',
|
|
|
|
'/': '/'
|
|
|
|
};
|
2020-05-15 23:24:56 +00:00
|
|
|
|
2020-05-16 16:20:04 +00:00
|
|
|
// Regex containing the keys listed immediately above.
|
|
|
|
var htmlEscaper = /[&<>"'\/]/g;
|
2020-05-15 23:24:56 +00:00
|
|
|
|
2020-05-16 16:20:04 +00:00
|
|
|
// Escape a string for HTML interpolation.
|
|
|
|
escape = function(string) {
|
|
|
|
return ('' + string).replace(htmlEscaper, function(match) {
|
|
|
|
return htmlEscapes[match];
|
|
|
|
});
|
|
|
|
};
|
2020-05-21 10:30:34 +00:00
|
|
|
var urlRegex = /(https?://[^\s]+)/g;
|
|
|
|
url = function(string){
|
|
|
|
return ''+string.replace(urlRegex, '<a target="_blank" href="$1">$1</a>')
|
|
|
|
}
|
|
|
|
var titleRegex = /^(.*\n)/;
|
|
|
|
title = function(string){
|
|
|
|
var str=''+string.replace(titleRegex, '<b>$1</b>');
|
|
|
|
return ''+string.replace(titleRegex, '<b>$1</b>');
|
|
|
|
}
|
2020-05-15 23:24:56 +00:00
|
|
|
|
|
|
|
function updatePage(data){
|
|
|
|
|
|
|
|
var content = "";
|
2020-05-22 15:54:33 +00:00
|
|
|
var item = {};
|
|
|
|
var id = '';
|
|
|
|
// If the log entry is unique, simulate a search result
|
|
|
|
if( ! data['hits'] ){
|
|
|
|
data = {hits:{hits:[data]}};
|
|
|
|
}
|
2020-05-15 23:24:56 +00:00
|
|
|
$.each(data.hits.hits, (k,v)=>{
|
2020-05-16 16:20:04 +00:00
|
|
|
|
2020-05-22 15:54:33 +00:00
|
|
|
item = v._source;
|
|
|
|
id = v._id;
|
2020-05-15 23:24:56 +00:00
|
|
|
content += `
|
|
|
|
|
|
|
|
<div class="log row-fluid">
|
|
|
|
<div class="span9">
|
2020-05-22 15:54:33 +00:00
|
|
|
<p><a href="/log/${id}">${escape(item.created_at)} -- ${escape(item.author)} -- ${escape(item.server)}</a></p>
|
2020-05-21 10:30:34 +00:00
|
|
|
<pre> ${title(url(escape(item.content)))}</pre>
|
2020-05-15 23:24:56 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
});
|
|
|
|
$("#content").html(content);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:20:04 +00:00
|
|
|
$("input").on("keyup",function(e){
|
2020-05-15 23:24:56 +00:00
|
|
|
const el = $(e.target);
|
|
|
|
const val = el.val();
|
|
|
|
if( val.length < 3 ){ return; }
|
|
|
|
$.ajax("/search",{
|
2020-05-16 16:20:04 +00:00
|
|
|
beforeSend: function(request) {
|
|
|
|
request.setRequestHeader("authorizationToken", authorizationToken);
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
q:val,
|
|
|
|
}
|
2020-05-15 23:24:56 +00:00
|
|
|
})
|
|
|
|
.done(function(data) {
|
|
|
|
updatePage(data);
|
|
|
|
})
|
|
|
|
.fail(function() {
|
|
|
|
alert( "error" );
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-21 10:30:34 +00:00
|
|
|
updatePage( initData );
|