changelog-server/public/js/app.js
2020-05-22 20:27:30 +00:00

82 lines
1.8 KiB
JavaScript

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