changelog-server/public/js/app.js

73 lines
1.6 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];
});
};
var urlRegex = /(https?:&#x2F;&#x2F;[^\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 = "";
$.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)}
<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" );
});
});
updatePage( initData );