changelog-server/public/js/app.js

63 lines
1.3 KiB
JavaScript
Raw Normal View History

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 = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;',
'/': '&#x2F;'
};
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-15 23:24:56 +00:00
function updatePage(data){
var content = "";
$.each(data.hits.hits, (k,v)=>{
var item = v._source;
2020-05-16 16:20:04 +00:00
2020-05-15 23:24:56 +00:00
content += `
<div class="log row-fluid">
<div class="span9">
2020-05-16 16:20:04 +00:00
<p>${escape(item.created_at)} -- ${escape(item.author)} -- ${escape(item.server)}
<h4> ${escape(item.content)}</h4>
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" );
});
});
updatePage( initData );