changelog-server/public/js/app.js

102 lines
2.5 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-22 20:27:22 +00:00
var urlRegex = /(\S+): (https?:&#x2F;&#x2F;[^\s]+)/g;
url = function(string){
2020-05-22 20:27:22 +00:00
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>');
}
2020-05-22 21:44:10 +00:00
date = function(date){
var D = new Date(date);
return D.toLocaleDateString()+" "+D.toLocaleTimeString();
}
2020-05-15 23:24:56 +00:00
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]}};
}
2020-05-15 23:24:56 +00:00
$.each(data.hits.hits, (k,v)=>{
2020-05-16 16:20:04 +00:00
item = v._source;
id = v._id;
2020-05-15 23:24:56 +00:00
content += `
2020-05-22 21:44:10 +00:00
<div class="log row">
<div class="col-2 text-right">
<a href="/log/${id}">
${date(escape(item.created_at))} <br/>
</a>
<div class="d-none d-lg-block">
<p class="server"> ${escape(item.server)} </p>
<p class="author"> ${escape(item.author)} </p>
<a class="actions-toggle btn-link btn-sm">Actions</a>
</div>
<div class="actions btn-group btn-group-sm" role="group" aria-label="log actions">
<a class="destroy btn btn btn-outline-secondary" href="/destroy/${id}">Remove</a>
<a class="edit btn btn btn-outline-secondary" href="/edit/${id}">Edit</a>
</div>
</div>
<div class="col-lg">
<pre> ${cmd(title(url(escape(item.content))))}</pre>
2020-05-22 21:44:10 +00:00
</div>
2020-05-15 23:24:56 +00:00
</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 );
2020-05-22 21:44:10 +00:00
$(".actions-toggle").on("click",(e) => { var el=e.target; $(el).next().show(); $(el).hide(); } )