diff --git a/src/assets/js/servers-log.js b/src/assets/js/servers-log.js index 7483892..c4f0721 100644 --- a/src/assets/js/servers-log.js +++ b/src/assets/js/servers-log.js @@ -1,335 +1,335 @@ /* ------------------------------------------------------------- Nasqueron infrastructure - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Project: Nasqueron Author: Sébastien Santoro aka Dereckson Dependencies: jQuery Filename: servers-log.js Licence: CC-BY 4.0, MIT, BSD-2-Clause (multi-licensing) ------------------------------------------------------------- */ /* ------------------------------------------------------------- Table of contents - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - :: Servers log :: Code to run when document is ready */ var ServersLog = function (url, container) { var serversLog = { /// /// Private properties /// /** * A JQuery selector expression to a DOM element to publish the log in. * * @var string */ container: "", /** * The URL to fetch the log. * * @var string */ url: "", /** * The log entries fetched. * * @var array */ logEntries: [], /// /// Constructor /// /** * Initializes an instance of this object. * * @param url The URL to the log * @param container The DOM element JQuery selector where to write */ load: function (url, container) { this.url = url; this.container = container; this.refreshData(); }, /// /// Main methods /// /** * Fetches log entries. That will trigger an UI refresh once fetched. */ refreshData: function () { this.fetchLogEntries(); }, /** * Refreshes the UI from the content in logEntries array. */ refreshUI: function () { $(this.container).html(this.formatEntries()); }, /// /// Data helper methods /// /** * Fetches the log entries at the log URL, fills logEntries array, * refreshes the UI. */ fetchLogEntries: function () { $.getJSON(this.url, function(data) { serversLog.logEntries = data.reverse(); // Log is chronological. serversLog.refreshUI(); }); }, /// /// UI helper methods /// formatEntries: function () { var currentDate = ""; var currentMonth = ""; var entries = ""; for (var i = 0; i < this.logEntries.length; i++) { var entry = this.logEntries[i]; var date = this.getDate(entry.date); if (date != currentDate) { // Month heading var month = this.getMonth(entry.date); if (month != currentMonth) { entries += this.formatMonthHeadings(entry.date); currentMonth = month; } // Day heading entries += this.formatDateHeadings(date); currentDate = date; } entries += this.formatEntry(entry); } return entries; }, formatEntry: function (entry) { var format = `
%%component%% %%date%% %%emitter%%:
`; return format .replace("%%component%%", entry.component) .replace("%%date%%", this.getTime(entry.date)) .replace("%%emitter%%", entry.emitter) .replace("%%message%%", this.formatMessage(entry.entry)) ; }, /// /// Formats date headings /// /** * The month names. * * @var array */ monthNames: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], /** ** Gets a day headings element. * * @param date The date to print * @returns {string} The day heading */ formatDateHeadings: function (date) { return '$1
'
}
],
/**
- * Whitelist of known hexadecimal words.
+ * Known hexadecimal words. Those won't be substituted as Git commits hashes.
*
* @var array
*/
hexadecimalKnownWord: [
"added",
"accede",
"adead",
"decade",
"deedeed",
"deface",
"ed25519",
"efface",
"facade",
"faced",
"faded",
],
/**
- * Determines if an expression matches a whitelisted hexadecimal word.
+ * Determines if an expression matches a known hexadecimal word.
*
* @param word The word to check
* @returns {boolean}
*/
isHexadecimalKnownWord: function (word) {
return this.hexadecimalKnownWord.indexOf(word) > -1;
},
/**
* Determines if the specified expression is probably an hash.
*
* An hash is anything hexadecimal with at least one digit < 10
* and one digit > 9 (A-F), not matching known vocabulary.
*
* @param hash
* @returns {boolean}
*/
isHash: function (hash) {
if (this.isHexadecimalKnownWord(hash)) {
return false;
}
if (/^\d+$/.test(hash) || /^[a-z]+$/i.test(hash)) {
// Contains only letter or digits,
// so not a good hash candidate.
return false;
}
return true;
},
formatMessage: function (message) {
for (var i = 0; i < this.messageDecorators.length; i++) {
var decorator = this.messageDecorators[i];
message = message.replace(decorator.re, decorator.replaceBy);
}
return message;
},
///
/// Date and time helper functions
///
pad: function (number) {
if (number < 10) {
return '0' + number;
}
return number;
},
getDate: function (timestamp) {
var date = new Date(timestamp);
return date.getUTCFullYear()
+ '-' + this.pad(date.getUTCMonth() + 1)
+ '-' + this.pad(date.getUTCDate());
},
getTime: function (timestamp) {
var date = new Date(timestamp);
return this.pad(date.getUTCHours())
+ ':' + this.pad(date.getUTCMinutes());
},
getMonth: function (timestamp) {
var date = new Date(timestamp);
return date.getUTCMonth();
}
}
typeof container === 'string' && serversLog.load(url, container);
return serversLog;
}
/* -------------------------------------------------------------
Code to run when document is ready
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$(document).ready(function() {
new ServersLog("https://api.nasqueron.org/servers-log/all.json", "#log");
});