changelog-server/public/js/app.js
2020-05-16 18:20:04 +02:00

63 lines
1.3 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];
});
};
function updatePage(data){
var content = "";
$.each(data.hits.hits, (k,v)=>{
var item = v._source;
content += `
<div class="log row-fluid">
<div class="span9">
<p>${escape(item.created_at)} -- ${escape(item.author)} -- ${escape(item.server)}
<h4> ${escape(item.content)}</h4>
</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 );